├── kamil.gawk ├── ranjan02.py ├── hajime.txt ├── cube.py ├── geoprob.py ├── .gitignore ├── brhar.cpp ├── alicesie.cpp ├── ngm.cpp ├── cliqsep.cpp ├── samer08f.cpp ├── tohu.cpp ├── tcount3.cpp ├── bomarble.cpp ├── ieeebgam.cpp ├── mcirgame.py ├── mangoes.cpp ├── ololo.cpp ├── act.cpp ├── crds.cpp ├── marbles.py ├── eboxes.cpp ├── rootciph.cpp ├── fence1.cpp ├── binstirl.cpp ├── cltz.py ├── guanggun.cpp ├── ae00.cpp ├── team2.cpp ├── hubullu.cpp ├── favdice.cpp ├── girlsnbs.cpp ├── lastdig.cpp ├── laser_beam.cpp ├── paying.py ├── shop.cpp ├── arctan.pas ├── amr12d.cpp ├── tcount2.cpp ├── hangover.cpp ├── nsteps.cpp ├── sumitr.cpp ├── worms.py ├── dcepc504.cpp ├── enigmath.cpp ├── silver.cpp ├── snowman.cpp ├── prismsa.cpp ├── anarc09b.cpp ├── knapsack.cpp ├── speed.cpp ├── lengfact.cpp ├── mirrored.c ├── rabbit1.cpp ├── tree.py ├── neg2.cpp ├── shahbg.cpp ├── tricount.cpp ├── ae1b.cpp ├── fctrl.cpp ├── quadarea.cpp ├── adv04j.cpp ├── lqdcandy.cpp ├── danger.cpp ├── may99_2.cpp ├── gny07a.cpp ├── hc.cpp ├── pb.cpp ├── amzseq.cpp ├── maxln.cpp ├── pebble.cpp ├── edit.cpp ├── mklabels.cpp ├── parket1.cpp ├── fusion.cpp ├── canton.cpp ├── mkmoney.cpp ├── quality.cpp ├── fancy.cpp ├── nitt2.cpp ├── willitst.cpp ├── dotaa.cpp ├── mrecaman.cpp ├── test.cpp ├── coef.cpp ├── acpc10a.cpp ├── beenums.cpp ├── gny07f.cpp ├── lastdig2.cpp ├── rplc.cpp ├── rplh.cpp ├── mcoins.cpp ├── minmove.cpp ├── ap2.cpp ├── billiard.cpp ├── permut2.cpp ├── uj.py ├── ny10e.cpp ├── temptisl.cpp ├── abcd.cpp ├── maya.cpp ├── absp1.cpp ├── gcd2.cpp ├── pebbmov.cpp ├── streetr.cpp ├── iitkwpcb.cpp ├── facefrnd.cpp ├── pro.cpp ├── gljive.cpp ├── sum1seq.cpp ├── toandfro.cpp ├── mincount.cpp ├── mbeewalk.cpp ├── nochange.cpp ├── ng0frctn-1.cpp ├── ae2a.cpp ├── gny07b.cpp ├── jnext-1.cpp ├── tree1.py ├── amr10g.cpp ├── cylinder.cpp ├── bc.cpp ├── farida.cpp ├── igarb.py ├── palace.cpp ├── poleval.cpp ├── trgrid.cpp ├── amzrck.cpp ├── bandw.cpp ├── hpynos.cpp ├── acpc10e.cpp ├── broul.cpp ├── ny10a.cpp ├── mzvrk.cpp ├── trt.cpp ├── cowcar.cpp ├── mkbook.cpp ├── scpc11b.cpp ├── yapp.cpp ├── byecakes.cpp ├── pucmm210.cpp ├── hamster1.cpp ├── ptime.cpp ├── assign.cpp ├── kpsort.cpp ├── rock.cpp ├── anarc09a-2.cpp ├── acode.cpp ├── amr11e.cpp ├── candy3.cpp ├── sqdance.cpp ├── cmpls.cpp ├── sprime.cpp ├── twosqrs.cpp ├── aps.cpp ├── coins.cpp ├── cprmt.cpp ├── mflar10.cpp ├── roadnet.cpp ├── acpc10d.cpp ├── sbets.cpp ├── flbrklin.cpp ├── major.cpp ├── dnpalin.cpp ├── faketsp.cpp ├── ng0frctn-2.cpp ├── beads.cpp ├── mmmgame.cpp ├── wordcnt.cpp ├── comdiv.cpp ├── notatri.cpp ├── sqrroot.py ├── cadydist.cpp ├── tshow1.cpp ├── iitkwpco-2.cpp ├── addrev.cpp ├── anarc08g.cpp ├── jnext-2.cpp ├── scubadiv.cpp ├── edge.cpp ├── hanoi07.cpp ├── headshot.cpp ├── maximus.py ├── pie.cpp ├── atms.py ├── miserman.cpp ├── aibohp.cpp ├── bytesm2.cpp ├── lines.cpp ├── lwar.cpp ├── rain3.cpp ├── cubes.cpp ├── fashion.cpp ├── party.cpp ├── assist.cpp ├── games.cpp ├── grocery.cpp ├── gnyr09f.cpp ├── mmaxper.cpp ├── rple.cpp ├── zsum.cpp ├── chicago.cpp ├── egypizza.cpp ├── phonelst.cpp ├── arith2.cpp ├── m3tile.cpp ├── polygame.cpp ├── candy.cpp ├── partsum.cpp ├── stone.cpp ├── arraysub.cpp ├── colcoin.cpp ├── icoder.cpp ├── sqrbr.cpp ├── wpc4f.cpp ├── alien.cpp ├── bytese2.cpp ├── cfrac2.cpp ├── dyzio.cpp ├── dsubseq.cpp ├── hepnum.cpp ├── aggrcow.cpp ├── arrange.cpp ├── glasnici.cpp ├── stpar.cpp ├── cz_prob1.cpp ├── eqbox.cpp ├── leonardo.cpp ├── acpc11b.cpp ├── cneasy.cpp ├── mixtures.cpp ├── treegame.cpp ├── anarc05h.cpp ├── army.cpp ├── balife.cpp ├── eights.cpp ├── loner.py ├── crscntry.cpp ├── genetic.cpp ├── adomino.cpp ├── gardenar.cpp ├── myq1.cpp ├── righttri.cpp ├── scavhunt.cpp ├── baised.cpp ├── jchard.cpp ├── ec_conb.cpp ├── edist.cpp ├── kpequ.py ├── babtwr.cpp ├── cerc07n.cpp ├── etf.cpp ├── incseq.cpp ├── quest5.cpp ├── pigbank.cpp ├── bwidow.cpp ├── cwc2015.cpp ├── gossipers.cpp ├── pqueue.cpp ├── subseq.cpp ├── pucmm025.cpp ├── divsum.cpp ├── hellokit.cpp ├── lmis.cpp ├── sumfour.cpp ├── tperml.py ├── lsort.cpp ├── cpcrc1c.cpp ├── ctrick.cpp ├── chocola.cpp ├── double.cpp ├── lisa.cpp ├── hfloor.cpp ├── robot.cpp ├── tricentr.cpp ├── offside.cpp ├── scale.cpp ├── ambm.cpp ├── bmj.cpp ├── acs.cpp ├── cubefr.cpp ├── div2.cpp ├── fcandy.cpp ├── period.cpp ├── posters.cpp ├── gny07h.cpp ├── tdkprime.cpp ├── diehard.cpp ├── image.cpp ├── mergsort.cpp ├── anarc08b.cpp ├── mst.cpp ├── stamps.cpp ├── boi7seq.cpp ├── pfdep.cpp ├── pt07x.cpp ├── chase.cpp ├── disubstr-1.cpp └── musket.cpp /kamil.gawk: -------------------------------------------------------------------------------- 1 | $0=2^gsub(/[DFLT]/,e) -------------------------------------------------------------------------------- /ranjan02.py: -------------------------------------------------------------------------------- 1 | # 2014-10-01 2 | t = int(raw_input()) 3 | for i in xrange(t): 4 | n = int(raw_input()) 5 | print 3**n-1 6 | -------------------------------------------------------------------------------- /hajime.txt: -------------------------------------------------------------------------------- 1 | typedef/**/struct{unsigned/**/aku;char*const/**/soku;}zan; 2 | struct{unsigned(aku);char*const(soku);}typedef(zan); 3 | -------------------------------------------------------------------------------- /cube.py: -------------------------------------------------------------------------------- 1 | # 2023-12-25 2 | t = int(input()) 3 | for _ in range(t): 4 | n, i = [int(w) for w in input().split()] 5 | print(n**i - (n-2)**i) 6 | -------------------------------------------------------------------------------- /geoprob.py: -------------------------------------------------------------------------------- 1 | # 2008-07-31 2 | t=int(raw_input()) 3 | for i in range(t): 4 | b,c,d=([int(x) for x in raw_input().split(' ')]) 5 | print 2*c-b-d 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # test case files 2 | *.in 3 | *.in.* 4 | *.out 5 | *.out.* 6 | 7 | # autosave files 8 | *~ 9 | 10 | .* 11 | __pycache__ 12 | 13 | # whitespace transpiler (binary) 14 | wstp 15 | -------------------------------------------------------------------------------- /brhar.cpp: -------------------------------------------------------------------------------- 1 | // 2009-12-26 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int P,L,M; 7 | cin >> P >> L >> M; 8 | cout << (P*M 3 | using namespace std; 4 | int main() { 5 | int T; cin >> T; 6 | while (T--) { 7 | int N; cin >> N; cout << (N+1)/2 << endl; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ngm.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-04 2 | #include 3 | int main() 4 | { 5 | int N; 6 | scanf("%d",&N); 7 | if (N%10) 8 | printf("1\n%d\n",N%10); 9 | else 10 | printf("2\n"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /cliqsep.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-05 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int k; 7 | for (;;) 8 | { 9 | if (scanf("%*d %*d %d",&k)!=1) 10 | return 0; 11 | puts(k%2?"no":"yes"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /samer08f.cpp: -------------------------------------------------------------------------------- 1 | // 2008-11-25 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int n; 7 | for(;;) 8 | { 9 | scanf("%d",&n); 10 | if (!n) return 0; 11 | printf("%d\n",n*(n+1)*(2*n+1)/6); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tohu.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | int main() { 4 | int T; scanf("%d", &T); 5 | while (T--) { 6 | double n; scanf("%lf", &n); 7 | printf("%.11f\n", 0.5/(n+2) - 0.5/(n+1) + 0.75); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tcount3.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-23 2 | #include 3 | int main() 4 | { 5 | int T,i; 6 | unsigned long long N; 7 | scanf("%d",&T); 8 | for(i=0;i 3 | using namespace std; 4 | int main() { 5 | for (;;) { 6 | int N; cin >> N; 7 | if (N == 0) return 0; 8 | cout << (3*(N+1)*(N+1) - (N+1))/2 << endl; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ieeebgam.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-02 2 | #include 3 | int main() { 4 | int T; scanf("%d", &T); 5 | while (T--) { 6 | double N; scanf("%lf", &N); 7 | printf("%.8f\n", (N-1)/N + 1/(N*(N+1))); 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /mcirgame.py: -------------------------------------------------------------------------------- 1 | # 2009-04-14 2 | def factorial(x): 3 | result = 1 4 | while x: 5 | result *= x 6 | x -= 1 7 | return result 8 | while 1: 9 | n = int(raw_input()) 10 | if n == -1: 11 | break; 12 | print factorial(2*n)/(factorial(n)*factorial(n+1)) 13 | -------------------------------------------------------------------------------- /mangoes.cpp: -------------------------------------------------------------------------------- 1 | // 2014-09-30 2 | #include 3 | using namespace std; 4 | int main() { 5 | int T; scanf("%d", &T); 6 | while (T--) { 7 | long long N; scanf("%lld", &N); 8 | printf("%lld\n", ((N-1)/2)*((N-1)/2)%N); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ololo.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | int res = 0; 7 | while (N--) { 8 | int x; 9 | scanf("%d", &x); 10 | res ^= x; 11 | } 12 | printf("%d\n", res); 13 | } 14 | -------------------------------------------------------------------------------- /act.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-05 2 | #include 3 | #include 4 | int main() 5 | { 6 | int t; 7 | char s[100000]; 8 | scanf("%d",&t); 9 | while (t--) 10 | { 11 | scanf("%*d %s",s); 12 | int N=strlen(s); 13 | puts(s+N-1); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /crds.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | using namespace std; 4 | int main() { 5 | int T; 6 | cin >> T; 7 | while (T--) { 8 | long long N; 9 | cin >> N; 10 | cout << (N*(N-1)/2 + N*(N+1))%1000007 << endl; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /marbles.py: -------------------------------------------------------------------------------- 1 | # 2008-07-14 2 | N=int(raw_input()) 3 | for i in range(N): 4 | n,k=([long(x) for x in raw_input().split()]) 5 | n-=1 6 | k-=1 7 | if k>n/2: 8 | k=n-k 9 | num=1 10 | den=1 11 | for j in range(k): 12 | num*=n-j 13 | den*=j+1 14 | print num/den 15 | 16 | -------------------------------------------------------------------------------- /eboxes.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-26 2 | #include 3 | using namespace std; 4 | int main() { 5 | int _t; cin >> _t; 6 | while (_t--) { 7 | int N, K, T, F; 8 | cin >> N >> K >> T >> F; 9 | cout << (F - N) * K / (K - 1) + N << endl; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /rootciph.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-26 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int T; cin >> T; 7 | while (T--) { 8 | long long a, b; string s; cin >> a >> b >> s; 9 | cout << a*a - 2*b << endl; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /fence1.cpp: -------------------------------------------------------------------------------- 1 | // 2009-06-21 2 | #include 3 | #include 4 | using namespace std; 5 | const double pi=acos(-1.0); 6 | int main() 7 | { 8 | int x; 9 | for(;;) 10 | { 11 | scanf("%d",&x); 12 | if (!x) return 0; 13 | printf("%.2f\n",x*x/(2.0*pi)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /binstirl.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-26 2 | #include 3 | int main() 4 | { 5 | int i,d,n,k; 6 | scanf("%d",&d); 7 | for (i=0; i 3 | using namespace std; 4 | int main() { 5 | for (;;) { 6 | long long n; cin >> n; 7 | if (cin.eof()) return 0; 8 | long long foo = 9*(n/9); 9 | cout << 9*foo + (n-foo)*(n-foo) << endl; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ae00.cpp: -------------------------------------------------------------------------------- 1 | // 2009-12-21 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int N,res=0,i,s; 8 | scanf("%d",&N); 9 | s=sqrt(N); 10 | for (i=1; i<=s; i++) 11 | res+=N/i-i+1; 12 | printf("%d\n",res); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /team2.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-02 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int a[4]; int cs = 0; 7 | while (~scanf("%d %d %d %d", a, a+1, a+2, a+3)) { 8 | sort(a, a+4); 9 | printf("Case %d: %d\n", ++cs, a[2] + a[3]); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hubullu.cpp: -------------------------------------------------------------------------------- 1 | // 2008-04-20 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int i,t,N,p; 7 | cin >> t; 8 | for (i=0; i> N >> p; 11 | if (p==0) 12 | cout << "Airborne wins.\n"; 13 | else 14 | cout << "Pagfloyd wins.\n"; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /favdice.cpp: -------------------------------------------------------------------------------- 1 | // 2008-04-30 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int t; 7 | cin >> t; 8 | while(t--) 9 | { 10 | double e=0.0; 11 | int N,j; 12 | cin >> N; 13 | for (j=1; j<=N; j++) 14 | e+=N/(double)j; 15 | printf("%.2f\n",float(e)); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /girlsnbs.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-24 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int x, y; 7 | for (;;) { 8 | cin >> x >> y; 9 | if (x == -1) return 0; 10 | if (x < y) swap(x, y); 11 | cout << (x + y)/(y + 1) << endl; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /lastdig.cpp: -------------------------------------------------------------------------------- 1 | // 2009-03-18 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int t,a,b,c; 7 | scanf("%d",&t); 8 | while (t--) 9 | { 10 | scanf("%d %d",&a,&b); 11 | if (b>4) b=(b%4)+4; 12 | int c=1; 13 | while (b--) c=(a*c)%10; 14 | printf("%d\n",c); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /laser_beam.cpp: -------------------------------------------------------------------------------- 1 | // 2016-07-24 2 | #include 3 | int gcd(int x, int y) { 4 | return x == 0 ? y : gcd(y%x, x); 5 | } 6 | int main() { 7 | int T; scanf("%d", &T); 8 | while (T--) { 9 | int x, y; 10 | scanf("%d %d", &x, &y); 11 | printf("%d\n", 1 + gcd(x-1, y-1)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /paying.py: -------------------------------------------------------------------------------- 1 | c = int(input()) 2 | while c > 0: 3 | c -= 1 4 | n = int(input()) 5 | power2 = 1 6 | while power2 <= n: 7 | power2 <<= 1 8 | result = 0 9 | while n > 0: 10 | while n < (power2 << 1) - 1: power2 >>= 1 11 | result += 1 12 | n -= power2 13 | print(result) 14 | -------------------------------------------------------------------------------- /shop.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-04 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | for(;;) 9 | { 10 | cin >> X >> Y; 11 | if (X==0) 12 | return 0; 13 | for (i=0; i> S; 16 | for (j=0; j 3 | #include 4 | #include 5 | using namespace std; 6 | int main() { 7 | int T; cin >> T; 8 | while (T--) { 9 | string s; cin >> s; 10 | string r = s; reverse(r.begin(), r.end()); 11 | cout << (s == r ? "YES" : "NO") << endl; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tcount2.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-23 2 | // modified 2014-10-07 3 | #include 4 | typedef long long int64; 5 | int main() 6 | { 7 | int i,T; 8 | int64 N; 9 | scanf("%d",&T); 10 | for (i=0; i 3 | using namespace std; 4 | int main() 5 | { 6 | double x; 7 | for(;;) 8 | { 9 | cin >> x; 10 | if (x==0.0) 11 | return 0; 12 | double sum=0.0; 13 | int i=1; 14 | while (sum 3 | int main() 4 | { 5 | int N,i,x,y; 6 | scanf("%d",&N); 7 | for (i=0; i 4 | using namespace std; 5 | #define F(y,z) for(y=1;y<=z;y++){ 6 | #define s scanf("%d", 7 | int main(){static int a[100][100],n,i,N,m,j,k,x;s&n);F(i,n)s&N);m=0;F(j,N)F(k,j)s&x);a[j][k]=x+max(a[j-1][k-1],a[j-1][k]);m>?=a[j][k];}}printf("%d\n",m);}} 8 | -------------------------------------------------------------------------------- /worms.py: -------------------------------------------------------------------------------- 1 | # 2025-05-09 2 | dp = [[1]+6*[0]] 3 | for i in range(1, 501): 4 | a = dp[-1] 5 | b = 7*[0] 6 | for j in range(7): 7 | for k in range(10): 8 | if k != 7: b[(10*j+k)%7] += a[j] 9 | dp.append(b) 10 | 11 | t = int(input()) 12 | for _ in range(t): 13 | n = int(input()) 14 | print(10**n - 1 - sum(dp[n][1:])) 15 | -------------------------------------------------------------------------------- /dcepc504.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | using namespace std; 4 | int main() { 5 | int T; scanf("%d", &T); 6 | while (T--) { 7 | long long k; scanf("%*d %lld", &k); k--; 8 | int cnt = 0; while (k) { cnt += k&1; k>>=1; } 9 | if (cnt%2) { printf("Female\n"); } 10 | else { printf("Male\n"); } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /enigmath.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-27 2 | #include 3 | using namespace std; 4 | int gcd(int x, int y) { 5 | if (x == 0) return y; else return gcd(y%x, x); 6 | } 7 | int main() { 8 | int T; cin >> T; 9 | while (T--) { 10 | int A, B; cin >> A >> B; 11 | int g = gcd(A, B); 12 | cout << B/g << ' ' << A/g << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /silver.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | using namespace std; 4 | int main() { 5 | for (;;) { 6 | int x; 7 | cin >> x; if (x == 0) return 0; 8 | int p = 1; 9 | int res = -1; 10 | while (p <= x) { 11 | p <<= 1; 12 | res++; 13 | } 14 | cout << res << endl; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /snowman.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-20 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int t,a; 9 | double r; 10 | scanf("%d",&t); 11 | while (t--) 12 | { 13 | scanf("%d",&a); 14 | r=pow(3.0*a/(516.0*3.1415926536),1.0/3); 15 | printf("%d\n",int(18.0*r)); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /prismsa.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-28 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int t; scanf("%d", &t); 7 | while (t--) { 8 | double V; scanf("%lf", &V); 9 | double a = pow(4.0*V, 1.0/3.0); 10 | double h = 4.0*V/(a*a*sqrt(3.0)); 11 | printf("%.2f\n", a*a*sqrt(3.0)/2.0 + 3.0*a*h); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /anarc09b.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | using namespace std; 4 | int gcd(int x, int y) { 5 | return x == 0 ? y : gcd(y%x, x); 6 | } 7 | int main() { 8 | for (;;) { 9 | int w, h; 10 | cin >> w >> h; if (w == 0) return 0; 11 | long long lcm = (long long)w*h/gcd(w, h); 12 | cout << (lcm/w)*(lcm/h) << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /knapsack.cpp: -------------------------------------------------------------------------------- 1 | // 2009-03-01 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | static int dp[2001]={0}; 7 | int S,N,i,size,val,j; 8 | scanf("%d %d",&S,&N); 9 | for (i=0; i=size; j--) 13 | dp[j]=max(dp[j],dp[j-size]+val); 14 | } 15 | printf("%d\n",dp[S]); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /speed.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | #include 4 | using namespace std; 5 | int gcd(int x, int y) { 6 | return x == 0 ? y : gcd(y%x, x); 7 | } 8 | int main() { 9 | int t; cin >> t; 10 | while (t--) { 11 | int x, y; cin >> x >> y; 12 | int g = gcd(abs(x), abs(y)); 13 | cout << abs(x/g-y/g) << endl; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /lengfact.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | #include 4 | #define pi acos(-1.0) 5 | using namespace std; 6 | int main() { 7 | int t; cin >> t; 8 | while (t--) { 9 | double x; cin >> x; 10 | if (x <= 1) cout << 1 << endl; 11 | else cout << (long long)(1 + floor((0.5*log(2.0*pi*x) + x*(log(x)-1))/log(10))) << endl; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /mirrored.c: -------------------------------------------------------------------------------- 1 | /* 2009-01-04 */ 2 | int main() 3 | { 4 | char c1,c2; 5 | printf("Ready\n"); 6 | for(;;) 7 | { 8 | c1=getchar(); c2=getchar(); getchar(); 9 | if (c1==' '&&c2==' ') 10 | return 0; 11 | if (c1=='b'&&c2=='d'||c1=='d'&&c2=='b'||c1=='p'&&c2=='q'||c1=='q'&&c2=='p') 12 | printf("Mirrored pair\n"); 13 | else 14 | printf("Ordinary pair\n"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /rabbit1.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-11 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | static int F[1572864]; 7 | int i,C,N,M; 8 | F[0]=1; 9 | F[1]=1; 10 | for (i=2; i<1572864; i++) 11 | F[i]=(F[i-1]+F[i-2])%1048576; 12 | cin >> C; 13 | for (i=0; i> N >> M; 16 | cout << F[N%1572864]%(1< 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int N; 8 | cin >> N; 9 | string s=""; 10 | while (N!=0) 11 | { 12 | if (N%2==0) 13 | { 14 | s='0'+s; 15 | N/=-2; 16 | } 17 | else 18 | { 19 | s='1'+s; 20 | N=-(N-1)/2; 21 | } 22 | } 23 | if (s=="") cout << "0\n"; else cout << s << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /shahbg.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-01 2 | #include 3 | int main() { 4 | bool occ[20002] = {0}; 5 | int Q; scanf("%d", &Q); 6 | int tot = 0; 7 | while (Q--) { 8 | int p; scanf("%d", &p); 9 | tot++; 10 | tot -= occ[p-1]; 11 | tot -= occ[p+1]; 12 | occ[p] = true; 13 | printf("%d\n", tot); 14 | } 15 | puts("Justice"); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /tricount.cpp: -------------------------------------------------------------------------------- 1 | // 2008-04-20 2 | // modified 2014-10-07 3 | #include 4 | typedef long long int64; 5 | int main() 6 | { 7 | int i,T; 8 | int64 N; 9 | scanf("%d",&T); 10 | for (i=0; i 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int N,f1,f2,K,i,res=0; 9 | int a[1000]; 10 | scanf("%d %d %d",&N,&f1,&f2); 11 | K=f1*f2; 12 | for (i=0; i()); 15 | for (i=0; res 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int i,N,x,y; 9 | cin >> N; 10 | for (i=0; i> x; 13 | y=0; 14 | do 15 | { 16 | x/=5; 17 | y+=x; 18 | } 19 | while (x>0); 20 | cout << y << endl; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /quadarea.cpp: -------------------------------------------------------------------------------- 1 | // 2008-05-31 2 | #include 3 | #include 4 | int main() 5 | { 6 | int T,i; 7 | long double a,b,c,d,s,r; 8 | scanf("%d",&T); 9 | for (i=0; i 3 | // compute ceil(log(x)/log(2)) 4 | int log2(int x) { 5 | int pwr = 1; 6 | int res = 0; 7 | while (pwr < x) { 8 | pwr <<= 1; 9 | res++; 10 | } 11 | return res; 12 | } 13 | int main() { 14 | int t; scanf("%d", &t); 15 | while (t--) { 16 | int n; scanf("%d", &n); 17 | printf("%d\n", 1+log2(n)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lqdcandy.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-27 2 | #include 3 | using namespace std; 4 | int main() { 5 | int T; scanf("%d", &T); 6 | while (T--) { 7 | long long N; scanf("%lld", &N); 8 | long long pwr = 1, exponent = 0; 9 | while (pwr < N) { pwr <<= 1; exponent++; } 10 | int cnt = 0; while ((N&1) == 0) { N >>= 1; cnt++; } 11 | printf("%lld %d\n", pwr, exponent-cnt); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /danger.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-21 2 | #include 3 | int main() 4 | { 5 | int x,y,z; 6 | for(;;) 7 | { 8 | x=getchar()-'0'; 9 | y=getchar()-'0'; 10 | getchar(); 11 | z=getchar()-'0'; 12 | if (x==0&&y==0&&z==0) 13 | return 0; 14 | int X=10*x+y; 15 | while (z--) 16 | X*=10; 17 | int p2=1; 18 | while (p2<=X) 19 | p2<<=1; 20 | p2>>=1; 21 | printf("%d\n",2*(X-p2)+1); 22 | getchar(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /may99_2.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-07 2 | #include 3 | #include 4 | using namespace std; 5 | string letters = "manku"; 6 | string manku(long long n) { 7 | if (n == -1) return ""; 8 | else return manku(n/5-1) + letters[n%5]; 9 | } 10 | int main() { 11 | int t; cin >> t; 12 | while (t--) { 13 | long long n; cin >> n; 14 | cout << manku(n-1) << endl; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /gny07a.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-14 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int N,x,i,j; 8 | char s[100]; 9 | scanf("%d",&N); 10 | for (j=1; j<=N; j++) 11 | { 12 | printf("%d ",j); 13 | scanf("%d %s",&x,s); 14 | x--; 15 | for (i=0; i 3 | #include 4 | using namespace std; 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | int T; cin >> T; 8 | while (T--) { 9 | int N; cin >> N; 10 | int cnt = 0; 11 | while (N--) { 12 | string s; cin >> s; 13 | if (s == "lxh") cnt++; 14 | } 15 | cout << (cnt % 2 ? "lxh" : "hhb") << endl; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /pb.cpp: -------------------------------------------------------------------------------- 1 | // 2009-06-18 2 | #include 3 | using namespace std; 4 | long long mulmod(long long b,long long e,long long m) 5 | { 6 | if (e==0) return 1; 7 | long long z=mulmod(b,e/2,m); 8 | z=(z*z)%m; 9 | if (e%2) return (z*b)%m; else return z; 10 | } 11 | int main() 12 | { 13 | int T,x; 14 | scanf("%d",&T); 15 | while (T--) 16 | { 17 | scanf("%d",&x); 18 | printf("%lld\n",mulmod(x+1,x-1,10007)); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /amzseq.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-27 2 | #include 3 | using namespace std; 4 | int dp[1000][3]; 5 | int main() { 6 | int N; cin >> N; 7 | dp[1][0] = dp[1][1] = dp[1][2] = 1; 8 | for (int i = 2; i <= N; i++) { 9 | dp[i][0] = dp[i-1][0] + dp[i-1][1]; 10 | dp[i][1] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2]; 11 | dp[i][2] = dp[i-1][1] + dp[i-1][2]; 12 | } 13 | cout << dp[N][0] + dp[N][1] + dp[N][2] << endl; 14 | } 15 | -------------------------------------------------------------------------------- /maxln.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int T; 7 | scanf("%d", &T); 8 | for (int i = 1; i <= T; i++) { 9 | double r; 10 | scanf("%lf", &r); 11 | printf("Case %d: ", i); 12 | if (r >= 0.25) { 13 | printf("%.2f\n", 4*r*r + 0.25); 14 | } else { 15 | printf("%.2f\n", 2*r); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /pebble.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-02 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | for (int cs = 1;; cs++) { 7 | string s; cin >> s; s = "0" + s; 8 | if (cin.eof()) return 0; 9 | printf("Game #%d: ", cs); 10 | int cnt = 0; 11 | for (int i = 1; i < s.length(); i++) { 12 | cnt += (s[i] != s[i-1]); 13 | } 14 | printf("%d\n", cnt); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /edit.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | for (;;) { 9 | string s = ""; cin >> s; if (s == "") return 0; 10 | int cnt = 0; 11 | for (int i = 0; i < s.length(); i++) { 12 | cnt += !!isupper(s[i]) ^ (i%2); 13 | } 14 | cout << min(cnt, (int)s.length() - cnt) << endl; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /mklabels.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | using namespace std; 4 | int f(int n) { 5 | int res = 1; 6 | for (int i = 0; i < n-2; i++) { 7 | res *= n; 8 | } 9 | return res; 10 | } 11 | int main() { 12 | for (int cs = 1; ; cs++) { 13 | int n; scanf("%d", &n); 14 | if (n == 0) return 0; 15 | printf("Case %d, N = %d, # of different labelings = %d\n", cs, n, 16 | f(n)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /parket1.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-27 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | long long R, B; cin >> R >> B; 9 | long long prod = R + B; 10 | long long sum = (prod - B)/2 + 2; 11 | long long diff = sqrt(sum*sum - 4*prod); 12 | long long x = (sum+diff)/2; 13 | long long y = abs(sum-diff)/2; 14 | cout << max(x, y) << ' ' << min(x, y) << endl; 15 | } 16 | -------------------------------------------------------------------------------- /fusion.cpp: -------------------------------------------------------------------------------- 1 | // 2025-05-22 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int T; cin >> T; 7 | while (T--) { 8 | int K, N; cin >> K >> N; 9 | int result = N; 10 | for (int i = 0; i < K; i++) { 11 | int x, y, z; cin >> x >> y >> z; 12 | result = min(result, max({x, N - x, y, N - y, z, N - z})); 13 | } 14 | cout << result << '\n'; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /canton.cpp: -------------------------------------------------------------------------------- 1 | // 2008-01-29 2 | #include 3 | #include 4 | int main() 5 | { 6 | int t,i,N,a,b,c,x,y; 7 | scanf("%d",&t); 8 | for (i=0; i 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int i,j,C; 8 | double P,P0,I,I0; 9 | for(j=1;;j++) 10 | { 11 | cin >> P >> I >> C; 12 | P0=P; 13 | I0=I; 14 | if (C==0) 15 | return 0; 16 | I=I/100.0/C; 17 | for (i=0; i 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int t1,t2,t3,p1,p2,p3,cnt=0; 8 | while (scanf("%d %d %d %d %d %d",&t1,&t2,&t3,&p1,&p2,&p3)==6) 9 | { 10 | int solved=(!!t1)+(!!t2)+(!!t3); 11 | int penalty= 12 | (t1?t1+1200*(p1-1):0)+ 13 | (t2?t2+1200*(p2-1):0)+ 14 | (t3?t3+1200*(p3-1):0); 15 | printf("team %d: %d, %d\n",++cnt,solved,penalty); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /fancy.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-07 2 | #include 3 | #include 4 | int main() { 5 | int t; scanf("%d", &t); 6 | while (t--) { 7 | char num[50]; 8 | scanf("%s", num); 9 | int l = strlen(num); 10 | int res = 1; 11 | for (int i = 1; i < l; i++) { 12 | if (num[i] == num[i-1]) { 13 | res <<= 1; 14 | } 15 | } 16 | printf("%d\n", res); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /nitt2.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-07 2 | #include 3 | #include 4 | char num[55555]; 5 | bool div(int d, char* n) { 6 | int rem = 0; 7 | while (*n) rem = (10*rem + *n++ - '0')%d; 8 | return rem == 0; 9 | } 10 | int main() { 11 | int T; scanf("%d\n", &T); 12 | while (T--) { 13 | scanf("%s", num); 14 | printf("%s %s\n", 15 | div(252, num) ? "Yes" : "No", 16 | div(525, num) ? "Yes" : "No"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /willitst.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-24 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | long long x; 7 | set S; 8 | cin >> x; 9 | while (S.find(x) == S.end() && x > 1) { 10 | S.insert(x); 11 | if (x%2 == 0) { 12 | x /= 2; 13 | } else { 14 | x = 3*x+3; 15 | } 16 | } 17 | if (x == 1) { cout << "TAK" << endl; } 18 | else { cout << "NIE" << endl; } 19 | } 20 | -------------------------------------------------------------------------------- /dotaa.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | using namespace std; 4 | int main() { 5 | int T; 6 | cin >> T; 7 | while (T--) { 8 | int n, m, d; 9 | cin >> n >> m >> d; 10 | for (int i = 0; i < n; i++) { 11 | int x; cin >> x; 12 | m -= (x - 1) / d; 13 | } 14 | if (m > 0) { 15 | cout << "NO" << endl; 16 | } else { 17 | cout << "YES" << endl; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /mrecaman.cpp: -------------------------------------------------------------------------------- 1 | // 2009-03-07 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int a[500001]; 7 | static int vis[4000000]={0}; 8 | int i,x; 9 | a[0]=0; 10 | vis[0]=1; 11 | int M=0; 12 | for (i=1; i<=500000; i++) 13 | { 14 | x=a[i-1]-i; 15 | if (x<=0||vis[x]) 16 | a[i]=a[i-1]+i; 17 | else 18 | a[i]=x; 19 | vis[a[i]]=1; 20 | } 21 | for(;;) 22 | { 23 | scanf("%d",&i); 24 | if (!~i) return 0; 25 | printf("%d\n",a[i]); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test.cpp: -------------------------------------------------------------------------------- 1 | // 2007-10-13 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int x; 9 | int b=false; 10 | do 11 | { 12 | cin >> x; 13 | if (x==42) 14 | break; 15 | else 16 | { 17 | if (b) 18 | cout << endl; 19 | else b=true; 20 | cout << x; 21 | } 22 | } 23 | while(true); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /coef.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-02 2 | #include 3 | using namespace std; 4 | int main() { 5 | long long fact[14]; 6 | fact[0] = 1; 7 | for (int i = 1; i <= 13; i++) { 8 | fact[i] = fact[i-1] * i; 9 | } 10 | int K, N; 11 | while (cin >> N >> K) { 12 | long long res = fact[N]; 13 | while (K--) { 14 | int x; cin >> x; 15 | res /= fact[x]; 16 | } 17 | cout << res << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /acpc10a.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-24 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | long long x, y, z; 7 | for (;;) { 8 | scanf("%lld %lld %lld", &x, &y, &z); 9 | if (x == 0 && y == 0 && z == 0) return 0; 10 | if (z - y == y - x) { 11 | printf("AP %lld\n", 2*z - y); 12 | } else { 13 | if (y == 0) printf("GP 0\n"); 14 | else printf("GP %lld\n", z*z/y); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /beenums.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | long long N; 7 | for (;;) { 8 | cin >> N; 9 | if (N == -1) return 0; 10 | long long disc = 9 + 12*(N-1); 11 | long long s = sqrt((double)disc); 12 | if (s*s == disc && (3 + s)%6 == 0 && (3+s)/6 >= 1) { 13 | cout << "Y" << endl; 14 | } else { 15 | cout << "N" << endl; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /gny07f.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-20 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | int main() 5 | { 6 | int i,j,T,depth,m; 7 | char s[200]; 8 | scanf("%d",&T); 9 | getchar(); 10 | for (i=1; i<=T; i++) 11 | { 12 | depth=m=0; 13 | gets(s); 14 | for (j=0; s[j]; j++) 15 | { 16 | if (s[j]-'[') 17 | depth--; 18 | else 19 | { 20 | depth++; 21 | if (depth>m) 22 | m=depth; 23 | } 24 | } 25 | printf("%d %d\n",i,1< 3 | #include 4 | using namespace std; 5 | int main() { 6 | int t; cin >> t; 7 | while (t--) { 8 | string a; long long b; 9 | cin >> a >> b; 10 | int last = a[a.length() - 1] - '0'; 11 | if (b >= 8) { 12 | b = 4 + (b%4); 13 | } 14 | int res = 1; 15 | while (b--) { 16 | res = (res*last)%10; 17 | } 18 | cout << res << endl; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /rplc.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int T; scanf("%d", &T); 7 | for (int cs = 1; cs <= T; cs++) { 8 | int N; scanf("%d", &N); 9 | long long res = 1; long long sum = 0; 10 | for (int i = 0; i < N; i++) { 11 | long long x; scanf("%lld", &x); 12 | sum += x; res = max(res, 1-sum); 13 | } 14 | printf("Scenario #%d: %lld\n", cs, res); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /rplh.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-27 2 | #include 3 | #include 4 | const double pi = 2*acos(0.0); 5 | const double g = 9.806; 6 | using namespace std; 7 | int main() { 8 | int T; cin >> T; 9 | for (int _cs = 1; _cs <= T; _cs++) { 10 | double Ta, Sp; cin >> Ta >> Sp; 11 | double sine = g*Ta/(Sp*Sp); 12 | printf("Scenario #%d: ", _cs); 13 | if (sine > 1) { cout << "-1\n"; } 14 | else { printf("%.2f\n", 0.5*asin(sine)*180.0/pi); } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /mcoins.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-25 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | static int num[1000001]={0}; 7 | int K,L,T,N,i; 8 | scanf("%d %d %d",&K,&L,&T); 9 | num[0]=0; //zero is a P-position 10 | for (i=1; i<=1000000; i++) 11 | { 12 | num[i]=~num[i-1]; 13 | if (i>=K) 14 | num[i]|=~num[i-K]; 15 | if (i>=L) 16 | num[i]|=~num[i-L]; 17 | } 18 | while (T--) 19 | { 20 | scanf("%d",&N); 21 | putchar(num[N]?'A':'B'); 22 | } 23 | puts(""); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /minmove.cpp: -------------------------------------------------------------------------------- 1 | // 2008-12-30 2 | #include 3 | #include 4 | int main() 5 | { 6 | int L,i,j,n; 7 | char s[200010]; 8 | gets(s); 9 | L=strlen(s); 10 | memcpy(s+L,s,L); 11 | i=0; 12 | j=1; 13 | n=0; 14 | while (i 3 | int main() { 4 | int T; 5 | scanf("%d", &T); 6 | while (T--) { 7 | long long x, y, sum; 8 | scanf("%lld %lld %lld", &x, &y, &sum); 9 | long long N = (2*sum)/(x+y); 10 | long long d = (y - x)/(N - 5); 11 | printf("%lld\n", N); 12 | x -= 3*d; 13 | for (long long i = 0; i < N - 1; i++) { 14 | printf("%lld ", x += d); 15 | } 16 | printf("%lld\n", x += d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /billiard.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-06 2 | #include 3 | #include 4 | using namespace std; 5 | const double pi=acos(-1.0); 6 | int main() 7 | { 8 | int a,b,s,m,n; 9 | for(;;) 10 | { 11 | scanf("%d %d %d %d %d",&a,&b,&s,&m,&n); 12 | if (!a) return 0; 13 | double horiz=a*m; //inches 14 | double vert=b*n; 15 | double angle=atan2(vert,horiz)*180.0/pi; 16 | double dist=sqrt(horiz*horiz+vert*vert); 17 | double speed=dist/s; 18 | printf("%.2f %.2f\n",angle,speed); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /permut2.cpp: -------------------------------------------------------------------------------- 1 | // 2008-01-30 2 | #include 3 | int main() 4 | { 5 | int a[100001],b[100001]; 6 | int n,i; 7 | bool B; 8 | for(;;) 9 | { 10 | scanf("%d",&n); 11 | if (n==0) return 0; 12 | for (i=1; i<=n; i++) 13 | { 14 | scanf("%d",&a[i]); 15 | b[a[i]]=i; 16 | } 17 | B=true; 18 | for (i=1; i<=n; i++) 19 | if (a[i]!=b[i]) 20 | { 21 | B=false; 22 | break; 23 | } 24 | if (B) 25 | printf("ambiguous\n"); 26 | else 27 | printf("not ambiguous\n"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /uj.py: -------------------------------------------------------------------------------- 1 | # 2008-06-24 2 | import sys 3 | while 1: 4 | s=raw_input() 5 | A=0 6 | B=0 7 | D=0 8 | j=0 9 | while (s[j]!=' '): 10 | j+=1 11 | k=0 12 | while (k 3 | using namespace std; 4 | long long ncr[80][80]; 5 | int main() { 6 | for (int n = 0; n < 80; n++) { 7 | ncr[n][0] = 1; 8 | } 9 | for (int n = 1; n < 80; n++) { 10 | for (int k = 1; k < 80; k++) { 11 | ncr[n][k] = ncr[n-1][k] + ncr[n-1][k-1]; 12 | } 13 | } 14 | int P; cin >> P; 15 | while (P--) { 16 | int cs, N; cin >> cs >> N; 17 | cout << cs << ' ' << ncr[N+9][N] << endl; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /temptisl.cpp: -------------------------------------------------------------------------------- 1 | // 2009-04-10 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | long long N,K,A,B,i,j; 8 | long long ways[55][55]; 9 | for(;;) 10 | { 11 | scanf("%lld %lld",&N,&K); 12 | if (!~N) return 0; 13 | scanf("%lld %lld",&A,&B); A--,B--; 14 | memset(ways,0,sizeof(ways)); 15 | ways[0][A]=1; 16 | for (i=1; i<=K; i++) 17 | for (j=0; j 3 | char s[100001]; 4 | char t[100001]; 5 | char other(char a, char b, char c) { 6 | for (char d = 'A';; d++) { 7 | if (d != a && d != b && d != c) return d; 8 | } 9 | } 10 | int main() { 11 | int N; scanf("%d\n", &N); 12 | gets(s); 13 | for (int i = 0; i < N; i++) { 14 | t[2*i] = other(s[2*i], s[2*i+1], i == 0 ? 'A' : t[2*i-1]); 15 | t[2*i+1] = other(t[2*i], s[2*i], s[2*i+1]); 16 | } 17 | t[2*N] = 0; 18 | puts(t); 19 | } 20 | -------------------------------------------------------------------------------- /maya.cpp: -------------------------------------------------------------------------------- 1 | // 2008-01-30 2 | #include 3 | const int placevalue[7]={1,20,360,7200,144000,2880000,57600000}; 4 | int main() 5 | { 6 | int n,N,i,j,k,d; 7 | char c; 8 | for (;;) 9 | { 10 | scanf("%d\n",&n); 11 | if (n==0) return 0; 12 | N=0; 13 | for (i=n-1; i>=0; i--) 14 | { 15 | j=0; 16 | k=0; 17 | while ((c=getchar())!='\n') 18 | if (c=='.') 19 | j++; 20 | else if (c=='-') 21 | k++; 22 | d=j+5*k; 23 | N+=d*placevalue[i]; 24 | } 25 | printf("%d\n",N); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /absp1.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-07 2 | #include 3 | int a[10000]; 4 | int main() { 5 | int T; scanf("%d", &T); 6 | while (T--) { 7 | int N; scanf("%d", &N); 8 | long long res = 0; 9 | long long sum = 0; 10 | for (int i = 0; i < N; i++) { 11 | scanf("%d", a + i); 12 | if (i > 0) { 13 | res += i*(long long)a[i] - sum; 14 | } 15 | sum += a[i]; 16 | } 17 | printf("%lld\n", res); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /gcd2.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-04 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | int gcd(int x,int y) 5 | { 6 | if (y==0) 7 | return x; 8 | return gcd(y,x%y); 9 | } 10 | int main() 11 | { 12 | int t,A,i,rem; 13 | char B[300]; 14 | scanf("%d",&t); 15 | while (t--) 16 | { 17 | scanf("%d %s",&A,B); 18 | if (!A) 19 | { 20 | printf("%s\n",B); 21 | continue; 22 | } 23 | rem=0; 24 | i=0; 25 | while (B[i]) 26 | rem=(10*rem+B[i++]-'0')%A; 27 | printf("%d\n",gcd(A,rem)); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /pebbmov.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-05 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int N,i; 9 | string s; 10 | for(;;) 11 | { 12 | set S; 13 | if (scanf("%d",&N)!=1) 14 | return 0; 15 | for (i=0; i> s; 19 | if (S.find(s)==S.end()) 20 | S.insert(s); 21 | else 22 | S.erase(s); 23 | } 24 | if (S.empty()) 25 | puts("second player"); 26 | else 27 | puts("first player"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /streetr.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | #include 4 | using namespace std; 5 | int gcd(int x, int y) { 6 | return x == 0 ? y : gcd(y%x, x); 7 | } 8 | int a[100000]; 9 | int main() { 10 | int N; scanf("%d", &N); 11 | for (int i = 0; i < N; i++) { 12 | scanf("%d", a+i); 13 | } 14 | sort(a, a+N); 15 | int g = 0; 16 | for (int i = 1; i < N; i++) { 17 | g = gcd(g, a[i] - a[i-1]); 18 | } 19 | printf("%d\n", (a[N-1] - a[0])/g - N + 1); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /iitkwpcb.cpp: -------------------------------------------------------------------------------- 1 | // 2014-09-22 2 | #include 3 | long long gcd(long long x, long long y) { 4 | if (x == 0) return y; 5 | else return gcd(y%x, x); 6 | } 7 | int main() { 8 | int T; scanf("%d", &T); 9 | while (T--) { 10 | long long n; scanf("%lld", &n); 11 | long long m = n/2; 12 | for (;;) { 13 | if (gcd(n, m) == 1) { 14 | printf("%lld\n", m); 15 | break; 16 | } 17 | m--; 18 | } 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /facefrnd.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int N; cin >> N; 7 | set frd; 8 | set ffrd; 9 | while (N--) { 10 | int x; cin >> x; frd.insert(x); 11 | int M; cin >> M; 12 | while (M--) { 13 | int y; cin >> y; ffrd.insert(y); 14 | } 15 | } 16 | for (set::iterator It = frd.begin(); It != frd.end(); It++) { 17 | ffrd.erase(*It); 18 | } 19 | cout << ffrd.size() << endl; 20 | } 21 | -------------------------------------------------------------------------------- /pro.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-05 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int N,n,x; 8 | scanf("%d",&N); 9 | long long res=0; 10 | multiset S; 11 | while (N--) 12 | { 13 | scanf("%d",&n); 14 | while (n--) 15 | { 16 | scanf("%d",&x); 17 | S.insert(x); 18 | } 19 | multiset::iterator begin=S.begin(); 20 | multiset::iterator end=S.end(); end--; 21 | res+=*end-*begin; 22 | S.erase(begin); S.erase(end); 23 | } 24 | printf("%lld\n",res); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /gljive.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int sum = 0; 7 | for (int i = 0; i < 10; i++) { 8 | int x; 9 | cin >> x; 10 | if (sum + x <= 100) sum += x; 11 | else if (abs(100 - sum) > abs(100 - (sum + x))) sum += x; 12 | else if (sum < 100 && sum + x - 100 == 100 - sum) sum += x; 13 | else { 14 | cout << sum << endl; 15 | return 0; 16 | } 17 | } 18 | cout << sum << endl; 19 | } 20 | -------------------------------------------------------------------------------- /sum1seq.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-21 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int t,s,n,i; 8 | scanf("%d",&t); 9 | while (t--) 10 | { 11 | scanf("%d %d",&n,&s); 12 | if (2*abs(s)>n*(n-1)||n%4<2&&s%2||n%4>1&&s%2==0) 13 | { 14 | printf("No\n\n"); 15 | continue; 16 | } 17 | printf("0\n"); 18 | int num=0; 19 | for (i=n-1; i; i--) 20 | { 21 | num+=s>0?(s-=i,1):(s+=i,-1); 22 | printf("%d\n",num); 23 | } 24 | putchar('\n'); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /toandfro.cpp: -------------------------------------------------------------------------------- 1 | // 2008-01-29 2 | #include 3 | #include 4 | int main() 5 | { 6 | int y,x,l,i,j; 7 | char s[201]; 8 | char t; 9 | for(;;) 10 | { 11 | scanf("%d",&x); 12 | if (x==0) break; 13 | scanf("%s",&s); 14 | l=strlen(s); 15 | y=l/x; 16 | for (j=x; j 3 | using namespace std; 4 | int main() 5 | { 6 | int N,i; 7 | cin >> N; 8 | unsigned long long L; 9 | for (i=0; i> L; 12 | switch(L%6) 13 | { 14 | case 0:cout << (L/6)*(L+1); break; 15 | case 1:cout << (L-1)/6*(L+2); break; 16 | case 2:cout << (L/2)*((L+1)/3); break; 17 | case 3:cout << (L/3)*((L+1)/2); break; 18 | case 4:cout << (L-1)/3*((L+2)/2); break; 19 | case 5:cout << L*((L+1)/6); 20 | } 21 | cout << endl; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /mbeewalk.cpp: -------------------------------------------------------------------------------- 1 | // 2009-03-01 2 | #include 3 | using namespace std; 4 | int dx[]={0,0,1,1,-1,-1}; 5 | int dy[]={1,-1,1,0,0,-1}; 6 | int main() 7 | { 8 | static int dp[15][40][40]={0}; //# steps; x coord; y coord 9 | int i,j,k,m; 10 | dp[0][20][20]=1; 11 | for (i=1; i<=14; i++) 12 | for (j=1; j<39; j++) 13 | for (k=1; k<39; k++) 14 | for (m=0; m<6; m++) 15 | dp[i][j][k]+=dp[i-1][j+dx[m]][k+dy[m]]; 16 | scanf("%d",&i); 17 | while (i--) 18 | { 19 | scanf("%d",&j); 20 | printf("%d\n",dp[j][20][20]); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /nochange.cpp: -------------------------------------------------------------------------------- 1 | // 2025-08-28 2 | #include 3 | using namespace std; 4 | int main() { 5 | int N, k; cin >> N >> k; 6 | int coin[5]; 7 | for (int i = 0; i < k; i++) { 8 | cin >> coin[i]; 9 | if (i > 0) coin[i] += coin[i - 1]; 10 | } 11 | bool dp[100001]; 12 | dp[0] = true; 13 | for (int i = 1; i <= N; i++) { 14 | dp[i] = false; 15 | for (int j = 0; j < k; j++) { 16 | if (i >= coin[j]) dp[i] |= dp[i - coin[j]]; 17 | } 18 | } 19 | puts(dp[N] ? "YES" : "NO"); 20 | } 21 | -------------------------------------------------------------------------------- /ng0frctn-1.cpp: -------------------------------------------------------------------------------- 1 | // 2009-12-24 2 | #include 3 | #include 4 | using namespace std; 5 | map memo; 6 | long long fusc(long long x) 7 | { 8 | if (x<2) return x; 9 | int z; 10 | if (z=memo[x]) 11 | return z; 12 | else 13 | if (x%2) //odd 14 | return memo[x]=fusc(x/2)+fusc(1+x/2); 15 | else 16 | return memo[x]=fusc(x/2); 17 | } 18 | int main() 19 | { 20 | int x; 21 | for (;;) 22 | { 23 | memo.clear(); 24 | scanf("%d",&x); 25 | if (!x) return 0; 26 | printf("%lld/%lld\n",fusc(x),fusc(x+1)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /ae2a.cpp: -------------------------------------------------------------------------------- 1 | // 2009-12-21 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | static double prob[600][3600]; 8 | int i,j,k,n; 9 | prob[0][0]=1; 10 | for (i=1; i<600; i++) 11 | for (j=0; j<=6*i; j++) 12 | { 13 | prob[i][j]=0.0; 14 | for (k=j-1; k>=j-6&&k>=0; k--) 15 | prob[i][j]+=prob[i-1][k]/6.0; 16 | } 17 | int T; 18 | scanf("%d",&T); 19 | while (T--) 20 | { 21 | scanf("%d %d",&n,&k); 22 | if (n>=600||k>=3600) printf("0\n"); 23 | else printf("%d\n",int(100*prob[n][k])); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /gny07b.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-21 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int N,i; 8 | double x; 9 | char s[100]; 10 | scanf("%d",&N); 11 | for (i=1; i<=N; i++) 12 | { 13 | scanf("%lf",&x); 14 | getchar(); 15 | gets(s); 16 | if (s[0]=='k') 17 | printf("%d %.4f lb\n",i,x*2.2046); 18 | else if (s[1]=='b') 19 | printf("%d %.4f kg\n",i,x*0.4536); 20 | else if (s[0]=='l') 21 | printf("%d %.4f g\n",i,x*0.2642); 22 | else 23 | printf("%d %.4f l\n",i,x*3.7854); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /jnext-1.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-26 2 | #include 3 | #include 4 | using namespace std; 5 | int a[1000000]; 6 | int main() { 7 | int t; scanf("%d", &t); 8 | while (t--) { 9 | int n; scanf("%d", &n); 10 | for (int i = 0; i < n; i++) { 11 | scanf("%d", a+i); 12 | } 13 | if (next_permutation(a, a+n)) { 14 | for (int i = 0; i < n; i++) { 15 | putchar(a[i] + '0'); 16 | } 17 | } else { 18 | printf("-1"); 19 | } 20 | putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tree1.py: -------------------------------------------------------------------------------- 1 | # 2021-10-11 2 | 3 | def fact(n): 4 | return n * fact(n - 1) if n > 1 else 1 5 | 6 | def calc(keys): 7 | l = list(filter(lambda x: x < keys[0], keys)) 8 | r = list(filter(lambda x: x > keys[0], keys)) 9 | result = fact(len(l) + len(r)) // fact(len(l)) // fact(len(r)) 10 | if len(l) > 0: 11 | result *= calc(l) 12 | if len(r) > 0: 13 | result *= calc(r) 14 | return result 15 | 16 | d = int(input()) 17 | for i in range(d): 18 | n = int(input()) 19 | keys = [int(x) for x in input().split()] 20 | print(calc(keys)) 21 | -------------------------------------------------------------------------------- /amr10g.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int a[20000]; 7 | int main() { 8 | int N, K; 9 | int T; scanf("%d", &T); 10 | while (T--) { 11 | scanf("%d %d", &N, &K); 12 | for (int i = 0; i < N; i++) { 13 | scanf("%d", a+i); 14 | } 15 | sort(a, a + N); 16 | int best = 2e9; 17 | for (int i = 0; i <= N - K; i++) { 18 | best = min(best, a[i + K - 1] - a[i]); 19 | } 20 | printf("%d\n", best); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /cylinder.cpp: -------------------------------------------------------------------------------- 1 | // 2010-01-30 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | for (;;) 8 | { 9 | double l,w; 10 | scanf("%lf %lf",&l,&w); 11 | if (l==0&&w==0) return 0; 12 | if (l=w*(1+M_PI)) 16 | res=max(res,M_PI*(w/2.0)*(w/2.0)*w); 17 | else 18 | { 19 | double r=l/(2.0+2.0*M_PI); 20 | res=max(res,M_PI*r*r*w); 21 | } 22 | //case 2 23 | double r=w/(2.0*M_PI); 24 | res=max(res,M_PI*r*r*(l-2.0*r)); 25 | printf("%.3f\n",res); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /bc.cpp: -------------------------------------------------------------------------------- 1 | // 2014-06-07 2 | #include 3 | // compute ceil(log(x)/log(2)) 4 | int log2(int x) { 5 | int pwr = 1; 6 | int res = 0; 7 | while (pwr < x) { 8 | pwr <<= 1; 9 | res++; 10 | } 11 | return res; 12 | } 13 | int main() { 14 | int T; scanf("%d", &T); 15 | for (int _cs = 1; _cs <= T; _cs++) { 16 | int N, M, K; 17 | scanf("%d %d %d", &N, &M, &K); 18 | long long volume = N*M; volume *= K; 19 | printf("Case #%d: %lld %d\n", _cs, volume-1, 20 | log2(N) + log2(M) + log2(K)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /farida.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-03 2 | #include 3 | #include 4 | using namespace std; 5 | long long dp[10001][2]; 6 | int main() { 7 | int T; scanf("%d", &T); 8 | for (int cs = 1; cs <= T; cs++) { 9 | int N; scanf("%d", &N); 10 | dp[0][0] = dp[0][1] = 0; 11 | for (int i = 0; i < N; i++) { 12 | int x; scanf("%d", &x); 13 | dp[i+1][0] = max(dp[i][0], dp[i][1]); 14 | dp[i+1][1] = x + dp[i][0]; 15 | } 16 | printf("Case %d: %lld\n", cs, max(dp[N][0], dp[N][1])); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /igarb.py: -------------------------------------------------------------------------------- 1 | # 2023-10-30 2 | t = int(input()) 3 | for testcase in range(t): 4 | x = int(input()) 5 | result = '' 6 | while x > 0: 7 | d = x % 7 8 | x //= 7 9 | if d == 0: 10 | result += '0' 11 | elif d == 1: 12 | result += '1' 13 | elif d == 2: 14 | result += '2' 15 | elif d == 3: 16 | result += '5' 17 | elif d == 4: 18 | result += '9' 19 | elif d == 5: 20 | result += '8' 21 | else: 22 | result += '6' 23 | print(result) 24 | -------------------------------------------------------------------------------- /palace.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-21 2 | // modified 2014-10-07 3 | #include 4 | typedef long long i64; 5 | int main() 6 | { 7 | int i,K,N; 8 | scanf("%d",&K); 9 | for (i=0; i>=1; 18 | while (p2) 19 | { 20 | prod=(prod*prod)%(i64)98777; 21 | if (N2&p2) 22 | { 23 | prod<<=1; 24 | if (prod>=98777) 25 | prod-=98777; 26 | } 27 | p2>>=1; 28 | } 29 | printf("%d\n",int(prod)); 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /poleval.cpp: -------------------------------------------------------------------------------- 1 | // 2009-04-22 2 | // modified 2014-10-07 3 | #include 4 | typedef long long __int64; 5 | using namespace std; 6 | int main() 7 | { 8 | int deg,i,j,k,x,cnt=0; 9 | int a[1000]; 10 | for (;;) 11 | { 12 | scanf("%d",°); 13 | if (!~deg) return 0; 14 | printf("Case %d:\n",++cnt); 15 | for (i=deg; i>=0; i--) 16 | scanf("%d",a+i); 17 | scanf("%d",&k); 18 | for (i=0; i=0; j--) 23 | result=result*x+a[j]; 24 | printf("%lld\n",result); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /trgrid.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | using namespace std; 4 | int main() { 5 | int T; cin >> T; 6 | while (T--) { 7 | int R, C; 8 | cin >> R >> C; 9 | if (C >= R) { 10 | if (R % 2 == 0) { 11 | cout << "L"; 12 | } else { 13 | cout << "R"; 14 | } 15 | } else { 16 | if (C % 2 == 0) { 17 | cout << "U"; 18 | } else { 19 | cout << "D"; 20 | } 21 | } 22 | cout << endl; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /amzrck.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-07 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | vector fib; 7 | fib.push_back(0); 8 | fib.push_back(1); 9 | for (;;) { 10 | long long x = fib[fib.size() - 2] + fib[fib.size() - 1]; 11 | if (x < 1000000000) { 12 | fib.push_back(x); 13 | } else { 14 | break; 15 | } 16 | } 17 | int T; scanf("%d", &T); 18 | while (T--) { 19 | int n; scanf("%d", &n); 20 | printf("%lld\n", fib[n+2]); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /bandw.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | for (;;) { 7 | string s, t; cin >> s >> t; if (s=="*") return 0; 8 | string w; 9 | int cnt = 0; 10 | int last = 0; 11 | for (int i = 0; i < s.length(); i++) { 12 | if (s[i] == 'B' ^ t[i] == 'B') { 13 | if (last == 0) { 14 | cnt++; 15 | } 16 | } 17 | last = s[i] == 'B' ^ t[i] == 'B'; 18 | } 19 | cout << cnt << endl; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /hpynos.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | #include 4 | using namespace std; 5 | int f(int x) { 6 | int res = 0; 7 | while (x > 0) { 8 | res += (x%10)*(x%10); 9 | x /= 10; 10 | } 11 | return res; 12 | } 13 | int main() { 14 | int N; 15 | cin >> N; 16 | int res = 0; 17 | set S; 18 | while (N != 1 && S.find(N) == S.end()) { 19 | S.insert(N); 20 | N = f(N); 21 | res++; 22 | } 23 | if (N == 1) { 24 | cout << res << endl; 25 | } else { 26 | cout << "-1" << endl; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /acpc10e.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-07 2 | #include 3 | using namespace std; 4 | int main() { 5 | for (;;) { 6 | long long G, T, A, D; 7 | scanf("%lld %lld %lld %lld", &G, &T, &A, &D); 8 | if (G == -1) return 0; 9 | printf("%lld*%lld/%lld+%lld=", G, A, T, D); 10 | long long stage1 = G*T*(T-1)/2; 11 | long long teams = G*A+D; 12 | long long newteams = 13 | teams == 1 ? 14 | 1 : 15 | 1ll << (64 - __builtin_clzll(teams - 1)); 16 | printf("%lld+%lld\n", stage1 + newteams - 1, newteams - teams); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /broul.cpp: -------------------------------------------------------------------------------- 1 | // 2024-09-05 2 | // Problem statement makes no sense... in the 8th bullet point it says that the 3 | // value of the pot increases when a player wins. But it is what it is. 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | int a, b, c; 9 | for (;;) { 10 | cin >> a >> b >> c; 11 | if (a == 0 && b == 0 && c == 0) return 0; 12 | if ((a - c) % b != 0) { 13 | cout << "No accounting tablet\n"; 14 | } else { 15 | cout << (abs((a - c) / b) + 2) / 3 << '\n'; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ny10a.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | int N; 9 | cin >> N; 10 | while (N--) { 11 | int stupid; scanf("%d\n", &stupid); 12 | map M; 13 | string s; cin >> s; 14 | for (int i = 0; i < 38; i++) { 15 | M[s.substr(i, 3)]++; 16 | } 17 | printf("%d %d %d %d %d %d %d %d %d\n", stupid, 18 | M["TTT"], M["TTH"], M["THT"], M["THH"], 19 | M["HTT"], M["HTH"], M["HHT"], M["HHH"]); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /mzvrk.cpp: -------------------------------------------------------------------------------- 1 | // 2023-11-02 2 | #include 3 | using namespace std; 4 | // returns the sum of whirligigs of 1 ... x 5 | unsigned long long compute(long long x) { 6 | unsigned long long result = 0; 7 | long long power = 1; 8 | for (;;) { 9 | // count the number of times that the whirligig is `power` 10 | long long cnt = x / power - x / (2 * power); 11 | if (cnt == 0) return result; 12 | result += cnt * power; 13 | power <<= 1; 14 | } 15 | } 16 | int main() { 17 | long long A, B; cin >> A >> B; 18 | cout << compute(B) - compute(A - 1) << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /trt.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-27 2 | // modified 2014-10-07 3 | #include 4 | typedef long long i64; 5 | inline i64 MAX(i64 x,i64 y) 6 | { 7 | if (x>y) return x; else return y; 8 | } 9 | int main() 10 | { 11 | int N,i,j; 12 | int treat[2000]; 13 | static i64 dp[2000][2000]; 14 | scanf("%d",&N); 15 | for (i=0; i 3 | #include 4 | #define getchar getchar_unlocked 5 | using namespace std; 6 | int input() 7 | { 8 | int x=0; 9 | char c; 10 | for(;;) 11 | { 12 | c=getchar(); 13 | if (c==' '||c=='\n') 14 | return x; 15 | x=10*x+c-'0'; 16 | } 17 | } 18 | int main() 19 | { 20 | int i,N,M,D,L,c,a[50000]; 21 | N=input(); 22 | M=input(); 23 | D=input(); 24 | L=input(); 25 | c=0; 26 | for (i=0; i=L) 31 | c++; 32 | printf("%d\n",c); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /mkbook.cpp: -------------------------------------------------------------------------------- 1 | // 2008-04-20 2 | #include 3 | using namespace std; 4 | int A[10]; 5 | void process(int i) 6 | { 7 | while (i) 8 | { 9 | A[i%10]++; 10 | i/=10; 11 | } 12 | } 13 | int main() 14 | { 15 | int a,b,i,j; 16 | for(j=1;;j++) 17 | { 18 | memset(A,0,sizeof(A)); 19 | cin >> a; 20 | if (a==0) return 0; 21 | cin >> b; 22 | if (a>b) swap(a,b); 23 | for (i=a; i<=b; i++) 24 | process(i); 25 | cout << "Case " << j << ": "; 26 | for (i=0; i<10; i++) 27 | { 28 | cout << i << ':' << A[i]; 29 | if (i<9) 30 | cout << ' '; 31 | else 32 | cout << endl; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /scpc11b.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | #include 4 | using namespace std; 5 | int a[1500]; 6 | int main() { 7 | for (;;) { 8 | int n; cin >> n; if (n == 0) return 0; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | } 12 | a[n] = 0; a[n+1] = 1522; 13 | sort(a, a+n+2); 14 | bool ok = true; 15 | for (int i = 1; i < n+2; i++) { 16 | if (a[i] - a[i-1] > 200) { 17 | ok = false; break; 18 | } 19 | } 20 | cout << (ok ? "POSSIBLE" : "IMPOSSIBLE") << endl; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /yapp.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-07 2 | #include 3 | const int MOD = 1000000007; 4 | int powermod(int base, int exponent, int mod) { 5 | int res = 1; 6 | int pwr = base; 7 | int cur = 1; 8 | while (cur <= exponent) { 9 | if (cur & exponent) { 10 | res = (res*(long long)pwr)%mod; 11 | } 12 | pwr = (pwr*(long long)pwr)%mod; 13 | cur <<= 1; 14 | } 15 | return res; 16 | } 17 | int main() { 18 | int T; scanf("%d", &T); 19 | while (T--) { 20 | int N; scanf("%d", &N); 21 | printf("%d\n", powermod(2, N-1, MOD)); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /byecakes.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-26 2 | #include 3 | using namespace std; 4 | int main() { 5 | for (;;) { 6 | int E, F, S, M, Ep, Fp, Sp, Mp; 7 | cin >> E >> F >> S >> M >> Ep >> Fp >> Sp >> Mp; 8 | if (E == -1) return 0; 9 | int n = 0; 10 | n = max(n, (E + (Ep - 1)) / Ep); 11 | n = max(n, (F + (Fp - 1)) / Fp); 12 | n = max(n, (S + (Sp - 1)) / Sp); 13 | n = max(n, (M + (Mp - 1)) / Mp); 14 | cout << Ep * n - E << " "; 15 | cout << Fp * n - F << " "; 16 | cout << Sp * n - S << " "; 17 | cout << Mp * n - M << endl; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /pucmm210.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-01 2 | #include 3 | const int MOD = 1000000003; 4 | int main() { 5 | int f[1000001]; f[0] = 0; 6 | int sum[1000001]; sum[0] = 0; 7 | for (int i = 1; i <= 1000000; i++) { 8 | long long cube = i; cube *= i; cube *= i; 9 | cube %= MOD; 10 | f[i] = f[i-1] + cube; 11 | if (f[i] >= MOD) f[i] -= MOD; 12 | sum[i] = sum[i-1] + f[i]; 13 | if (sum[i] >= MOD) sum[i] -= MOD; 14 | } 15 | int T; scanf("%d", &T); 16 | while (T--) { 17 | int n; scanf("%d", &n); 18 | printf("%d\n", sum[n]); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /hamster1.cpp: -------------------------------------------------------------------------------- 1 | // 2009-04-11 2 | #include 3 | #include 4 | using namespace std; 5 | double pi=acos(-1.0); 6 | int main() 7 | { 8 | int T; 9 | double V0,K1,K2,theta,x,y; 10 | scanf("%d",&T); 11 | while (T--) 12 | { 13 | scanf("%lf %lf %lf",&V0,&K1,&K2); 14 | theta=atan(-4.0*K1/K2); //tan theta = -4k1/k2 15 | while (theta<=0) theta+=pi; //pi! 16 | theta/=2.0; 17 | double sintheta=sin(theta); 18 | double sinsquaredtheta=sintheta*sintheta; 19 | x=V0*V0*2*sintheta*sqrt(1-sinsquaredtheta)/10.0; 20 | y=V0*V0*sinsquaredtheta/20.0; 21 | printf("%.3f %.3f\n",theta,K1*x+K2*y); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ptime.cpp: -------------------------------------------------------------------------------- 1 | // 2014-09-26 2 | #include 3 | bool isprime(int x) { 4 | for (int i = 2; i*i <= x; i++) { 5 | if (x%i == 0) return false; 6 | } 7 | return true; 8 | } 9 | int main() { 10 | int N; scanf("%d", &N); 11 | bool first = true; 12 | for (int i = 2; i <= N; i++) { 13 | if (!isprime(i)) continue; 14 | int exp = 0, n = N/i; 15 | while (n) { 16 | exp += n; 17 | n /= i; 18 | } 19 | if (!first) printf(" * "); 20 | first = false; 21 | printf("%d^%d", i, exp); 22 | } 23 | putchar('\n'); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /assign.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-05 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int T,i,j,N; 7 | int likes[20][20]; 8 | long long dp[1<<20]; 9 | scanf("%d",&T); 10 | while (T--) 11 | { 12 | scanf("%d",&N); 13 | for (i=0; i 3 | int main() 4 | { 5 | int N,i,j,_count; 6 | int a[100]; 7 | bool b; 8 | for(;;) 9 | { 10 | scanf("%d",&N); 11 | for (i=0; i 4 | int main() 5 | { 6 | int t,i,j,N; 7 | int dp[201]; 8 | char r[200]; 9 | scanf("%d",&t); 10 | while (t--) 11 | { 12 | scanf("%d\n",&N); 13 | dp[0]=0; 14 | for (i=1; i<=N; i++) 15 | { 16 | scanf("%c",&r[i-1]); 17 | dp[i]=0; 18 | int sour=0; 19 | int sweet=0; 20 | for (j=1; j<=i; j++) 21 | { 22 | if (r[i-j]-'0') 23 | sweet++; 24 | else 25 | sour++; 26 | if (dp[i]sour?j:0)) 27 | dp[i]=dp[i-j]+(sweet>sour?j:0); 28 | } 29 | } 30 | printf("%d\n",dp[N]); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /anarc09a-2.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | using namespace std; 4 | char s[2001]; 5 | int main() { 6 | for (int cs = 1;; cs++) { 7 | scanf("%s", s); 8 | if (s[0] == '-') return 0; 9 | int cnt = 0; 10 | int wt = 0; 11 | for (char* p = s; *p; p++) { 12 | if (*p == '{') { 13 | wt++; 14 | } else { 15 | if (wt == 0) { 16 | wt = 1; cnt++; 17 | } else { 18 | wt--; 19 | } 20 | } 21 | } 22 | printf("%d. %d\n", cs, cnt + wt/2); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /acode.cpp: -------------------------------------------------------------------------------- 1 | // 2008-02-02 2 | // modified 2014-10-07 3 | #include 4 | #include 5 | typedef long long int64; 6 | int main() 7 | { 8 | int64 a[5001]; 9 | int i; 10 | char s[5005]; 11 | for(;;) 12 | { 13 | scanf("%s",s); 14 | int l=strlen(s); 15 | if ((l==1)&&(s[0]=='0')) 16 | return 0; 17 | for (i=0; i<=l; i++) 18 | a[i]=0; 19 | a[0]=1; 20 | for (i=0; i'2') continue; 26 | if ((s[i]=='2')&&(s[i+1]>'6')) continue; 27 | a[i+2]+=a[i]; 28 | } 29 | printf("%lld\n", a[l]); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /amr11e.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | bool islucky(int x) { 4 | int cnt = 0; 5 | int p = 2; 6 | while (x > 1) { 7 | while (x % p) p++; 8 | cnt++; if (cnt == 3) return true; 9 | while (x % p == 0) x /= p; 10 | } 11 | return false; 12 | } 13 | int lucky[1000]; 14 | int main() { 15 | int cnt = 0; int x = 1; 16 | while (cnt < 1000) { 17 | if (islucky(x)) { 18 | lucky[cnt++] = x; 19 | } 20 | x++; 21 | } 22 | int T; scanf("%d", &T); 23 | while (T--) { 24 | int n; scanf("%d", &n); 25 | printf("%d\n", lucky[n-1]); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /candy3.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-11 2 | #include 3 | long long input() 4 | { 5 | long long x=0; 6 | char c; 7 | for(;;) 8 | { 9 | c=getchar_unlocked(); 10 | if (c=='\n') return x; 11 | x=10*x+c-'0'; 12 | } 13 | return x; 14 | } 15 | int main() 16 | { 17 | int T,i,N,j; 18 | long long sum,c; 19 | T=input(); 20 | for (i=0; i 3 | #include 4 | using namespace std; 5 | void do_testcase() { 6 | int V, E; scanf("%d %d", &V, &E); 7 | vector id(V); 8 | for (int i = 0; i < V; i++) id[i] = i; 9 | int result = 0; 10 | while (E--) { 11 | int p, q; 12 | scanf("%d %d", &p, &q); --p; --q; 13 | while (p != id[p]) p = id[p] = id[id[p]]; 14 | while (q != id[q]) q = id[q] = id[id[q]]; 15 | if (p == q) ++result; else id[p] = q; 16 | } 17 | printf("%d\n", result); 18 | } 19 | int main() { 20 | int T; scanf("%d", &T); 21 | while (T--) do_testcase(); 22 | } 23 | -------------------------------------------------------------------------------- /cmpls.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-24 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | int main() 5 | { 6 | int T,i,j,S,C; 7 | int d[100][100]; 8 | scanf("%d",&T); 9 | while (T--) 10 | { 11 | scanf("%d %d",&S,&C); 12 | for (i=0; i=0; i--) 20 | for (j=S-i; j 3 | #include 4 | int primes[1000000]; 5 | bool prime[11111111]; 6 | int sieve() 7 | { 8 | int i,j,k=0; 9 | prime[0]=false; 10 | prime[1]=false; 11 | for (i=2; i<11111111; i++) 12 | prime[i]=true; 13 | for (i=2;;i++) 14 | { 15 | while ((i<11111111)&&(!prime[i])) i++; 16 | if (i>=11111111) break; 17 | primes[k++]=i; 18 | for (j=2*i; j<11111111; j+=i) 19 | prime[j]=false; 20 | } 21 | return k; 22 | } 23 | int main() 24 | { 25 | int N=sieve(); 26 | int i; 27 | for (i=0; i<=N&&primes[i]<10000000; i++) 28 | if (prime[i+1]) 29 | printf("%d\n",primes[i]); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /twosqrs.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-21 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | int main() 6 | { 7 | int c,i,j,t; 8 | long long n; 9 | static long long sqr[1000001]; 10 | for (i=0; i<=1000000; i++) 11 | sqr[i]=(long long)i*i; 12 | scanf("%d",&c); 13 | for (t=0; t 3 | int minfact[10000000]; 4 | long long a[10000000]; 5 | int main() { 6 | for (int i = 2; i*i < 10000000; i++) { 7 | if (!minfact[i]) { 8 | // it's a prime 9 | for (int j = i; j < 10000000; j+=i) { 10 | if (!minfact[j]) minfact[j] = i; 11 | } 12 | } 13 | } 14 | for (int i = 2; i < 10000000; i++) { 15 | a[i] = a[i-1] + minfact[i]; 16 | if (!minfact[i]) a[i] += i; 17 | } 18 | int T; scanf("%d", &T); 19 | while (T--) { 20 | int N; scanf("%d", &N); printf("%lld\n", a[N]); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /coins.cpp: -------------------------------------------------------------------------------- 1 | // 2007-11-16 2 | #include 3 | #define max(x,y) (x>y?x:y) 4 | 5 | using namespace std; 6 | 7 | static long long a[1000000]; 8 | 9 | long long f(long long n) 10 | { 11 | if (n>=1000000) 12 | return max(n,f(n/2)+f(n/3)+f(n/4)); 13 | if (a[n]>=0) return a[n]; 14 | long long m; 15 | if (n<12) m=n; 16 | else m=max(n,f(n/2)+f(n/3)+f(n/4)); 17 | a[n]=m; 18 | return m; 19 | } 20 | 21 | int main() 22 | { 23 | int i; 24 | for (i=0; i<1000000; i++) 25 | a[i]=-1; 26 | int N; 27 | int c; 28 | c=0; 29 | while (!cin.eof()) 30 | { 31 | cin >> N; 32 | c++; 33 | cout << f(N) << endl; 34 | if (c==10) break; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /cprmt.cpp: -------------------------------------------------------------------------------- 1 | // 2008-04-19 2 | #include 3 | #include 4 | #define MIN(a,b) ((a)<(b)?(a):(b)) 5 | using namespace std; 6 | int main() 7 | { 8 | string s1,s2; 9 | int i; 10 | char c; 11 | int let1[256]; 12 | int let2[256]; 13 | while (!cin.eof()) 14 | { 15 | cin >> s1 >> s2; 16 | memset(let1,0,sizeof(let1)); 17 | memset(let2,0,sizeof(let2)); 18 | for (i=0; i 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | for (;;) { 9 | string s; 10 | getline(cin, s); 11 | if (s == "*") return 0; 12 | if (s == "") continue; 13 | istringstream iss(s); 14 | string word; iss >> word; 15 | char letter = tolower(word[0]); bool ok = true; 16 | for (;;) { 17 | string word; iss >> word; 18 | if (word == "") break; 19 | ok &= tolower(word[0]) == letter; 20 | } 21 | cout << (ok ? "Y" : "N") << endl; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /roadnet.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-08 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int i,j,k,m,n,t; 7 | int a[200][200]; 8 | cin >> t; 9 | for (i=0; i> n; 12 | for (j=0; j> a[j][k]; 15 | for (j=0; j 3 | using namespace std; 4 | int main() { 5 | for (int cs = 1;; cs++) { 6 | int N; scanf("%d", &N); 7 | if (N == 0) return 0; 8 | int X, Y, Z, X_, Y_, Z_; 9 | int x, y, z; 10 | scanf("%d %d %d", &x, &y, &z); 11 | X = 2e9; Y = y; Z = y + z; 12 | for (int i = 1; i < N; i++) { 13 | scanf("%d %d %d", &x, &y, &z); 14 | X_ = x + min(X, Y); 15 | Y_ = y + min(min(X_, X), min(Y, Z)); 16 | Z_ = z + min(min(Y_, Y), Z); 17 | X = X_; Y = Y_; Z = Z_; 18 | } 19 | printf("%d. %d\n", cs, Y); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /sbets.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() { 7 | int c; cin >> c; 8 | while (c--) { 9 | set winners; 10 | set losers; 11 | for (int i = 0; i < 16; i++) { 12 | string s, t; int x, y; cin >> s >> t >> x >> y; 13 | if (x < y) swap(s, t); 14 | winners.insert(s); losers.insert(t); 15 | } 16 | for (set::iterator I = losers.begin(); I != losers.end(); I++) { 17 | winners.erase(*I); 18 | } 19 | cout << *winners.begin() << endl; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /flbrklin.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-01 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int T,N,i,x,y; 9 | pair A[30000]; 10 | scanf("%d",&T); 11 | while (T--) 12 | { 13 | scanf("%d",&N); 14 | for (i=0; i S; 21 | for (i=0; i::iterator It=S.upper_bound(A[i].second); 24 | if (It!=S.begin()) 25 | { 26 | It--; 27 | S.erase(It); 28 | } 29 | S.insert(A[i].second); 30 | } 31 | printf("%d\n",S.size()); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /major.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() { 7 | int t; scanf("%d", &t); 8 | while (t--) { 9 | int n; scanf("%d", &n); 10 | map m; 11 | for (int i = 0; i < n; i++) { 12 | int x; scanf("%d", &x); m[x]++; 13 | } 14 | bool found = false; 15 | for (map::iterator I = m.begin(); I != m.end(); I++) { 16 | if (I->second > n/2) { 17 | found = true; 18 | printf("YES %d\n", I->first); 19 | } 20 | } 21 | if (!found) printf("NO\n"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /dnpalin.cpp: -------------------------------------------------------------------------------- 1 | // 2009-06-28 2 | #include 3 | #include 4 | using namespace std; 5 | bool dnpalin(string s) 6 | { 7 | int cnt=0,i,j; 8 | for (i=0,j=s.length()-1; i> s; 23 | if (s=="*END*") 24 | return 0; 25 | bool ok=false; 26 | for (i=1; i 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() 8 | { 9 | char s[1000],t[1000]; 10 | bool print=false; 11 | double X,Y,x,y,dist=0; 12 | for (;;) 13 | { 14 | s[0]=0; 15 | gets(s); 16 | int l=strlen(s); 17 | if (!*s) return 0; 18 | reverse(s,s+l); 19 | sscanf(s,".)%[^(](",&t); 20 | l=strlen(t); 21 | reverse(t,t+l); 22 | sscanf(t,"%lf ,%lf",&x,&y); 23 | if (print) 24 | { 25 | dist+=hypot(X-x,Y-y); 26 | printf("The salesman has traveled a total of %.3f kilometers.\n",dist); 27 | } 28 | else 29 | print=true; 30 | X=x; Y=y; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /ng0frctn-2.cpp: -------------------------------------------------------------------------------- 1 | // 2009-12-24 2 | #include 3 | using namespace std; 4 | long long fusc(long long x) 5 | { 6 | //The non-recursive version. 7 | long long n_a=1,n_b=0,a=x,b=x-1; 8 | //compute n_a * fusc(a) + n_b * fusc(b); a=b+1 9 | for (;;) 10 | if (b) 11 | if (a%2) //a odd, b=a-1 12 | { 13 | n_b+=n_a; 14 | b>>=1; 15 | a=b+1; 16 | } 17 | else //a even, b=a-1 18 | { 19 | n_a+=n_b; 20 | a>>=1; 21 | b=a-1; 22 | } 23 | else //b=0, a=1 24 | return n_a; 25 | } 26 | int main() 27 | { 28 | int x; 29 | for (;;) 30 | { 31 | scanf("%d",&x); 32 | if (!x) return 0; 33 | printf("%lld/%lld\n",fusc(x),fusc(x+1)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /beads.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-20 2 | // Note: I'm pretty sure I cheated on this problem by looking at the solution 3 | #define _CRT_SECURE_NO_WARNINGS 4 | #include 5 | #include 6 | int main() 7 | { 8 | int T,L,i,j,n; 9 | scanf("%d\n",&T); 10 | char s[10010]; 11 | while (T--) 12 | { 13 | gets(s); 14 | L=strlen(s); 15 | i=0; 16 | j=1; 17 | n=0; 18 | while (i 4 | using namespace std; 5 | int get() 6 | { 7 | char c; 8 | int x=0; 9 | do 10 | { 11 | c=getchar_unlocked(); 12 | } 13 | while (c<=32); 14 | do 15 | { 16 | x=(x<<1)+(x<<3)+c-'0'; 17 | c=getchar_unlocked(); 18 | } 19 | while (c>32); 20 | return x; 21 | } 22 | int main() 23 | { 24 | int T,N,i,x,nimber; 25 | bool onesonly; 26 | T=get(); 27 | while (T--) 28 | { 29 | N=get(); 30 | onesonly=true; 31 | nimber=0; 32 | for (i=0; i1) onesonly=false; 36 | nimber^=x; 37 | } 38 | puts(!nimber^onesonly?"Brother":"John"); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /wordcnt.cpp: -------------------------------------------------------------------------------- 1 | // 2009-06-12 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int T,i,cnt,res,cur; 9 | int lengths[10000]; 10 | char s[100000]; 11 | cin >> T; cin.getline(s,10000); 12 | while (T--) 13 | { 14 | cin.getline(s,100000); 15 | istringstream ISS(s); 16 | string t; 17 | cnt=0; 18 | while (ISS >> t) 19 | lengths[cnt++]=t.length(); 20 | lengths[cnt++]=0; 21 | res=0; 22 | cur=1; 23 | for (i=1; ires) res=cur; 29 | cur=1; 30 | } 31 | printf("%d\n",res); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /comdiv.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | using namespace std; 4 | bool prime[1000]; 5 | int gcd(int x, int y) { 6 | if (x == 0) return y; 7 | return gcd(y%x, x); 8 | } 9 | int main() { 10 | int T; scanf("%d", &T); 11 | while (T--) { 12 | int a, b; scanf("%d %d", &a, &b); 13 | int g = gcd(a, b); 14 | int res = 1, p = 2; 15 | while (g > 1) { 16 | while (p < 1000 && g % p) p++; 17 | if (p == 1000) p = g; 18 | int pwr = 0; 19 | while (g % p == 0) { 20 | g /= p; ++pwr; 21 | } 22 | res *= pwr+1; 23 | } 24 | printf("%d\n", res); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /notatri.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-04 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int a[2001]; 9 | int i,j,N; 10 | for(;;) 11 | { 12 | scanf("%d",&N); 13 | if (!N) return 0; 14 | for (i=0; il) 26 | { 27 | m=(l+u+1)/2; 28 | if (a[m]+a[j]>=a[i]) 29 | u=m-1; 30 | else 31 | l=m; 32 | } 33 | total+=l; 34 | } 35 | printf("%lld\n",total); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /sqrroot.py: -------------------------------------------------------------------------------- 1 | # 2023-10-20 2 | import math 3 | num_testcases = int(input()) 4 | for testcase in range(num_testcases): 5 | n = int(input()) 6 | # generate an integer estimate for sqrt(x) 7 | l = math.log10(n) / 2 8 | l2 = 0 9 | while l >= 10: 10 | l -= 10 11 | l2 += 10 12 | x = int(10**l) * 10**l2 13 | # improve the approximation using Newton's method; stop when 14 | # we get into a cycle 15 | s = set() 16 | while not (x in s): 17 | s.add(x) 18 | x = (x * x + n) // (2 * x) 19 | # adjust until we reach the final result 20 | while x * x > n: 21 | x -= 1 22 | while x * x < n: 23 | x += 1 24 | print(x) 25 | -------------------------------------------------------------------------------- /cadydist.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | long long a[100000]; 7 | long long b[100000]; 8 | int main() { 9 | for (;;) { 10 | int N; scanf("%d", &N); if (N == 0) return 0; 11 | for (int i = 0; i < N; i++) { 12 | scanf("%lld", a+i); 13 | } 14 | for (int i = 0; i < N; i++) { 15 | scanf("%lld", b+i); 16 | } 17 | sort(a, a+N); 18 | sort(b, b+N, greater()); 19 | long long sum = 0; 20 | for (int i = 0; i < N; i++) { 21 | sum += a[i]*b[i]; 22 | } 23 | printf("%lld\n", sum); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tshow1.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() { 7 | int N; cin >> N; 8 | while (N--) { 9 | long long k; cin >> k; k--; 10 | long long pwr = 2; int len = 1; 11 | while (k >= pwr) { 12 | k -= pwr; pwr <<= 1; len++; 13 | } 14 | string res; 15 | while (len--) { 16 | if (k%2) { 17 | res.push_back('6'); 18 | } else { 19 | res.push_back('5'); 20 | } 21 | k >>= 1; 22 | } 23 | reverse(res.begin(), res.end()); 24 | cout << res << endl; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /iitkwpco-2.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-07 2 | #include 3 | #include 4 | using namespace std; 5 | int a[100]; 6 | int main() { 7 | int T; scanf("%d", &T); 8 | while (T--) { 9 | int n; scanf("%d", &n); 10 | for (int i = 0; i < n; i++) { 11 | scanf("%d", a + i); 12 | } 13 | sort(a, a+n); 14 | int res = 0; 15 | for (int i = 0; i < n; i++) { 16 | for (int j = i + 1; j < n; j++) { 17 | if (a[j] == 2*a[i]) { 18 | a[j] = -1; 19 | res++; 20 | break; 21 | } 22 | } 23 | } 24 | printf("%d\n", res); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /addrev.cpp: -------------------------------------------------------------------------------- 1 | // 2007-11-16 2 | #include 3 | 4 | int digits; 5 | int i; 6 | int d[15]; 7 | 8 | inline int rev(int n) 9 | { 10 | int digits; 11 | int i; 12 | int d[15]; 13 | if (n==0) return 0; 14 | digits=0; 15 | while (n>0) 16 | { 17 | d[digits++]=n%10; 18 | n/=10; 19 | } 20 | n=0; 21 | for (i=0; i 3 | #include 4 | using namespace std; 5 | int main() { 6 | for (int _cs = 1;; _cs++) { 7 | int N; scanf("%d", &N); 8 | if (N == 0) return 0; 9 | vector bal(N); 10 | int before = 0, after = 0; 11 | for (int i = 0; i < N; i++) { 12 | for (int j = 0; j < N; j++) { 13 | int x; scanf("%d", &x); 14 | before += x; 15 | bal[i] += x; bal[j] -= x; 16 | } 17 | } 18 | for (int i = 0; i < N; i++) { 19 | if (bal[i] > 0) after += bal[i]; 20 | } 21 | printf("%d. %d %d\n", _cs, before, after); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /jnext-2.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-27 2 | // custom implementation of next permutation 3 | #include 4 | #include 5 | using namespace std; 6 | int a[1000000]; 7 | int main() { 8 | int t; scanf("%d", &t); 9 | while (t--) { 10 | int n; scanf("%d", &n); 11 | for (int i = 0; i < n; i++) { 12 | scanf("%d", a+i); 13 | } 14 | int r = n-2; 15 | while (r >= 0 && a[r] >= a[r+1]) r--; 16 | if (r == -1) { puts("-1"); continue; } 17 | reverse(a+r+1, a+n); 18 | int i = r+1; while (a[i] <= a[r]) i++; 19 | swap(a[i], a[r]); 20 | for (int i = 0; i < n; i++) putchar('0' + a[i]); 21 | putchar('\n'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /scubadiv.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-02 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int T,O,N,n,_o,_n,_w,i,j; 8 | scanf("%d",&T); 9 | int dp[22][80]; 10 | int tmp[22][80]; 11 | while(T--) 12 | { 13 | scanf("%d %d %d",&O,&N,&n); 14 | memset(dp,0x7F,sizeof(dp)); 15 | dp[0][0]=0; 16 | while (n--) 17 | { 18 | scanf("%d %d %d",&_o,&_n,&_w); 19 | memcpy(tmp,dp,sizeof(dp)); 20 | for (i=0; i<=O; i++) 21 | for (j=0; j<=N; j++) 22 | if (tmp[min(i+_o,O)][min(j+_n,N)]>dp[i][j]+_w) 23 | tmp[min(i+_o,O)][min(j+_n,N)]=dp[i][j]+_w; 24 | memcpy(dp,tmp,sizeof(dp)); 25 | } 26 | printf("%d\n",dp[O][N]); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /edge.cpp: -------------------------------------------------------------------------------- 1 | // 2025-07-03 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | void do_testcase(string s) { 7 | cout << "300 420 moveto\n310 420 lineto\n"; 8 | int x = 310, y = 420; 9 | int dx = 10, dy = 0; 10 | for (const char c : s) { 11 | swap(dx, dy); 12 | if (c == 'V') { 13 | dx = -dx; 14 | } else { 15 | dy = -dy; 16 | } 17 | x += dx; 18 | y += dy; 19 | cout << x << ' ' << y << " lineto\n"; 20 | } 21 | cout << "stroke\nshowpage\n"; 22 | } 23 | int main() { 24 | for (;;) { 25 | string s; if (!(cin >> s)) return 0; 26 | do_testcase(s); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /hanoi07.cpp: -------------------------------------------------------------------------------- 1 | // 2024-07-26 2 | #include 3 | #include 4 | using namespace std; 5 | long long dp[32768][64][71]; // # blocks, height, # blocks on last level 6 | long long calc(int n, int h, int m) { 7 | if (n < 0) return 0; 8 | if (h == 0) return 1; 9 | long long& result = dp[n][h][m]; 10 | if (result == -1) { 11 | result = calc(n - m - 1, h - 1, m + 1); 12 | if (m > 1) result += calc(n - m + 1, h - 1, m - 1); 13 | } 14 | return result; 15 | } 16 | int main() { 17 | memset(dp, -1, sizeof(dp)); 18 | int T; cin >> T; 19 | while (T--) { 20 | int N, H, M; cin >> N >> H >> M; 21 | cout << calc(N - M, H - 1, M) << '\n'; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /headshot.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-27 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | string s; cin >> s; 7 | int n = s.length(); 8 | int a = 0, b = n, c = 0, d; 9 | for (int i = 0; i < s.length(); i++) { 10 | if (s[i] == '1') a++; 11 | } 12 | d = n - a; 13 | for (int i = 1; i < s.length(); i++) { 14 | if (s[i] == '1' && s[i-1] == '0') c++; 15 | } 16 | c += (s[n-1] == '0' && s[0] == '1'); 17 | // if a/b is greater, then shoot, otherwise rotate 18 | if (a*d > b*c) { 19 | printf("SHOOT\n"); 20 | } else if (a*d == b*c) { 21 | printf("EQUAL\n"); 22 | } else { 23 | printf("ROTATE\n"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /maximus.py: -------------------------------------------------------------------------------- 1 | # 2024-08-20 2 | 3 | import functools 4 | import sys 5 | 6 | is_prime = [True] * 5001 7 | for i in range(2, 5001): 8 | if is_prime[i]: 9 | j = 2*i 10 | while j <= 5000: 11 | is_prime[j] = False 12 | j += i 13 | 14 | @functools.cache 15 | def move(a, n, is_forward): 16 | if a > n: return 0 17 | if is_forward == is_prime[a]: 18 | return 1 + 2 * move(a + 1, n, not is_forward) 19 | else: 20 | return (2 + 2 * move(a + 1, n, is_forward) + 21 | move(a + 1, n, not is_forward)) 22 | 23 | sys.setrecursionlimit(1000000) 24 | 25 | while True: 26 | N = int(input()) 27 | if N == 0: break 28 | print(move(1, N, False)) 29 | -------------------------------------------------------------------------------- /pie.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-22 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #define PI 3.1415926535898 5 | int main() 6 | { 7 | int T,i,N,F,j; 8 | double A[10000]; 9 | scanf("%d",&T); 10 | for (i=0; i0.0005) 25 | { 26 | m=(l+u)/2; 27 | int __c=0; 28 | for (j=0; j=F+1) 31 | l=m; 32 | else 33 | u=m; 34 | } 35 | printf("%.3lf\n",l); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /atms.py: -------------------------------------------------------------------------------- 1 | # 2023-10-01 2 | def get_rep(x): 3 | result = [] 4 | while x != 0: 5 | if x % 2 == 0: 6 | result.append(0) 7 | else: 8 | result.append(1) 9 | x -= 1 10 | x //= -2; 11 | return result 12 | 13 | def print_rep(rep): 14 | if len(rep) > 100: 15 | print('No') 16 | return 17 | i = len(rep) - 1 18 | used = [] 19 | while i >= 0: 20 | if rep[i] > 0: 21 | used.append(str(i)) 22 | i -= 1 23 | print(' '.join(used)) 24 | 25 | n = int(input()) 26 | for testcase in range(n): 27 | x = int(input()) 28 | rep = get_rep(x) 29 | print_rep(rep) 30 | rep = get_rep(-x) 31 | print_rep(rep) 32 | -------------------------------------------------------------------------------- /miserman.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-26 2 | #include 3 | #include 4 | using namespace std; 5 | int dp[101][100]; 6 | int main() { 7 | int N, M; 8 | cin >> N >> M; 9 | memset(dp[0], 0, sizeof(dp[0])); 10 | for (int i = 0; i < N; i++) { 11 | for (int j = 0; j < M; j++) { 12 | int val; cin >> val; 13 | dp[i+1][j] = val + dp[i][j]; 14 | if (j > 0) dp[i+1][j] = min(dp[i+1][j], val + dp[i][j-1]); 15 | if (j < M-1) dp[i+1][j] = min(dp[i+1][j], val + dp[i][j+1]); 16 | } 17 | } 18 | int best = 1e9; 19 | for (int i = 0; i < M; i++) { 20 | best = min(best, dp[N][i]); 21 | } 22 | cout << best << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /aibohp.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-23 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() 8 | { 9 | int t,L,i,j; 10 | char s[6101]; 11 | char s2[6101]; 12 | static short a[6101][6101]; 13 | scanf("%d",&t); 14 | getchar(); 15 | while(t--) 16 | { 17 | gets(s); 18 | L=strlen(s); 19 | strcpy(s2,s); 20 | reverse(s2,s2+L); 21 | for (i=0; i<=L; i++) 22 | { 23 | a[i][0]=0; 24 | a[0][i]=0; 25 | } 26 | for (i=1; i<=L; i++) 27 | for (j=1; j<=L; j++) 28 | if (s[i-1]==s2[j-1]) 29 | a[i][j]=a[i-1][j-1]+1; 30 | else 31 | a[i][j]=max(a[i-1][j],a[i][j-1]); 32 | printf("%d\n",L-a[L][L]); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /bytesm2.cpp: -------------------------------------------------------------------------------- 1 | // 2009-03-01 2 | #include 3 | using namespace std; 4 | int in() 5 | { 6 | int x=0; 7 | char c; 8 | do 9 | { 10 | c=getchar_unlocked(); 11 | } 12 | while (c<=32); 13 | do 14 | { 15 | x=(x<<1)+(x<<3)+c-'0'; 16 | c=getchar_unlocked(); 17 | } 18 | while (c>32); 19 | return x; 20 | } 21 | int main() 22 | { 23 | int T,R,C,i,j,x; 24 | T=in(); 25 | while (T--) 26 | { 27 | int dp[102][102]={0}; 28 | R=in(); C=in(); 29 | for (i=1; i<=R; i++) 30 | for (j=1; j<=C; j++) 31 | { 32 | x=in(); 33 | dp[i][j]=x+max(max(dp[i-1][j-1],dp[i-1][j]),dp[i-1][j+1]); 34 | } 35 | x=0; 36 | for (i=1; i<=C; i++) 37 | x=max(x,dp[R][i]); 38 | printf("%d\n",x); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /lines.cpp: -------------------------------------------------------------------------------- 1 | // 2008-10-24 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define INF 99999 8 | #define MAXN 20001 9 | 10 | int main () { 11 | int N; 12 | int i,j; 13 | float slope,x1,y1; 14 | int x[MAXN],y[MAXN]; 15 | 16 | for(;;) 17 | { 18 | scanf("%d",&N); 19 | if (!N) return 0; 20 | set s; 21 | for(i=0;i 3 | #include 4 | char buf[33333]; 5 | void do_testcase() { 6 | fgets(buf, sizeof(buf), stdin); 7 | const int N = strlen(buf) - 1; 8 | buf[N] = 0; 9 | for (int i = 0; i < N; i += 2) { 10 | if (i == N - 1 || buf[i] == '0') { 11 | break; 12 | } 13 | if (buf[i + 1] == '1') { 14 | for (int j = i + 1; j < N; j++) { 15 | buf[j] = '0' + '1' - buf[j]; 16 | } 17 | break; 18 | } 19 | buf[i + 1] = '1'; 20 | } 21 | puts(buf); 22 | } 23 | int main() { 24 | int T; scanf("%d", &T); 25 | fgets(buf, sizeof(buf), stdin); 26 | while (T--) do_testcase(); 27 | } 28 | -------------------------------------------------------------------------------- /rain3.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-02 2 | // modified 2014-10-07 3 | const int items = 15000000; 4 | #include 5 | int main() 6 | { 7 | int T,i,s,t,N,M,begin,sum; 8 | static int rain[items]; 9 | scanf("%d",&T); 10 | while (T--) 11 | { 12 | scanf("%d %d %d %d",&s,&t,&N,&M); 13 | sum=0; 14 | begin=0; 15 | int m=1000000000; 16 | for (i=0; iM&&begin<=i) 23 | { 24 | sum-=rain[begin]; 25 | begin++; 26 | } 27 | if (begin>0&&m>i-begin+1) 28 | m=i-begin+1; 29 | } 30 | if (m==1000000000) 31 | m=N; 32 | printf("%d\n",m); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /cubes.cpp: -------------------------------------------------------------------------------- 1 | // 2008-04-19 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | static bool cube[1000000]; 7 | static int cbrt[1000000]; 8 | int i,a,b,c; 9 | for (i=1; i<=100; i++) 10 | { 11 | cube[i*i*i]=true; 12 | cbrt[i*i*i]=i; 13 | } 14 | for (a=2; a<=100; a++) 15 | { 16 | if (a==9) continue; //stupid 17 | int a3=a*a*a; 18 | for (b=1; b 3 | void sort(int a[],int l,int u) 4 | { 5 | int i,j,k; 6 | unsigned int x; 7 | for (i=l+1; i<=u; i++) 8 | { 9 | j=i; 10 | x=a[i]; 11 | while ((j>l)&&(a[j-1]>x)) j--; 12 | for (k=i; k>j; k--) 13 | a[k]=a[k-1]; 14 | a[j]=x; 15 | } 16 | } 17 | int main() 18 | { 19 | int t,i,j,N,total; 20 | int M[1000]; 21 | int W[1000]; 22 | scanf("%d",&t); 23 | for (i=0; i 3 | using namespace std; 4 | int main() 5 | { 6 | int dp[501]; 7 | int b,n,i,cost,fun,j; 8 | for(;;) 9 | { 10 | cin >> b >> n; 11 | if (b==0 && n==0) 12 | return 0; 13 | memset(dp,0,sizeof(dp)); 14 | for (i=0; i> cost >> fun; 17 | for (j=b-cost; j>=0; j--) 18 | if (dp[j+cost] 3 | #define MAX 34000 4 | using namespace std; 5 | int main() 6 | { 7 | static bool a[MAX]; 8 | int b[3000]; 9 | int i,j,k,m,n,N; 10 | memset(a,true,sizeof(a)); 11 | j=2; 12 | n=0; 13 | for (i=0; i<3000; i++) 14 | { 15 | while (!a[j]) j++; 16 | a[j]=false; 17 | b[n++]=j; 18 | //cout << j << endl; 19 | k=j; 20 | while (k> N; 38 | if (!N) break; 39 | cout << b[N-1] << endl; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /games.cpp: -------------------------------------------------------------------------------- 1 | // 2014-09-22 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int gcd(int x, int y) { 7 | if (x == 0) return y; 8 | else return gcd(y%x, x); 9 | } 10 | int main() { 11 | ios::sync_with_stdio(false); 12 | int t; cin >> t; 13 | while (t--) { 14 | string s; cin >> s; 15 | int p = s.find("."); 16 | if (p == string::npos) { 17 | cout << "1\n"; 18 | } else { 19 | string f = s.substr(p+1); 20 | while (f.size() < 4) f += "0"; 21 | istringstream iss(f); 22 | int frac; iss >> frac; 23 | cout << 10000/gcd(10000, frac) << "\n"; 24 | } 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /grocery.cpp: -------------------------------------------------------------------------------- 1 | // 2025-05-31 2 | // The idea is to first write a slow solution, which lets us then determine that 3 | // all the outputs fall within certain bounds, so we don't need to check 4 | // anything outside those bounds. 5 | #include 6 | #define ll long long 7 | int main() { 8 | ll M=1e6; 9 | #define F(i,s) for(ll i=s;i<2000;i++) 10 | F(a,1) F(b,a) F(c,b) { 11 | if (a>125||b>592||c>884) break; 12 | ll p=M*(a+b+c),q=a*b*c-M; 13 | if (q&&!(p%q)&&p/q>0) { 14 | ll d = p/q; 15 | #define D(v) int(v/100),int(v%100) 16 | if (c<=d&&a+b+c+d<=2000) 17 | printf("%d.%02d %d.%02d %d.%02d %d.%02d\n", 18 | D(a),D(b),D(c),D(d)); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /gnyr09f.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-03 2 | #include 3 | using namespace std; 4 | int dp[101][100][2]; // initialized to 0 5 | int main() { 6 | dp[1][0][0] = 1; 7 | dp[1][0][1] = 1; 8 | for (int n = 2; n <= 100; n++) { 9 | for (int k = 0; k < n; k++) { 10 | dp[n][k][0] = dp[n-1][k][0] + dp[n-1][k][1]; 11 | dp[n][k][1] = dp[n-1][k][0]; 12 | if (k > 0) dp[n][k][1] += dp[n-1][k-1][1]; 13 | } 14 | } 15 | int P; cin >> P; 16 | while (P--) { 17 | int cs, n, k; scanf("%d %d %d", &cs, &n, &k); 18 | if (k < 0 || k >= n) { 19 | printf("%d 0\n", cs); 20 | } else { 21 | printf("%d %d\n", cs, dp[n][k][0] + dp[n][k][1]); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mmaxper.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-02 2 | #include 3 | #include 4 | using namespace std; 5 | int w[1000], h[1000]; 6 | int dp[1000][2]; 7 | int main() { 8 | int n; cin >> n; 9 | for (int i = 0; i < n; i++) { 10 | cin >> w[i] >> h[i]; 11 | if (i == 0) { 12 | dp[0][0] = w[0]; 13 | dp[0][1] = h[0]; 14 | } 15 | else { 16 | dp[i][0] = w[i] + max(dp[i-1][0] + abs(h[i] - h[i-1]), 17 | dp[i-1][1] + abs(h[i] - w[i-1])); 18 | dp[i][1] = h[i] + max(dp[i-1][0] + abs(w[i] - h[i-1]), 19 | dp[i-1][1] + abs(w[i] - w[i-1])); 20 | } 21 | } 22 | cout << max(dp[n-1][0], dp[n-1][1]) << endl; 23 | } 24 | -------------------------------------------------------------------------------- /rple.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-07 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int T; scanf("%d", &T); 7 | for (int _cs = 1; _cs <= T; _cs++) { 8 | int N, R; scanf("%d %d", &N, &R); 9 | set spies, targets; 10 | bool spied = false; 11 | while (R--) { 12 | int x, y; scanf("%d %d", &x, &y); 13 | spies.insert(x); targets.insert(y); 14 | } 15 | for (set::iterator I = spies.begin(); I != spies.end(); I++) { 16 | if (targets.find(*I) != targets.end()) { 17 | spied = true; 18 | } 19 | } 20 | printf("Scenario #%d: %s\n\n", _cs, spied ? "spied" : "spying"); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /zsum.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-30 2 | #include 3 | using namespace std; 4 | const long long MOD = 10000007; 5 | long long modexp(long long base, long long exponent) { 6 | long long pwr = base; 7 | long long res = 1; 8 | while (exponent) { 9 | if (exponent & 1) { 10 | res = (res * pwr) % MOD; 11 | } 12 | exponent >>= 1; 13 | pwr = (pwr * pwr) % MOD; 14 | } 15 | return res; 16 | } 17 | int main() { 18 | ios::sync_with_stdio(false); 19 | for (;;) { 20 | int n, k; cin >> n >> k; if (n == 0) return 0; 21 | long long res = modexp(n, k) + 2*modexp(n-1, k) 22 | + modexp(n, n) + 2*modexp(n-1, n-1); 23 | cout << res % MOD << endl; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /chicago.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-22 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | int main() 6 | { 7 | long double dist[100][100]; 8 | int i,j,k,V,E; 9 | int V1,V2,D; 10 | for(;;) 11 | { 12 | scanf("%d %d",&V,&E); 13 | if (V==0) 14 | return 0; 15 | for (i=0; i 3 | #include 4 | using namespace std; 5 | int main() { 6 | int N; 7 | scanf("%d", &N); 8 | int x = 0, y = 0, z = 0; 9 | for (int i = 0; i < N; i++) { 10 | int a, b; 11 | scanf("%d/%d", &a, &b); 12 | if (a == 3) z++; 13 | else if (b == 4) x++; 14 | else y++; 15 | } 16 | int res = 0; 17 | res += z; 18 | x = max(0, x - z); 19 | res += y/2; 20 | y %= 2; 21 | res += x/4; 22 | x %= 4; 23 | if (y == 1) { 24 | if (x <= 2) { 25 | res += 1; 26 | } else { 27 | res += 2; 28 | } 29 | } else if (x >= 1) { 30 | res += 1; 31 | } 32 | printf("%d\n", res + 1); 33 | } 34 | -------------------------------------------------------------------------------- /phonelst.cpp: -------------------------------------------------------------------------------- 1 | // 2011-03-20 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | bool is_prefix(string& s1,string& s2) 8 | { 9 | int i; 10 | for (i=0; i=s2.length()||s1[i]!=s2[i]) return false; 12 | return true; 13 | } 14 | int main() 15 | { 16 | int T,n,i; 17 | char s[100]; 18 | string a[10000]; 19 | scanf("%d",&T); 20 | while (T--) 21 | { 22 | scanf("%d",&n); 23 | for (i=0; i 3 | #include 4 | using namespace std; 5 | int main() { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | long long res; cin >> res; 10 | string op; 11 | for (;;) { 12 | long long x; 13 | cin >> op; 14 | if (op == "+") { 15 | cin >> x; res += x; 16 | } else if (op == "-") { 17 | cin >> x; res -= x; 18 | } else if (op == "*") { 19 | cin >> x; res *= x; 20 | } else if (op == "/") { 21 | cin >> x; res /= x; 22 | } else { 23 | cout << res << endl; 24 | break; 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /m3tile.cpp: -------------------------------------------------------------------------------- 1 | // 2009-02-28 2 | #include 3 | using namespace std; 4 | int dp[31][2][2][2]; 5 | int main() 6 | { 7 | dp[0][1][1][1]=1; 8 | dp[1][0][0][0]=1; 9 | dp[1][1][1][0]=1; 10 | dp[1][0][1][1]=1; 11 | int i; 12 | for (i=2; i<=30; i++) 13 | { 14 | dp[i][0][0][0]=dp[i-1][1][1][1]; 15 | dp[i][0][0][1]=dp[i-1][1][1][0]; 16 | dp[i][0][1][0]=dp[i-1][1][0][1]; 17 | dp[i][0][1][1]=dp[i-1][1][1][1]+dp[i-1][1][0][0]; 18 | dp[i][1][0][0]=dp[i-1][0][1][1]; 19 | dp[i][1][0][1]=dp[i-1][0][1][0]; 20 | dp[i][1][1][0]=dp[i-1][1][1][1]+dp[i-1][0][0][1]; 21 | dp[i][1][1][1]=dp[i-1][0][0][0]+dp[i-1][0][1][1]+dp[i-1][1][1][0]; 22 | } 23 | for(;;) 24 | { 25 | scanf("%d",&i); 26 | if (!~i) return 0; 27 | printf("%d\n",dp[i][1][1][1]); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /polygame.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-23 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int t,V,x,y,z,i,w,n1,n2,n3; 8 | scanf("%d",&t); 9 | while (t--) 10 | { 11 | scanf("%d",&V); 12 | scanf("%d %d %d",&x,&y,&z); 13 | for (i=0; iy) 16 | swap(x,y); 17 | if (y>z) 18 | swap(y,z); 19 | if (x>y) 20 | swap(x,y); 21 | n1=y-x-1; 22 | n2=z-y-1; 23 | n3=x-z+V-1; 24 | if (n1>n2) 25 | swap(n1,n2); 26 | if (n2>n3) 27 | swap(n2,n3); 28 | if (n1>n2) 29 | swap(n1,n2); 30 | if (n1==0&&n2==0) 31 | printf("YES\n"); 32 | else if ((n1+n2+n3)&1) 33 | printf("YES\n"); 34 | else 35 | printf("NO\n"); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /candy.cpp: -------------------------------------------------------------------------------- 1 | // 2008-04-19 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | static int a[10000]; 8 | int N,i; 9 | for(;;) 10 | { 11 | cin >> N; 12 | if (N==-1) return 0; 13 | int sum=0; 14 | for (i=0; i> a[i]; 17 | sum+=a[i]; 18 | } 19 | if (sum%N) 20 | { 21 | cout << "-1\n"; 22 | continue; 23 | } 24 | sort(a,a+N); 25 | int last=N-1; 26 | int first=0; 27 | int _count=0; 28 | while (a[last]>a[first]) 29 | { 30 | while (a[first]sum/N) 31 | { 32 | a[first]++; 33 | a[last]--; 34 | _count++; 35 | } 36 | if (a[first]>=sum/N) 37 | first++; 38 | else 39 | last--; 40 | } 41 | cout << _count << endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /partsum.cpp: -------------------------------------------------------------------------------- 1 | // 2024-09-20 2 | #include 3 | #include 4 | using namespace std; 5 | void do_testcase() { 6 | int N, K, P; scanf("%d %d %d", &N, &K, &P); 7 | set S = {0}; 8 | int psum = 0; 9 | int best = P; 10 | for (int i = 0; i < N; i++) { 11 | int x; scanf("%d", &x); 12 | psum = (psum + x) % P; 13 | auto it = S.upper_bound((psum - K + P) % P); 14 | if (it == S.begin()) { 15 | it = S.end(); 16 | } 17 | --it; 18 | const int rem = (psum - *it + P) % P; 19 | if (rem >= K && rem < best) best = rem; 20 | S.insert(psum); 21 | } 22 | printf("%d\n", best); 23 | } 24 | int main() { 25 | int T; scanf("%d", &T); 26 | while (T--) do_testcase(); 27 | } 28 | -------------------------------------------------------------------------------- /stone.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-26 2 | // modified 2014-10-07 3 | #include 4 | typedef long long i64; 5 | int main() 6 | { 7 | int T,i,N,j; 8 | static int X[1000000]; 9 | static int Y[1000000]; 10 | scanf("%d",&T); 11 | for (i=0; i 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int a[1000000]; 8 | int main() { 9 | int n, k; 10 | scanf("%d", &n); 11 | for (int i = 0; i < n; i++) { 12 | scanf("%d", a+i); 13 | } 14 | scanf("%d", &k); 15 | deque > D; 16 | for (int r = 0; r < n; r++) { 17 | while (!D.empty() && a[r] >= D.front().first) { 18 | D.pop_front(); 19 | } 20 | D.push_front(make_pair(a[r], r)); 21 | while (D.back().second <= r - k) { 22 | D.pop_back(); 23 | } 24 | if (r >= k) putchar(' '); 25 | if (r >= k-1) printf("%d", D.back().first); 26 | } 27 | putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /colcoin.cpp: -------------------------------------------------------------------------------- 1 | // 2023-09-25 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() { 7 | int T; scanf("%d", &T); 8 | for (int i = 1; i <= T; i++) { 9 | int n; scanf("%d", &n); 10 | vector coins(n); 11 | for (int j = 0; j < n; j++) { 12 | scanf("%d", &coins[j]); 13 | } 14 | sort(coins.begin(), coins.end()); 15 | int result = 0; 16 | long long sum = 0; 17 | for (int i = 0; i < n - 1; i++) { 18 | if (sum + coins[i] < coins[i+1]) { 19 | sum += coins[i]; 20 | ++result; 21 | } 22 | } 23 | if (n > 0) ++result; 24 | printf("Case #%d: %d\n", i, result); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /icoder.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-20 2 | #include 3 | #ifdef _MSC_VER 4 | #define GC getchar 5 | #else 6 | #define GC getchar_unlocked 7 | #endif 8 | int input() 9 | { 10 | int x=0; 11 | char c; 12 | for(;;) 13 | { 14 | c=GC(); 15 | if (c==' '||c=='\n') 16 | return x; 17 | x=10*x+c-'0'; 18 | } 19 | } 20 | int main() 21 | { 22 | int N,X,i,p; 23 | char c; 24 | for(;;) 25 | { 26 | N=input(); 27 | p=16; 28 | if (N==0) 29 | return 0; 30 | for (i=0; i>=1; 40 | C++; 41 | } 42 | if (C>=p) 43 | p=0; 44 | else 45 | p-=C; 46 | } 47 | printf("%d\n",1< 3 | using namespace std; 4 | int main() 5 | { 6 | int d; 7 | int i,n,k,j,x; 8 | bool must[38]; 9 | int dp[38][39]; 10 | cin >> d; 11 | for (i=0; i> n >> k; 14 | memset(must,false,sizeof(must)); 15 | for (j=0; j> x; 18 | must[x-1]=true; 19 | } 20 | //DP state: character | #opening-#closing 21 | memset(dp,0,sizeof(dp)); 22 | dp[0][1]=1; 23 | for (j=1; j<2*n; j++) 24 | { 25 | //add opening bracket 26 | for (k=1; k<=38; k++) 27 | dp[j][k]+=dp[j-1][k-1]; 28 | //add closing bracket if possible 29 | if (!must[j]) 30 | for (k=0; k<38; k++) 31 | dp[j][k]+=dp[j-1][k+1]; 32 | } 33 | cout << dp[2*n-1][0] << endl; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /wpc4f.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-01 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int T; cin >> T; 7 | while (T--) { 8 | int n; cin >> n; 9 | int dp[21][3]; 10 | memset(dp, 0x3f, sizeof dp); 11 | dp[0][0] = dp[0][1] = dp[0][2] = 0; 12 | for (int i = 0; i < n; i++) { 13 | int a[3]; cin >> a[0] >> a[1] >> a[2]; 14 | for (int j = 0; j < 3; j++) { 15 | for (int k = 0; k < 3; k++) { 16 | if (j != k) { 17 | dp[i+1][j] = min(dp[i+1][j], dp[i][k] + a[j]); 18 | } 19 | } 20 | } 21 | } 22 | printf("%d\n", min(min(dp[n][0], dp[n][1]), dp[n][2])); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /alien.cpp: -------------------------------------------------------------------------------- 1 | // 2014-09-23 2 | #include 3 | int main() { 4 | int T; scanf("%d", &T); 5 | int p[100000]; 6 | while (T--) { 7 | int A, B; 8 | scanf("%d %d", &A, &B); 9 | for (int i = 0; i < A; i++) { 10 | scanf("%d", p+i); 11 | } 12 | int l = 0, r = 0, sum = 0; 13 | int bests = -1, bestp; 14 | for (;;) { 15 | if (sum <= B) { 16 | if (r - l > bests || r - l == bests && sum < bestp) { 17 | bests = r -l; bestp = sum; 18 | } 19 | if (r == A) break; 20 | else sum += p[r++]; 21 | } else { 22 | sum -= p[l++]; 23 | } 24 | } 25 | printf("%d %d\n", bestp, bests); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /bytese2.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() { 7 | int T; scanf("%d", &T); 8 | while (T--) { 9 | int N; scanf("%d", &N); 10 | pair a[200]; 11 | for (int i = 0; i < N; i++) { 12 | int x, y; scanf("%d %d", &x, &y); 13 | a[2*i] = make_pair(x, 0); a[2*i+1] = make_pair(y, 1); 14 | } 15 | sort(a, a+2*N); 16 | int res = 0, cur = 0; 17 | for (int i = 0; i < 2*N; i++) { 18 | if (a[i].second) { 19 | cur--; 20 | } else { 21 | cur++; 22 | } 23 | res = max(res, cur); 24 | } 25 | printf("%d\n", res); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /cfrac2.cpp: -------------------------------------------------------------------------------- 1 | // 2024-09-20 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | void do_testcase(int R, int) { 8 | string s; 9 | vector a; 10 | for (int i = 0; i < R; i++) { 11 | cin >> s; 12 | if (i % 2) { 13 | s.erase(0, s.find_first_not_of(".")); 14 | a.push_back(stoll(s)); 15 | } 16 | } 17 | unsigned long long p = 1, q = 1; 18 | for (int i = a.size() - 1; i >= 0; i--) { 19 | swap(p, q); 20 | p += a[i] * q; 21 | } 22 | cout << p << ' ' << q << '\n'; 23 | } 24 | int main() { 25 | int R, C; 26 | for (;;) { 27 | cin >> R >> C; 28 | if (R == 0) return 0; 29 | do_testcase(R, C); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /dyzio.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-14 2 | #include 3 | #ifdef _MSC_VER 4 | #define getchar_unlocked getchar 5 | #endif 6 | int input() 7 | { 8 | int x=0; 9 | char c; 10 | for(;;) 11 | { 12 | c=getchar_unlocked(); 13 | if (c=='\n') 14 | return x; 15 | x=10*x+c-'0'; 16 | } 17 | } 18 | int a,b,c; 19 | void solve(int level) 20 | { 21 | if (level>a) 22 | { 23 | a=level; 24 | c=b; 25 | } 26 | char C=getchar_unlocked(); 27 | if (C=='0') 28 | return; 29 | else 30 | { 31 | b++; 32 | solve(level+1); 33 | solve(level+1); 34 | } 35 | } 36 | int main() 37 | { 38 | int i,n; 39 | for (i=0; i<10; i++) 40 | { 41 | n=input(); 42 | a=-1; 43 | b=0; 44 | solve(0); 45 | getchar_unlocked(); 46 | printf("%d\n",c); 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /dsubseq.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-02 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int dp[100001]; 7 | int sum[100001]; 8 | int last[100]; 9 | const int MOD = 1000000007; 10 | int main() { 11 | ios::sync_with_stdio(false); 12 | int T; cin >> T; 13 | while (T--) { 14 | string s; cin >> s; 15 | dp[0] = 1; sum[0] = 1; 16 | memset(last, -1, sizeof last); 17 | for (int i = 0; i < s.length(); i++) { 18 | dp[i+1] = sum[i]; 19 | if (~last[s[i]]) { 20 | dp[i+1] = (dp[i+1] - sum[last[s[i]]] + MOD)%MOD; 21 | } 22 | sum[i+1] = (sum[i] + dp[i+1])%MOD; 23 | last[s[i]] = i; 24 | } 25 | cout << sum[s.length()] << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /hepnum.cpp: -------------------------------------------------------------------------------- 1 | // 2014-06-01 2 | #include 3 | #include 4 | using namespace std; 5 | char s1[111111], s2[111111]; 6 | int main() { 7 | for (;;) { 8 | scanf("%s %s", s1, s2); 9 | if (s1[0] == '*') return 0; 10 | char *n1 = s1, *n2 = s2; 11 | while (n1[0] == '0') n1++; 12 | while (n2[0] == '0') n2++; 13 | int l1 = strlen(n1); 14 | int l2 = strlen(n2); 15 | if (l1 < l2) { 16 | puts("<"); continue; 17 | } else if (l1 > l2) { 18 | puts(">"); continue; 19 | } 20 | int res = strcmp(n1, n2); 21 | if (res < 0) { 22 | puts("<"); 23 | } else if (res == 0) { 24 | puts("="); 25 | } else { 26 | puts(">"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /aggrcow.cpp: -------------------------------------------------------------------------------- 1 | // 2008-04-20 2 | #include 3 | #include 4 | using namespace std; 5 | int N,C; 6 | static int a[100000]; 7 | bool doable(int dist) 8 | { 9 | int _count=1; 10 | int i=0,j; 11 | while (_count 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int T; 8 | cin >> T; 9 | int i; 10 | int amplifier[100000]; 11 | int a[100000]; 12 | for (i=0; i> Ni; 16 | for (j=0; j> n; 20 | if (n==1) ones++; else amplifier[j-ones]=n; 21 | } 22 | Ni-=ones; 23 | sort(amplifier,amplifier+Ni); 24 | if (Ni==2 && amplifier[0]==2 && amplifier[1]==3); 25 | else 26 | reverse(amplifier,amplifier+Ni); 27 | for (j=0; j 3 | #define MIN(x,y) ((x)<(y)?(x):(y)) 4 | double dist[100000]; 5 | double K; 6 | int N; 7 | bool f(double T) 8 | { 9 | double d=dist[0]+T; 10 | int i; 11 | for (i=1; id+K) 14 | return false; 15 | d=MIN(d+K,dist[i]+T); 16 | } 17 | return true; 18 | } 19 | int main() 20 | { 21 | int i,t,j; 22 | scanf("%d",&t); 23 | for (i=0; i0.005) 32 | { 33 | TMid=(THigh+TLow)/2.0; 34 | if (f(TMid)) 35 | THigh=TMid; 36 | else 37 | TLow=TMid; 38 | } 39 | printf("%.2lf\n",TMid); 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /stpar.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-07 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int n,i; 8 | int l[2000]; 9 | int N; 10 | for(;;) 11 | { 12 | scanf("%d",&n); 13 | stack S; 14 | if (n==0) return 0; 15 | for (i=0; inext) 27 | { 28 | if (S.size()&&S.top() 3 | #include 4 | using namespace std; 5 | vector primes; 6 | void do_testcase() { 7 | int n, k; cin >> n >> k; --n; 8 | n = primes[n]; 9 | vector dp(n + 1, 0); 10 | dp[0] = 1; 11 | for (int i = 1; i <= k; i++) { 12 | for (int j = i; j <= n; j++) { 13 | dp[j] += dp[j - i]; 14 | } 15 | } 16 | cout << dp[n] << '\n'; 17 | } 18 | int main() { 19 | bool isComposite[8000] = {0}; 20 | for (int i = 2; i < 8000; i++) { 21 | if (isComposite[i]) continue; 22 | if (i == 2 || (i % 4 == 1)) primes.push_back(i); 23 | for (int j = 2*i; j < 8000; j += i) { 24 | isComposite[j] = true; 25 | } 26 | } 27 | int T; cin >> T; 28 | while (T--) do_testcase(); 29 | } 30 | -------------------------------------------------------------------------------- /eqbox.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-07 2 | // modified 2010-10-07 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | typedef long long i64; 8 | inline i64 SQR(i64 x) 9 | { 10 | return x*x; 11 | } 12 | int main() 13 | { 14 | int i,t; 15 | i64 A,B,X,Y; 16 | scanf("%d",&t); 17 | for (i=0; iX) 23 | if (B>Y) 24 | printf("Escape is possible.\n"); 25 | else 26 | printf("Box cannot be dropped.\n"); 27 | else if (Y>=B) 28 | printf("Box cannot be dropped.\n"); 29 | else if (B*(X*X+Y*Y)-2*A*X*Y>(X*X-Y*Y)*sqrt(X*X+Y*Y-A*A)) 30 | printf("Escape is possible.\n"); 31 | else 32 | printf("Box cannot be dropped.\n"); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /leonardo.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-15 2 | #include 3 | int main() 4 | { 5 | char s[30]; 6 | bool vis[26]; 7 | int len[27]; 8 | int i,j,k,/*m,*/t; 9 | scanf("%d",&t); 10 | for (i=0; i 3 | #include 4 | #include 5 | using namespace std; 6 | int a[1000], b[1000]; 7 | int main() { 8 | int T; cin >> T; 9 | while (T--) { 10 | int M, N; cin >> M; 11 | for (int i = 0; i < M; i++) { 12 | cin >> a[i]; 13 | } 14 | cin >> N; 15 | for (int i = 0; i < N; i++) { 16 | cin >> b[i]; 17 | } 18 | sort(a, a+M); sort(b, b+N); 19 | int i = 0, j = 0; int best = 1e9; 20 | while (i < M && j < N) { 21 | best = min(best, abs(a[i] - b[j])); 22 | if (a[i] <= b[j]) { 23 | i++; 24 | } else if (a[i] > b[j]) { 25 | j++; 26 | } 27 | } 28 | cout << best << endl; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /cneasy.cpp: -------------------------------------------------------------------------------- 1 | // 2008-12-11 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int ceil(double x) 7 | { 8 | x=x+1e-8; 9 | double z=x-int(x); 10 | if (z<1e-8) 11 | return int(x); 12 | else 13 | return int(x)+1; 14 | } 15 | int main() 16 | { 17 | int t,i,n,j; 18 | string s; 19 | cin >> t; 20 | double a[1000]; 21 | for (i=0; i> n; 24 | for (j=0; j> s >> a[j]; 26 | sort(a,a+n); 27 | double maxdiff=0.0; 28 | for (j=0; jmaxdiff) 32 | maxdiff=diff; 33 | else if (diff<0.0 && diff+360.0>maxdiff) 34 | maxdiff=diff+360.0; 35 | } 36 | double seconds=12.0*(360.0-maxdiff); 37 | cout << ceil(seconds) << endl; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /mixtures.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-24 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | inline int min(int x,int y) 5 | { 6 | return x 3 | #define _(x) ((x)%2?(x)-1:(x)+1) 4 | int in() 5 | { 6 | int x=0; 7 | char c; 8 | for(;;) 9 | { 10 | c=getchar_unlocked(); 11 | if (c<=32) return x; 12 | x=(x<<1)+(x<<3)+c-'0'; 13 | } 14 | } 15 | int main() 16 | { 17 | static int tree[16][32768]; 18 | int h,i,H,n,m; 19 | h=in(); 20 | for (H=0; H<=h; H++) 21 | for (i=0; i<(1<=0) 28 | H--,m/=2; 29 | if ((h-H)%2) 30 | tree[h][n]=0; 31 | else 32 | tree[h][n]=1; 33 | putchar_unlocked(tree[h][n]+'0'); putchar_unlocked('\n'); 34 | m=n; H=h; 35 | while (tree[H][_(m)]>=0) 36 | tree[H-1][m/2]=!tree[H],H--,m/=2; 37 | } 38 | //scanf("%d",&n); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /anarc05h.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-28 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | long long memo[26][230]; 7 | long long rec(const string& s, int pos, int last) { 8 | if (pos == s.length()) return 1; 9 | long long& res = memo[pos][last]; 10 | if (res == -1) { 11 | res = 0; 12 | int sum = 0; 13 | for (int i = pos; i < s.length(); i++) { 14 | sum += s[i] - '0'; 15 | if (sum >= last) { 16 | res += rec(s, i+1, sum); 17 | } 18 | } 19 | } 20 | return res; 21 | } 22 | int main() { 23 | for (int _cs = 1;; _cs++) { 24 | string s; cin >> s; 25 | if (s == "bye") return 0; 26 | memset(memo, -1, sizeof memo); 27 | printf("%d. %lld\n", _cs, rec(s, 0, 0)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /army.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-10 2 | #include 3 | #ifndef _MSC_VER 4 | #define getchar getchar_unlocked 5 | #endif 6 | int input() 7 | { 8 | int result=0; 9 | char c; 10 | for(;;) 11 | { 12 | c=getchar(); 13 | if (c==' '||c=='\n') break; 14 | result=10*result+(c-'0'); 15 | } 16 | //scanf("%d",&result); 17 | return result; 18 | } 19 | int main() 20 | { 21 | int T,i,j,NG,NM,maxG,maxM,x; 22 | T=input(); 23 | for (i=0; imaxG) maxG=x; 34 | } 35 | for (j=0; jmaxM) maxM=x; 39 | } 40 | if (maxG>=maxM) 41 | printf("\nGodzilla"); 42 | else 43 | printf("\nMechaGodzilla"); 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /balife.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-03 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | for (;;) { 9 | int N; scanf("%d", &N); if (N == -1) return 0; 10 | int sum = 0; 11 | vector a; 12 | for (int i = 0; i < N; i++) { 13 | int x; scanf("%d", &x); a.push_back(x); 14 | sum += x; 15 | } 16 | if (sum % N != 0) { 17 | puts("-1"); continue; 18 | } 19 | for (int i = 0; i < N; i++) { 20 | a[i] -= sum/N; 21 | } 22 | int res = 0; 23 | sum = 0; 24 | for (int i = 0; i < N; i++) { 25 | sum += a[i]; 26 | res = max(res, abs(sum)); 27 | } 28 | printf("%d\n", res); 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /eights.cpp: -------------------------------------------------------------------------------- 1 | // 2008-02-02 2 | // modified 2014-10-07 3 | #include 4 | #include 5 | typedef long long int64; 6 | int64 a[4]={-58,192,442,692}; 7 | int64 in() 8 | { 9 | char s[20]; 10 | scanf("%s",&s); 11 | int l=strlen(s); 12 | int i; 13 | int64 result=0; 14 | for (i=0; i0) 25 | { 26 | a[digits++]=x%10; 27 | x/=10; 28 | } 29 | for (i=digits-1; i>=0; i--) 30 | putchar(a[i]+48); 31 | putchar('\n'); 32 | } 33 | int main() 34 | { 35 | int t,i; 36 | int64 k; 37 | scanf("%d",&t); 38 | for (i=0; i 0: 17 | t -= 1 18 | n = int(input()) # unnecessary 19 | s = input() 20 | if (any([p.fullmatch(s) for p in patterns]) or 21 | any([p.fullmatch(s[::-1]) for p in patterns])): 22 | print("yes") 23 | else: 24 | print("no") 25 | -------------------------------------------------------------------------------- /crscntry.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-06 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int a[2222]; 7 | int t[2222]; 8 | static int dp[2222][2222]; 9 | int T,x,i,j; 10 | scanf("%d",&T); 11 | while (T--) 12 | { 13 | int N_a=0; 14 | for (;;) 15 | { 16 | scanf("%d",&x); 17 | if (!x) break; 18 | a[N_a++]=x; 19 | } 20 | int best=0; 21 | for (;;) 22 | { 23 | int N_t=0; 24 | for (;;) 25 | { 26 | scanf("%d",&x); 27 | if (!x) break; 28 | t[N_t++]=x; 29 | } 30 | if (!N_t) break; 31 | for (i=1; i<=N_a; i++) 32 | for (j=1; j<=N_t; j++) 33 | { 34 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 35 | if (a[i-1]==t[j-1]) 36 | dp[i][j]=max(dp[i][j],1+dp[i-1][j-1]); 37 | } 38 | best=max(best,dp[N_a][N_t]); 39 | } 40 | printf("%d\n",best); 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /genetic.cpp: -------------------------------------------------------------------------------- 1 | // 2025-07-03 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | string thue = "0"; 7 | while (thue.length() < 5001) { 8 | int n = thue.length(); 9 | for (int i = 0; i < n; i++) { 10 | thue.push_back(thue[i] == '0' ? '1' : '0'); 11 | } 12 | } 13 | string thuediff; 14 | for (int i = 0; i < 5000; i++) { 15 | if (thue[i] < thue[i + 1]) { 16 | thuediff.push_back('N'); 17 | } else if (thue[i] == thue[i + 1]) { 18 | thuediff.push_back('O'); 19 | } else { 20 | thuediff.push_back('P'); 21 | } 22 | } 23 | for (;;) { 24 | int n; scanf("%d", &n); if (n == 0) return 0; 25 | for (int i = 0; i < n; i++) putchar(thuediff[i]); 26 | putchar('\n'); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /adomino.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-04 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | using namespace std; 6 | int N,C; 7 | static int a[100000]; 8 | bool doable(int dist) 9 | { 10 | int _count=1; 11 | int i=0,j; 12 | while (_count 3 | #include 4 | using namespace std; 5 | double max3(double x,double y,double z) 6 | { 7 | return max(x,max(y,z)); 8 | } 9 | double min3(double x,double y,double z) 10 | { 11 | return min(x,min(y,z)); 12 | } 13 | int main() 14 | { 15 | int T; 16 | double x,y,z; 17 | scanf("%d",&T); 18 | while (T--) 19 | { 20 | scanf("%lf %lf %lf",&x,&y,&z); 21 | double l=max3(fabs(x-y),fabs(y-z),fabs(z-x)); 22 | double u=min3(x+y,y+z,z+x); 23 | while (u-l>1e-9) 24 | { 25 | double s=(l+u)/2.0; 26 | double a1=acos((x*x+y*y-s*s)/(2.0*x*y)); 27 | double a2=acos((y*y+z*z-s*s)/(2.0*y*z)); 28 | double a3=acos((z*z+x*x-s*s)/(2.0*z*x)); 29 | if (a1+a2+a3<2.0*acos(-1.0)) 30 | l=s; 31 | else 32 | u=s; 33 | } 34 | printf("%.2f\n",sqrt(3.0)*l*l/4.0); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /myq1.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | using namespace std; 4 | int main() { 5 | int t; scanf("%d", &t); 6 | while (t--) { 7 | int x; scanf("%d", &x); 8 | if (x == 1) { 9 | printf("poor conductor\n"); 10 | continue; 11 | } 12 | int foo = (x-2)/10; 13 | int bar = (x-2)%10; 14 | int row; 15 | if (bar < 5) { 16 | row = 2*foo+1; 17 | } else { 18 | row = 2*foo+2; 19 | bar = 9 - bar; 20 | } 21 | printf("%d ", row); 22 | switch (bar) { 23 | case 0: printf("W L\n"); break; 24 | case 1: printf("A L\n"); break; 25 | case 2: printf("A R\n"); break; 26 | case 3: printf("M R\n"); break; 27 | case 4: printf("W R\n"); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /righttri.cpp: -------------------------------------------------------------------------------- 1 | // 2010-11-13 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | typedef long double LD; 9 | const LD pi=3.14159265358979323846; 10 | int main() 11 | { 12 | int N,cnt=0,i,j; 13 | pair P[1500]; 14 | LD A[1500],ang; 15 | scanf("%d",&N); 16 | for (i=0; ii) A[j-1]=ang; 25 | } 26 | sort(A,A+N-1); 27 | for (j=0; j 3 | #include 4 | #include 5 | using namespace std; 6 | int main() { 7 | int T; cin >> T; 8 | for (int _cs = 1; _cs <= T; _cs++) { 9 | int S; cin >> S; 10 | map next; 11 | set dests; 12 | for (int i = 0; i < S-1; i++) { 13 | string src, dest; cin >> src >> dest; 14 | next[src] = dest; 15 | dests.insert(dest); 16 | } 17 | map::iterator I = next.begin(); 18 | while (dests.find(I->first) != dests.end()) I++; 19 | printf("Scenario #%d:\n", _cs); 20 | string cur = I->first; 21 | for (int i = 0; i < S; i++) { 22 | cout << cur << endl; 23 | cur = next[cur]; 24 | } 25 | cout << endl; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /baised.cpp: -------------------------------------------------------------------------------- 1 | // 2009-06-28 2 | // modified 2010-10-07 3 | #include 4 | #include 5 | using namespace std; 6 | #define i64 long long 7 | #define GC getchar_unlocked 8 | int input() 9 | { 10 | int x=0; 11 | char c; 12 | for(;;) 13 | { 14 | c=GC(); 15 | if (c==' '||c=='\n') 16 | return x; 17 | x=10*x+c-'0'; 18 | } 19 | } 20 | int main() 21 | { 22 | char c; 23 | i64 answer; 24 | int T,i,N,j; 25 | static int a[1000000]; 26 | T=input(); 27 | for (i=0; i 6 | #include 7 | using namespace std; 8 | int main() { 9 | bool a[100][100]; 10 | int i,j; 11 | memset(a,false,sizeof(a)); 12 | for (i=0; i<50; i++) { 13 | for (j=0; j<50; j++) { 14 | a[i][j]=true; 15 | } 16 | } 17 | for (i=50; i<99; i++) { 18 | a[i][i-1]=true; 19 | a[i][i+1]=true; 20 | a[i-1][i]=true; 21 | a[i+1][i]=true; 22 | } 23 | a[49][50]=true; 24 | a[99][98]=true; 25 | puts("50"); 26 | for (i=0; i<100; i++) { 27 | for (j=0; j<100; j++) { 28 | printf("%d ",a[i][j]?1:0); 29 | } 30 | putchar('\n'); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /ec_conb.cpp: -------------------------------------------------------------------------------- 1 | // 2014-06-06 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | string to_binary(int x) { 8 | string s; 9 | do { 10 | s += '0' + (x&1); 11 | x >>= 1; 12 | } while (x); 13 | reverse(s.begin(), s.end()); 14 | return s; 15 | } 16 | int from_binary(string s) { 17 | int res = 0; 18 | for (int i = 0; i < s.length(); i++) { 19 | res <<= 1; 20 | res += s[i] - '0'; 21 | } 22 | return res; 23 | } 24 | int main() { 25 | int N; scanf("%d", &N); 26 | while (N--) { 27 | int x; scanf("%d", &x); 28 | if (x&1) printf("%d\n", x); 29 | else { 30 | string s = to_binary(x); 31 | reverse(s.begin(), s.end()); 32 | printf("%d\n", from_binary(s)); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /edist.cpp: -------------------------------------------------------------------------------- 1 | // 2014-04-25 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | int T; 9 | cin >> T; 10 | while (T--) { 11 | string a, b; 12 | cin >> a >> b; 13 | vector > dp(a.length() + 1, vector(b.length() + 1)); 14 | for (int i = 0; i <= a.length(); i++) { 15 | for (int j = 0; j <= b.length(); j++) { 16 | if (i == 0) dp[i][j] = j; 17 | else if (j == 0) dp[i][j] = i; 18 | else if (a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1]; 19 | else { 20 | dp[i][j] = 1 + min(min(dp[i-1][j-1], dp[i][j-1]), dp[i-1][j]); 21 | } 22 | } 23 | } 24 | cout << dp[a.length()][b.length()] << endl; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /kpequ.py: -------------------------------------------------------------------------------- 1 | # 2024-09-29 2 | # https://math.stackexchange.com/a/3888967 explains how to solve the optic 3 | # equation. From this we eventually obtain that the number of solutions as 4 | # ordered pairs (X, Y) is equal to (2e_1 + 1) * (2e_2 + 2) * ... where each e_i 5 | # is the exponent of a prime factor of N. 6 | isPrime = [True] * 10001 7 | primes = [] 8 | for i in range(2, 10001): 9 | if not isPrime[i]: continue 10 | primes.append(i) 11 | j = 2*i 12 | while j <= 10000: 13 | isPrime[j] = False 14 | j += i 15 | 16 | while True: 17 | N = int(input()) 18 | if N == 0: break 19 | result = 1 20 | for p in primes: 21 | if p > N: break 22 | exponent = 0 23 | N2 = N 24 | while N2 > 0: 25 | N2 //= p 26 | exponent += N2 27 | result *= 2*exponent + 1 28 | print(result) 29 | -------------------------------------------------------------------------------- /babtwr.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-26 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | using namespace std; 5 | int n; 6 | int x[91]; 7 | int y[91]; 8 | int h[91]; 9 | int memo[91]; 10 | int height(int k) 11 | { 12 | int i; 13 | if (memo[k]!=-1) 14 | return memo[k]; 15 | int top=0; 16 | for (i=0; i 4 | using namespace std; 5 | int main() 6 | { 7 | char c; 8 | int base,N,cnt,x,i,num; 9 | char res[50]; 10 | char s[50]; 11 | for(;;) 12 | { 13 | c=getchar(); 14 | if (c=='e') return 0; 15 | if (c=='t') 16 | { 17 | scanf("o%d %d",&base,&N); 18 | getchar(); 19 | cnt=0; 20 | while (N) 21 | { 22 | res[cnt++]=(N%base-base)%base; 23 | N=(N-((N%base-base)%base))/base; 24 | } 25 | for (i=cnt; i; i--) 26 | putchar(res[i-1]+'0'); 27 | if (!cnt) putchar('0'); 28 | putchar('\n'); 29 | } 30 | else 31 | { 32 | scanf("rom%d %s",&base,s); 33 | getchar(); 34 | int x=1; 35 | num=0; 36 | for (i=strlen(s)-1; i>=0; i--) 37 | { 38 | num+=x*(s[i]-'0'); 39 | x*=base; 40 | } 41 | printf("%d\n",num); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /etf.cpp: -------------------------------------------------------------------------------- 1 | // 2009-08-31 2 | #include 3 | using namespace std; 4 | int primes[100]; 5 | int cnt=0; 6 | bool prime(int x) 7 | { 8 | int i; 9 | for (i=2; i*i<=x; i++) 10 | if (x%i==0) 11 | return false; 12 | return true; 13 | } 14 | void precompute() 15 | { 16 | int i; 17 | for (i=2; i<=1000; i++) 18 | if (prime(i)) 19 | primes[cnt++]=i; 20 | } 21 | int tot(int x) 22 | { 23 | int res=1,i; 24 | if (x==1) return 1; 25 | for (i=0; i 3 | const int MAX = 100000; 4 | const int MOD = 5000000; 5 | int BIT[51][MAX]; 6 | int query(int* b, int idx) { 7 | int res = 0; 8 | while (idx >= 0) { 9 | res += b[idx]; 10 | if (res >= MOD) res -= MOD; 11 | idx = (idx&(idx+1))-1; 12 | } 13 | return res; 14 | } 15 | void update(int* b, int idx, int val) { 16 | while (idx < MAX) { 17 | b[idx] += val; 18 | if (b[idx] >= MOD) b[idx] -= MOD; 19 | idx |= idx+1; 20 | } 21 | } 22 | int main() { 23 | int N, K; scanf("%d %d", &N, &K); 24 | while (N--) { 25 | int x; scanf("%d", &x); 26 | update(BIT[1], x, 1); 27 | for (int i = 2; i <= K; i++) { 28 | update(BIT[i], x, query(BIT[i-1], x-1)); 29 | } 30 | } 31 | printf("%d\n", query(BIT[K], 99999)); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /quest5.cpp: -------------------------------------------------------------------------------- 1 | // 2023-01-17 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | void do_testcase() { 8 | int n; cin >> n; 9 | vector> planks; 10 | for (int i = 0; i < n; i++) { 11 | int a, b; cin >> a >> b; 12 | planks.emplace_back(a, b); 13 | } 14 | sort(planks.begin(), planks.end()); 15 | int result = 0; 16 | const int INF = 1e9; 17 | int end = INF; 18 | for (int i = 0; i < planks.size(); i++) { 19 | if (planks[i].first > end) { 20 | ++result; 21 | end = INF; 22 | } 23 | end = min(end, planks[i].second); 24 | } 25 | if (n > 0) ++result; 26 | cout << result << '\n'; 27 | } 28 | int main() { 29 | ios::sync_with_stdio(false); 30 | int t; cin >> t; 31 | while (t--) do_testcase(); 32 | } 33 | -------------------------------------------------------------------------------- /pigbank.cpp: -------------------------------------------------------------------------------- 1 | // 2008-02-02 2 | #include 3 | int main() 4 | { 5 | int a[10001]; 6 | int t,i,M,F,E,N,j,k; 7 | int P[500]; 8 | int W[500]; 9 | scanf("%d",&t); 10 | for (i=0; ia[j-W[k]]+P[k]) 28 | min=a[j-W[k]]+P[k]; 29 | } 30 | if (min==2000000000) 31 | a[j]=-1; 32 | else 33 | a[j]=min; 34 | } 35 | if (a[M]==-1) 36 | printf("This is impossible.\n"); 37 | else 38 | printf("The minimum amount of money in the piggy-bank is %d.\n",a[M]); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /bwidow.cpp: -------------------------------------------------------------------------------- 1 | // 2014-10-02 2 | #include 3 | int r[1000]; 4 | int R[1000]; 5 | int main() { 6 | int T; scanf("%d", &T); 7 | while (T--) { 8 | int N; scanf("%d", &N); 9 | int biggest_R = -1; 10 | int biggest_pos; 11 | for (int i = 0; i < N; i++) { 12 | scanf("%d %d", r+i, R+i); 13 | if (R[i] > biggest_R) { 14 | biggest_R = R[i]; 15 | biggest_pos = i; 16 | } 17 | } 18 | bool ok = true; 19 | for (int i = 0; i < N; i++) { 20 | if (i == biggest_pos) continue; 21 | if (R[i] >= r[biggest_pos]) { 22 | ok = false; 23 | break; 24 | } 25 | } 26 | if (ok) { 27 | printf("%d\n", biggest_pos+1); 28 | } else { 29 | puts("-1"); 30 | } 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /cwc2015.cpp: -------------------------------------------------------------------------------- 1 | // 2015-11-04 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() { 8 | int T; 9 | cin >> T; 10 | for (int _cs = 1; _cs <= T; _cs++) { 11 | int N; 12 | cin >> N; 13 | int sum = 0; 14 | vector x(N); 15 | for (int i = 0; i < N; i++) { 16 | cin >> x[i]; 17 | sum += x[i]; 18 | } 19 | printf("Case %d: ", _cs); 20 | if (sum%2 || N%2) { 21 | puts("No"); 22 | continue; 23 | } 24 | vector> dp(sum+1); 25 | dp[0][0] = 1; 26 | for (int i = 0; i < N; i++) { 27 | for (int j = dp.size() -1; j >= x[i]; j--) { 28 | dp[j] |= dp[j-x[i]] << 1; 29 | } 30 | } 31 | puts(dp[sum/2][N/2] ? "Yes" : "No"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /gossipers.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-23 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main() 8 | { 9 | int i,j,N,M; 10 | char s[100],s1[100],s2[100]; 11 | string S; 12 | static int a[2100][2100]; 13 | for(;;) 14 | { 15 | scanf("%d %d",&N,&M); 16 | if (!N) return 0; 17 | map _M; 18 | for (i=0; i 3 | #include 4 | using namespace std; 5 | struct job 6 | { 7 | int priority; 8 | int position; 9 | }; 10 | int main() 11 | { 12 | int t; 13 | job J; 14 | cin >> t; 15 | int i,j,k,m,n,p; 16 | int P[10]; 17 | for (i=0; i Q; 20 | cin >> n >> m; 21 | memset(P,0,sizeof(P)); 22 | for (j=0; j> p; 25 | J.position=j; 26 | J.priority=p; 27 | P[p]++; 28 | Q.push(J); 29 | } 30 | int biggest=9; 31 | while (!P[biggest]) biggest--; 32 | int _time=0; 33 | for(;;) 34 | { 35 | J=Q.front(); 36 | Q.pop(); 37 | if (J.priority 3 | #include 4 | #ifdef _MSC_VER 5 | #define GC getchar 6 | #else 7 | #define GC getchar_unlocked 8 | #endif 9 | using namespace std; 10 | int input() 11 | { 12 | bool minus=false; 13 | char c; 14 | int x=0; 15 | do 16 | c=GC(); 17 | while (c==' '||c=='\n'); 18 | for(;;) 19 | { 20 | if (c=='-') 21 | minus=true; 22 | else if (c==' '||c=='\n') 23 | return minus?-x:x; 24 | else 25 | x=10*x+c-'0'; 26 | c=GC(); 27 | } 28 | } 29 | int main() 30 | { 31 | int T,i,j,N,s; 32 | long long psum,c; 33 | // freopen("subseq.in","r",stdin); 34 | T=input(); 35 | for (i=0; i M; 39 | M[0]=1; 40 | psum=0; 41 | c=0; 42 | for (j=0; j 3 | #include 4 | using namespace std; 5 | int main() { 6 | char s[250]; 7 | while (~scanf("%s", s)) { 8 | int n = strlen(s); 9 | int rem[10]; 10 | for (int i = 1; i <= 9; i++) { 11 | int m = 1; 12 | // constant optimization: construct mod tables 13 | int mod[100]; 14 | for (int j = 0; j < 100; j++) { 15 | mod[j] = j%i; 16 | } 17 | rem[i] = 0; 18 | for (int j = n-1; j >= 0; j--) { 19 | rem[i] = mod[rem[i] + m*(s[j] - '0')]; 20 | m = mod[m*10]; 21 | } 22 | } 23 | int res = 0; 24 | for (int i = 0; i < n; i++) { 25 | if (s[i] != '0' && rem[s[i] - '0'] == 0) { 26 | res++; 27 | } 28 | } 29 | printf("%d\n", res); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /divsum.cpp: -------------------------------------------------------------------------------- 1 | // 2008-01-29 2 | #include 3 | #include 4 | bool prime[500001]; 5 | int factor[500001]; 6 | void sieve() 7 | { 8 | int i,j; 9 | for (i=2; i<=500000; i++) 10 | { 11 | prime[i]=true; 12 | factor[i]=i; 13 | } 14 | for (i=2;;i++) 15 | { 16 | while ((!prime[i])&&(i<=701)) i++; 17 | if (i>701) break; 18 | for (j=2*i; j<=500000; j+=i) 19 | { 20 | prime[j]=false; 21 | factor[j]=i; 22 | } 23 | } 24 | } 25 | int main() 26 | { 27 | sieve(); 28 | int N,i,n,n2,k,divsum,sum; 29 | scanf("%d",&N); 30 | for (i=0; i1) 40 | { 41 | k=factor[n]; 42 | sum=1; 43 | while (n%k==0) 44 | { 45 | n/=k; 46 | sum=k*sum+1; 47 | } 48 | divsum*=sum; 49 | } 50 | printf("%d\n",divsum-n2); 51 | } 52 | } 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /hellokit.cpp: -------------------------------------------------------------------------------- 1 | // 2011-04-05 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | using namespace std; 22 | int main() 23 | { 24 | cin.sync_with_stdio(false); 25 | for (;;) 26 | { 27 | string s; 28 | int n,i,j,k; 29 | cin >> s; if (s==".") return 0; 30 | cin >> n; 31 | for (i=0; i 3 | using namespace std; 4 | int process(char* s,int* p) 5 | { 6 | int* init=p; 7 | char c; 8 | int x; 9 | for(;;) 10 | { 11 | x=0; 12 | for(;;) 13 | { 14 | if ((c=*s++)<=32) 15 | break; 16 | x=(x<<1)+(x<<3)+c-'0'; 17 | } 18 | *p++=x; 19 | if (!c) return p-init; 20 | } 21 | } 22 | int main() 23 | { 24 | int N,i=0,L,l,r,m; 25 | char input[7000000]; 26 | static int a[1000010]; 27 | static int least[1000010]; 28 | gets(input); 29 | N=process(input,a); 30 | // a[N]=2e9; 31 | //now for the LIS algorithm... 32 | L=0; 33 | least[0]=-2e9; 34 | for (i=0; il) 39 | { 40 | m=(l+r+1)/2; 41 | if (least[m]<=a[i]) 42 | l=m; 43 | else 44 | r=m-1; 45 | } 46 | if (l==L) 47 | least[++L]=a[i]; 48 | else 49 | least[l+1]=min(least[l+1],a[i]); 50 | } 51 | printf("%d\n",N-L); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /sumfour.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-06 2 | #include 3 | #include 4 | #define MAX 16000000 5 | using namespace std; 6 | int main() 7 | { 8 | int i,j,i2,j2,n; 9 | int A[4000],B[4000],C[4000],D[4000]; 10 | static int E[MAX],F[MAX]; 11 | cin >> n; 12 | for (i=0; i> A[i] >> B[i] >> C[i] >> D[i]; 14 | for (i=0; i=0) 28 | { 29 | if (i2==i) 30 | while (i2=0&&F[j2]==F[j]) j2--; 33 | if (E[i]+F[j]==0) 34 | { 35 | __count+=(i2-i)*(j-j2); 36 | i=i2; 37 | j=j2; 38 | } 39 | else if (E[i]+F[j]>0) 40 | j=j2; 41 | else 42 | i=i2; 43 | } 44 | cout << __count << endl; 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /tperml.py: -------------------------------------------------------------------------------- 1 | # 2024-04-04 2 | # Slow, but passes. Implementing `next_permutation` would give a faster solution 3 | # but I'd rather spend time on more interesting things. 4 | 5 | def generate_permutation(n, fact, rank): 6 | avail = [] 7 | for i in range(1, n + 1): 8 | avail.append(i) 9 | perm = [] 10 | for i in range(n, 0, -1): 11 | fact //= i 12 | idx = rank // fact 13 | rank -= fact * idx 14 | perm.append(avail[idx]) 15 | avail.remove(avail[idx]) 16 | return perm 17 | 18 | t = int(input()) 19 | for testcase in range(t): 20 | n, rank, cnt = [int(w) for w in input().split()] 21 | fact = 1 22 | for i in range(1, n + 1): 23 | fact *= i 24 | for i in range(cnt): 25 | perm = generate_permutation(n, fact, rank) 26 | print(' '.join([str(x) for x in perm])) 27 | rank += 1 28 | if rank == fact: 29 | rank = 0 30 | print('') 31 | -------------------------------------------------------------------------------- /lsort.cpp: -------------------------------------------------------------------------------- 1 | // 2008-07-23 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | inline int min(int x,int y) 5 | { 6 | return x 3 | #include 4 | using namespace std; 5 | int sum_of_digits(int x) { 6 | if (x == 0) return 0; else return x%10 + sum_of_digits(x/10); 7 | } 8 | long long sum(int x) { 9 | // returns sum of digits in 0 + 1 + ... + (x-1) 10 | long long res = 0; 11 | long long cur = 0; 12 | long long increment = 1; 13 | int exponent = 0; 14 | while (10*increment <= x) { increment *= 10; exponent++; } 15 | while (cur < x) { 16 | if (cur + increment > x) { 17 | increment /= 10; exponent--; continue; 18 | } 19 | long long sod = sum_of_digits(cur); 20 | res += increment*sod + 45*exponent*increment/10; 21 | cur += increment; 22 | } 23 | return res; 24 | } 25 | int main() { 26 | for (;;) { 27 | int a, b; scanf("%d %d", &a, &b); 28 | if (a == -1) return 0; 29 | printf("%lld\n", sum(b+1) - sum(a)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ctrick.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-19 2 | #include 3 | using namespace std; 4 | int n; 5 | int BIT[20000]; 6 | int index(int num) 7 | { 8 | int x; 9 | for (x=num-1; x>=0; x=(x&(x+1))-1) 10 | num-=BIT[x]; 11 | return num; 12 | } 13 | int query(int pos) 14 | { 15 | int l=0; 16 | int u=n-1; 17 | int m=0; 18 | while (u>l) 19 | { 20 | m=(l+u)/2+1; 21 | if (index(m)>pos) 22 | u=m-1; 23 | else 24 | l=m; 25 | } 26 | return l; 27 | } 28 | void del(int num) 29 | { 30 | for (;num 4 | #include 5 | #define MIN(x,y) ((x)<(y)?(x):(y)) 6 | using namespace std; 7 | int main() 8 | { 9 | int t,i,m,n,j,k; 10 | int a[1000]; 11 | int b[1000]; 12 | static int cost[1000][1000]; 13 | scanf("%d",&t); 14 | for (i=0; i 3 | #include 4 | using namespace std; 5 | bool better(deque a,deque b) 6 | { 7 | while (a.size()&&a.front()==0) a.pop_front(); 8 | while (b.size()&&b.front()==0) b.pop_front(); 9 | if (!b.size()) return true; 10 | return a.size()> T; 16 | while (T--) 17 | { 18 | deque best; best.resize(300); 19 | cin >> B; 20 | for (i=1; i cur; 24 | j=i; 25 | do 26 | { 27 | cur.push_front(j); 28 | j=j*2+rem; 29 | if (j>=B) 30 | { 31 | rem=1; 32 | j-=B; 33 | } 34 | else 35 | rem=0; 36 | } 37 | while (j!=i||rem); 38 | if (better(cur,best)) best=cur; 39 | } 40 | for (i=0; i 4 | #include 5 | #include 6 | using namespace std; 7 | int main() 8 | { 9 | int T,i,N,j,k; 10 | unsigned long long m[100][100]; 11 | unsigned long long M[100][100]; 12 | scanf("%d",&T); 13 | char s[100]; 14 | getchar(); 15 | while (T--) 16 | { 17 | fgets(s, 100, stdin); 18 | N=(strlen(s)-1)/2+1; 19 | for (i=0; i 3 | char a[100][100]; 4 | int X,Y; 5 | void explore(int x,int y) 6 | { 7 | a[x][y]='#'; 8 | if (x>0&&a[x-1][y]=='-') 9 | explore(x-1,y); 10 | if (y>0&&a[x][y-1]=='-') 11 | explore(x,y-1); 12 | if (x 3 | using namespace std; 4 | constexpr int MOD = 10000; 5 | int modexp(int p, int e) { 6 | if (e == 0) return 1; 7 | int t = modexp(p, e / 2); 8 | t = (t * t) % MOD; 9 | if (e % 2) t = (t * p) % MOD; 10 | return t; 11 | } 12 | void do_testcase() { 13 | int K; cin >> K; 14 | int even = 1, odd = 0, prod = 1; 15 | for (int i = 0; i < K; i++) { 16 | int p, e; cin >> p >> e; 17 | prod = (prod * modexp(p, e)) % MOD; 18 | if (p == 2) continue; else --p; 19 | const int new_even = (even + p*odd) % MOD; 20 | const int new_odd = (odd + p*even) % MOD; 21 | even = new_even; 22 | odd = new_odd; 23 | } 24 | cout << (even + MOD - 1) % MOD << '\n'; 25 | cout << odd << '\n'; 26 | cout << (prod - even - odd + 2*MOD) % MOD << '\n'; 27 | } 28 | int main() { 29 | ios::sync_with_stdio(false); 30 | int T; cin >> T; 31 | while (T--) do_testcase(); 32 | } 33 | -------------------------------------------------------------------------------- /tricentr.cpp: -------------------------------------------------------------------------------- 1 | // 2008-10-09 2 | #include 3 | #include 4 | 5 | double round1(double value, unsigned int decimals) 6 | { 7 | double factor = pow(10,decimals); 8 | return floor((value * factor) + 0.5) / factor; 9 | } 10 | 11 | int main() 12 | { 13 | int t,i; double a,b,c,a1,b1,c1,s,R,A,OH,GH,exp; 14 | scanf("%d",&t); 15 | 16 | for(i=0;i 3 | int input() 4 | { 5 | int x=0; 6 | char c; 7 | for(;;) 8 | { 9 | c=getchar_unlocked(); 10 | if (c==' '||c=='\n') 11 | return x; 12 | x=10*x+c-'0'; 13 | } 14 | } 15 | int main() 16 | { 17 | int i,A,D,mina,mind,min2d,x; 18 | for(;;) 19 | { 20 | A=input(); 21 | D=input(); 22 | if (A==0) return 0; 23 | mina=1000000; 24 | mind=1000000; 25 | min2d=1000000; 26 | for (i=0; ix) 30 | mina=x; 31 | } 32 | for (i=0; i 3 | using namespace std; 4 | int main() 5 | { 6 | int N,i,X,x; 7 | cin >> N >> X; 8 | int a[20]; 9 | int b[20]; 10 | int c[20]; 11 | int B,C; 12 | x=1; 13 | for (i=0; i=0; i--,x/=3) 16 | if (X==0) 17 | a[i]=0; 18 | else if (X<0&&-X>x/2) 19 | { 20 | X+=x; 21 | a[i]=-1; 22 | } 23 | else if (X>0&&X>x/2) 24 | { 25 | X-=x; 26 | a[i]=1; 27 | } 28 | if (X) 29 | cout << -1 << endl; 30 | else 31 | { 32 | B=C=0; 33 | for (i=0; i 4 | #include 5 | #include 6 | using namespace std; 7 | void do_testcase() { 8 | long long N; cin >> N; 9 | int K; cin >> K; 10 | vector a(K); 11 | for (int i = 0; i < K; i++) cin >> a[i]; 12 | vector<__int128> b(K); 13 | b[0] = a[0]; 14 | for (int i = 1; i < K; i++) { 15 | b[i] = 2*b[i - 1] + a[i]; 16 | } 17 | deque result; 18 | for (int i = K - 1; i >= 0; i--) { 19 | if (b[i] <= N) { 20 | result.push_front(i); 21 | N -= b[i]; 22 | } 23 | } 24 | if (N > 0) cout << "-1\n"; 25 | else { 26 | cout << result[0] + 1; 27 | for (int i = 1; i < result.size(); i++) { 28 | cout << ' ' << result[i] + 1; 29 | } 30 | cout << '\n'; 31 | } 32 | } 33 | int main() { 34 | int T; cin >> T; 35 | while (T--) do_testcase(); 36 | } 37 | -------------------------------------------------------------------------------- /bmj.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-26 2 | #include 3 | #include 4 | int input() 5 | { 6 | char c=getchar(); 7 | if (c==-1) return -1; 8 | else ungetc(c,stdin); 9 | int x=0; 10 | for(;;) 11 | { 12 | c=getchar(); 13 | if (c==' '||c=='\n') return x; 14 | x=10*x+c-'0'; 15 | } 16 | } 17 | int main() 18 | { 19 | for(;;) 20 | { 21 | int x=input(); 22 | int X,Y; 23 | if (x==-1) return 0; 24 | int l=1e-7+(3.0+sqrt(12.0*x-15.0))/6.0; 25 | if (x<=3*l*l-2*l+1) 26 | { 27 | X=3*l*l-2*l+1-x; 28 | Y=l-X; 29 | } 30 | else if (x<=3*l*l-l+1) 31 | { 32 | X=3*l*l-2*l+1-x; 33 | Y=l; 34 | } 35 | else if (x<=3*l*l+1) 36 | { 37 | X=-l; 38 | Y=3*l*l+1-x; 39 | } 40 | else if (x<=3*l*l+l+1) 41 | { 42 | X=x-(3*l*l+l+1); 43 | Y=3*l*l+1-x; 44 | } 45 | else if (x<=3*l*l+2*l+1) 46 | { 47 | X=x-(3*l*l+l+1); 48 | Y=-l; 49 | } 50 | else //x<=3*l*l+3*l+1 51 | { 52 | X=l; 53 | Y=x-(3*l*l+3*l+1); 54 | } 55 | printf("%d %d\n",X,Y); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /acs.cpp: -------------------------------------------------------------------------------- 1 | // 2008-06-20 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int x[5678]; 7 | int X[5678]; 8 | int y[1234]; 9 | int Y[1234]; 10 | int _x,_y,z,a,b,i; 11 | char c; 12 | for (i=0; i<1234; i++) 13 | { 14 | y[i]=i; 15 | Y[i]=i; 16 | } 17 | for (i=0; i<5678; i++) 18 | { 19 | x[i]=i; 20 | X[i]=i; 21 | } 22 | while (!cin.eof()) 23 | { 24 | c=getchar(); 25 | if (c=='C') 26 | { 27 | scanf("%d %d",&a,&b); 28 | swap(X[x[a-1]],X[x[b-1]]); 29 | swap(x[a-1],x[b-1]); 30 | } 31 | else if (c=='R') 32 | { 33 | scanf("%d %d",&a,&b); 34 | swap(Y[y[a-1]],Y[y[b-1]]); 35 | swap(y[a-1],y[b-1]); 36 | } 37 | else if (c=='Q') 38 | { 39 | scanf("%d %d",&a,&b); 40 | printf("%d\n",5678*y[a-1]+x[b-1]+1); 41 | } 42 | else if (c=='W') 43 | { 44 | scanf("%d",&z); 45 | _x=(z-1)%5678; 46 | _y=(z-1)/5678; 47 | printf("%d %d\n",Y[_y]+1,X[_x]+1); 48 | } 49 | else if (c!=' '&&c!='\n') 50 | break; 51 | } 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /cubefr.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-01 2 | #include 3 | #include 4 | using namespace std; 5 | const int primes[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 6 | 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; 7 | bool cubefree[1000001]; 8 | int rank[1000001]; 9 | int main() { 10 | memset(cubefree, 1, sizeof cubefree); 11 | for (int i = 0; i < 25; i++) { 12 | int cube = primes[i]*primes[i]*primes[i]; 13 | for (int j = cube; j <= 1000000; j += cube) { 14 | cubefree[j] = false; 15 | } 16 | } 17 | int sum = 0; 18 | for (int i = 1; i <= 1000000; i++) { 19 | if (cubefree[i]) { 20 | rank[i] = ++sum; 21 | } 22 | } 23 | int T; scanf("%d", &T); 24 | for (int cs = 1; cs <= T; cs++) { 25 | int n; scanf("%d", &n); 26 | printf("Case %d: ", cs); 27 | if (cubefree[n]) { 28 | printf("%d\n", rank[n]); 29 | } else { 30 | printf("Not Cube Free\n"); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /div2.cpp: -------------------------------------------------------------------------------- 1 | // 2024-05-11 2 | #include 3 | using namespace std; 4 | int factor[1000001]; 5 | int main() { 6 | for (int i = 2; i <= 1000; i++) { 7 | if (factor[i] > 0) continue; 8 | for (int j = i; j <= 1000000; j += i) { 9 | factor[j] = i; 10 | } 11 | } 12 | int cnt = 0; 13 | for (int i = 6; i <= 1000000; i++) { 14 | if (i > 1000 && factor[i] == 0) continue; 15 | int nf = 0; 16 | int lastf = -1; 17 | int j = i; 18 | bool success = true; 19 | while (j > 1) { 20 | if (factor[j] == 0) factor[j] = j; 21 | if (factor[j] == lastf) { 22 | success = false; 23 | break; 24 | } 25 | lastf = factor[j]; 26 | j /= lastf; 27 | ++nf; 28 | } 29 | if (success && nf >= 2) { 30 | ++cnt; 31 | if (cnt % 108 == 0) { 32 | printf("%d\n", i); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /fcandy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | int N; cin >> N; 8 | int sum = 0; 9 | int cnt[100]; 10 | int cal[100]; 11 | for (int i = 0; i < N; i++) { 12 | cin >> cnt[i] >> cal[i]; 13 | sum += cnt[i] * cal[i]; 14 | } 15 | int MAX = sum / 2; 16 | vector dp(MAX + 1); 17 | dp[0] = 1; 18 | int best = 0; 19 | for (int i = 0; i < N; i++) { 20 | for (int j = 0; j < cal[i]; j++) { 21 | // j is the remainder 22 | int last = -1000000; 23 | for (int k = j; k <= MAX; k += cal[i]) { 24 | if (dp[k]) { 25 | last = k; 26 | } else if (k - last <= cnt[i] * cal[i]) { 27 | dp[k] = 1; 28 | } 29 | if (dp[k]) best = max(best, k); 30 | } 31 | } 32 | } 33 | cout << 2 * (MAX - best) + sum % 2 << '\n'; 34 | } 35 | -------------------------------------------------------------------------------- /period.cpp: -------------------------------------------------------------------------------- 1 | // 2023-10-24 2 | #include 3 | char s[1000000]; 4 | int p[1000001]; 5 | void do_testcase() { 6 | int N; scanf("%d", &N); 7 | int c; 8 | do { 9 | c = getchar_unlocked(); 10 | } while (c <= 32); 11 | int i = 0; 12 | do { 13 | s[i++] = c; 14 | c = getchar_unlocked(); 15 | } while (c > 32); 16 | p[0] = -1; 17 | for (int i = 1; i <= N; i++) { 18 | int x = p[i - 1]; 19 | p[i] = 0; 20 | while (x >= 0) { 21 | if (s[i - 1] == s[x]) { 22 | p[i] = x + 1; 23 | break; 24 | } else { 25 | x = p[x]; 26 | } 27 | } 28 | if (p[i] > 0 && (i % (i - p[i])) == 0) { 29 | printf("%d %d\n", i, i / (i - p[i])); 30 | } 31 | } 32 | } 33 | int main() { 34 | int T; scanf("%d", &T); 35 | for (int i = 1; i <= T; i++) { 36 | printf("Test case #%d\n", i); 37 | do_testcase(); 38 | putchar('\n'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /posters.cpp: -------------------------------------------------------------------------------- 1 | // 2010-11-15 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | int main() 10 | { 11 | int t; 12 | scanf("%d",&t); 13 | while (t--) 14 | { 15 | int n,i; 16 | int area[40000]; 17 | pair a[80000]; 18 | scanf("%d",&n); 19 | for (i=0; i active; 29 | for (i=0; i<2*n; i++) 30 | { 31 | if (!active.empty()) 32 | { 33 | set::iterator It=active.end(); 34 | --It; 35 | area[*It]+=a[i].first-a[i-1].first; 36 | } 37 | if (a[i].second<1e6) //start 38 | active.insert(a[i].second); 39 | else 40 | active.erase(a[i].second-1e6); 41 | } 42 | int cnt=0; 43 | for (i=0; i 4 | #define MAXSIZE 21 5 | using namespace std; 6 | int dp[MAXSIZE][2][2][2][2]; 7 | int main() 8 | { 9 | int T,i,N; 10 | dp[1][0][0][0][0]=1; 11 | dp[1][0][0][1][1]=1; 12 | dp[1][0][1][1][0]=1; 13 | dp[1][1][1][0][0]=1; 14 | dp[1][1][1][1][1]=1; 15 | for (i=2; i<=MAXSIZE; i++) 16 | { 17 | dp[i][0][0][0][0]=dp[i-1][1][1][1][1]; 18 | dp[i][0][0][1][1]=dp[i-1][1][1][0][0]+dp[i-1][1][1][1][1]; 19 | dp[i][0][1][1][0]=dp[i-1][1][0][0][1]+dp[i-1][1][1][1][1]; 20 | dp[i][1][1][0][0]=dp[i-1][0][0][1][1]+dp[i-1][1][1][1][1]; 21 | dp[i][0][1][0][1]=dp[i-1][1][0][1][0]; 22 | dp[i][1][0][1][0]=dp[i-1][0][1][0][1]; 23 | dp[i][1][0][0][1]=dp[i-1][0][1][1][0]; 24 | dp[i][1][1][1][1]=dp[i-1][1][1][1][1]+dp[i-1][1][1][0][0]+dp[i-1][0][0][1][1]+dp[i-1][0][1][1][0]+dp[i-1][0][0][0][0]; 25 | } 26 | scanf("%d",&T); 27 | for (i=1; i<=T; i++) 28 | { 29 | scanf("%d",&N); 30 | printf("%d %d\n",i,dp[N][1][1][1][1]); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /tdkprime.cpp: -------------------------------------------------------------------------------- 1 | // 2014-06-07 2 | #include 3 | #include 4 | 5 | // This is the famous "Yarin sieve", for use when memory is tight. 6 | #define MAXSIEVE 100000000 // All prime numbers up to this 7 | #define MAXSIEVEHALF (MAXSIEVE/2) 8 | #define MAXSQRT 5000 // sqrt(MAXSIEVE)/2 9 | char a[MAXSIEVE/16+2]; 10 | #define isprime(n) (a[(n)>>4]&(1<<(((n)>>1)&7))) // Works when n is odd 11 | void sieve() { 12 | int i,j; 13 | memset(a,255,sizeof(a)); 14 | a[0]=0xFE; 15 | for(i=1;i>3]&(1<<(i&7))) 17 | for(j=i+i+i+1;j>3]&=~(1<<(j&7)); 19 | } 20 | // end of Yarin sieve 21 | int prime[5000001]; 22 | int main() { 23 | sieve(); 24 | prime[1] = 2; 25 | for (int cnt = 2, cur = 3; cnt <= 5000000; cur+=2) { 26 | if (isprime(cur)) { 27 | prime[cnt++] = cur; 28 | } 29 | } 30 | int Q; scanf("%d", &Q); 31 | while (Q--) { 32 | int k; scanf("%d", &k); 33 | printf("%d\n", prime[k]); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /diehard.cpp: -------------------------------------------------------------------------------- 1 | // 2014-05-02 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | // 0 = air 7 | // 1 = water 8 | // 2 = fire 9 | const int dh[3] = {3, -5, -20}; 10 | const int da[3] = {2, -10, 5}; 11 | struct S { 12 | int h; 13 | int a; 14 | int loc; 15 | bool operator<(const S& s) const { 16 | return h < s.h || h == s.h && a < s.a || h == s.h && a == s.a && loc < s.loc; 17 | } 18 | }; 19 | map memo; 20 | int maxtime(int h, int a, int loc) { 21 | h += dh[loc]; a += da[loc]; 22 | if (h <= 0 || a <= 0) return 0; 23 | int& res = memo[(S){h, a, loc}]; 24 | if (res) return res; 25 | res = 0; 26 | for (int newloc = 0; newloc < 3; newloc++) { 27 | if (newloc != loc) { 28 | res = max(res, 1 + maxtime(h, a, newloc)); 29 | } 30 | } 31 | return res; 32 | } 33 | int main() { 34 | int t; cin >> t; 35 | while (t--) { 36 | int h, a; cin >> h >> a; 37 | cout << maxtime(h, a, 0) << endl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /image.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-15 2 | #include 3 | using namespace std; 4 | bool vis[20][20]; 5 | int R,C; 6 | int res; 7 | int vx[4]={0,1,0,-1}; 8 | int vy[4]={1,0,-1,0}; 9 | int mx[8]={0,0,1,1,1,-1,-1,-1}; 10 | int my[8]={1,-1,1,0,-1,1,0,-1}; 11 | char a[20][20]; 12 | bool ok(int x,int y) 13 | { 14 | return x>=0&&x<20&&y>=0&&y<20; 15 | } 16 | void dfs(int r,int c) 17 | { 18 | if (!ok(r,c)) return; 19 | if (a[r][c]!='X') return; 20 | if (vis[r][c]) return; 21 | vis[r][c]=1; 22 | res+=4; 23 | int i; 24 | for (i=0; i<4; i++) 25 | if (ok(r+vx[i],c+vy[i])&&a[r+vx[i]][c+vy[i]]=='X') 26 | res--; 27 | for (i=0; i<8; i++) 28 | dfs(r+mx[i],c+my[i]); 29 | } 30 | int main() 31 | { 32 | int r,c,i,j; 33 | for(;;) 34 | { 35 | scanf("%d %d %d %d",&R,&C,&r,&c); 36 | if (!R) return 0; 37 | memset(vis,0,sizeof(vis)); 38 | memset(a,0,sizeof(a)); 39 | for (i=0; i 3 | #include 4 | #include 5 | using namespace std; 6 | int buf[100000]; 7 | void mergesort(int* a,int l,int r) 8 | { 9 | if (r==l) return; 10 | int m=(l+r)/2; 11 | mergesort(a,l,m); 12 | mergesort(a,m+1,r); 13 | int i=0,j=l,k=m+1; 14 | while (j<=m&&k<=r) 15 | if (a[j]<=a[k]) 16 | buf[i++]=a[j++]; 17 | else 18 | buf[i++]=a[k++]; 19 | while (j<=m) buf[i++]=a[j++]; 20 | while (k<=r) buf[i++]=a[k++]; 21 | memcpy(a+l,buf,sizeof(int)*(r-l+1)); 22 | } 23 | int process(char* s,int* p) 24 | { 25 | int* init=p; 26 | char c; 27 | int x; 28 | for(;;) 29 | { 30 | x=0; 31 | for(;;) 32 | { 33 | if ((c=*s++)<=32) break; 34 | x=(x<<1)+(x<<3)+c-'0'; 35 | } 36 | *p++=x; 37 | if (!c) return p-init; 38 | } 39 | } 40 | int main() 41 | { 42 | int N,i,a[100000]; 43 | static char input[1100000]; 44 | gets(input); 45 | N=process(input,a); 46 | sort(a,a+N); 47 | for (i=0; i 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | string codes[10] = {"063", "010", "093", "079", "106", 8 | "103", "119", "011", "127", "107"}; 9 | map digits; 10 | int main() { 11 | ios::sync_with_stdio(false); 12 | for (int i = 0; i < 10; i++) { 13 | digits[codes[i]] = i; 14 | } 15 | for (;;) { 16 | string s; cin >> s; 17 | if (s == "BYE") return 0; 18 | int x = 0, i = 0; 19 | while (s[i] != '+') { 20 | x = 10*x + digits[s.substr(i, 3)]; 21 | i += 3; 22 | } 23 | i++; int y = 0; 24 | while (s[i] != '=') { 25 | y = 10*y + digits[s.substr(i, 3)]; 26 | i += 3; 27 | } 28 | cout << s; 29 | ostringstream oss; oss << x + y; string foo = oss.str(); 30 | for (int i = 0; i < foo.length(); i++) { 31 | cout << codes[foo[i] - '0']; 32 | } 33 | cout << endl; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /mst.cpp: -------------------------------------------------------------------------------- 1 | // 2008-10-24 2 | #include 3 | #include 4 | using namespace std; 5 | int id[100000]; 6 | int sz[100000]; 7 | pair > a[100000]; 8 | int main() 9 | { 10 | int V,E,i,v1,v2,wt; 11 | scanf("%d %d",&V,&E); 12 | for (i=0; i 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | using namespace std; 22 | int main() 23 | { 24 | int T; 25 | scanf("%d",&T); 26 | int i; 27 | static int a[1000000]; 28 | for (i=0; i()); 36 | int sum=0; 37 | j=0; 38 | while (j=x) printf("%d\n",j); 41 | else puts("impossible"); 42 | putchar('\n'); 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /boi7seq.cpp: -------------------------------------------------------------------------------- 1 | // 2016-04-28 2 | #include 3 | #include 4 | using namespace std; 5 | int input() { 6 | int res = 0; char c; 7 | do { 8 | c = getchar_unlocked(); 9 | } while (c <= 32); 10 | do { 11 | res = res*10 + c - '0'; 12 | c = getchar_unlocked(); 13 | } while (c > 32); 14 | return res; 15 | } 16 | int main() { 17 | int n = input(); 18 | long long total = 0; 19 | vector stk; 20 | stk.push_back(input()); 21 | for (int i = 1; i < n; i++) { 22 | int x = input(); 23 | if (x >= stk.back()) { 24 | while (stk.size() >= 1 && x >= stk.back()) { 25 | stk.pop_back(); 26 | if (stk.empty() || x < stk.back()) { 27 | total += x; 28 | } else { 29 | total += stk.back(); 30 | } 31 | } 32 | } 33 | stk.push_back(x); 34 | } 35 | for (int i = 0; i+1 < stk.size(); i++) { 36 | total += stk[i]; 37 | } 38 | printf("%lld\n", total); 39 | } 40 | -------------------------------------------------------------------------------- /pfdep.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-23 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int depends_on_howmany[100]; 8 | int depend_on_this[100][100]; 9 | int howmany_depend_on_this[100]; 10 | int main() 11 | { 12 | int N,M,i,j,x,y,z; 13 | scanf("%d %d",&N,&M); 14 | //memset(exist,false,N); 15 | for (i=0; i,greater > Q; 26 | for (i=0; i 3 | using namespace std; 4 | static int neighbours[222222]={0}; 5 | static int Link[222222]={0}; 6 | static int first[222222]={0}; 7 | static int last[222222]={0}; 8 | int top=1; 9 | void f(int parent,int node,int& i,int& n) 10 | { 11 | i=1; 12 | n=0; 13 | for (int I=first[node]; I; I=Link[I]) 14 | { 15 | int x,y; 16 | if (neighbours[I]!=parent) 17 | { 18 | f(node,neighbours[I],x,y); 19 | i+=min(x,y); 20 | n+=x; 21 | } 22 | } 23 | } 24 | int in() 25 | { 26 | char c; 27 | int x=0; 28 | for(;;) 29 | { 30 | c=getchar_unlocked(); 31 | if (c<=32) return x; 32 | x=(x<<1)+(x<<3)+c-'0'; 33 | } 34 | } 35 | void add(int s,int t) 36 | { 37 | if (!first[s]) 38 | first[s]=last[s]=top; 39 | else 40 | last[s]=Link[last[s]]=top; 41 | neighbours[top++]=t; 42 | } 43 | int main() 44 | { 45 | int N,i,u,v; 46 | N=in(); 47 | for (i=0; i 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | int gcd(int x, int y) { 9 | if (x == 0) return y; else return gcd(y % x, x); 10 | } 11 | void do_testcase() { 12 | int N; cin >> N; 13 | vector x(N), y(N); 14 | for (int i = 0; i < N; i++) cin >> x[i] >> y[i]; 15 | int best = 0; 16 | for (int i = 0; i < N; i++) { 17 | unordered_map M; 18 | for (int j = 0; j < N; j++) { 19 | const int dx = x[j] - x[i]; 20 | const int dy = y[j] - y[i]; 21 | if (dy < 0 || (dy == 0 && dx <= 0)) continue; 22 | const int g = gcd(dx, dy); 23 | long long key = (((long long)(dx / g)) << 32) + (dy / g); 24 | M[key]++; 25 | } 26 | for (const auto& p : M) { 27 | best = max(best, p.second); 28 | } 29 | } 30 | cout << best << '\n'; 31 | } 32 | int main() { 33 | int T; cin >> T; 34 | while (T--) do_testcase(); 35 | } 36 | -------------------------------------------------------------------------------- /disubstr-1.cpp: -------------------------------------------------------------------------------- 1 | // 2009-05-05 2 | #include 3 | using namespace std; 4 | struct trienode 5 | { 6 | trienode* next[26]; 7 | trienode() 8 | { 9 | memset(next,0,sizeof(next)); 10 | } 11 | /*~trienode() 12 | { 13 | for (int i=0; i<26; i++) 14 | if (next[i]) 15 | delete next[i]; 16 | }*/ 17 | }; 18 | struct trie 19 | { 20 | trienode* root; 21 | int size; 22 | trie() 23 | { 24 | root=new trienode; 25 | size=0; 26 | } 27 | /* ~trie() 28 | { 29 | delete root; 30 | }*/ 31 | void insert(char* s) 32 | { 33 | trienode *p=root, *q; 34 | int i; 35 | for (i=0; s[i]; i++) 36 | { 37 | if (!p->next[s[i]-'A']) //doesn't exist 38 | { 39 | size++; 40 | p->next[s[i]-'A']=new trienode; 41 | } 42 | p=p->next[s[i]-'A']; 43 | } 44 | } 45 | }; 46 | int main() 47 | { 48 | int T,i; 49 | char s[1010]; 50 | scanf("%d",&T); 51 | trie* t; 52 | while (T--) 53 | { 54 | t=new trie; 55 | scanf("%s",s); 56 | for (i=0; s[i]; i++) 57 | t->insert(s+i); 58 | printf("%d\n",t->size); 59 | } 60 | //insert all non-empty suffixes 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /musket.cpp: -------------------------------------------------------------------------------- 1 | // 2008-08-02 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | int main() 5 | { 6 | /*#ifndef ONLINE_JUDGE 7 | freopen("musket.in","r",stdin); 8 | freopen("musket.out","w",stdout); 9 | #endif*/ 10 | int T,N,i,j,k; 11 | bool dp[105][105]; 12 | int matrix[105][105]; 13 | bool win[105]; 14 | char s[105]; 15 | scanf("%d",&T); 16 | while (T--) 17 | { 18 | scanf("%d",&N); 19 | for (j=0; j