├── la ├── 3469.cpp ├── 4215.cpp ├── 4825.cpp ├── 3656.cpp ├── 4191.cpp ├── 4820.cpp ├── 4556.cpp ├── 4552.cpp ├── 3389.cpp ├── 4810.cpp ├── 3392.cpp ├── 3470.cpp ├── 3910.cpp ├── 3468.cpp ├── 4481.cpp ├── 3911.cpp ├── 3934.cpp ├── 3708.cpp ├── 4806.cpp ├── 3571.cpp ├── 3651.cpp └── 4216.cpp ├── uva ├── plist ├── Volume CXV │ ├── 11507.cpp │ ├── 11547.cpp │ ├── 11556.cpp │ ├── 11526.cpp │ ├── 11567.cpp │ ├── 11554.cpp │ ├── 11500.cpp │ └── 11541.cpp ├── Volume CXVIII │ ├── 11877.cpp │ ├── 11847.cpp │ ├── 11805.cpp │ ├── 11879.cpp │ ├── 11875.cpp │ ├── 11892.cpp │ ├── 11878.cpp │ ├── 11839.cpp │ ├── 11854.cpp │ ├── 11830.cpp │ ├── 11850.cpp │ ├── 11824.cpp │ └── 11889.cpp ├── Volume I │ ├── 00136.cpp │ ├── 00113.cpp │ ├── 00146.cpp │ ├── 00128.cpp │ └── 00151.cpp ├── Volume CXVII │ ├── 11714.cpp │ ├── 11723.cpp │ ├── 11799.cpp │ ├── 11727.cpp │ ├── 11764.cpp │ ├── 11717.cpp │ ├── 11786.cpp │ ├── 11716.cpp │ ├── 11701.cpp │ ├── 11712.cpp │ ├── 11743.cpp │ ├── 11777.cpp │ └── 11713.cpp ├── Volume IV │ ├── 00458.cpp │ ├── 00494.cpp │ ├── 00490.cpp │ ├── 00465.cpp │ ├── 00424.cpp │ ├── 00408.cpp │ ├── 00483.cpp │ └── 00499.cpp ├── Volume CVII │ ├── 10784.cpp │ └── 10783.cpp ├── Volume CXVI │ ├── 11636.cpp │ ├── 11666.cpp │ ├── 11677.cpp │ ├── 11689.cpp │ ├── 11608.cpp │ ├── 11679.cpp │ ├── 11687.cpp │ ├── 11683.cpp │ └── 11650.cpp ├── Volume CIV │ ├── 10491.cpp │ ├── 10499.cpp │ ├── 10407.cpp │ ├── 10432.cpp │ ├── 10473.cpp │ ├── 10451.cpp │ ├── 10440.cpp │ ├── 10450.cpp │ └── 10408.cpp ├── Volume IX │ ├── 00900.cpp │ ├── 00991.cpp │ └── 00913.cpp ├── Volume C │ ├── 10071.cpp │ ├── 10055.cpp │ ├── 10079.cpp │ ├── 10025.cpp │ ├── 10098.cpp │ ├── 10019.cpp │ └── 10050.cpp ├── Volume VI │ ├── 00675.cpp │ ├── 00674.cpp │ ├── 00640.cpp │ ├── 00621.cpp │ └── 00644.cpp ├── Volume CII │ ├── 10286.cpp │ ├── 10219.cpp │ ├── 10209.cpp │ ├── 10276.cpp │ ├── 10223.cpp │ └── 10221.cpp ├── Volume VIII │ ├── 00847.cpp │ └── 00846.cpp ├── Volume CIII │ ├── 10302.cpp │ ├── 10346.cpp │ ├── 10300.cpp │ ├── 10323.cpp │ └── 10370.cpp ├── Volume CVI │ ├── 10696.cpp │ ├── 10678.cpp │ ├── 10684.cpp │ └── 10642.cpp ├── Volume II │ ├── 00272.cpp │ ├── 00271.cpp │ ├── 00278.cpp │ ├── 00256.cpp │ └── 00264.cpp ├── Volume CX │ ├── 11054.cpp │ ├── 11044.cpp │ ├── 11069.cpp │ ├── 11000.cpp │ └── 11068.cpp ├── Volume CXIII │ ├── 11310.cpp │ ├── 11388.cpp │ ├── 11364.cpp │ └── 11398.cpp ├── Volume V │ ├── 00530.cpp │ ├── 00579.cpp │ ├── 00568.cpp │ ├── 00594.cpp │ └── 00575.cpp ├── Volume CI │ ├── 10105.cpp │ ├── 10110.cpp │ ├── 10195.cpp │ └── 10107.cpp ├── Volume CXI │ ├── 11172.cpp │ ├── 11185.cpp │ ├── 11192.cpp │ ├── 11137.cpp │ └── 11121.cpp ├── Volume CV │ ├── 10509.cpp │ └── 10550.cpp ├── Volume CXIV │ ├── 11480.cpp │ ├── 11401.cpp │ ├── 11494.cpp │ ├── 11498.cpp │ └── 11461.cpp ├── Volume III │ ├── 00386.cpp │ ├── 00374.cpp │ └── 00369.cpp ├── Volume CIX │ ├── 10931.cpp │ ├── 10970.cpp │ └── 10929.cpp ├── Volume CVIII │ ├── 10878.cpp │ └── 10812.cpp ├── Volume VII │ ├── 00756.cpp │ └── 00729.cpp └── Volume CXII │ └── 11207.cpp ├── codeforces ├── 050A.py ├── 041A.py ├── 047A.py ├── 046A.py ├── 025A.py ├── 038A.py ├── 009C.py ├── 037A.py ├── 004A.cpp ├── 006A.py ├── 034B.py ├── 058C.py ├── 013A.py ├── 001A.cpp ├── 005A.py ├── 008A.py ├── 034A.py ├── 043A.py ├── 025B.cpp ├── 049A.py ├── 009A.cpp ├── 009C.cpp ├── 043C.cpp ├── 059B.cpp ├── 026B.cpp ├── 053C.cpp ├── 058A.cpp ├── 005C.py ├── 005B.py ├── 066A.py ├── 022A.cpp ├── 027A.cpp ├── 055A.cpp ├── 011A.cpp ├── 032A.cpp ├── 029B.cpp ├── 043B.cpp ├── 012A.cpp ├── 005A.cpp ├── 038C.cpp ├── 054A.cpp ├── 046B.py ├── 050B.cpp ├── 066B.cpp ├── 033A.cpp ├── 026A.cpp ├── 036D.cpp ├── 058B.cpp ├── 029A.cpp ├── 058C.cpp ├── 023A.cpp ├── 056C.cpp ├── 008B.py ├── 053A.cpp ├── 056B.cpp ├── 066D.cpp ├── 048A.cpp ├── 059A.cpp └── 031A.cpp ├── pdf ├── gitbook.pdf ├── icalp03.pdf └── moresuffix.pdf ├── python ├── connect4_v1_0_0.sis └── mastermind_v1_0_0.sis ├── sulamericana ├── 2005 │ ├── mission.cpp │ ├── pascal.cpp │ └── tornado.cpp └── 2006 │ ├── odd.cpp │ └── lottery.cpp ├── spoj ├── classical │ ├── BISHOPS.py │ ├── UJ.py │ ├── EMP.cpp │ ├── TEST.cpp │ ├── SAMER08F.cpp │ ├── NGM.cpp │ ├── BINSTIRL.cpp │ ├── HUBULLU.cpp │ ├── ANARC08E.cpp │ ├── FENCE1.cpp │ ├── TRICOUNT.cpp │ ├── FCTRL.cpp │ ├── FAMILYP.cpp │ ├── NSTEPS.cpp │ ├── CANDY3.cpp │ ├── ANARC08H.cpp │ ├── RESN04.cpp │ ├── AE00.cpp │ ├── MARBLES.cpp │ ├── GNY07A.cpp │ ├── HELLOKIT.cpp │ ├── NEG2.cpp │ ├── ACPC10A.cpp │ ├── SUMITR.cpp │ ├── ADDREV.cpp │ ├── AE1B.cpp │ ├── EIGHTS.cpp │ ├── ANARC09A.cpp │ ├── HANGOVER.cpp │ ├── MIRRORED.cpp │ ├── TOANDFRO.cpp │ ├── PERMUT2.cpp │ ├── CANTON.cpp │ ├── ACODE.cpp │ ├── COINS.cpp │ ├── CANDY.cpp │ ├── MFLAR10.cpp │ ├── CPRMT.cpp │ ├── GNY07B.cpp │ ├── LASTDIG.cpp │ ├── OFFSIDE.cpp │ ├── BISHOPS.cpp │ ├── POLEVAL.cpp │ ├── FASHION.cpp │ ├── TRT.cpp │ ├── LAGRANGE.cpp │ ├── MRECAMAN.cpp │ ├── MAYA.cpp │ ├── PERMUT1.cpp │ ├── ROADNET.cpp │ ├── SAMER08G.cpp │ ├── STAMPS.cpp │ ├── MSTRMND.cpp │ ├── PIR.cpp │ ├── ANARC08G.cpp │ ├── SBANK.cpp │ ├── YAPP.cpp │ ├── DSUBSEQ.cpp │ ├── TTREAT.cpp │ ├── QUALITY.cpp │ └── PARTY.cpp ├── seletiva │ ├── .CALCULADORA.cpp.swp │ ├── F91.cpp │ ├── PARIDADE.cpp │ ├── ONZE.cpp │ ├── FATORIAL.cpp │ ├── WCW.cpp │ ├── BAPOSTAS.cpp │ ├── POPULAR.cpp │ ├── SANDUBA.cpp │ ├── RECUPERA.cpp │ ├── PLACAR.cpp │ └── DVORONOI.cpp ├── obi │ ├── QUADRAD2.cpp │ ├── OBIHANOI.cpp │ ├── DOBRA.cpp │ ├── FROTATAX.cpp │ ├── SOMA.cpp │ ├── FATORIA2.cpp │ ├── FLIPERAM.cpp │ ├── BAFO.cpp │ ├── COFRE.cpp │ ├── PAR.cpp │ ├── PRIMO.cpp │ ├── BIT.cpp │ ├── SUPERMER.cpp │ ├── QUERM.cpp │ ├── ESTAGIO.cpp │ ├── METEORO.cpp │ ├── PARPROX.cpp │ ├── AERO.cpp │ ├── ELEICOES.cpp │ ├── MINHOCA.cpp │ ├── CALCULA.cpp │ ├── SALDO.cpp │ ├── TRILHAS.cpp │ ├── LUA.cpp │ └── VIVO.cpp ├── sulamericana │ ├── FEYNMAN.cpp │ ├── ODDOREVE.cpp │ ├── IMPEDIDO.cpp │ ├── LOTERIA.cpp │ └── POLEPOS.cpp └── regional │ ├── VARETAS.cpp │ ├── COPA.cpp │ ├── DAMA.cpp │ ├── HIST.cpp │ ├── NLOGONIA.cpp │ ├── VAMPIROS.cpp │ ├── BALDES.cpp │ ├── BICHO.cpp │ └── BOLHAS.cpp ├── euler ├── e063.py ├── e056.py ├── e071.py ├── e057.py ├── e069.py ├── e065.py ├── e005.cpp ├── skel.cpp ├── pell.txt ├── e001.cpp ├── e015.cpp ├── e062.py ├── e006.cpp ├── e120.cpp ├── e002.cpp ├── e028.cpp ├── e117.cpp ├── e053.cpp ├── keylog.txt ├── e076.cpp ├── e008.cpp ├── e009.cpp ├── e048.cpp ├── e129.cpp ├── e003.cpp ├── e031.cpp ├── e138.cpp ├── e097.cpp ├── e099.cpp ├── e073.cpp ├── e042.cpp ├── e040.cpp ├── e034.cpp ├── e004.cpp ├── e007.cpp ├── e104.py ├── e173.cpp ├── e121.cpp ├── e026.cpp ├── e094.cpp └── e072.cpp ├── timus ├── T1000.cpp ├── T1224.cpp ├── T1083.cpp ├── T1068.cpp ├── T1225.cpp ├── T1025.cpp ├── T1073.cpp ├── T1014.cpp ├── T1009.cpp ├── T1005.cpp ├── T1049.cpp └── T1081.cpp ├── regional ├── 2006 │ └── copa.cpp ├── 2007 │ ├── varetas.cpp │ ├── hist.cpp │ └── bolhas.cpp └── 2008 │ └── dama.cpp ├── seletiva ├── IME-USP2009 │ ├── dias.cpp │ └── cinema.cpp ├── IME-USP2008-1 │ └── wcw.cpp └── IME-USP2008-2 │ ├── sanduba.cpp │ └── voronoi.cpp ├── fb └── hoppity.cpp └── tc ├── SRM363 └── HandsShaking.cpp ├── SRM412 └── ForbiddenStrings.cpp ├── SRM340 └── ProblemsToSolve.cpp └── SRM495 └── CarrotBoxesEasy.cpp /la/3469.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/la/3469.cpp -------------------------------------------------------------------------------- /uva/plist: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/uva/plist -------------------------------------------------------------------------------- /codeforces/050A.py: -------------------------------------------------------------------------------- 1 | m, n = map(int,raw_input().split()) 2 | print m*n/2 3 | -------------------------------------------------------------------------------- /pdf/gitbook.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/pdf/gitbook.pdf -------------------------------------------------------------------------------- /pdf/icalp03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/pdf/icalp03.pdf -------------------------------------------------------------------------------- /pdf/moresuffix.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/pdf/moresuffix.pdf -------------------------------------------------------------------------------- /codeforces/041A.py: -------------------------------------------------------------------------------- 1 | r = raw_input 2 | a, b = r()[::-1], r() 3 | print["NO","YES"][a==b] 4 | -------------------------------------------------------------------------------- /python/connect4_v1_0_0.sis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/python/connect4_v1_0_0.sis -------------------------------------------------------------------------------- /uva/Volume CXV/11507.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/uva/Volume CXV/11507.cpp -------------------------------------------------------------------------------- /codeforces/047A.py: -------------------------------------------------------------------------------- 1 | r = (1 + 8*input())**.5 2 | print ["NO","YES"][int(r) == r and not ((int(r)-1) & 1)] 3 | -------------------------------------------------------------------------------- /python/mastermind_v1_0_0.sis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/python/mastermind_v1_0_0.sis -------------------------------------------------------------------------------- /codeforces/046A.py: -------------------------------------------------------------------------------- 1 | x, n = 0, input() 2 | for i in range(1,n): 3 | x = (x + i) % n 4 | print x+1, 5 | print 6 | -------------------------------------------------------------------------------- /sulamericana/2005/mission.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/sulamericana/2005/mission.cpp -------------------------------------------------------------------------------- /spoj/classical/BISHOPS.py: -------------------------------------------------------------------------------- 1 | import sys 2 | for s in sys.stdin: 3 | x = int(s) - 1 4 | print(x << 1 if x else 1) 5 | -------------------------------------------------------------------------------- /spoj/seletiva/.CALCULADORA.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marinvhs/coding/HEAD/spoj/seletiva/.CALCULADORA.cpp.swp -------------------------------------------------------------------------------- /codeforces/025A.py: -------------------------------------------------------------------------------- 1 | input() 2 | x = map(lambda x: int(x)&1, raw_input().split()) 3 | print x.index(x.count(0) > 1) + 1 4 | 5 | -------------------------------------------------------------------------------- /codeforces/038A.py: -------------------------------------------------------------------------------- 1 | n, x = input(), map(int,raw_input().split()) 2 | a, b = map(int, raw_input().split()) 3 | print sum(x[a-1:b-1]) 4 | -------------------------------------------------------------------------------- /spoj/classical/UJ.py: -------------------------------------------------------------------------------- 1 | import sys 2 | while True: 3 | b, e = map(int,input().split()); 4 | if not(b or e): break 5 | print(b**e) 6 | 7 | -------------------------------------------------------------------------------- /spoj/obi/QUADRAD2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n; 4 | scanf("%d",&n); 5 | printf("%d\n",n*n); 6 | return 0; 7 | } -------------------------------------------------------------------------------- /codeforces/009C.py: -------------------------------------------------------------------------------- 1 | n, x, z = raw_input(), 0, '0' 2 | for c in n: 3 | x = (x << 1) + (c > '0' or z > '1') 4 | z = max(z, c) 5 | print x 6 | -------------------------------------------------------------------------------- /euler/e063.py: -------------------------------------------------------------------------------- 1 | from math import * 2 | cnt = 0 3 | for b in range(1, 10): 4 | cnt += int(1 / (1 - log10(b))) 5 | print('Answer is', cnt) 6 | -------------------------------------------------------------------------------- /la/4215.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n; 4 | while(scanf("%d",&n) && n) printf("%d\n",n*(n+1)*(2*n+1)/6); 5 | return 0; 6 | } -------------------------------------------------------------------------------- /codeforces/037A.py: -------------------------------------------------------------------------------- 1 | n, x = input(), map(int,raw_input().split()) 2 | c, s = [0] * 1024, 0 3 | for k in x: s += not c[k]; c[k] += 1 4 | print max(c),s 5 | -------------------------------------------------------------------------------- /euler/e056.py: -------------------------------------------------------------------------------- 1 | maxs = 0 2 | for i in range(1, 100): 3 | for j in range(1, 100): 4 | maxs = max(maxs, sum(int(x) for x in str(i ** j))) 5 | print(maxs) 6 | -------------------------------------------------------------------------------- /euler/e071.py: -------------------------------------------------------------------------------- 1 | a, b, c, d = 2, 5, 3, 7 2 | while b + d < 1000000: 3 | a, b = a + c, b + d 4 | print('Answer is {}, from fraction {}/{}'.format(a,a,b)) 5 | -------------------------------------------------------------------------------- /timus/T1000.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int a, b; scanf("%d %d", &a, &b)==2; printf("%d\n", a+b)); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /codeforces/004A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n; scanf("%d", &n); 5 | puts(n < 3 || n & 1 ? "NO" : "YES"); 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /la/4825.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n, s; scanf("%d %d", &n, &s) == 2; ) printf("%d\n", s / (n + 1)); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /spoj/classical/EMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int m, n; 4 | scanf("%d %d", &m, &n); 5 | printf("%d\n", m + n); 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /spoj/classical/TEST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) == 1 && n != 42; printf("%d\n", n)); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11877.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int n; scanf("%d", &n) == 1 && n; printf("%d\n", n >> 1)); 4 | return 0; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /spoj/sulamericana/FEYNMAN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n; 4 | while(scanf("%d",&n) && n) printf("%d\n",n*(n+1)*(2*n+1)/6); 5 | return 0; 6 | } -------------------------------------------------------------------------------- /codeforces/006A.py: -------------------------------------------------------------------------------- 1 | a = sorted(map(int, raw_input().split())) 2 | m = max(a[0]+a[1]-a[2],a[1]+a[2]-a[3]) 3 | print ["IMPOSSIBLE", "SEGMENT", "TRIANGLE"][(m>=0)+(m>0)] 4 | -------------------------------------------------------------------------------- /spoj/classical/SAMER08F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int n; scanf("%d", &n) == 1 && n; printf("%d\n", (n*(n+1)*(2*n+1))/6)); 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /codeforces/034B.py: -------------------------------------------------------------------------------- 1 | n, m = map(int,raw_input().split()) 2 | x, s = sorted(map(int,raw_input().split())), 0 3 | for i in range(m): 4 | s += -x[i] if x[i] < 0 else 0 5 | print s 6 | -------------------------------------------------------------------------------- /spoj/obi/OBIHANOI.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n, tnum = 1; 4 | while(scanf("%d",&n) && n) printf("Teste %d\n%d\n\n",tnum++,(1<0:x[d]+=1; 7 | print n-max(x) 8 | -------------------------------------------------------------------------------- /euler/e057.py: -------------------------------------------------------------------------------- 1 | from math import * 2 | ans, d, n = 0, 1, 1 3 | for i in range(999): 4 | d, n = n + d, n + (d << 1) 5 | ans += floor(log10(n)) > floor(log10(d)) 6 | print(ans) 7 | 8 | -------------------------------------------------------------------------------- /codeforces/013A.py: -------------------------------------------------------------------------------- 1 | import fractions 2 | n, s = input(), 0 3 | for i in range(2, n): 4 | x = n 5 | while x: s += x % i; x /= i 6 | g = fractions.gcd(s, n-2) 7 | print str(s/g)+'/'+str((n-2)/g) 8 | -------------------------------------------------------------------------------- /codeforces/001A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void){ 4 | long long a, m, n; cin >> m >> n >> a; 5 | cout << ((m+a-1)/a) * ((n+a-1)/a) << endl; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /codeforces/005A.py: -------------------------------------------------------------------------------- 1 | import sys 2 | a, c = 0, 0 3 | for s in sys.stdin: 4 | if s[0] == '+' or s[0] == '-': c += ((s[0] == '+') << 1) - 1 5 | else: a += c * (len(s) - s.index(':') - 2) 6 | print a 7 | -------------------------------------------------------------------------------- /spoj/classical/NGM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n; scanf("%d", &n); 5 | if(n % 10) printf("%d\n%d\n", 1, n % 10); 6 | else puts("2"); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /spoj/obi/DOBRA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int k, n, t; 4 | for(t = 1; scanf("%d",&n) && n != -1 ; t++) 5 | printf("Teste %d\n%d\n\n", t,(k = (1< 2 | int main(void){ 3 | float a, g, ra, rg; 4 | scanf("%f%f%f%f",&a,&g,&ra,&rg); 5 | if((ra/a) > (rg/g)) puts("A"); 6 | else puts("G"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /spoj/obi/SOMA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n, sum = 0, v; 4 | scanf("%d",&n); 5 | while(n--) 6 | scanf("%d",&v), sum += v; 7 | printf("%d\n",sum); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /euler/e069.py: -------------------------------------------------------------------------------- 1 | primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] 2 | ans = 1 3 | for i in primes: 4 | if ans * i > 1000000: 5 | print('Answer is', ans) 6 | break 7 | else: 8 | ans *= i 9 | -------------------------------------------------------------------------------- /codeforces/008A.py: -------------------------------------------------------------------------------- 1 | import re 2 | r = raw_input 3 | o, p = r(), r()+'.*'+r() 4 | x = re.compile(p) 5 | f = x.search(o) > 0 6 | b = x.search(o[::-1]) > 0 7 | print ["fantasy","backward","forward","both"][b + (f << 1)] 8 | -------------------------------------------------------------------------------- /codeforces/034A.py: -------------------------------------------------------------------------------- 1 | n, h = input(), map(int, raw_input().split()) 2 | idx, mind = -1, 1024 3 | for i in range(n): 4 | t = abs(h[i] - h[i - 1]) 5 | if t < mind: mind = t; idx = i 6 | print idx+1, 1 + (idx + n - 1) % n 7 | -------------------------------------------------------------------------------- /spoj/seletiva/F91.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int n; 3 | int main(void){ 4 | scanf("%d",&n); 5 | while(n){ 6 | printf("f91(%d) = %d\n",n,(n<102)?91:n-10); 7 | scanf("%d",&n); 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /spoj/classical/BINSTIRL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int m, n, t; 4 | for(scanf("%d", &t); t-- && scanf("%d %d", &n, &m); ) 5 | printf("%d\n", 0 == ((n - m) & ((m - 1) >> 1))); 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /spoj/obi/FATORIA2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int fat[13] = {1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600}; 3 | int main(void){ 4 | int n; 5 | scanf("%d",&n); 6 | printf("%d\n",fat[n]); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /spoj/classical/HUBULLU.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int s, t; 5 | for(scanf("%d", &t); t-- && scanf("%*d %d", &s); ) 6 | puts(1-s ? "Airborne wins." : "Pagfloyd wins."); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /timus/T1224.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int m, n; scanf("%d %d", &n, &m) == 2; ) 5 | if(n <= m) printf("%u\n", (n << 1) - 2); 6 | else printf("%u\n", (m << 1) - 1); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /spoj/classical/ANARC08E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int a, b; scanf("%d %d", &a, &b) == 2 && (a > -1 || b > -1); ) 4 | printf("%d+%d%s%d\n", a, b, a == 1 || b == 1 ? "=" : "!=", a + b); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /spoj/classical/FENCE1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const double c = .5/acos(-1); 4 | int main(void){ 5 | for(int L; scanf("%d", &L) == 1 && L; ) 6 | printf("%.2lf\n", L * L * c); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /spoj/classical/TRICOUNT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | for(long long n; scanf("%lld", &n) == 1; ) 6 | printf("%lld\n", (n * (2*n+1) * (n + 2)) >> 3); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11847.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int n; scanf("%d", &n) == 1 && n; ){ 4 | int ans = 0; 5 | while(n > 1) ++ans, n >>= 1; 6 | printf("%d\n", ans); 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume I/00136.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00 136 - Ugly Numbers 3 | ///////////////////////////////// 4 | #include 5 | int main(){ 6 | printf("The 1500'th ugly number is 859963392.\n"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11714.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int n; 6 | while(scanf("%d", &n) == 1){ 7 | int ans = --n + (int)log2(n); 8 | printf("%d\n", ans); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume IV/00458.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00458 - The Decoder 3 | ///////////////////////////////// 4 | #include 5 | int main(){ 6 | char c; 7 | while((c = getchar())!=EOF) putchar(c=='\n'?'\n': (char)(c-7)); 8 | } 9 | -------------------------------------------------------------------------------- /codeforces/043A.py: -------------------------------------------------------------------------------- 1 | score = [0, 1] 2 | n = input(); 3 | team1, team2 = raw_input(), "" 4 | for i in range(n-1): 5 | s = raw_input() 6 | score[s == team1] += 1 7 | if s != team1: team2 = s 8 | print team1 if score[1] > score[0] else team2 9 | 10 | -------------------------------------------------------------------------------- /euler/e065.py: -------------------------------------------------------------------------------- 1 | d, dsum, n = 1, 0, 0 2 | for i in range(1, 101): 3 | if not(i % 3): x = int(2 * i / 3) 4 | else: x = 1 + (i == 1) 5 | d, n = x * d + n, d 6 | dsum = 0 7 | sn = str(d) 8 | for x in str(d): 9 | dsum += int(x) 10 | print(dsum) 11 | -------------------------------------------------------------------------------- /spoj/classical/FCTRL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, t; scanf("%d", &t); 5 | while(t--){ 6 | int ans = 0; 7 | scanf("%d", &n); 8 | while(n) ans += n /= 5; 9 | printf("%d\n", ans); 10 | } 11 | return 0; 12 | } -------------------------------------------------------------------------------- /codeforces/025B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int c = 0, n; scanf("%d\n", &n); 4 | while(n--){ 5 | putc(getc(stdin), stdout); 6 | if(++c == 2 && n>1) c = 0, putc('-', stdout); 7 | } 8 | putc(10, stdout); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /codeforces/049A.py: -------------------------------------------------------------------------------- 1 | def is_letter(x): return 'a' <= x <= 'z' or 'A' <= x <= 'Z' 2 | def is_vowel(x): return x in ['A','E','I','O','U','Y'] 3 | s = raw_input()[::-1].upper() 4 | for c in s: 5 | if is_letter(c): 6 | print "YES" if is_vowel(c) else "NO" 7 | break 8 | -------------------------------------------------------------------------------- /uva/Volume CVII/10784.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int cs = 1; 5 | for(long long k; scanf("%lld", &k) && k; ++cs) 6 | printf("Case %d: %.0lf\n", cs, ceil(1.5 + sqrt(9 + (k << 3))/2)); 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /codeforces/009A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int W, Y; scanf("%d %d", &Y, &W); 5 | int n = 7 - (W > Y? W : Y), d = 6; 6 | if(!(n % 3)) n /= 3, d = 2; d >>= !(n & 1); n >>= !(n & 1); 7 | printf("%d/%d\n", n, d); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /euler/e005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 7 | printf("Answer: %d\n", 2*2*2*2*3*3*5*7*11*13*17*19); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /euler/skel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 8 | printf("Answer: %d\n", ans); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /spoj/classical/FAMILYP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | for(int n, t; scanf("%d", &n) == 1; ){ 5 | t = ceil(sqrt(1 + (n<<3)) / 2 - .5); 6 | printf("TERM %d is %c\n", n, ('A' + (t-1) % 26)); 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11636.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int c = 0, n; scanf("%d", &n) == 1 && n > 0; ){ 4 | int ans = !!(n & (n - 1)); 5 | while(n > 1) ++ans, n >>= 1; 6 | printf("Case %d: %d\n", ++c, ans); 7 | } 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /spoj/classical/NSTEPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int t, x, y; scanf("%d", &t); 4 | while(t--){ 5 | scanf("%d %d", &x, &y); 6 | if(x != y && x - y != 2) puts("No Number"); 7 | else printf("%d\n", x + y - (y & 1)); 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /timus/T1083.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char f[32]; 5 | for(int n; scanf("%d%s", &n, f) == 2; ){ 6 | int r = 1, k = strlen(f); 7 | while(n > 0) r *= n, n -= k; 8 | printf("%d\n", r); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CIV/10491.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int NC, NW, NS; 5 | while(scanf("%d %d %d", &NW, &NC, &NS) == 3){ 6 | int T = NW + NC; 7 | printf("%.5lf\n", (NC * (T - 1)/(double)(T * (T - NS - 1)))); 8 | } 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /uva/Volume IX/00900.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long f[55]; 4 | int main(void){ 5 | f[1] = 1; f[2] = 2; 6 | for(int k = 3; k < 55; ++k) f[k] = f[k - 1] + f[k - 2]; 7 | for(int n; scanf("%d",&n)==1 && n; ) printf("%lld\n",f[n]); 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /uva/Volume C/10071.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10071 - Back to High School Physics 3 | ///////////////////////////////// 4 | #include 5 | int a, b; 6 | int main(){ 7 | while(scanf("%d %d",&a,&b)!=EOF){ 8 | printf("%d\n",2*a*b); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /regional/2007/varetas.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int c, n, pares, v; 3 | int main(void){ 4 | while(scanf("%d",&n) && n){ 5 | pares = 0; 6 | while(n--){ 7 | scanf("%d%d",&c,&v); 8 | pares += (v>>1); 9 | } 10 | printf("%d\n",pares>>1); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /spoj/regional/VARETAS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int c, n, pares, v; 3 | int main(void){ 4 | while(scanf("%d",&n) && n){ 5 | pares = 0; 6 | while(n--){ 7 | scanf("%d%d",&c,&v); 8 | pares += (v>>1); 9 | } 10 | printf("%d\n",pares>>1); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /timus/T1068.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) == 1; ){ 5 | bool neg = n < 1; 6 | if(n < 0) n = -n; 7 | if(neg) printf("%d\n", 1 - (n * n + n) / 2); 8 | else printf("%d\n", (n * n + n) / 2); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11805.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int k, n, p, t; 3 | int main(void){ 4 | scanf("%d", &t); 5 | for(int cnum = 0; cnum < t; ){ 6 | scanf("%d %d %d", &n, &k, &p); 7 | k--; 8 | printf("Case %d: %d\n", ++cnum, ((k + p) % n) + 1); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume VI/00675.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00675 - Convex Hull of the Polygon 3 | ///////////////////////////////// 4 | #include 5 | const char *F = "FUCK"; 6 | int main(void){ 7 | char s[100]; 8 | while(gets(s)) puts(F); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /codeforces/009C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char c, p = '0'; 5 | int x = 0; 6 | for(c = getc(stdin); isdigit(c); p = c > p ? c : p, c = getc(stdin)) 7 | x = (x << 1) + (c > '0' || p > '1'); 8 | printf("%d\n", x); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /codeforces/043C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int mod[3]; 3 | int main(void){ 4 | int n, t; scanf("%d", &n); 5 | mod[0] = mod[1] = mod[2] = 0; 6 | while(n--){ scanf("%d", &t); ++mod[t % 3]; } 7 | printf("%d\n", (mod[0] >> 1)+ (mod[1] < mod[2] ? mod[1] : mod[2])); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /codeforces/059B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int m = 128, n, s = 0, t; scanf("%d", &n); 4 | while(n--){ 5 | scanf("%d", &t); 6 | s += t; 7 | if(t & 1 && t < m) m = t; 8 | } 9 | if(!(s & 1)) s -= m & 1 ? m : s; 10 | printf("%d\n", s); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /uva/Volume IX/00991.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int catalan[10] = {1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796}; 4 | int main(void){ 5 | for(int n, p = 0; scanf("%d", &n) == 1; ){ 6 | if(p++) putc(10, stdout); 7 | printf("%d\n", catalan[n - 1]); 8 | } 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /codeforces/026B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int ans = 0, bal = 0; 4 | for(char c = getc(stdin); c == '(' || c == ')'; c = getc(stdin)){ 5 | if(c == '(') ++ans, ++bal; 6 | if(c == ')' && bal) ++ans, --bal; 7 | } 8 | printf("%d\n", ans - bal); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /uva/Volume CII/10286.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const double pi = acos(-1); 5 | const double cv = sin((108 * pi)/ 180) / sin((63 * pi)/180); 6 | 7 | int main(void){ 8 | double n; 9 | while(scanf("%lf",&n)==1) printf("%.10lf\n",n * cv); 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11879.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char num[128]; 4 | int main(void){ 5 | while(scanf("%s", num) == 1 && num[0] != '0'){ 6 | int mod = 0; 7 | for(char *p = num; *p; ++p) mod = (10 * mod + *p - '0') % 17; 8 | printf("%d\n", !mod); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume VIII/00847.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const char *w[2] = {"Stan wins.","Ollie wins."}; 3 | unsigned int a, div[2] = {2,9}; 4 | int main(void){ 5 | for(bool x; scanf("%u",&a)!=EOF; puts(w[x])) 6 | for(x = 1; a > 1; x=!x) 7 | a = (a+div[x]-1)/div[x]; 8 | return 0; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /codeforces/053C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int n; scanf("%d", &n); 4 | int jump = n - 1; 5 | for(int i = 1; ; ){ 6 | printf("%d%c", i, jump ? ' ' : '\n'); 7 | if(!jump) break; 8 | i += jump; 9 | jump = -jump + (jump > 0) - (jump < 0); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /codeforces/058A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char h[16] = "hello"; 4 | char s[128]; 5 | int main(void){ 6 | scanf("%s", s); 7 | int j = 0; 8 | for(int i = 0; s[i]; ++i){ 9 | if(s[i] == h[j]) ++j; 10 | } 11 | if(j == 5) puts("YES"); 12 | else puts("NO"); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume C/10055.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10055 - Hashmat the Brave Warrior 3 | ///////////////////////////////// 4 | #include 5 | int main(){ 6 | register unsigned long long a,b; 7 | while(scanf("%llu %llu",&a,&b)!=EOF) 8 | printf("%llu\n",(b>a)?b-a:a-b); 9 | } 10 | -------------------------------------------------------------------------------- /uva/Volume CIII/10302.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10302 - Summation of Polynomials 3 | ///////////////////////////////// 4 | #include 5 | unsigned long long int n; 6 | int main(void){ 7 | while(scanf("%llu",&n)==1) printf("%llu\n",n*n*(n+1)*(n+1)/4); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11666.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int L, h, n; 6 | while(scanf("%d",&n) && n){ 7 | L = 0; 8 | h = n>>1; 9 | while(pow(M_E, L) < h) L++; 10 | printf("%d %.8lf\n",L, 1. - (double)n/pow(M_E,L)); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /codeforces/005C.py: -------------------------------------------------------------------------------- 1 | b, big, cnt, s, z = -1, 0, 1, [], raw_input() 2 | for i in range(len(z)): 3 | if z[i] == '(': s.append(i) 4 | elif s: 5 | s.pop() 6 | L = i - s[-1] if len(s) else i - b 7 | if L > big: big = L; cnt = 1; 8 | else: cnt += L == big 9 | else: b = i 10 | print big, cnt 11 | 12 | -------------------------------------------------------------------------------- /spoj/classical/CANDY3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int i, n, t; scanf("%d", &t); 5 | long long s, z; 6 | while(t--){ 7 | scanf("%d", &n); 8 | for(i = s = 0; i < n; ++i) scanf("%lld", &z), s = (s + z) % n; 9 | puts(s ? "NO" : "YES"); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11723.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int R, N; 5 | for(int cnum = 0; (scanf("%d %d", &R, &N) && R); ){ 6 | printf("Case %d: ", ++cnum); 7 | int s = (R - 1)/N; 8 | if(s > 26) printf("impossible\n"); 9 | else printf("%d\n", s); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /codeforces/005B.py: -------------------------------------------------------------------------------- 1 | import sys 2 | s = [z.strip() for z in sys.stdin] 3 | L = max(map(len,s)) 4 | print '*' * (L + 2) 5 | l = 0; 6 | for z in s: 7 | sp = L - len(z) 8 | spl = (sp + l) >> 1; spr = sp - spl 9 | print '*' + ' ' * spl + z + ' ' * spr + '*' 10 | if sp & 1: l = 1 - l 11 | print '*' * (L + 2) 12 | -------------------------------------------------------------------------------- /uva/Volume CVI/10696.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10696 - f91 3 | ///////////////////////////////// 4 | #include 5 | int n; 6 | int main(void){ 7 | scanf("%d",&n); 8 | while(n){ 9 | printf("f91(%d) = %d\n",n,(n<102)?91:n-10); 10 | scanf("%d",&n); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11677.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int h1, m1, h2, m2; 5 | while(scanf("%d %d %d %d",&h1,&m1,&h2,&m2)){ 6 | if((h1 + h2 + m1 + m2) == 0) break; 7 | if(h2 < h1 || (h2 == h1 && m2 < m1)) h2 += 24; 8 | printf("%d\n",(h2-h1)*60+(m2-m1)); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11799.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int k, n, t, x; scanf("%d", &t); 5 | for(int cnum = 0; cnum++ < t; ){ 6 | x = 0; 7 | scanf("%d", &n); 8 | while(n--) scanf("%d", &k), x = x > k ? x : k; 9 | printf("Case %d: %d\n", cnum , x); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11875.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int v[128]; 3 | int main(void){ 4 | int n, t; scanf("%d", &t); 5 | for(int c = 0; c < t; ++c){ 6 | scanf("%d", &n); 7 | for(int i = 0; i < n; ++i) scanf("%d", &v[i]); 8 | printf("Case %d: %d\n", c + 1, v[n >> 1]); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /spoj/classical/ANARC08H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int dp[1048576]; 4 | int main(void){ 5 | dp[1] = 0; 6 | for(int d, i, n; scanf("%d %d", &n, &d) == 2 && (n || d); ){ 7 | for(i = 2; i <= n; ++i) dp[i] = (dp[i - 1] + d) % i; 8 | printf("%d %d %d\n", n, d, dp[n] + 1); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /spoj/regional/COPA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int T, N; 4 | 5 | int main(void){ 6 | char ign[128]; 7 | while(scanf("%d %d", &T, &N) && T){ 8 | int E = 0, p; 9 | for(int i = 0; i < T; ++i) scanf("%s %d", &ign, &p), E += p; 10 | printf("%d\n", N + (N << 1) - E); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /uva/Volume CVI/10678.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const double hhpi = atan(1); 4 | int sqr(int x){ return x * x; } 5 | int main(void){ 6 | int D, L, z; scanf("%d", &z); 7 | while(scanf("%d %d", &D, &L) == 2) 8 | printf("%.3lf\n", hhpi * L * sqrt(sqr(L) - sqr(D))); 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /uva/Volume I/00113.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00113 - Power of Cryptography 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | double a,b; 7 | int main(void){ 8 | while(scanf("%lf %lf",&a,&b)!=EOF) 9 | printf("%.0lf\n",exp(log(b)/a)); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume II/00272.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00272 - TEX Quotes 3 | ///////////////////////////////// 4 | #include 5 | int main(){ 6 | char c; 7 | for (int count=0; (c = getchar())!=EOF;){ 8 | if(c=='"') printf((++count&1)? "``":"''" ); 9 | else putchar(c); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /codeforces/066A.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if n < -9223372036854775808 or n > 9223372036854775807: 3 | print("BigInteger") 4 | elif n < -2147483648 or n > 2147483647: 5 | print("long") 6 | elif n < -32768 or n > 32767: 7 | print("int") 8 | elif n < -128 or n > 127: 9 | print("short") 10 | else: 11 | print("byte") 12 | -------------------------------------------------------------------------------- /la/3656.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int i, k, n, v1, v2; 5 | while(scanf("%d",&n) && n){ 6 | for(v1 = i = 0; i < n; i++) scanf("%d",&k), v1 += k&1; 7 | for(v2 = i = 0; i < n; i++) scanf("%d",&k), v2 += !(k&1); 8 | printf("%d\n",abs(v1-v2)); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume IX/00913.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00913 - Joana and the Odd Numbers 3 | ///////////////////////////////// 4 | #include 5 | unsigned long long int k; 6 | int main(void){ 7 | while(scanf("%llu",&k)==1){ 8 | k = (k*(k+2)-5)/2*3; 9 | printf("%llu\n",k); 10 | } 11 | return 0; 12 | } -------------------------------------------------------------------------------- /codeforces/022A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, t, x = 128, y = 128; 5 | scanf("%d", &n); 6 | while(n--){ 7 | scanf("%d", &t); 8 | if(t < x) y = x, x = t; 9 | else if(t != x && t < y) y = t; 10 | } 11 | if(y == 1 << 7) puts("NO"); 12 | else printf("%d\n", y); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /euler/pell.txt: -------------------------------------------------------------------------------- 1 | Pell equations 2 | 3 | Pell's equation is any Diophantine equation of the form 4 | 5 | x^2 - n y^2 = 1 6 | 7 | (x/y) ~ sqrt(n) when x-> inf 8 | 9 | After finding the fundamental solution, new solutions can be generated: 10 | 11 | x_k = x_1 * x_k-1 + n * y_1 * y_k-1 12 | y_k = x_1 * y_k-1 + y_1 * x_k-1 13 | -------------------------------------------------------------------------------- /spoj/classical/RESN04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, s, t, z; scanf("%d", &t); 5 | while(t-- && scanf("%d", &n) == 1){ 6 | s = 0; 7 | for(int i = 1; i <= n; ++i){ 8 | scanf("%d", &z); 9 | s += z / i; 10 | } 11 | puts(s & 1 ? "ALICE" : "BOB"); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /spoj/classical/AE00.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int ans[10048]; 4 | int main(void){ 5 | ans[0] = 0; 6 | for(int i = 1; i < 10001; ++i){ 7 | ans[i] = ans[i - 1]; 8 | for(int j = 1; j * j <= i; ++j) ans[i] += !(i % j); 9 | } 10 | for(int n; scanf("%d", &n) == 1; printf("%d\n", ans[n])); 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /codeforces/027A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | bool used[3072]; 4 | int main(void){ 5 | memset(used, 0, 3072); 6 | int a, n; scanf("%d", &n); 7 | while(n--){ 8 | scanf("%d", &a); 9 | used[a] = 1; 10 | } 11 | for(int i = 1; ; ++i) 12 | if(!used[i]){ printf("%d\n", i); break; } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /euler/e001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | int sum = 0; 7 | for(int i = 3; i < 1000; ++i) sum += (!(i % 3) || !(i % 5) ? i : 0); 8 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 9 | printf("Answer: %d\n", sum); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /euler/e015.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | long long ans = 1; 7 | for(int c = 40, d = 1; c > 20; c--, d++) ans = (ans * c)/d; 8 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 9 | printf("Answer: %lld\n", ans); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /spoj/classical/MARBLES.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, k, t; scanf("%d", &t); 5 | while(t--){ 6 | scanf("%d %d", &n, &k); --n; --k; 7 | long long r = 1; 8 | if(n - k < k) k = n - k; 9 | for(int i = 0; i < k; ) r *= n--, r /= ++i; 10 | printf("%lld\n", r); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /sulamericana/2006/odd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int i, k, n, v1, v2; 5 | while(scanf("%d",&n) && n){ 6 | for(v1 = i = 0; i < n; i++) scanf("%d",&k), v1 += k&1; 7 | for(v2 = i = 0; i < n; i++) scanf("%d",&k), v2 += !(k&1); 8 | printf("%d\n",abs(v1-v2)); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume CIV/10499.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10499 - The Land of Justice 3 | ///////////////////////////////// 4 | #include 5 | long long int n; 6 | int main(void){ 7 | while(scanf("%lld\n",&n) && n>0) 8 | if(n!=1) printf("%llu%%\n",n*25); 9 | else printf("0%%\n"); 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11892.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char *ans[2] = {"piloop", "poopi"}; 4 | int main(void){ 5 | int c, i, n, t, x; scanf("%d", &t); 6 | while(t--){ 7 | scanf("%d", &n); 8 | for(c = i = 0; i < n; ++i) scanf("%d", &x), c += x == 1; 9 | puts(ans[n - c || c & 1]); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /seletiva/IME-USP2009/dias.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | long long gcd(long long a, long long b) { 5 | if(a % b == 0) return b; 6 | return gcd(b, a % b); 7 | } 8 | int main(void){ 9 | long long a, b; 10 | while(scanf("%lld%lld",&a,&b) && a){ 11 | printf("%lld\n",((a*b)/gcd(a,b))%1713371337LL); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /spoj/obi/FLIPERAM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int v[10000], i, m, n; 4 | bool inv(int a, int b){ 5 | return a > b; 6 | } 7 | int main(void){ 8 | scanf("%d%d",&n,&m); 9 | for(i = 0; i < n; i++) scanf("%d",&v[i]); 10 | std::sort(v,v+n,inv); 11 | for(i = 0; i < m; i++) printf("%d\n",v[i]); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /spoj/sulamericana/ODDOREVE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int i, k, n, v1, v2; 5 | while(scanf("%d",&n) && n){ 6 | for(v1 = i = 0; i < n; i++) scanf("%d",&k), v1 += k&1; 7 | for(v2 = i = 0; i < n; i++) scanf("%d",&k), v2 += !(k&1); 8 | printf("%d\n",abs(v1-v2)); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /uva/Volume C/10079.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10079 - Pizza Cutting 3 | ///////////////////////////////// 4 | #include 5 | int k; 6 | long long int n; 7 | int main(void){ 8 | scanf("%d",&k); 9 | while(k >= 0){ 10 | n = k; 11 | n = n*(n+1)/2+1; 12 | printf("%lld\n",n); 13 | scanf("%d",&k); 14 | } 15 | } -------------------------------------------------------------------------------- /uva/Volume CIV/10407.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(void){ 5 | for(int ans, n, p; scanf("%d", &n) && n; ){ 6 | scanf("%d", &p); 7 | ans = abs(p - n); 8 | while(scanf("%d", &p) && p) ans = __gcd(ans, abs(p - n)); 9 | printf("%d\n", ans); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /euler/e062.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | ans, d, f, s = '', {}, {}, set() 3 | for i in range(1, 10000): 4 | nums = ''.join(sorted(str(i ** 3))) 5 | l = len(s) 6 | s.add(nums) 7 | if len(s) == l: 8 | d[nums] += 1 9 | else: 10 | d[nums] = 1 11 | f[nums] = i ** 3 12 | if d[nums] == 5: 13 | print('Answer is', f[nums]) 14 | break 15 | 16 | -------------------------------------------------------------------------------- /spoj/classical/GNY07A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int i, k, t; scanf("%d", &t); 5 | for(int c = 0; c++ < t; ){ 6 | scanf("%d\n", &k); i = 0; 7 | printf("%d ", c); 8 | for(char c; (c = getc(stdin)) != 10 && c != EOF; ) 9 | if(++i != k) putc(c, stdout); 10 | putc(10, stdout); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /spoj/obi/BAFO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int a, b, n, tnum = 1, v1, v2; 4 | while(scanf("%d",&n) && n){ 5 | a = b = 0; 6 | printf("Teste %d\n",tnum++); 7 | while(n--){ 8 | scanf("%d%d",&v1,&v2); 9 | a += v1; b += v2; 10 | } 11 | if(a > b) puts("Aldo\n"); 12 | else puts("Beto\n"); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /uva/Volume CX/11054.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int n; scanf("%d", &n) && n; ){ 5 | long long balance = 0, mov = 0, x; 6 | while(n--){ 7 | scanf("%lld", &x); 8 | x = x + balance; 9 | mov += (x < 0 ? -x:x); 10 | balance = x; 11 | } 12 | printf("%lld\n", mov); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /codeforces/055A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool v[1024]; 4 | int main(void){ 5 | int n; scanf("%d", &n); 6 | for(int i = 0; i < n; ++i) v[i] = 0; 7 | int vc = 0; 8 | for(int at = 0, c = 0; c < n; ++c){ 9 | vc += !v[at]; 10 | v[at] = 1; 11 | at = (at + c + 1) % n; 12 | } 13 | puts(vc == n ? "YES" : "NO"); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /spoj/classical/HELLOKIT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | char w[1024]; 5 | for(int l, n, p; scanf("%s %d", w, &n) == 2; ){ 6 | p = (l = strlen(w))*n; 7 | for(int i = 0; i < l; ++i){ 8 | for(int j = 0; j < p; ++j) putc(w[(i+j)%l], stdout); 9 | puts(""); 10 | } 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/classical/NEG2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char s[128]; 4 | int main(void){ 5 | int d, n, t = 0; scanf("%d", &n); 6 | if(n == 0) puts("0"); 7 | else { 8 | while(n){ 9 | s[t++] = '0' + (n & 1); 10 | n = (n - (n < 0)) / -2; 11 | } 12 | while(t--) putc(s[t], stdout); 13 | putc(10, stdout); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /spoj/obi/COFRE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int a, b, dep, diff, teste = 1; 4 | while(scanf("%d",&dep) && dep){ 5 | diff = 0; 6 | printf("Teste %d\n",teste++); 7 | while(dep--){ 8 | scanf("%d%d",&a,&b); 9 | diff = diff + (a - b); 10 | printf("%d\n",diff); 11 | } 12 | putchar('\n'); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /codeforces/011A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int add = 0, pv, v = 0; 4 | int n, d; scanf("%d %d", &n, &d); 5 | for(int i = 0; i < n; ++i){ 6 | pv = v; 7 | scanf("%d", &v); 8 | if(v <= pv){ 9 | int c = (pv - v) / d + 1; 10 | v += c * d; 11 | add += c; 12 | } 13 | } 14 | printf("%d\n", add); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /codeforces/032A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int v[1000]; 3 | int abs(int a){ return a < 0 ? -a : a; } 4 | int main(void){ 5 | int ans = 0, d, n; scanf("%d %d", &n, &d); 6 | for(int i = 0; i < n; ++i){ 7 | scanf("%d", &v[i]); 8 | for(int j = 0; j < i; ++j) if(abs(v[j] - v[i]) <= d) ans += 2; 9 | } 10 | printf("%d\n", ans); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /euler/e006.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define n 100 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | int sqsum = (n * (n + 1) * (2 * n + 1)) / 6, sumsq = (1 + n) * n / 2; 8 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 9 | printf("Answer: %d\n", sumsq*sumsq - sqsum); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /euler/e120.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | int ans = 0; 8 | for(int i = 3; i < 1001; ++i) ans += (i << 1) * ((i - 1) >> 1); 9 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 10 | printf("Answer: %d\n", ans); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /spoj/obi/PAR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | char name[2][15]; 4 | int n, a, b, tnum = 1; 5 | while(scanf("%d",&n) && n){ 6 | scanf("%s%s",&name[0],&name[1]); 7 | printf("Teste %d\n",tnum++); 8 | while(n--){ 9 | scanf("%d%d",&a,&b); 10 | puts(name[(a+b)&1]); 11 | } 12 | putchar('\n'); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /uva/Volume CX/11044.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11044 - Searching for Nessy 3 | ///////////////////////////////// 4 | #include 5 | unsigned int m,n,cnum; 6 | int main(void){ 7 | scanf("%u\n",&cnum); 8 | while(cnum--){ 9 | scanf("%u %u\n",&m,&n); 10 | m = (m/3)*(n/3); 11 | printf("%u\n",m); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /codeforces/029B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int d, g, l, r, ttl, v; 6 | double eta; 7 | scanf("%d %d %d %d %d", &l, &d, &v, &g, &r); 8 | eta = l /(double) v; 9 | ttl = d % ((g + r) * v); 10 | if(ttl >= g * v) eta += ((r + g) * v - ttl)/(double)v; 11 | printf("%.8lf\n", eta); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/obi/PRIMO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int n; 4 | int main(void){ 5 | scanf("%d",&n); 6 | if(n < 0) n = -n; 7 | int i, lim = (int)(sqrt(n)+1); 8 | bool primo = !(n == 1 || (!(n&1) && n != 2)); 9 | for(i = 3; primo && i < lim; i+=2) 10 | primo = n%i; 11 | if(primo) puts("sim"); 12 | else puts("nao"); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /uva/Volume CXV/11547.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11547 - Automatic Answer 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #define pos(a) (a < 0? -a: a) 7 | int n,c; 8 | int main(void){ 9 | scanf("%d",&c); 10 | while(c--){ 11 | scanf("%d",&n); 12 | printf("%d\n",pos((315*n+36962)%100/10)); 13 | } 14 | } -------------------------------------------------------------------------------- /codeforces/043B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define IN getc(stdin) 3 | #define READL(x) for(char c = IN; c != EOF && c != 10; c = IN) 4 | int cnt[128]; 5 | int main(void){ 6 | bool yes = true; 7 | for(int i = 0; i < 128; ++i) cnt[i] = 0; 8 | READL(c) ++cnt[c]; 9 | READL(c) yes &= c == 32 || cnt[c]--; 10 | puts(yes ? "YES":"NO"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /fb/hoppity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *args[]){ 4 | int a; 5 | if(argc != 2) return 1; 6 | freopen(args[1], "r", stdin); 7 | scanf("%d", &a); 8 | for(int i = 1; i <= a; ++i) 9 | if(!(i % 3) && !(i % 5)) puts("Hop"); 10 | else if(!(i % 3)) puts("Hoppity"); 11 | else if(!(i % 5)) puts("Hophop"); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/classical/ACPC10A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int a, b, c; scanf("%d %d %d", &a, &b, &c) == 3; ){ 5 | if(!(a || b || c)) break; 6 | if(c - b == b - a) printf("AP %d\n", (c<<1) - b); 7 | else printf("GP %d\n", (c * c) / b); 8 | } 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /spoj/seletiva/PARIDADE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | unsigned int n; 3 | char ans[35],i,par; 4 | int main(void){ 5 | ans[34] = 0; 6 | while(scanf("%u",&n) && n){ 7 | i = 33; par = 0; 8 | do { 9 | ans[i--] = (n&1)+'0'; par += n&1; n>>=1; 10 | } while(n); 11 | printf("The parity of %s is %u (mod 2).\n",ans+i+1,par); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CXIII/11310.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long s[41]; 4 | int main(void){ 5 | int N, T; 6 | s[1] = 1; s[2] = 5; s[3] = 11; 7 | for(int k = 4; k < 41; ++k) 8 | s[k] = s[k-1] + (s[k - 2] << 2) + (s[k - 3] << 1); 9 | scanf("%d",&T); 10 | while(T--){ 11 | scanf("%d", &N); 12 | printf("%lld\n",s[N]); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CXIII/11388.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11388 - GCD LCM 3 | ///////////////////////////////// 4 | #include 5 | unsigned int a,b,tnum; 6 | int main(void){ 7 | scanf("%u",&tnum); 8 | while(tnum--){ 9 | scanf("%u %u",&a,&b); 10 | if(!(b%a)) printf("%u %u\n",a,b); 11 | else printf("-1\n"); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /spoj/classical/SUMITR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define F(i,n) for(i=1;i<=n;++i) 3 | #define R(x) scanf("%d",&x) 4 | int main(void){ 5 | int i,j,*k,m[108][108],n,t,z; R(t); 6 | while(t--){ 7 | R(n);z=0; 8 | F(i,n)F(j,i) 9 | R(m[i][j]), 10 | k=m[i-1], 11 | z=(m[i][j]+=k[j-1]>k[j]?k[j-1]:k[j])>z?m[i][j]:z; 12 | printf("%d\n",z); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CIV/10432.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10432 - Polygon Inside A Circle 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | const double PI2 = 4*acos(0); 7 | unsigned int n; 8 | double R; 9 | int main(void){ 10 | while(scanf("%lf %u\n",&R,&n)!=EOF) printf("%.3lf\n",sin(PI2/n)*n*R*R/2); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /uva/Volume CXV/11556.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11556 - Best Compression Ever 3 | ///////////////////////////////// 4 | #include 5 | unsigned long long int a,b; 6 | const char *y = "yes", *n = "no"; 7 | int main(void){ 8 | while(scanf("%llu %llu",&a,&b)!=EOF) 9 | if(a < 1LLU<<(b+1)) puts(y); 10 | else puts(n); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /spoj/classical/ADDREV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template _T reverse(_T a){ 4 | for(_T r = 0; ; a /= 10) 5 | if(a) r = r * 10 + (a % 10); 6 | else return r; 7 | } 8 | int main(void){ 9 | int a, b, t; scanf("%d", &t); 10 | while(t--){ 11 | scanf("%d %d", &a, &b); 12 | printf("%d\n", reverse(reverse(a) + reverse(b))); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /spoj/classical/AE1B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int x[1024]; 4 | int main(void){ 5 | int k, n, s; scanf("%d %d %d", &n, &k, &s); 6 | int i, need = k * s; 7 | for(i = s = 0; i < n; ++i) scanf("%d", x + i), s += x[i]; 8 | std::sort(x, x + n); 9 | for(i = 0; s - x[i] >= need; s -= x[i++]); 10 | printf("%d\n", n - i); 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /la/4191.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | LL f(LL t, LL c, LL n){ 4 | return t * (c - t * n); 5 | } 6 | int main(void){ 7 | for(int c, n; scanf("%d %d", &n, &c) == 2; ){ 8 | LL t; 9 | if(n) { 10 | t = (c / (2 * n)); 11 | while(f(t + 1, c, n) > f(t, c, n)) t++; 12 | } 13 | printf("%d\n", n ? int(t) : 0); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /regional/2006/copa.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int n, t, npontos, temp; 4 | char nome[32]; 5 | 6 | int main(void) { 7 | while(scanf("%d %d",&t,&n)) { 8 | if(n == 0 && t == 0) break; 9 | npontos = 0; 10 | for(int i=0;i < t; i++) { 11 | scanf("%s %d",nome,&temp); 12 | npontos+=temp; 13 | } 14 | printf("%d\n",3*n-npontos); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /spoj/classical/EIGHTS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int end[4] = {192, 442, 692, 942}; 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | for(long long n; t-- && scanf("%lld", &n); ){ 6 | int idx = --n & 3; 7 | if(n >>= 2) printf("%lld", n); 8 | printf("%d\n", end[idx]); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /spoj/obi/BIT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int u[4]; 4 | for(int a = 1, n; scanf("%d",&n) && n; a++){ 5 | u[0] = u[1] = u[2] = u[3] = 0; 6 | if(n>49) u[0] = n/50, n -= u[0]*50; 7 | if(n>9) u[1] = n/10, n -= u[1]*10; 8 | if(n>4) u[2] = n/5, n -= u[2]*5; 9 | u[3] = n; 10 | printf("Teste %d\n%d %d %d %d\n",a,u[0],u[1],u[2],u[3]); 11 | } 12 | return 0; 13 | } -------------------------------------------------------------------------------- /spoj/seletiva/ONZE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | char num[1005]; 3 | unsigned short mod11,i; 4 | int main(void){ 5 | while(scanf("%s\n",&num)!=EOF){ 6 | if(num[0] == '0' && !num[1]) return 0; 7 | for(mod11 = i = 0; num[i]; i++) mod11 = (mod11*10+num[i]-'0')%11; 8 | if(mod11) printf("%s is not a multiple of 11.\n"); 9 | else printf("%s is a multiple of 11.\n"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /timus/T1225.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int dp[45][2]; 4 | int main(void){ 5 | dp[0][0] = dp[0][1] = dp[1][0] = dp[1][1] = 1; 6 | for(int i = 2; i < 45; ++i){ 7 | dp[i][0] = dp[i - 1][1] + dp[i - 2][1]; 8 | dp[i][1] = dp[i - 1][0] + dp[i - 2][0]; 9 | } 10 | for(int n; scanf("%d", &n) == 1; ) 11 | printf("%u\n", dp[n-1][0] + dp[n-1][1]); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/classical/ANARC09A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | char input[2048]; 3 | int main(void){ 4 | for(int cnum = 0; scanf("%s", input) && input[0] != '-'; ){ 5 | int b = 0, c = 0; 6 | for(int i = 0; input[i]; ++i){ 7 | b += (input[i] == '{') - (input[i] == '}'); 8 | if(b < 0) b += 2, ++c; 9 | } 10 | printf("%d. %d\n", ++cnum, c + (b >> 1)); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/classical/HANGOVER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float p[512]; 4 | int main(void){ 5 | p[0] = 0; 6 | for(int i = 1; i < 512; ++i) p[i] = p[i - 1] + 1. / (i + 1); 7 | for(float x; scanf("%f", &x) && x; ){ 8 | int i; for(i = 0; p[i] < x; ++i); 9 | printf("%d card(s)\n", i); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /spoj/obi/SUPERMER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int i, n; 4 | int vx[1000] ,vy[1000]; 5 | int main(void){ 6 | for(int t = 1; scanf("%d",&n) && n; t++){ 7 | for(i = 0; i < n; i++) scanf("%d%d",&vx[i],&vy[i]); 8 | std::sort(vx,vx+n); 9 | std::sort(vy,vy+n); 10 | n >>= 1; 11 | printf("Teste %d\n%d %d\n\n",t,vx[n],vy[n]); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /timus/T1025.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int v[128]; 5 | int main(void){ 6 | for(int k; scanf("%d", &k) == 1; ){ 7 | int ans = 0; 8 | for(int i = 0; i < k; ++i) scanf("%d", v + i); 9 | sort(v, v + k); 10 | for(int i = 0; i < (k + 1) >>1; ++i) ans += (1 + v[i]) >> 1; 11 | printf("%d\n", ans); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /codeforces/012A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int dx[4] = {-1, 0, 1, -1}; 4 | int dy[4] = {-1, -1, -1, 0}; 5 | char s[3][16]; 6 | int main(void){ 7 | bool symmetric = true; 8 | for(int i = 0; i < 3; ++i) scanf("%s", s[i]); 9 | for(int d = 0; d < 4; ++d) 10 | symmetric &= s[1 + dx[d]][1 + dy[d]] == s[1 - dx[d]][1 - dy[d]]; 11 | puts(symmetric ? "YES" : "NO"); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /la/4820.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const double lim = 1 - (1e-9); 4 | int main(void){ 5 | for(int n; scanf("%d", &n) == 1; ){ 6 | if(n == 0) { puts("0"); continue; } 7 | int cnt = 1; double w, z; scanf("%lf", &w); 8 | for(int i = 1; i < n; ++i){ 9 | scanf("%lf", &z); 10 | if(z - w > lim) w = z, ++cnt; 11 | } 12 | printf("%d\n", cnt); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /spoj/classical/MIRRORED.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int f[128]; 4 | char s[16]; 5 | int main(void){ 6 | for(int i = 0; i < 128; ++i) f[i] = 0; 7 | f['p'] = f['q'] = 1; 8 | f['b'] = f['d'] = 2; 9 | puts("Ready"); 10 | while(fgets(s, 16, stdin) && (*s != ' ' || *(s+1) != ' ')) 11 | puts(f[*s] && f[*s] == f[*(s+1)] ? "Mirrored pair" : "Ordinary pair"); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spoj/regional/DAMA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int xi,yi,xf,yf; 4 | bool a,b; 5 | int main(void){ 6 | while(scanf("%d %d %d %d\n",&xi,&yi,&xf,&yf) && xi){ 7 | a = xi == xf; 8 | b = yi == yf; 9 | if(a && b) putchar('0'); 10 | else if(a || b || (abs(xf-xi) == abs(yf-yi))) putchar('1'); 11 | else putchar('2'); 12 | putchar('\n'); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /uva/Volume V/00530.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00530 - Binomial Showdown 3 | ///////////////////////////////// 4 | #include 5 | long double n,k,sum,c; 6 | int main(void){ 7 | while(scanf("%Lf %Lf",&n,&k)){ 8 | if(n==0) break; 9 | if(k> n-k) k=n-k; 10 | for(sum=1,c=k;c>0;c--,n--) sum*=n; 11 | for(;k>1;k--) sum/=k; 12 | printf("%.0Lf\n",sum); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /codeforces/005A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | char l[128]; 3 | int main(void){ 4 | int ans = 0, c = 0; 5 | while(fgets(l, 128, stdin)){ 6 | if(*l == '+' || *l == '-') c += ((*l == '+') << 1) - 1; 7 | else { 8 | char *p = l, *q; 9 | while(*p && *p++ != ':'); q = p; while(*p++ > 30); 10 | ans += (p - q - 1) * c; 11 | } 12 | } 13 | printf("%d\n", ans); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /codeforces/038C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int v[128]; 3 | int main(void){ 4 | int ans = 0, n, l; scanf("%d %d", &n, &l); 5 | for(int i = 0; i < n; ++i) scanf("%d", &v[i]); 6 | for(int i = l; i < 101; ++i){ 7 | int c = 0; 8 | for(int j = 0; j < n; ++j) 9 | c += v[j] / i; 10 | if(c * i > ans) ans = c * i; 11 | if(!c) break; 12 | } 13 | printf("%d\n", ans); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /spoj/classical/TOANDFRO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | char s[256]; 4 | for(int c; scanf("%d", &c) && c; ){ 5 | int l, t = c << 1; 6 | scanf("%s", s); 7 | for(l = -1; s[++l]; ); 8 | for(int i = 0; i < c; ++i){ 9 | int j = 1 + (i << 1); 10 | for(int p = i; p < l; p += j = t - j) 11 | putc(s[p], stdout); 12 | } 13 | puts(""); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /uva/Volume CI/10105.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long f[14]; 4 | 5 | int main(void){ 6 | int k, K, N; 7 | f[0] = 1; 8 | for(k = 1; k < 14; ++k) 9 | f[k] = k * f[k - 1]; 10 | while(scanf("%d%d",&K,&N) == 2){ 11 | long long r = f[K]; 12 | for(int i = 0; i < N; ++i){ 13 | scanf("%d",&k); 14 | r /= f[k]; 15 | } 16 | printf("%lld\n", r); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CX/11069.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11069 - A Graph Problem 3 | ///////////////////////////////// 4 | #include 5 | unsigned int ways[77],i; 6 | int main(void){ 7 | ways[1] = 1; 8 | ways[2] = ways[3] = 2; 9 | for(i = 4; i != 77; i++) 10 | ways[i] = ways[i-2]+ways[i-3]; 11 | while(scanf("%u",&i)!=EOF) printf("%u\n",ways[i]); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /codeforces/054A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int v[512]; 4 | int main(void){ 5 | int c, k, n; scanf("%d %d %d", &n, &k, &c); 6 | int idx = 0, lp = 0, pc = 0; 7 | for(int i = 0; i < c; ++i) 8 | scanf("%d", &v[i]); 9 | for(int i = 1; i <= n; ++i) 10 | if(i - lp == k || v[idx] == i){ 11 | idx += v[idx] == i; 12 | lp = i; 13 | ++pc; 14 | } 15 | printf("%d\n", pc); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /la/4556.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | char number[128]; 6 | int main(void){ 7 | int z; scanf("%d", &z); 8 | for(int c; scanf("%d %s", &c, number) == 2; ){ 9 | if(next_permutation(number, number + strlen(number))) 10 | printf("%d %s\n", c, number); 11 | else printf("%d BIGGEST\n", c); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CIII/10346.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10346 - Peter's Smoke 3 | ///////////////////////////////// 4 | #include 5 | int ciggs,n,k,butts; 6 | int main(void){ 7 | while(scanf("%d %d",&n,&k)!=EOF){ 8 | ciggs = n; 9 | while(n >= k){ 10 | butts = n%k; 11 | n/=k; 12 | ciggs+= n; 13 | n+=butts; 14 | } 15 | printf("%d\n",ciggs); 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /uva/Volume CX/11000.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11000 - Bee 3 | ///////////////////////////////// 4 | #include 5 | unsigned int f[45],i,m[45]; 6 | int k; 7 | int main(void){ 8 | m[0] = 0; f[0] = 1; 9 | for(i = 1; i < 45; i++) 10 | m[i] = f[i-1], f[i] = m[i-1]+f[i-1]+1; 11 | while(scanf("%d",&k) && k != -1) 12 | printf("%u %u\n",m[k],f[k]); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CXI/11172.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11172 - Relational Operator 3 | ///////////////////////////////// 4 | #include 5 | unsigned int a,b,cnum; 6 | int main(void){ 7 | scanf("%u\n",&cnum); 8 | while(cnum--){ 9 | scanf("%u %u\n",&a,&b); 10 | if(a < b) printf("<\n"); 11 | else if(a > b) printf(">\n"); 12 | else printf("=\n"); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /uva/Volume I/00146.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00146 - ID Codes 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | char c[55]; 7 | int i; 8 | int main(void){ 9 | while(gets(c)){ 10 | if(c[0] == '#') break; 11 | for(i = 0; c[i]; i++); 12 | if(std::next_permutation(c,c+i)) printf("%s\n",c); 13 | else printf("No Successor\n"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /codeforces/046B.py: -------------------------------------------------------------------------------- 1 | size = ['S', 'M', 'L', 'XL', 'XXL'] 2 | have = map(int, raw_input().split()) 3 | n = input() 4 | for i in range(n): 5 | s = raw_input() 6 | x = size.index(s) 7 | for i in range(5): 8 | if x + i < 5 and have[x + i]: 9 | print size[x + i] 10 | have[x + i] -= 1 11 | break 12 | if x - i > -1 and have[x - i]: 13 | print size[x - i] 14 | have[x - i] -= 1 15 | break 16 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11878.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int ans = 0; char op, x; 5 | int a, b, c; 6 | while(scanf("%d%c%d=", &a, &op, &b) == 3){ 7 | x = getc(stdin); 8 | if(x == '?') continue; 9 | ungetc(x, stdin); 10 | scanf("%d", &c); 11 | if(op == '-' && a - b == c) ans++; 12 | if(op == '+' && a + b == c) ans++; 13 | } 14 | printf("%d\n", ans); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /uva/Volume V/00579.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00579 - ClockHands 3 | ///////////////////////////////// 4 | #include 5 | int a,b; 6 | float res; 7 | int main(void){ 8 | scanf("%d:%d",&a,&b); 9 | while(a){ 10 | a%=12; 11 | res = 30*a-5.5*b; 12 | if(res < 0) res = -res; 13 | if(res > 180) res = 360-res; 14 | printf("%.3lf\n",res); 15 | scanf("%d:%d",&a,&b); 16 | } 17 | } -------------------------------------------------------------------------------- /euler/e002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | int f1, f2, f3, sum = 0; 7 | f1 = f2 = 1; 8 | while(f3 < 4000000){ 9 | f3 = f1 + f2; 10 | if(!(f3 & 1)) sum += f3; 11 | f1 = f2; 12 | f2 = f3; 13 | } 14 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 15 | printf("Answer: %d\n", sum); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /la/4552.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int z; scanf("%d", &z); 5 | for(int c; scanf("%d", &c) == 1; ){ 6 | int f, n, s, t; 7 | f = s = t = -1; 8 | for(int i = 0; i < 10; ++i){ 9 | scanf("%d", &n); 10 | if(n >= f) t = s, s = f, f = n; 11 | else if(n >= s) t = s, s = n; 12 | else if(n >= t) t = n; 13 | } 14 | printf("%d %d\n", c, t); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /spoj/classical/PERMUT2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int x[1<<17], y[1<<17]; 4 | int main(void){ 5 | for(int n; scanf("%d", &n) && n; ){ 6 | bool ambiguous = true; 7 | for(int i = 0; i < n; ++i){ 8 | scanf("%d", x + i); 9 | y[x[i]-1] = i + 1; 10 | } 11 | for(int i = 0; i < n; ++i) ambiguous &= x[i] == y[i]; 12 | puts(ambiguous ? "ambiguous" : "not ambiguous"); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /spoj/seletiva/FATORIAL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1000001 3 | #define K 20000 4 | long long int i, r[MAX]; 5 | int inum = 1; 6 | int main(void){ 7 | r[0] = r[1] = 1; 8 | for(i = 2; i < MAX; i++){ 9 | r[i] = r[i-1]*i; 10 | while(r[i]%10 == 0) r[i] /= 10; 11 | r[i] %= 20000; 12 | } 13 | while(scanf("%lld",&i)!= EOF) 14 | printf("Instancia %d\n%lld\n\n",inum++,r[i]%10); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /timus/T1073.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int N = 60048; 4 | int dp[N]; 5 | int min(int a, int b){ return a < b ? a : b; } 6 | int main(void){ 7 | int i, isq, j; 8 | for(i = 0; i < N; ++i) dp[i] = i; 9 | for(i = 1; i*i <= N; ++i) 10 | for(j = isq = i * i; j <= N; ++j) 11 | dp[j] = min(dp[j], dp[j-isq] + 1); 12 | for(int n; scanf("%d", &n) == 1; ) printf("%d\n", dp[n]); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CII/10219.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | long long max(long long a, long long b){ return a > b ? a : b; } 4 | int main(void){ 5 | for(long long k, n; scanf("%lld %lld", &n, &k) == 2; ){ 6 | double r = 0; 7 | k = max(k, n - k); 8 | for(long long i = 1, j = k + 1; j <= n; ++j, ++i) 9 | r += log10(j) - log10(i); 10 | printf("%.0lf\n", floor(r) + 1); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /uva/Volume CIV/10473.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10473 - Simple Base Conversion 3 | ///////////////////////////////// 4 | #include 5 | char line[30]; 6 | unsigned int n; 7 | int main(void){ 8 | while(fgets(line,30,stdin)){ 9 | if(line[0]=='-') return 0; 10 | if(line[1]!='x') sscanf(line,"%u",&n), printf("0x%X\n",n); 11 | else sscanf(line+2,"%X",&n), printf("%u\n",n); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /uva/Volume CV/10509.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10509 - R U Kidding Mr. Feynman! 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #define EPS 1e-6 7 | double dx,n, div = 1.0/3; 8 | int a; 9 | int main(void){ 10 | while(scanf("%lf",&n) && n){ 11 | a = pow(n+EPS,div); 12 | dx = (n-a*a*a)/(3*a*a); 13 | printf("%.4lf\n",dx+a); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /codeforces/050B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | long long cnt[128]; 4 | int main(void){ 5 | long long ans = 0; 6 | for(int i = 0; i < 128; ++i) cnt[i] = 0; 7 | for(char c = getc(stdin); c != EOF; c = getc(stdin)) ++cnt[c]; 8 | for(char c = '0'; c <= '9'; ++c) ans += cnt[c]*cnt[c]; 9 | for(char c = 'a'; c <= 'z'; ++c) ans += cnt[c]*cnt[c]; 10 | std::cout << ans << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /la/3389.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int N, T; 5 | scanf("%d", &T); 6 | for(int cnum = 0; cnum < T; ){ 7 | scanf("%d",&N); 8 | int q, d, n, p; 9 | q = N/25; 10 | N%= 25; 11 | d = N/10; 12 | N%= 10; 13 | n = N/5; 14 | N%= 5; 15 | p = N; 16 | printf("%d %d QUARTER(S), %d DIME(S), %d NICKEL(S), %d PENNY(S)\n", 17 | ++cnum, q, d, n, p); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume CXIV/11480.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11480 - Jimmy's Balls 3 | ///////////////////////////////// 4 | #include 5 | long long n; 6 | int cnum = 0; 7 | int main(void){ 8 | while(scanf("%lld",&n) && n) 9 | if(n%6) printf("Case %d: %lld\n", ++cnum, ((n%6) + 3 * ((n-7)/6)) * ((n-1)/6)); 10 | else printf("Case %d: %lld\n", ++cnum, 3 * ((n-7)/6) * ((n-1)/6) + n-5); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /uva/Volume CXVI/11689.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int e, f, c, T; 5 | int drank, empty, more; 6 | scanf("%d",&T); 7 | while(T--){ 8 | scanf("%d%d%d",&e,&f,&c); 9 | drank = 0; 10 | empty = f+e; 11 | while(empty >= c){ 12 | more = empty/c; 13 | empty = empty%c; 14 | drank += more; 15 | empty += more; 16 | } 17 | printf("%d\n",drank); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /spoj/classical/CANTON.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int n, t; scanf("%d", &t); 5 | while(t--){ 6 | scanf("%d", &n); 7 | int top = int(ceil(-.5 + sqrt(1 + (n << 3))/2.)); 8 | int u = (top * (top + 1)) >> 1; 9 | if(top & 1) printf("TERM %d IS %d/%d\n", n, 1 + u - n, n - u + top); 10 | else printf("TERM %d IS %d/%d\n", n, n - u + top, 1 + u - n); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /spoj/seletiva/WCW.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int aux, cn, i, n, swap, v[10001]; 3 | int main(void){ 4 | scanf("%d",&cn); 5 | while(cn--){ 6 | scanf("%d",&n); swap = 0; 7 | for(i = 1; i <= n; i++) scanf("%d",&v[i]); 8 | for(i = 1; i <= n; i++) 9 | while(v[i] != i){ 10 | aux = v[i]; 11 | v[i] = v[v[i]]; 12 | v[aux] = aux; 13 | swap++; 14 | } 15 | printf("%d\n",swap); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume CII/10209.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10209 - Is This Integration ? 3 | ///////////////////////////////// 4 | #include 5 | double a,a1,a2,a3; 6 | int main(void){ 7 | while(scanf("%lf",&a)!=EOF){ 8 | a*=a; 9 | a1 = 0.315146743627720*a; 10 | a2 = 0.511299166334351*a; 11 | a3 = 0.173554090037927*a; 12 | printf("%.3lf %.3lf %.3lf\n",a1,a2,a3); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11727.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int a, ans, b, c, cnum = 0, t; 5 | scanf("%d", &t); 6 | while(t--){ 7 | scanf("%d %d %d", &a, &b, &c); 8 | if(a > b){ 9 | if(b > c) ans = b; 10 | else if(a > c) ans = c; 11 | else ans = a; 12 | } else if(a > c) ans = a; 13 | else if(b > c) ans = c; 14 | else ans = b; 15 | printf("Case %d: %d\n", ++cnum, ans); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /codeforces/066B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int h[1024]; 3 | int main(void){ 4 | int maxw = 0, n; 5 | scanf("%d", &n); 6 | for(int i = 0; i < n; ++i) scanf("%d", h + i); 7 | for(int i = 0; i < n; ++i){ 8 | int l, r; 9 | for(l = i; l > 0 && h[l - 1] <= h[l]; --l); 10 | for(r = i; r < n-1 && h[r + 1] <= h[r]; ++r); 11 | if(r - l + 1 > maxw) maxw = r - l + 1; 12 | } 13 | printf("%d\n", maxw); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /regional/2007/hist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int lastexe[1000001], i, k, n, total; 4 | int main(void){ 5 | while(scanf("%d",&n) && n){ 6 | memset(lastexe,-1,sizeof(lastexe)); 7 | for(i = total = 0; i < n; i++){ 8 | scanf("%d",&k); 9 | if(lastexe[k]!=-1) total += (i-lastexe[k]); 10 | else total += k+i; 11 | lastexe[k] = i; 12 | } 13 | printf("%d\n",total); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /spoj/regional/HIST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int lastexe[1000001], i, k, n, total; 4 | int main(void){ 5 | while(scanf("%d",&n) && n){ 6 | memset(lastexe,-1,sizeof(lastexe)); 7 | for(i = total = 0; i < n; i++){ 8 | scanf("%d",&k); 9 | if(lastexe[k]!=-1) total += (i-lastexe[k]); 10 | else total += k+i; 11 | lastexe[k] = i; 12 | } 13 | printf("%d\n",total); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /spoj/seletiva/BAPOSTAS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | for(int n, max; scanf("%d",&n) && n; ){ 4 | max = -1; 5 | for(int i = 0, x, sum = 0; i < n; i++){ 6 | scanf("%d",&x); 7 | sum += x; 8 | if(sum > max) max = sum; 9 | else if(sum < 0) sum = 0; 10 | } 11 | if(max > 0) printf("The maximum winning streak is %d.\n",max); 12 | else printf("Losing streak.\n"); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CXI/11185.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11185 - Ternary 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #define MAXCAND 8 7 | int n; 8 | char ans[30],idx; 9 | int main(void){ 10 | ans[29] = 0; 11 | while(scanf("%d",&n) && n >= 0){ 12 | idx = 29; 13 | do{ 14 | ans[--idx] = ('0'+n%3); 15 | n/=3; 16 | } while(n); 17 | puts(ans+idx); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11764.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int h, l, n, p, r, t; 5 | scanf("%d", &t); 6 | for(int cnum = 0; cnum < t; ){ 7 | scanf("%d", &n); 8 | h = l = 0; 9 | for(int i = 0; i < n; ++i){ 10 | p = r; 11 | scanf("%d", &r); 12 | if(i){ 13 | h += r > p; 14 | l += r < p; 15 | } 16 | } 17 | printf("Case %d: %d %d\n", ++cnum, h, l); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume V/00568.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00568 - Just the Facts 3 | ///////////////////////////////// 4 | #include 5 | unsigned int i,n,last; 6 | int main(void){ 7 | while(scanf("%d",&n)==1){ 8 | for(last = i = 1; !(i>n); ++i){ 9 | last *= i; 10 | while(!(last%10)) last/=10; 11 | if(last>20000) last%=20000; 12 | } 13 | printf("%5u -> %u\n",n,last%10); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /euler/e028.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define LIM 1001 6 | int main(void){ 7 | clock_t ini = clock(); 8 | int ans = 1, n = 1, inc = 2; 9 | for(int i = 3; i <= LIM; i += 2, inc += 2) 10 | for(int c = 0; c < 4; ++c) 11 | ans += (n += inc); 12 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 13 | printf("Answer: %d\n", ans); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /seletiva/IME-USP2008-1/wcw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int aux, cn, i, n, swap, v[10001]; 3 | int main(void){ 4 | scanf("%d",&cn); 5 | while(cn--){ 6 | scanf("%d",&n); swap = 0; 7 | for(i = 1; i <= n; i++) scanf("%d",&v[i]); 8 | for(i = 1; i <= n; i++) 9 | while(v[i] != i){ 10 | aux = v[i]; 11 | v[i] = v[v[i]]; 12 | v[aux] = aux; 13 | swap++; 14 | } 15 | printf("%d\n",swap); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /seletiva/IME-USP2009/cinema.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int N, M; 3 | int main(void){ 4 | int i, M, N, T; 5 | scanf("%d",&T); 6 | while(T--){ 7 | scanf("%d%d",&N,&M); 8 | long long res = 1; 9 | for(i = 0; i < M; i++){ 10 | long long x = N-M-i; 11 | res = (res*x)%1300031LL; 12 | } 13 | for(i = 0; i < M; i++){ 14 | res = (res<<1)%1300031LL; 15 | } 16 | printf("%lld\n",res); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /spoj/classical/ACODE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long w[5005]; 4 | int main(void){ 5 | for(char c, p; (c = getc(stdin) - '0') > 0; ){ 6 | w[0] = w[1] = 1; 7 | int i; 8 | for(i = 1, p = c; (c = getc(stdin) - '0') > -1; p = c, ++i) 9 | w[i + 1] = (c ? w[i] : 0) + (p && 10*p+c < 27 ? w[i - 1]: 0); 10 | printf("%lld\n", w[i]); 11 | c += '0'; 12 | while(c!=10) c = getc(stdin); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /spoj/classical/COINS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | std::map < unsigned, unsigned > m; 4 | unsigned exchange(unsigned n){ 5 | unsigned &r = m[n]; 6 | if(r) return r; 7 | unsigned sum = (n/2) + (n/3) + (n/4); 8 | if(sum > n) return r = exchange(n/2) + exchange(n/3) + exchange(n/4); 9 | return r = n; 10 | } 11 | int main(void){ 12 | for(int n; scanf("%d", &n) == 1; ) printf("%u\n", exchange(n)); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /spoj/seletiva/POPULAR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int i, j, max, n, v[100]; 3 | int main(void){ 4 | register char c; 5 | while(scanf("%d\n",&n) && n){ 6 | for(i = 0; i < n; i++) 7 | for(j = 0; j < n; j++){ 8 | c = getchar(); 9 | getchar(); 10 | if(c == '1') v[j]++; 11 | } 12 | for(max = i = 0; i < n; v[i] = 0, i++) 13 | if(v[i] > max) max = v[i], j = i; 14 | printf("%d\n",max); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /uva/Volume CIV/10451.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const double PI = 2 * acos(0); 5 | const double PI2 = 2 * PI; 6 | int main(void){ 7 | int n; 8 | double A; 9 | for(int c = 1; (scanf("%d %lf", &n, &A) && n > 2); ++c){ 10 | double R = (A * 2.) / (n * sin(PI2 / n)); 11 | double r = A / (n * tan(PI / n)); 12 | printf("Case %d: %.5lf %.5lf\n", c, PI * R - A, A - PI * r); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11608.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int w[16]; 3 | int main(void){ 4 | for(int c = 0, n, z; scanf("%d", &n) == 1 && n >= 0; ){ 5 | printf("Case %d:\n", ++c); 6 | for(int i = 0; i < 12; ++i) scanf("%d", &w[i]); 7 | for(int i = 0; i < 12; ++i){ 8 | scanf("%d", &z); 9 | if(n >= z) n -= z, puts("No problem! :D"); 10 | else puts("No problem. :("); 11 | n += w[i]; 12 | } 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11839.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int n, v; 5 | while(scanf("%d", &n) && n){ 6 | for(int k = 0; k < n; ++k){ 7 | char ans = 0; 8 | for(int i = 0; i < 5; ++i){ 9 | scanf("%d", &v); 10 | ans = v < 128 ? ans ? '*' : i + 'A' : ans; 11 | } 12 | if(ans) putc(ans, stdout); 13 | else putc('*', stdout); 14 | putc(10, stdout); 15 | } 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /codeforces/033A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int t[1005], r[1005]; 4 | int main(void){ 5 | int k, m, n, x, y; scanf("%d %d %d", &n, &m, &k); 6 | for(int i = 0; i < 1005; ++i) r[i] = 0; 7 | while(n--){ 8 | scanf("%d %d", &x, &y); 9 | if(!r[x]) t[x] = y; 10 | else if(y < t[x]) t[x] = y; 11 | r[x]++; 12 | } 13 | x = 0; 14 | for(int i = 1; i <= m; ++i) x += t[i]; 15 | printf("%d\n", x > k ? k : x); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /spoj/classical/CANDY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int x[10005]; 3 | int main(void){ 4 | for(int mean, n, s = 0, t; scanf("%d", &n) && n != -1; ){ 5 | s = 0; 6 | for(int i = 0; i < n; ++i) scanf("%d", x + i), s += x[i]; 7 | mean = s / n; 8 | if(!(s % n)){ 9 | mean = s / n; s = 0; 10 | for(int i = 0; i < n; ++i) s += x[i] > mean ? x[i] - mean : 0; 11 | printf("%d\n", s); 12 | } else puts("-1"); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /spoj/obi/QUERM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | char line[90000]; 5 | int main(void){ 6 | int n; 7 | for(int a = 1, n; scanf("%d\n",&n) && n; a++){ 8 | gets(line); 9 | char *ptr = strtok(line," "); 10 | int i = 0; 11 | while(ptr!=NULL){ 12 | int k = atoi(ptr); 13 | if(k == ++i) break; 14 | ptr = strtok(NULL," "); 15 | } 16 | printf("Teste %d\n%d\n\n",a,i); 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /codeforces/026A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | bool ap(int x){ 3 | int div = !(x & 1); 4 | while(!(x&1)) x >>= 1; 5 | for(int i = 3; x != 1; i += 2){ 6 | div += !(x % i); 7 | while(!(x%i)) x /= i; 8 | if(i * i > x) break; 9 | } 10 | div += x != 1; 11 | return div == 2; 12 | } 13 | int main(void){ 14 | int ans = 0, n; scanf("%d", &n); 15 | for(int i = 6; i <= n; ++i) ans += ap(i); 16 | printf("%d\n", ans); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /codeforces/036D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | #ifdef ONLINE_JUDGE 4 | freopen("input.txt", "r", stdin); 5 | freopen("output.txt", "w", stdout); 6 | #endif 7 | const char *ans[2] = {"-", "+"}; 8 | int t, k; scanf("%d %d", &t, &k); ++k; 9 | for(int m, n; t-- && scanf("%d %d", &m, &n) == 2; ){ 10 | if(n > m) n ^= m ^= n ^= m; 11 | puts(ans[!(n % k) || ((n / k) & k > 2)^((m + n) & 1)]); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /euler/e117.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | long long w[51]; 6 | int main(void){ 7 | clock_t ini = clock(); 8 | w[0] = w[1] = 1; w[2] = 2; w[3] = 4; 9 | for(int i = 4; i < 51; ++i) 10 | w[i] = w[i - 1] + w[i - 2] + w[i - 3] + w[i - 4]; 11 | long long ans = w[50]; 12 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 13 | printf("Answer: %lld\n", ans); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /la/4810.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char line[2048]; 6 | int main(void){ 7 | while(fgets(line, 2048, stdin) && line[0] != '*'){ 8 | char *p = strtok(line, " "); 9 | char letter = tolower(line[0]); 10 | while(p != NULL){ 11 | if(tolower(*p) != letter) break; 12 | p = strtok(NULL, " "); 13 | } 14 | if(p == NULL) puts("Y"); 15 | else puts("N"); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /uva/Volume CI/10110.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10110 - Light, More Light 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | long long i,n,root; 7 | 8 | bool on; 9 | int main(void){ 10 | scanf("%lld",&n); 11 | while(n){ 12 | on = 0; 13 | root = (int)sqrt(n); 14 | on = (root*root == n); 15 | if(on) printf("yes\n"); 16 | else printf("no\n"); 17 | scanf("%lld",&n); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /uva/Volume II/00271.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const char *r[2] = {"NO", "YES"}; 4 | char s[1024]; 5 | int c, l, ok; 6 | int main(void){ 7 | while(scanf("%s", s) == 1){ 8 | c = 0; ok = 1; 9 | for(l = 0; s[l]; ++l); 10 | while(ok && l--){ 11 | if(s[l] == 'N') ok &= c > 0; 12 | else if(islower(s[l])) c++; 13 | else ok &= c-- > 1; 14 | } 15 | ok &= c == 1; 16 | puts(r[ok]); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /euler/e053.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | int ans = 0; 8 | for(int i = 23; i < 101; ++i) 9 | for(int d = 1, n = i, v = 1; n > i / 2; n--, d++) 10 | if((v = v*n/d) > 1000000){ ans += (i + 1) - (d << 1); break; } 11 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 12 | printf("Answer: %d\n", ans); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /euler/keylog.txt: -------------------------------------------------------------------------------- 1 | 319 2 | 680 3 | 180 4 | 690 5 | 129 6 | 620 7 | 762 8 | 689 9 | 762 10 | 318 11 | 368 12 | 710 13 | 720 14 | 710 15 | 629 16 | 168 17 | 160 18 | 689 19 | 716 20 | 731 21 | 736 22 | 729 23 | 316 24 | 729 25 | 729 26 | 710 27 | 769 28 | 290 29 | 719 30 | 680 31 | 318 32 | 389 33 | 162 34 | 289 35 | 162 36 | 718 37 | 729 38 | 319 39 | 790 40 | 680 41 | 890 42 | 362 43 | 319 44 | 760 45 | 316 46 | 729 47 | 380 48 | 319 49 | 728 50 | 716 51 | -------------------------------------------------------------------------------- /la/3392.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define NN 302 4 | long long tri[NN]; 5 | int main(void){ 6 | for(int i = 1; i < NN; ++i) tri[i] = (i*(i+1))/2; 7 | for(int i = 2; i < NN; ++i) tri[i] *= (i-1); 8 | int T, N; 9 | scanf("%d",&T); 10 | for(int cnum = 0; cnum < T; ){ 11 | scanf("%d",&N); 12 | long long acc = 0; 13 | for(int i = 1; i <= N; ++i) acc += tri[i+1]; 14 | printf("%d %d %d\n",++cnum,N,acc); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /spoj/regional/NLOGONIA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int k,cx,cy,x,y; 3 | char c[3]; 4 | int main(void){ 5 | while(scanf("%d",&k)){ 6 | if(k==0) break; 7 | scanf("%d %d",&cx,&cy); 8 | for(int i = 0; i < k; i++){ 9 | scanf("%d %d",&x,&y); 10 | if(x == cx || y == cy) printf("divisa\n"); 11 | else{ 12 | c[0] = (y > cy? 'N':'S'); 13 | c[1] = (x > cx? 'E':'O'); 14 | printf("%s\n",c); 15 | } 16 | } 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /uva/Volume III/00386.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00386 - Perfect Cubes 3 | ///////////////////////////////// 4 | #include 5 | unsigned int i,j,k,l; 6 | int main(void){ 7 | for(i = 6; i < 201; i++) 8 | for(j = 2; j < i-2; j++) 9 | for(k = j+1; k < i-1; k++) 10 | for(l = k+1; l < i; l++) 11 | if(j*j*j+k*k*k+l*l*l == i*i*i) 12 | printf("Cube = %u, Triple = (%u,%u,%u)\n",i,j,k,l); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /la/3470.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const char *y = "yes", *n = "no"; 4 | int main(void){ 5 | register int i, j, D, N; 6 | register bool w[100], x; 7 | while(scanf("%d %d\n",&N,&D) && N){ 8 | memset(w,1,N); 9 | for(x = i = 0; i < D; i++) 10 | for(j = 0; j < N; getc(stdin), j++) 11 | w[j] &= (getc(stdin) - '0'); 12 | for(i = 0; i < N; i++) x |= w[i]; 13 | ((x)?(puts(y)):(puts(n))); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /uva/Volume CIX/10931.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10931 - Parity 3 | ///////////////////////////////// 4 | #include 5 | unsigned int n; 6 | char ans[35],i,par; 7 | int main(void){ 8 | ans[34] = 0; 9 | while(scanf("%u",&n) && n){ 10 | i = 33; par = 0; 11 | do { 12 | ans[i--] = (n&1)+'0'; par += n&1; n>>=1; 13 | } while(n); 14 | printf("The parity of %s is %u (mod 2).\n",ans+i+1,par); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /spoj/classical/MFLAR10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char line[2048]; 6 | int main(void){ 7 | while(fgets(line, 2048, stdin) && line[0] != '*'){ 8 | char *p = strtok(line, " "); 9 | char letter = tolower(line[0]); 10 | while(p != NULL){ 11 | if(tolower(*p) != letter) break; 12 | p = strtok(NULL, " "); 13 | } 14 | if(p == NULL) puts("Y"); 15 | else puts("N"); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /spoj/seletiva/SANDUBA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int c, i, j, n; 4 | long long acc, sum, v[100025]; 5 | int main(void){ 6 | scanf("%d",&c); 7 | while(c--){ 8 | sum = 0; 9 | scanf("%d",&n); 10 | for(i = 0; i < n; i++) scanf("%lld",&v[i]), acc += v[i]; 11 | for(i = 0; i < n; i++){ 12 | sum += ((acc%1300031)*(v[i]%1300031))%1300031; 13 | acc -= v[i]; 14 | } 15 | printf("%lld\n",sum%1300031); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume CVII/10783.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10783 - Odd Sum 3 | ///////////////////////////////// 4 | #include 5 | int a,b,c,ncase,sum; 6 | int main(void){ 7 | while(scanf("%d",&c)!=EOF){ 8 | ncase =1; 9 | while(c){ 10 | scanf("%d\n%d",&a,&b); 11 | c--; 12 | if(a%2==0) a++; 13 | for(sum = 0; a <= b; sum+=a,a+=2); 14 | printf("Case %d: %d\n",ncase,sum); 15 | ncase++; 16 | } 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /euler/e076.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int ans = 0; 5 | void r(int s, int m){ 6 | if(s == 100){ 7 | ans++; 8 | return; 9 | } 10 | int lim = 101 - s; 11 | for(int i = m; i < lim; ++i) r(s + i, i); 12 | } 13 | int main(void){ 14 | clock_t ini = clock(); 15 | for(int i = 1; i < 51; ++i) r(i, i); 16 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 17 | printf("Answer: %d\n", ans); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /spoj/seletiva/RECUPERA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int ans, i, n, sum, v[100]; 3 | int main(void){ 4 | for(int inst = 1; scanf("%d",&n)==1; inst++){ 5 | printf("Instancia %d\n",inst); 6 | ans = -1; 7 | for(i = 0; i < n; i++) scanf("%d",&v[i]); 8 | for(sum = i = 0; i < n; i++){ 9 | if(sum == v[i]){ ans = i; break; } 10 | sum += v[i]; 11 | } 12 | if(ans == -1) puts("nao achei\n"); 13 | else printf("%d\n\n",v[ans]); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /codeforces/058B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int reduce(int n){ 4 | if(!(n & 1)) return n >> 1; 5 | if(!(n % 3)) return n / 3; 6 | for(int i = 6; ; i += 6){ 7 | int t1 = i + 1, t2 = i - 1; 8 | if(!(n % t2)) return n / t2; 9 | if(!(n % t1)) return n / t1; 10 | if(t1 * t1 > n) return 1; 11 | } 12 | } 13 | int main(void){ 14 | int n; scanf("%d", &n); 15 | while(n != 1){ 16 | printf("%d ", n); 17 | n = reduce(n); 18 | } 19 | printf("1\n"); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /euler/e008.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int v[1000]; 5 | int main(void){ 6 | clock_t ini = clock(); 7 | int ans = 0; 8 | for(int i = 0; i < 1000; ++i) scanf("%1d", &v[i]); 9 | for(int i = 0; i < 996; ++i){ 10 | int p = v[i] * v[i + 1] * v[i + 2] * v[i + 3] * v[i + 4]; 11 | if(p > ans) ans = p; 12 | } 13 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 14 | printf("Answer: %d\n", ans); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /euler/e009.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | int ans = 0; 7 | for(int n = 1; !ans && n < 499; ++n) 8 | for(int m = n + 1; !ans && m < 499; ++m){ 9 | int a = 2 * m * n, b = m * m - n * n, c = m * m + n * n; 10 | if(a + b + c == 1000) ans = a * b * c; 11 | } 12 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 13 | printf("Answer: %d\n", ans); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /seletiva/IME-USP2008-2/sanduba.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int c, i, j, n; 4 | long long acc, sum, v[100025]; 5 | int main(void){ 6 | scanf("%d",&c); 7 | while(c--){ 8 | sum = 0; 9 | scanf("%d",&n); 10 | for(i = 0; i < n; i++) scanf("%lld",&v[i]), acc += v[i]; 11 | for(i = 0; i < n; i++){ 12 | sum += ((acc%1300031)*(v[i]%1300031))%1300031; 13 | acc -= v[i]; 14 | } 15 | printf("%lld\n",sum%1300031); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume CXV/11526.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int t; scanf("%d", &t); 6 | for(int n; t-- && scanf("%d", &n) == 1; ){ 7 | long long s = 0; 8 | if(n > 0){ 9 | int L = floor(sqrt(n)); 10 | for(int i = 1, q, pq = n; i <= L; ++i){ 11 | q = n / i; 12 | s += q + (pq - q) * (i - 1); 13 | pq = q; 14 | } 15 | if(n / L > L) s += L; 16 | } 17 | printf("%lld\n", s); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /euler/e048.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | long long ans = 0; 8 | for(int i = 1; i < 1001; ++i){ 9 | long long c = i, r = i; 10 | for(int j = 1; j < i; ++j) r = (r * c) % 10000000000LL; 11 | ans = (ans + r) % 10000000000LL; 12 | } 13 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 14 | printf("Answer: %lld\n", ans); 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /la/3910.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define IN getc( stdin ) 5 | 6 | int main(void){ 7 | int k, N, T; 8 | register char c; 9 | scanf("%d", &T); 10 | for(int cnum = 0; cnum < T; ){ 11 | scanf("%d ", &N); 12 | printf("%d ", ++cnum); 13 | for(c = IN; !isupper(c); c = IN); 14 | ungetc(c, stdin); 15 | for(k = 0, c = IN; isupper(c); c = IN) 16 | if(++k != N) putc(c, stdout); 17 | putc(10, stdout); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /spoj/obi/ESTAGIO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | struct A { 3 | int id, nota; 4 | }aln[1032]; 5 | int main(void){ 6 | for(int i, max, n, t = 1; scanf("%d",&n) && n; t++){ 7 | for(max = i = 0; i < n; i++){ 8 | scanf("%d%d",&aln[i].id,&aln[i].nota); 9 | if(aln[i].nota > max) max = aln[i].nota; 10 | } 11 | printf("Turma %d\n",t); 12 | for(i = 0; i < n; i++) 13 | if(aln[i].nota == max) printf("%d ",aln[i].id); 14 | printf("\n\n"); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /spoj/obi/METEORO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int c[4], meteor, hazard, teste = 1, x, y; 4 | while(scanf("%d%d%d%d",&c[0],&c[1],&c[2],&c[3]) 5 | && (c[0] || c[1] || c[2] || c[3])){ 6 | hazard = 0; 7 | scanf("%d",&meteor); 8 | while(meteor--){ 9 | scanf("%d%d",&x,&y); 10 | if(x >= c[0] && x <= c[2] 11 | && y <= c[1] && y >= c[3]) 12 | hazard++; 13 | } 14 | printf("Teste %d\n%d\n\n",teste++,hazard); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /sulamericana/2005/pascal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const char *y = "yes", *n = "no"; 4 | int main(void){ 5 | register int i, j, D, N; 6 | register bool w[100], x; 7 | while(scanf("%d %d\n",&N,&D) && N){ 8 | memset(w,1,N); 9 | for(x = i = 0; i < D; i++) 10 | for(j = 0; j < N; getc(stdin), j++) 11 | w[j] &= (getc(stdin) - '0'); 12 | for(i = 0; i < N; i++) x |= w[i]; 13 | ((x)?(puts(y)):(puts(n))); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11854.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char *ans[2] = {"wrong", "right"}; 4 | int sqr(int x){ return x * x; } 5 | int main(void){ 6 | int a, b, c; 7 | bool r; 8 | while(scanf("%d %d %d", &a, &b, &c) == 3 && (a || b || c)){ 9 | if(c > b && c > a) r = sqr(a) + sqr(b) == sqr(c); 10 | if(b > c && b > a) r = sqr(a) + sqr(c) == sqr(b); 11 | if(a > b && a > c) r = sqr(b) + sqr(c) == sqr(a); 12 | puts(ans[r]); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume II/00278.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00278 - Chess 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | float a,b; 7 | char c; 8 | int n; 9 | int main(void){ 10 | scanf("%d\n",&n); 11 | while(n){ 12 | n--; 13 | scanf("%c %f %f\n",&c,&a,&b); 14 | if(c == 'k') printf("%.0f\n",ceil(a*b/2.0)); 15 | else if(c == 'K') printf("%.0f\n",(ceil(a/2.0)*ceil(b/2.0))); 16 | else printf("%.0f\n",(a < b? a:b)); 17 | } 18 | } -------------------------------------------------------------------------------- /euler/e129.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int getk(int n){ 5 | for(int x = 1, k = 1; ; x = (x * 10 + 1) % n, ++k) 6 | if(!x) return k; 7 | } 8 | int main(void){ 9 | clock_t ini = clock(); 10 | int ans = -1; 11 | for(int i = 1000001; ; i += 2) 12 | if((i % 5) && getk(i) > 1000000){ ans = i; break; } 13 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 14 | printf("Answer: %d\n", ans); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /spoj/classical/CPRMT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int c[128][2]; 4 | char s[1024], t[1024]; 5 | int main(void){ 6 | while(scanf("%s %s", s, t) == 2){ 7 | for(int i = 'a'; i <= 'z'; ++i) c[i][0] = c[i][1] = 0; 8 | for(char *p = s; *p; ) ++c[*p++][0]; 9 | for(char *p = t; *p; ) ++c[*p++][1]; 10 | for(int i = 'a'; i <= 'z'; ++i) 11 | for(int &c0 = c[i][0], &c1 = c[i][1]; c0-- && c1--; ) 12 | putc(i, stdout); 13 | puts(""); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /spoj/classical/GNY07B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | char unit[8]; 4 | float v; 5 | int t; scanf("%d", &t); 6 | for(int c = 0; c++ < t; ){ 7 | scanf("%f %s", &v, unit); 8 | if(*unit == 'k') printf("%d %.4lf lb\n", c, v * 2.2046); 9 | else if(*unit == 'g') printf("%d %.4lf l\n", c, v * 3.7854); 10 | else if(*(unit+1) == 'b') printf("%d %.4lf kg\n", c, v * 0.4536); 11 | else printf("%d %.4lf g\n", c, v * 0.2642); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /spoj/classical/LASTDIG.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int t; scanf("%d", &t); 5 | while(t--){ 6 | int a, b, r = 1; scanf("%d %d", &a, &b); a %= 10; 7 | while(b){ 8 | if(b & 1) r = (r * a) % 10; 9 | a = (a * a) % 10; 10 | b >>= 1; 11 | } 12 | printf("%d\n", r); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /spoj/classical/OFFSIDE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | for(int a, d; scanf("%d %d", &a, &d) == 2 && (a || d); ){ 5 | int atk, m, n, o; 6 | atk = m = n = 1 << 15; 7 | for(int i = 0; i < a; ++i){ 8 | scanf("%d", &o); 9 | if(o < atk) atk = o; 10 | } 11 | for(int i = 0; i < d; ++i){ 12 | scanf("%d", &o); 13 | if(o < m) n = m, m = o; 14 | else if(o < n) n = o; 15 | } 16 | puts(atk < n ? "Y" : "N"); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /spoj/regional/VAMPIROS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | double ev1,ev2,at,d,p,res; 4 | int main(void){ 5 | double p; 6 | scanf("%d",&ev1); 7 | while(ev1){ 8 | scanf("%lf %lf %lf", &ev2,&at,&d); 9 | ev1 = ceil(ev1/d); 10 | ev2 = ceil(ev2/d); 11 | d = 1; 12 | p = at/6; 13 | if(at == 3) res = ev1/(ev1+ev2); 14 | else res = (1 - pow((1-p)/p,ev1)) / (1 - pow((1-p)/p,ev1+ev2)); 15 | printf("%.1lf\n",res*100); 16 | scanf("%lf",&ev1); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume CIX/10970.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10970 - Big Chocolate 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | int main(void){ 7 | register unsigned short int a,b,i; 8 | register char line[100]; 9 | while(gets(line)){ 10 | a = b = i = 0; 11 | for(i = 0; !isspace(line[i]); i++) a *= 10, a += line[i]-'0'; 12 | while(line[++i]) b *= 10, b+=line[i]-'0'; 13 | printf("%u\n",a*b-1); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11830.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char dig[1024]; 4 | 5 | int main(void){ 6 | int d; 7 | char c; 8 | while(scanf("%d", &d) && d){ 9 | scanf("%s", dig); 10 | int shown = 0; 11 | for(int i = 0; dig[i]; ++i){ 12 | if(dig[i] - '0' == d) continue; 13 | if(dig[i] != '0') putc(dig[i], stdout), shown = 1; 14 | else if(shown) putc('0', stdout); 15 | } 16 | if(!shown) puts("0"); 17 | else putc(10, stdout); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume I/00128.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00128 - Software CRC 3 | ///////////////////////////////// 4 | #include 5 | #define g 34943 6 | char line[1050]; 7 | unsigned int crc,i,val,show; 8 | int main(void){ 9 | while(gets(line)){ 10 | if(line[0] == '#') return 0; 11 | val = i = 0; 12 | while(line[i]) val = ((val<<8) + line[i++])%g; 13 | val = (val<<16)%g; 14 | crc = (val ? g-val: 0); 15 | printf("%02X %02X\n", crc>>8, crc&255); 16 | } 17 | } -------------------------------------------------------------------------------- /codeforces/029A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int x[128], d[128]; 4 | 5 | int main(void){ 6 | int n; 7 | scanf("%d", &n); 8 | for(int i = 0; i < n; ++i){ 9 | scanf("%d %d", &x[i], &d[i]); 10 | } 11 | bool y = 0; 12 | for(int i = 0; i < n; ++i){ 13 | for(int j = i + 1; j < n; ++j){ 14 | int s1 = x[i] + d[i]; 15 | int s2 = x[j] + d[j]; 16 | y |= x[i] == s2 && x[j] == s1; 17 | } 18 | } 19 | if(y) puts("YES"); 20 | else puts("NO"); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /euler/e003.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | long long num = 600851475143LL; 7 | int ans = 0; 8 | for(int div = 3; ; div += 2) 9 | if(!(num % div)){ 10 | do { num /= div; } while (!(num % div)); 11 | if(num == 1){ 12 | ans = div; 13 | break; 14 | } 15 | } 16 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 17 | printf("Answer: %d\n", ans); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /spoj/classical/BISHOPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char n[128]; 4 | int main(void){ 5 | while(scanf("%s", n + 1) == 1){ 6 | int c = 0, i, len = 0; while(n[++len]); 7 | for(i = len; n[--i] == '0'; n[i] = '9'); 8 | if(len == 2 && n[1] == '1'){ puts("1"); continue; } 9 | --n[i]; 10 | for(i = len; --i; ) n[i] = '0' + (c = c + (n[i] - '0') * 2) % 10, c /= 10; 11 | if(c){ 12 | n[0] = c + '0'; 13 | puts(n); 14 | } else puts(n+1); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /spoj/classical/POLEVAL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int c[1024]; 4 | int main(void){ 5 | for(int cn = 1, k, n, x; scanf("%d", &n) && n != -1; ++cn){ 6 | for(int i = n+1; i--; ) scanf("%d", c + i); 7 | printf("Case %d:\n", cn); 8 | scanf("%d", &k); 9 | for(int i = 0; i < k; ++i){ 10 | scanf("%d", &x); 11 | long long r = c[0], y = x; 12 | for(int j = 0; j++ < n; y *= x) r += y * c[j]; 13 | printf("%lld\n", r); 14 | } 15 | } 16 | return 0; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /uva/Volume C/10025.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int k, n, t, v; 6 | for(scanf("%d", &t); t-- && scanf("%d", &k) == 1; ){ 7 | if(k == 0) puts("3"); 8 | else { 9 | if(k < 0) k = -k; 10 | v = ceil(sqrt(1 + k * 8LL)); 11 | v += !(v & 1); 12 | n = (-1 + v) >> 1; 13 | if((((n + 1) >> 1) & 1) != (k & 1)) n = n + 1 + (n & 1); 14 | printf("%d\n", n); 15 | } 16 | if(t) putc(10, stdout); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CXI/11192.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11192 - Group Reverse 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #include 7 | char s[105]; 8 | unsigned int i, part, pnum; 9 | int main(void){ 10 | while(scanf("%u",&pnum) && pnum){ 11 | scanf("%s",&s); 12 | part = strlen(s)/pnum; 13 | for(i = 0; i < pnum; i++) 14 | std::reverse(s+i*part,s+(i+1)*part); 15 | puts(s); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /la/3468.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int a, az, i, r, z, N; 5 | register char c; 6 | while(scanf("%d\n",&N) && N){ 7 | for(a = i = r = z = 0, az = 1; i < N; i++){ 8 | c = getc( stdin ); 9 | if(c == '0') z++; 10 | else { 11 | if(i - z) r += (z>>1); 12 | else a += z; 13 | az = z = 0; 14 | } 15 | getc( stdin ); 16 | } 17 | if(!az) r += ((a+z)>>1); 18 | else r = (z+1)>>1; 19 | printf("%d\n",r); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /spoj/classical/FASHION.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int x[1024], y[1024]; 5 | int main(void){ 6 | int n, t, v; scanf("%d", &t); 7 | while(t--){ 8 | scanf("%d", &n); 9 | v = 0; 10 | for(int i = 0; i < n; ++i) scanf("%d", x + i); 11 | for(int i = 0; i < n; ++i) scanf("%d", y + i); 12 | sort(x, x + n); 13 | sort(y, y + n); 14 | for(int i = 0; i < n; ++i) v += x[i] * y[i]; 15 | printf("%d\n", v); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume CIX/10929.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10929 - You can say 11 3 | ///////////////////////////////// 4 | #include 5 | char num[1005]; 6 | unsigned short mod11,i; 7 | int main(void){ 8 | while(scanf("%s\n",&num)!=EOF){ 9 | if(num[0] == '0' && !num[1]) return 0; 10 | for(mod11 = i = 0; num[i]; i++) mod11 = (mod11*10+num[i]-'0')%11; 11 | if(mod11) printf("%s is not a multiple of 11.\n"); 12 | else printf("%s is a multiple of 11.\n"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uva/Volume CXIV/11401.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11401 - Triangle Counting 3 | ///////////////////////////////// 4 | #include 5 | long long int i, triangles[1000001]; 6 | int main(void){ 7 | triangles[3] = 0; 8 | for(i = 4; i < 1000001; i++) 9 | if(i&1) triangles[i] = (i-1)*(i-3)/4 + triangles[i-1]; 10 | else triangles[i] = (i-2)*(i-2)/4 + triangles[i-1]; 11 | while(scanf("%lld",&i) && i > 2) 12 | printf("%lld\n",triangles[i]); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /uva/Volume CXIV/11494.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11494 - Queen 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | int xi,yi,xf,yf; 7 | bool a,b; 8 | int main(void){ 9 | while(scanf("%d %d %d %d\n",&xi,&yi,&xf,&yf) && xi){ 10 | a = xi == xf; 11 | b = yi == yf; 12 | if(a && b) putchar('0'); 13 | else if(a || b || (abs(xf-xi) == abs(yf-yi))) putchar('1'); 14 | else putchar('2'); 15 | putchar('\n'); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume CXV/11567.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11567 - Moliu Number Generator 3 | ///////////////////////////////// 4 | #include 5 | unsigned int n; 6 | unsigned int calc(unsigned int k){ 7 | if(k<4) return k; 8 | if(k&1){ 9 | if(!((k+1)%4)) return 3+calc((k+1)>>2); 10 | else return 3+calc((k-1)>>2); 11 | } else return 1+calc(k>>1); 12 | } 13 | int main(void){ 14 | while(scanf("%u",&n)!=EOF){ 15 | printf("%u\n",calc(n)); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11850.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | const char *ans[2] = {"IMPOSSIBLE", "POSSIBLE"}; 5 | int v[2048]; 6 | int main(void){ 7 | bool ok; 8 | for(int n; scanf("%d", &n) == 1 && n; ){ 9 | for(int i = 0; i < n; ++i) scanf("%d", &v[i]); 10 | sort(v, v + n); 11 | ok = n > 6 && v[n - 1] >= 1322; 12 | for(int i = 1; ok && i < n; ++i) ok &= (v[i] - v[i - 1]) <= 200; 13 | puts(ans[ok]); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /uva/Volume II/00256.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00256 - Quirksome Squares 3 | ///////////////////////////////// 4 | #include 5 | char *s[] = {"00\n01\n81\n","0000\n0001\n2025\n3025\n9801\n", 6 | "000000\n000001\n088209\n494209\n998001\n","00000000\n00000001\n04941729\n07441984\n24502500\n25502500\n52881984\n60481729\n99980001\n"}; 7 | int main(void) { 8 | int n; 9 | while(scanf("%d", &n) != EOF) { 10 | printf("%s",s[n/2-1]); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /codeforces/058C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int c[100048]; 3 | int min(int a, int b){ return a < b ? a : b; } 4 | int max(int a, int b){ return a > b ? a : b; } 5 | int main(void){ 6 | int k = 0, d, n, z; 7 | while(scanf("%d", &n) == 1){ 8 | k = 0; 9 | for(int i = 0; i < 100048; ++i) c[i] = 0; 10 | for(int i = 0; i < n; ++i){ 11 | scanf("%d", &z); 12 | if((d = z - min(i, n - i - 1)) > 0) k = max(k, ++c[d]); 13 | } 14 | printf("%d\n", n - k); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /euler/e031.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int c[8] = { 1, 2, 5, 10, 20, 50, 100, 200 }; 6 | int ways[201] = {0}; 7 | int main(void){ 8 | clock_t ini = clock(); 9 | ways[0] = 1; 10 | for(int i = 0; i < 8; ++i) 11 | for(int j = c[i]; j < 201; ++j) 12 | ways[j] += ways[j - c[i]]; 13 | int ans = ways[200]; 14 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 15 | printf("Answer: %d\n", ans); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /euler/e138.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | long long sqr(long long x){ return ((long long)x) * x; } 6 | int main(void){ 7 | clock_t ini = clock(); 8 | long long ans = 0, a = 0, b = 1; 9 | for(int c = 0, g = 2; c < 12; ++g){ 10 | b ^= a ^= b ^= a += b; 11 | if(g > 3 && g % 6 == 3) ans += b >> 1, ++c; 12 | } 13 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 14 | printf("Answer: %lld\n", ans); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /spoj/obi/PARPROX.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int n; 5 | long long p[1000][2], dmin = 1LL<<50; 6 | scanf("%d",&n); 7 | for(int i = 0; i < n; i++) scanf("%lld%lld",&p[i][0],&p[i][1]); 8 | for(int i = 0; i < n; i++) 9 | for(int j = i+1; j < n; j++){ 10 | long long a, b, c; 11 | a = p[j][0] - p[i][0]; 12 | b = p[j][1] - p[i][1]; 13 | if((c = (a*a)+(b*b)) < dmin) dmin = c; 14 | } 15 | printf("%.3lf\n",sqrt(((double)dmin))); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tc/SRM363/HandsShaking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | long long catalan(int x){ 11 | long long r = 1; 12 | for(int i = x << 1, j = 1; i > x; --i, ++j) r = (r * i) / j; 13 | return r / (x + 1); 14 | } 15 | 16 | class HandsShaking { 17 | public: 18 | long long countPerfect( int n ) { 19 | return catalan(n >> 1); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /uva/Volume CIII/10300.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10300 - Ecological Premium 3 | ///////////////////////////////// 4 | #include 5 | int n,f; 6 | long long a,b,c; 7 | long long sum; 8 | int main(void){ 9 | scanf("%d",&n); 10 | while(n){ 11 | n--; 12 | scanf("%d",&f); 13 | sum = 0; 14 | while(f){ 15 | f--; 16 | scanf("%d %d %d",&a,&b,&c); 17 | if(b==0) continue; 18 | sum += a*c; 19 | } 20 | printf("%lld\n",sum); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /uva/Volume III/00374.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00374 - Big Mod 3 | ///////////////////////////////// 4 | #include 5 | typedef unsigned long long int ULL; 6 | ULL b,m,p,r; 7 | ULL pwab5(ULL b,ULL p, ULL m) { 8 | r=1; 9 | while(p) 10 | { 11 | if(p%2) r=(r*b)%m; 12 | b=(b*b)%m; 13 | p/=2; 14 | } 15 | return r; 16 | } 17 | int main(void){ 18 | while(scanf("%llu\n%llu\n%llu\n",&b,&p,&m)!=EOF){ 19 | printf("%llu\n",pwab5(b,p,m)); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /euler/e097.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | long long ans = 1; 8 | int power = 7830457; 9 | while(power > 26){ 10 | ans = (ans << 27) % 10000000000LL; 11 | power -= 27; 12 | } 13 | ans = (ans << power) % 10000000000LL; 14 | ans = (28433 * ans + 1) % 10000000000LL; 15 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 16 | printf("Answer: %lld\n", ans); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /spoj/classical/TRT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int N = 2048; 3 | int n, m[N][N], v[N]; 4 | int max(int a, int b){ return a > b ? a : b; } 5 | int main(void){ 6 | while(scanf("%d", &n) == 1){ 7 | for(int i = 0; i < n; ++i) scanf("%d", v + i), m[i][i] = v[i] * n; 8 | for(int a = n - 1, L = 1; L < n; --a, ++L) 9 | for(int s = 0, e = s + L; e < n; ++s, ++e) 10 | m[s][e] = max(v[s]*a + m[s+1][e], v[e]*a + m[s][e-1]); 11 | printf("%d\n", m[0][n-1]); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /timus/T1014.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int c[10]; 4 | int main(void){ 5 | for(int n; scanf("%d", &n) == 1; ){ 6 | if(n == 0){ puts("10"); continue; } 7 | for(int i = 0; i < 10; ++i) c[i] = 0; 8 | c[1] = (n == 1); 9 | for(int i = 10; n > 1 && --i > 1; ) 10 | while(!(n % i)) ++c[i], n /= i; 11 | if(n > 1) puts("-1"); 12 | else { 13 | for(int i = 0; i < 10; ++i) 14 | while(c[i] > 0) putc(i + '0', stdout), c[i]--; 15 | puts(""); 16 | } 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CIV/10440.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int v[4096]; 5 | int main(void){ 6 | int act, c, m, n, t, tp; 7 | for(scanf("%d", &c); c-- && scanf("%d %d %d", &n, &t, &m) == 3; ){ 8 | t <<= 1; 9 | act = tp = 0; 10 | for(int i = 0; i < m; ++i) scanf("%d", &v[i]); 11 | for(int i = (m + n - 1) % n; i < m; i += n) act = max(tp, v[i]), tp = act + t; 12 | printf("%d %d\n", tp - (t >> 1), (m + n - 1)/n); 13 | } 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /uva/Volume CVI/10684.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10684 - The jackpot 3 | ///////////////////////////////// 4 | #include 5 | int curr,k,max,n; 6 | int main(void){ 7 | while(scanf("%u",&n) && n){ 8 | curr = max = 0; 9 | while(n--){ 10 | scanf("%d",&k); 11 | curr += k; 12 | if(curr > max) max = curr; 13 | if(curr < 0) curr = 0; 14 | } 15 | if(max) printf("The maximum winning streak is %u.\n",max); 16 | else printf("Losing streak.\n"); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /uva/Volume CVIII/10878.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10878 - Decode the tape 3 | ///////////////////////////////// 4 | #include 5 | char i,line[12],val; 6 | char w[] = {64,32,16,8,4,2,1}; 7 | int main(void){ 8 | gets(line); 9 | while(gets(line) && line[2] != '_'){ 10 | val = 0; 11 | for(i = 2; i < 6; i++) 12 | if(line[i] == 'o') val += w[i-2]; 13 | for(i = 7; i < 10; i++) 14 | if(line[i] == 'o') val += w[i-3]; 15 | putchar(val); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /euler/e099.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | clock_t ini = clock(); 7 | int ans = -1; double maxv = -1; 8 | freopen("base_exp.txt", "r", stdin); 9 | for(int b, e, line = 1; scanf("%d,%d", &b, &e) == 2; ++line){ 10 | if(e * log(b) > maxv){ 11 | maxv = e * log(b); 12 | ans = line; 13 | } 14 | } 15 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 16 | printf("Answer: %d\n", ans); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /spoj/classical/LAGRANGE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int LIM = 32769; 5 | int lim[LIM], w[LIM]; 6 | void bt(int m, int s, int d){ 7 | if(s >= LIM) return; 8 | ++w[s]; 9 | if(d == 4) return; 10 | for(int i = m; i < lim[s]; ++i) bt(i, s + i*i, d + 1); 11 | } 12 | int main(void){ 13 | for(int i = 0; i < LIM; ++i) w[i] = 0, lim[i] = floor(sqrt(LIM - i) + 1); 14 | bt(1, 0, 0); 15 | for(int n; scanf("%d", &n) == 1 && n; printf("%d\n", w[n])); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /spoj/classical/MRECAMAN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int a[500001]; 5 | set < int > s; 6 | int main(void){ 7 | for(int i = 1, t; i < 500001; ++i){ 8 | t = a[i] = a[i - 1] - i; 9 | a[i] += (t <= 0 || s.find(t) != s.end()) ? i << 1 : 0; 10 | s.insert(a[i]); 11 | } 12 | for(int n; scanf("%d", &n) == 1 && n != -1; ) 13 | printf("%d\n", a[n]); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /spoj/regional/BALDES.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int i,n, winner,aux; 3 | int v[100001]; 4 | int main(void){ 5 | scanf("%d",&n); 6 | while(n){ 7 | for(i = 1; i <= n; scanf("%d",&v[i]),i++); 8 | winner = 1; 9 | for(i = 1; i <= n;) 10 | if(v[i] != i){ 11 | aux = v[i]; 12 | v[i] = v[v[i]]; 13 | v[aux] = aux; 14 | winner = !winner; 15 | } 16 | else i++; 17 | if(winner) printf("Carlos\n"); 18 | else printf("Marcelo\n"); 19 | scanf("%d",&n); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /timus/T1009.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int dp[20][10]; 4 | int main(void){ 5 | for(int base, n; scanf("%d %d", &n, &base) == 2; ){ 6 | for(int i = 0; i < base; ++i) dp[0][i] = i>0; 7 | for(int i = 1; i < n; ++i) 8 | for(int j = 0; j < base; ++j){ 9 | dp[i][j] = 0; 10 | for(int k = j == 0; k < base; ++k) 11 | dp[i][j] += dp[i-1][k]; 12 | } 13 | int ans = 0; 14 | for(int i = 0; i < base; ++i) ans += dp[n-1][i]; 15 | printf("%d\n", ans); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /uva/Volume IV/00494.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00494 - Kindergarten Counting Game 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | char line[100000],first,last; 7 | int count,i; 8 | int main(void){ 9 | while(gets(line)){ 10 | for(count=i=0;line[i]!='\0';){ 11 | while(!isalpha(line[i]) && line[i]!='\0')i++; 12 | if(line[i]!='\0') count++; 13 | while(isalpha(line[i]))i++; 14 | } 15 | printf("%d\n",count); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume V/00594.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00594 - One Little, Two Little, Three Little Endians 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | int a, i, n; 7 | bool bin[33]; 8 | int main(void){ 9 | bin[33] = 0; 10 | while(scanf("%d",&n)!=EOF){ 11 | a = n; i = 0; 12 | a = 0; 13 | for (i=0; i<32; i++) 14 | if (n&(1<>3))<<3)+(i%8); 15 | printf("%d converts to %d\n",n,a); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /la/4481.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define IN getc( stdin ) 4 | 5 | char map[128]; 6 | int main(void){ 7 | int n, o; 8 | map['E'] = 8; map['H'] = 32; map['Q'] = 16; 9 | map['S'] = 4; map['T'] = 2; map['W'] = 64; 10 | map['X'] = 1; 11 | register char c; 12 | while(IN != '*'){ 13 | c = IN; 14 | o = 0; 15 | while(c != 10){ 16 | n = 0; 17 | while(c != '/') n += *(map + c), c = IN; 18 | o += (n == 64); 19 | c = IN; 20 | } 21 | printf("%d\n", o); 22 | } 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /spoj/classical/MAYA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int v[7] = {1, 20, 360}; 5 | char line[1024]; 6 | for(int i = 3; i < 7; ++i) v[i] = 20 * v[i - 1]; 7 | for(int n, r, z; scanf("%d\n", &n) == 1 && n; ){ 8 | r = 0; 9 | while(n--){ 10 | fgets(line, 1024, stdin); 11 | z = 0; 12 | for(char *p = line; *p; ++p){ 13 | if(*p == '.') ++z; 14 | else if(*p == '-') z += 5; 15 | } 16 | r += z * v[n]; 17 | } 18 | printf("%d\n", r); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /spoj/regional/BICHO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int i, m, n, v1, v2; 4 | double v; 5 | while(scanf("%lf%d%d",&v,&n,&m) && v){ 6 | int mult = 0; 7 | if(n%10000 == m%10000) mult = 3000; 8 | else if(n%1000 == m%1000) mult = 500; 9 | else if(n%100 == m%100) mult = 50; 10 | else { 11 | v1 = n%100; v2 = m%100; 12 | if(!v1) v1 = 100; 13 | if(!v2) v2 = 100; 14 | if((v1-1)>>2 == (v2-1)>>2) mult = 16; 15 | } 16 | printf("%.2lf\n",v*mult); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /sulamericana/2005/tornado.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int a, az, i, r, z, N; 5 | register char c; 6 | while(scanf("%d\n",&N) && N){ 7 | for(a = i = r = z = 0, az = 1; i < N; i++){ 8 | c = getc( stdin ); 9 | if(c == '0') z++; 10 | else { 11 | if(i - z) r += (z>>1); 12 | else a += z; 13 | az = z = 0; 14 | } 15 | getc( stdin ); 16 | } 17 | if(!az) r += ((a+z)>>1); 18 | else r = (z+1)>>1; 19 | printf("%d\n",r); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /timus/T1005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int v[32]; 3 | int min(int a, int b){ return a < b ? a : b; } 4 | int abs(int a){ return a < 0 ? -a : a; } 5 | int main(void){ 6 | for(int n; scanf("%d", &n) == 1; ){ 7 | int i, j, md = 1<<29, s, t = 0; 8 | for(i = 0; i < n; ++i) scanf("%d", v + i), t += v[i]; 9 | for(i = 0; i < (1< 2 | 3 | #define LIM 46341 4 | 5 | unsigned int sq[LIM]; 6 | int main(void){ 7 | int a, b, c, i, T; 8 | for(i = 0; i < LIM; i++) sq[i] = i*i; 9 | scanf("%d",&T); 10 | while(T--){ 11 | scanf("%d %d",&a,&b); 12 | a = (b-a); 13 | if(a == 0){ puts("0"); continue; } 14 | for(b = 1, i = LIM; b < i; ){ 15 | c = (b+i)>>1; 16 | if(sq[c] > a) i = c; else b = c+1; 17 | } 18 | printf("%d\n",(a-sq[--i]+i-1)/i + (i<<1) - 1); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /codeforces/023A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char s[128]; 4 | bool eq(int a, int b, int x, int y){ 5 | for(int i = 0; a + i < b; ++i) 6 | if(s[a + i] != s[x + i]) return false; 7 | return true; 8 | } 9 | int main(void){ 10 | scanf("%s", s); 11 | int i, j, k, l, m = 0; 12 | for(l = 0; s[l]; ++l); 13 | for(i = l; !m && i > 0; --i) 14 | for(j = 0; !m && j + i <= l; ++j) 15 | for(k = j+1; !m && k + i <= l; ++k) 16 | if(eq(j, j + i, k, k + i)) m = i; 17 | printf("%d\n", m); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /spoj/classical/PERMUT1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int m[16][128]; 4 | int main(void){ 5 | int i, j, sum; 6 | for(i = 0; i < 13; ++i) 7 | for(j = 0; j < 100; ++j) 8 | m[i][j] = 0; 9 | m[1][0] = 1; 10 | for(i = 2; i < 13; ++i) 11 | for(j = sum = 0; j < 100; ++j){ 12 | sum += m[i - 1][j]; 13 | if(j >= i) sum -= m[i - 1][j - i]; 14 | m[i][j] = sum; 15 | } 16 | for(scanf("%d", &sum); sum--; ){ 17 | scanf("%d %d", &i, &j); 18 | printf("%d\n", m[i][j]); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /spoj/obi/AERO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define _m_(a,b) ((a>b)?(a):(b)) 3 | int a, c[101], i, max, v; 4 | int main(void){ 5 | for(int t = 1; scanf("%d%d",&a,&v) && v; t++){ 6 | for(i = 0; i < a; i++) c[i] = 0; 7 | for(max = i = 0; i < v; i++){ 8 | int _, __; 9 | scanf("%d %d",&_,&__), ++c[--_], ++c[--__]; 10 | max = _m_(max,_m_(c[_],c[__])); 11 | } 12 | printf("Teste %d\n",t); 13 | for(i = 0; i < a; i++) if(max == c[i]) printf("%d ",i+1); 14 | puts("\n"); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /uva/Volume CIV/10450.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10450 - World Cup Noise 3 | ///////////////////////////////// 4 | #include 5 | long long int fib[52]; 6 | unsigned char i; 7 | unsigned int cnum,tnum; 8 | int main(void){ 9 | fib[0] = fib[1] = 1; 10 | for(i = 2; i != 52; i++) 11 | fib[i] = fib[i-1]+fib[i-2]; 12 | scanf("%u",&cnum); 13 | while(cnum--){ 14 | tnum++; 15 | scanf("%u\n",&i); 16 | printf("Scenario #%u:\n%llu\n\n",tnum,fib[i+1]); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CXIII/11364.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11364 - Parking 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | unsigned int cases,i,store_pos[20],stores; 7 | int main(void){ 8 | scanf("%d",&cases); 9 | while(cases--){ 10 | scanf("%u\n",&stores); 11 | for(i = 0; i < stores; i++) scanf("%u ",&store_pos[i]); 12 | std::sort(store_pos,store_pos+stores); 13 | printf("%u\n",(store_pos[stores-1]-store_pos[0])*2); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11679.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define NN 32 4 | 5 | int b, n; 6 | int v[NN]; 7 | 8 | int main(void){ 9 | int i; 10 | while(scanf("%d %d", &b, &n) && (b || n)){ 11 | for(i = 0; i < b; i++) scanf("%d",&v[i]); 12 | for(i = 0; i < n; i++) { 13 | int d, c, val; 14 | scanf("%d %d %d", &d, &c, &val), d--, c--; 15 | v[d] -= val; 16 | v[c] += val; 17 | } 18 | for(i = 0; i < b; i++) if(v[i] < 0) break; 19 | printf("%c\n", i == b ? 'S': 'N'); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /euler/e073.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const int L = 12001; 6 | int gcd(int n,int m){ return m ? gcd(m, n % m) : n;} 7 | int main(void){ 8 | clock_t ini = clock(); 9 | int ans = 0; 10 | for(int d = 1; d < L; ++d){ 11 | int l = d / 3 + 1, r = ((d + 1) >> 1); 12 | for(int n = l; n < r; ++n) 13 | ans += gcd(n, d) == 1; 14 | } 15 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 16 | printf("Answer: %d\n", ans); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /spoj/obi/ELEICOES.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | map < int , int > m; 5 | struct CAND{ 6 | int s, id; 7 | } cand[100000]; 8 | int main(void){ 9 | int c, i, idx, max, n, v; 10 | scanf("%d",&n); 11 | c = idx = max = 0; 12 | m.clear(); 13 | while(n--){ 14 | scanf("%d",&v); 15 | if(m.find(v) == m.end()) m[v] = c++, cand[m[v]].s = 0; 16 | i = m[v]; 17 | cand[i].id = v; 18 | if(++cand[i].s > max) max = cand[i].s, idx = i; 19 | } 20 | printf("%d\n",cand[idx].id); 21 | } 22 | -------------------------------------------------------------------------------- /uva/Volume III/00369.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00369 - Combinations 3 | ///////////////////////////////// 4 | #include 5 | int a,b,i; 6 | double res; 7 | void doComb(){ 8 | res = 1; 9 | for(i = a; i > b; i--){ 10 | res*=i; 11 | res/=(i-b); 12 | } 13 | } 14 | int main(void){ 15 | scanf("%d",&a); 16 | while(a){ 17 | scanf("%d",&b); 18 | doComb(); 19 | printf("%d things taken %d at a time is %.0lf exactly.\n",a,b,res); 20 | scanf("%d",&a); 21 | } 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /uva/Volume IV/00490.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00490 - Rotating Sentences 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | char sent[105][105], i,j,k,maxlen; 7 | int main(void){ 8 | for(maxlen = i = 0;gets(sent[i]);i++){ 9 | k = strlen(sent[i]); 10 | if(k > maxlen) maxlen = k; 11 | } 12 | for(j = 0; j != maxlen; putchar('\n'),j++) 13 | for(k = i-1; k >= 0; k--) 14 | if(sent[k][j]) putchar(sent[k][j]); 15 | else putchar(' '); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /uva/Volume VI/00674.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00674 - Coin Change 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | unsigned short i,j; 7 | unsigned short coin[] = {1,5,10,25,50}; 8 | unsigned int poss[7490],n; 9 | int main(void){ 10 | memset(poss,0,sizeof(poss)); 11 | poss[0] = 1; 12 | for(i = 0; i < 5; i++) 13 | for(j = coin[i]; j < 7490; j++) 14 | poss[j] += poss[j-coin[i]]; 15 | while(scanf("%u",&n)==1) 16 | printf("%u\n",poss[n]); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /uva/Volume VII/00756.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00756 - Biorhythms 3 | ///////////////////////////////// 4 | #include 5 | int main(void){ 6 | int a,b,c,d,sum,m,w,i=1; 7 | while(scanf("%d%d%d%d",&a,&b,&c,&d)==4 && a+b+c+d!=-4){ 8 | sum=0; 9 | a=a%23; b=b%28; c=c%33; 10 | sum=a*6*924+b*-9*759+c*2*644; 11 | while(sum<0) sum+= 21252; 12 | sum%=21252; 13 | if(sum <= d) sum+= 21252; 14 | printf("Case %d: the next triple peak occurs in %d days.\n",i++,sum-d); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /spoj/classical/ROADNET.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int m[200][200]; 3 | int main(void){ 4 | int i, j, k, n, p, t; 5 | for(scanf("%d", &t); t--; ){ 6 | scanf("%d", &n); 7 | for(i = 0; i < n; ++i) 8 | for(j = 0; j < n; ++j) 9 | scanf("%d", &m[i][j]); 10 | for(i = 0; i < n; ++i) 11 | for(j = i + 1; j < n; ++j){ 12 | for(k = 0, p = 1; p && k < n; ++k) 13 | p &= k == i || k == j || (m[i][k] + m[k][j] > m[i][j]); 14 | if(p) printf("%d %d\n", i+1, j+1); 15 | } 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /spoj/classical/SAMER08G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int p[1024]; 4 | int main(void){ 5 | int dpos, i, n, pos, who; 6 | while(scanf("%d",&n) && n){ 7 | bool valid = true; 8 | memset(p,0,n<<2); 9 | for(i = 0; i < n; i++){ 10 | scanf("%d %d", &who, &dpos); 11 | pos = i + dpos; 12 | if(valid &= !(pos < 0 || pos >= n || p[pos])) p[pos] = who; 13 | } 14 | if(valid) for(i = 0; i < n; i++) printf("%d%c", p[i], i == n - 1 ? 10 : 32); 15 | else puts("-1"); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /spoj/classical/STAMPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void){ 4 | int c, f, i, L[1024], need, s, t; 5 | for(c = 0, scanf("%d", &t); c++ < t; ){ 6 | s = 0; 7 | scanf("%d %d", &need, &f); 8 | for(i = 0; i < f; ++i) scanf("%d", L + i), s += L[i]; 9 | std::sort(L, L + f); 10 | for(i = 0; s - L[i] >= need; s -= L[i++]); 11 | printf("Scenario #%d:\n", c, f - i); 12 | if(s >= need) printf("%d\n", f - i); 13 | else puts("impossible"); 14 | puts(""); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /spoj/obi/MINHOCA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define _m_(a,b) ((a>b)?(a):(b)) 3 | int C[100], L[100]; 4 | int main(void){ 5 | int i, j, k, m, n, max = 0; 6 | scanf("%d%d",&n,&m); 7 | for(i = 0; i < n || i < m; i++) L[i] = C[i] = 0; 8 | for(i = 0; i < n; i++){ 9 | for(j = 0; j < m; j++){ 10 | scanf("%d",&k); 11 | L[i] += k; 12 | C[j] += k; 13 | } 14 | max = _m_(max,L[i]); 15 | if(i == n-1) 16 | for(j = 0; j < m; j++) 17 | max = _m_(max,C[j]); 18 | } 19 | printf("%d\n",max); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /uva/Volume C/10098.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10098 - Generating Fast 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | unsigned int cnum; 9 | char word[15],len; 10 | int main(void){ 11 | scanf("%u\n",&cnum); 12 | while(cnum--){ 13 | gets(word); 14 | len = strlen(word); 15 | sort(word,word+len); 16 | do { puts(word); } while(next_permutation(word,word+len)); 17 | putchar('\n'); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /uva/Volume CI/10195.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10195 - The Knights Of The Round Table 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | long double a,b,c,r,s; 7 | int main(void){ 8 | while(scanf("%Lf %Lf %Lf",&a,&b,&c)!=EOF){ 9 | if(a||b||c) 10 | s = (a+b+c)/2, 11 | r = sqrt((s-a)*(s-b)*(s-c)/s), 12 | printf("The radius of the round table is: %.3Lf\n",r); 13 | else 14 | printf("The radius of the round table is: 0.000\n"); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /uva/Volume CIII/10323.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10323 - Factorial! You Must be Kidding!!! 3 | ///////////////////////////////// 4 | #include 5 | int n; 6 | const char *under = "Underflow!", *over = "Overflow!"; 7 | const char *fact[] = {"40320","362880","3628800","39916800","479001600","6227020800"}; 8 | int main(void){ 9 | while(scanf("%d\n",&n)!=EOF){ 10 | if(n > 13 || (n < 0 && n&1)) puts(over); 11 | else if(n < 8) puts(under); 12 | else puts(fact[n-8]); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /uva/Volume CXI/11137.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11137 - Ingenuous Cubrency 3 | ///////////////////////////////// 4 | #include 5 | long long ways[10000]; 6 | unsigned int i,j; 7 | int coins[] = {1,8,27,64,125,216,343,512,729,1000, 8 | 1331,1728,2197,2744,3375,4096,4913,5832,6859,8000,9261}; 9 | int main(void){ 10 | ways[0] = 1; 11 | for(i = 0; i < 21; i++) 12 | for(j = coins[i]; j < 10000; j++) 13 | ways[j] += ways[j-coins[i]]; 14 | while(scanf("%u",&i)!=EOF) printf("%lld\n",ways[i]); 15 | } 16 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11717.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int i, k, n, t, x; 5 | int ign, ina, act; 6 | scanf("%d", &t); 7 | for(int cnum = 0; cnum < t; ){ 8 | ign = ina = act = 0; 9 | scanf("%d %d %d", &n, &i, &k); 10 | for(int c = 0; c < n; ++c) { 11 | scanf("%d", &x); 12 | if(x < act) ign++; 13 | else if(x >= act + i){ 14 | ina++; 15 | act = x + k; 16 | } 17 | if(x > act) act = x; 18 | } 19 | printf("Case %d: %d %d\n", ++cnum, ina, ign); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11786.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define IN getc( stdin ) 3 | const int N = 20050, M = 10025; 4 | int d[N]; 5 | int main(void){ 6 | int ans, h, i, t; scanf("%d\n", &t); 7 | register char c; 8 | while(t--){ 9 | for(i = 0; i < N; ++i) d[i] = -1; 10 | for(ans = i = 0, c = IN, h = M; c != 10; c = IN, i++){ 11 | if(c == '\\') d[h--] = i; 12 | else if(c == '/' && d[++h] != -1){ 13 | ans += i - d[h]; 14 | d[h] = -1; 15 | } 16 | } 17 | printf("%d\n", ans); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /uva/Volume II/00264.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00264 - Count on Cantor 3 | ///////////////////////////////// 4 | #include 5 | 6 | #include 7 | int ntri, tri, n; 8 | int nup(int k){ 9 | return (int)ceil((-1+sqrt(1+8*k))/2); 10 | } 11 | int main(void){ 12 | while(scanf("%d",&n)!=EOF){ 13 | ntri = nup(n); 14 | tri = ntri*(ntri+1)/2; 15 | if(ntri&1) printf("TERM %d IS %d/%d\n",n,1+(tri-n),ntri-(tri-n)); 16 | else printf("TERM %d IS %d/%d\n",n,ntri-(tri-n),1+(tri-n)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume IV/00465.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define LIM 2147483647 4 | char a[2048], b[2048]; 5 | int main(void){ 6 | long double n1, n2; 7 | char op; 8 | while(scanf("%s %c %s", a, &op, &b) == 3){ 9 | n1 = atof(a); n2 = atof(b); 10 | printf("%s %c %s\n", a, op, b); 11 | if(n1 > LIM) puts("first number too big"); 12 | if(n2 > LIM) puts("second number too big"); 13 | if(op == '+') n1 += n2; else n1 *= n2; 14 | if(n1 > LIM) puts("result too big"); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CII/10276.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10276 - Hanoi Tower Troubles Again! 3 | ///////////////////////////////// 4 | #include 5 | unsigned int m,tnum, 6 | ans[] = {1,3,7,11,17,23,31,39,49,59,71,83,97,111, 7 | 127,143,161,179,199,219,241,263,287,311,337,363, 8 | 391,419,449,479,511,543,577,611,647,683,721,759, 9 | 799,839,881,923,967,1011,1057,1103,1151,1199,1249,1299}; 10 | int main(void){ 11 | scanf("%u",&tnum); 12 | while(tnum--) 13 | scanf("%u",&m), 14 | printf("%u\n",ans[m-1]); 15 | } 16 | -------------------------------------------------------------------------------- /uva/Volume CVIII/10812.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10812 - Beat the Spread! 3 | ///////////////////////////////// 4 | #include 5 | int sum,dif,a,cases; 6 | int main(void){ 7 | scanf("%d",&cases); 8 | while(cases){ 9 | cases--; 10 | scanf("%d %d",&sum,&dif); 11 | a = (sum+dif)/2; 12 | if(sum < dif || (sum&1 && !(dif&1)) || (!(sum&1) && dif&1)) printf("impossible\n"); 13 | else if(a > sum-a) printf("%d %d\n",a,sum-a); 14 | else printf("%d %d\n",sum-a,a); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /euler/e042.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char word[64]; 6 | int main(void){ 7 | clock_t ini = clock(); 8 | int ans = 0, value; 9 | while(scanf("%s", word) == 1){ 10 | char *p = word; 11 | value = 0; 12 | while(*p) value += *p++ - 'A' + 1; 13 | int test = (value << 3) + 1, root = (int)sqrt(test); 14 | ans += root * root == test; 15 | } 16 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 17 | printf("Answer: %d\n", ans); 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /la/3911.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int N, T; 5 | char t[5]; 6 | float q; 7 | scanf("%d", &T); 8 | for(int cnum = 0; cnum < T; ){ 9 | printf("%d ", ++cnum); 10 | scanf("%f %s", &q, t); 11 | switch(t[0]){ 12 | case 'k': printf("%.4f lb\n", q * 2.2046); break; 13 | case 'g': printf("%.4f l\n", q * 3.7854); break; 14 | case 'l': 15 | if(t[1] == 'b') printf("%.4f kg\n", q * 0.4536); 16 | else printf("%.4f g\n", q * 0.2642); 17 | break; 18 | } 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11716.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char word[10005]; 5 | int main(void){ 6 | int l, t; 7 | scanf("%d\n", &t); 8 | while(t--){ 9 | fgets(word, 10005, stdin); 10 | l = -1; 11 | for(char *p = word; *p; ++p, ++l); 12 | int root = floor(sqrt(l)); 13 | if(root * root != l) puts("INVALID"); 14 | else { 15 | for(int c = 0; c < root; ++c) 16 | for(int k = c; k < l; k += root) 17 | putc(word[k], stdout); 18 | putc(10, stdout); 19 | } 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11824.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int v[128]; 6 | int main(void){ 7 | int n, t; scanf("%d", &t); 8 | while(t--){ 9 | double s = 0; 10 | for(n = 0; scanf("%d", &v[n]) == 1 && v[n]; ++n); 11 | sort(v, v + n); 12 | for(int i = n; i--; ){ 13 | s += pow((double)v[i], (double)n - i); 14 | if(s > 2500000) break; 15 | } 16 | if(s > 2500000) puts("Too expensive"); 17 | else printf("%.0lf\n", 2 * s); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /euler/e040.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int nd[10], s[7] = {1, 10, 100, 1000, 10000, 100000, 1000000 }; 6 | int main(void){ 7 | clock_t ini = clock(); 8 | int ans = 1, d = 0, si = 0; 9 | for(int i = 1; d < 1000000; ++i){ 10 | int x = i, p = 0; 11 | while(x) nd[p++] = x % 10, x /= 10; 12 | while(p--) if(++d == s[si]) si++, ans *= nd[p]; 13 | } 14 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 15 | printf("Answer: %d\n", ans); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /la/3934.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int a, d, i, pa[11],pd[11]; 4 | bool offside; 5 | int main(void){ 6 | while(scanf("%d%d",&a,&d) && (a || d)){ 7 | for(i = 0; i < a; i++) 8 | scanf("%d",&pa[i]); 9 | for(i = 0; i < d; i++) 10 | scanf("%d",&pd[i]); 11 | std::sort(pa,pa+a); 12 | std::sort(pd,pd+d); 13 | for(offside = i = 0; !offside && i < a; i++) 14 | if(pa[i] < pd[1]) offside = 1; 15 | if(offside) putchar('Y'); 16 | else putchar('N'); 17 | putchar('\n'); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /spoj/classical/MSTRMND.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int a[4] = {1, 1, 1, 1}, b[4], sum; 4 | while(sum != 4){ 5 | for(int i = 0; i < 4; ++i) printf("%d%c", a[i], i == 3 ? 10 : 32); 6 | fflush(stdout); sum = 0; 7 | for(int i = 0; i < 4; ++i){ 8 | scanf("%d", b + i); 9 | if(b[i] != 1) ++a[i]; 10 | sum += b[i]; 11 | } 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /spoj/seletiva/PLACAR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct team{ 5 | char name[25]; 6 | int p; 7 | }; 8 | bool cmp(team a, team b){ 9 | return ((a.p > b.p) || (a.p == b.p && strcmp(a.name,b.name) < 0)); 10 | } 11 | team t[100]; 12 | int main(void){ 13 | int i, n, inst = 1; 14 | while(scanf("%d",&n)==1){ 15 | for(i = 0; i < n; i++) 16 | scanf("%s %d",&t[i].name,&t[i].p); 17 | std::sort(t,t+n,cmp); 18 | printf("Instancia %d\n%s\n\n",inst++,t[n-1].name); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /uva/Volume CVI/10642.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10642 - Can You Solve It? 3 | ///////////////////////////////// 4 | #include 5 | typedef unsigned long long int ULLI; 6 | ULLI x1,x2,y1,y2; 7 | unsigned int cnum,tnum; 8 | ULLI rank(ULLI a, ULLI b){ 9 | return((a+b+1)*(a+b+2)/2 - b); 10 | } 11 | int main(void){ 12 | scanf("%u",&cnum); 13 | while(cnum--){ 14 | tnum++; 15 | scanf("%llu %llu %llu %llu",&x1,&y1,&x2,&y2); 16 | printf("Case %u: %llu\n",tnum,rank(x2,y2)-rank(x1,y1)); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /uva/Volume CXV/11554.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11554 - Hapless Hedonism 3 | ///////////////////////////////// 4 | #include 5 | unsigned short int t; 6 | unsigned long long int i, triangles[1000001]; 7 | int main(void){ 8 | triangles[3] = 0; 9 | for(i = 4; i < 1000001; i++) 10 | if(i&1) triangles[i] = (i-1)*(i-3)/4 + triangles[i-1]; 11 | else triangles[i] = (i-2)*(i-2)/4 + triangles[i-1]; 12 | scanf("%hu",&t); 13 | while(t--) 14 | scanf("%llu",&i), printf("%llu\n",triangles[i]); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /uva/Volume VI/00640.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00640 - Self Numbers 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | bool self[1000001]; 7 | unsigned int i; 8 | unsigned short int res; 9 | void dsum(unsigned int k){ 10 | res = 0; 11 | while(k){ res+=k%10; k/=10;} 12 | } 13 | int main(void){ 14 | memset(self,1,sizeof(self)); 15 | for(i = 1; i < 1000001; i++){ 16 | dsum(i); 17 | if(res+i < 1000001) self[res+i] = 0; 18 | if(self[i]) printf("%u\n",i); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /codeforces/056C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | map < string, int > m; 6 | int n = 0; 7 | char name[32]; 8 | bool capital(char x){ return x >= 'A' && x <= 'Z'; } 9 | void readEmployee(){ 10 | string s = ""; 11 | char c; 12 | while(capital(c = getc(stdin))) s.push_back(c); 13 | n += m[s]++; 14 | if(c == ':') while(c != '.') readEmployee(), c = getc(stdin); 15 | m[s]--; 16 | 17 | } 18 | int main(void){ 19 | readEmployee(); 20 | printf("%d\n", n); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /euler/e034.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int f[10] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; 6 | bool curious(int x){ 7 | int sum = 0, k = x; 8 | while(x) sum += f[x % 10], x /= 10; 9 | return sum == k; 10 | } 11 | int main(void){ 12 | clock_t ini = clock(); 13 | int ans = 0; 14 | for(int i = 3; i < 2540160; ++i) 15 | if(curious(i)) ans += i; 16 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 17 | printf("Answer: %d\n", ans); 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /spoj/classical/PIR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | long long sqr(long long a){ return a * a; } 4 | int main(void){ 5 | long long s, u, v, w, U, V, W; 6 | int t; scanf("%d", &t); 7 | while(t--){ 8 | scanf("%lld %lld %lld %lld %lld %lld", &W, &U, &v, &V, &u, &w); 9 | u = sqr(u); v = sqr(v); w = sqr(w); 10 | U = sqr(U); V = sqr(V); W = sqr(W); 11 | s = 4*u*v*w - u*sqr(v+w-U) - v*sqr(u+w-V) - w*sqr(u+v-W); 12 | s += (u+v-W)*(u+w-V)*(v+w-U); 13 | printf("%.4lf\n", sqrt(s) / 12); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /timus/T1049.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int MAX = 10000; 3 | int cnt[MAX]; 4 | int main(void){ 5 | for(int i = 0; i < MAX; ++i) cnt[i] = 0; 6 | for(int i = 0, z; i < 10; ++i){ 7 | scanf("%d", &z); 8 | while(!(z & 1)) ++cnt[2], z >>= 1; 9 | for(int j = 3; z != 1 && j * j <= z; j += 2) 10 | while(!(z % j)) ++cnt[j], z /= j; 11 | if(z != 1) ++cnt[z]; 12 | } 13 | int r = (cnt[2] + 1) % 10; 14 | for(int i = 3; i < MAX; i += 2) 15 | if(cnt[i]) r = (r * (cnt[i] + 1)) % 10; 16 | printf("%d\n", r); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /uva/Volume CV/10550.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10550 - Combination Lock 3 | ///////////////////////////////// 4 | #include 5 | unsigned int a,b,c,d; 6 | unsigned short turn, ans; 7 | int main(void){ 8 | while(scanf("%u %u %u %u\n",&a,&b,&c,&d) && (a||b||c||d)){ 9 | ans = 1080; 10 | if(b>a) ans += (40+a-b)*9; 11 | else ans += (a-b)*9; 12 | if(b>c) ans += (40+c-b)*9; 13 | else ans += (c-b)*9; 14 | if(d>c) ans += (40+c-d)*9; 15 | else ans += (c-d)*9; 16 | printf("%hu\n",ans); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /uva/Volume V/00575.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00575 - Skew Binary 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #include 7 | long int result; 8 | char c[35]; 9 | int i; 10 | int length; 11 | int main(void){ 12 | while(scanf("%s",&c)){ 13 | 14 | if(c[0] == '0') break; 15 | 16 | result = 0; 17 | length = strlen(c); 18 | for(i = 0; i < length; i++){ 19 | c[i]-='0'; 20 | result+= c[i]*(pow(2,length-i)-1); 21 | } 22 | printf("%ld\n",result); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spoj/obi/CALCULA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | char line[500]; 4 | int main(void){ 5 | int a, i, num, sum, test = 1; 6 | char sign; 7 | while(scanf("%d\n",&num) && num){ 8 | gets(line); 9 | sign = '+'; sum = 0; 10 | for(i = 0; line[i]; i++){ 11 | a = 0; 12 | while(isdigit(line[i])) 13 | a *= 10, a += (line[i]-'0'), i++; 14 | if(sign == '+') sum += a; 15 | else sum -= a; 16 | sign = line[i]; 17 | if(!sign) break; 18 | } 19 | printf("Teste %d\n%d\n\n",test++, sum); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /codeforces/008B.py: -------------------------------------------------------------------------------- 1 | s = raw_input().strip() 2 | depth, OK, x, y = 1, 1, 150, 150 3 | v = [[0] * 300 for i in range(300)] 4 | d = [[-1, 0],[1, 0],[0, 1],[0, -1]] 5 | 6 | def check(x, y): 7 | z = depth 8 | for i in range(4): 9 | t = v[x+d[i][0]][y+d[i][1]] 10 | if t and t != z - 1: return False 11 | return True 12 | 13 | for c in s: 14 | OK &= v[x][y] == 0 15 | v[x][y] = depth 16 | depth += 1 17 | x, y = x + (c == 'R') - (c == 'L'), y + (c == 'U') - (c == 'D') 18 | OK &= check(x, y) 19 | if not OK: break 20 | 21 | print ["BUG", "OK"][OK] 22 | -------------------------------------------------------------------------------- /euler/e004.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int rev(int x){ 5 | int y = x, z = 0; 6 | while(y){ 7 | z = z * 10 + (y % 10); 8 | y /= 10; 9 | } 10 | return z; 11 | } 12 | int main(void){ 13 | clock_t ini = clock(); 14 | int ans = 0; 15 | for(int a = 999; a > 1; --a) 16 | for(int b = a; b > 1; --b){ 17 | int x = a * b, z = rev(x); 18 | if(x == z && x > ans) ans = x; 19 | } 20 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 21 | printf("Answer: %d\n", ans); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /uva/Volume CII/10223.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10223 - How many nodes ? 3 | ///////////////////////////////// 4 | #include 5 | unsigned int c[] = {1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190}; 6 | unsigned int n; 7 | char s,u,v; 8 | int main(void){ 9 | while(scanf("%u",&n)!=EOF){ 10 | for(u = 0, v = 19; u < v; ){ 11 | s = (u+v)/2; 12 | if(c[s] 5 | unsigned int a,b,c,count,d,k,n,preva,prevb,show; 6 | void farey(){ 7 | a = count = 0; b = c = 1; d = n; 8 | while (count != show){ 9 | k = (n + b)/d; 10 | preva = a; 11 | prevb = b; 12 | a = c; b = d; 13 | c = k*c-preva; d = k*d-prevb; 14 | count++; 15 | } 16 | printf("%u/%u\n",a,b); 17 | } 18 | int main(void){ 19 | while(scanf("%u %u",&n,&show)!=EOF) farey(); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11701.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | #define PREC 17 7 | #define EPS 1E-7 8 | #define FORI(a,b,c) for(int a = b; a < c; ++a) 9 | 10 | double p[50]; 11 | int main(void){ 12 | 13 | double div = 3.; 14 | FORI(i, 1, PREC){ 15 | p[i] = (2/div); 16 | div *= 3.; 17 | } 18 | double N; 19 | while(scanf("%lf",&N)==1){ 20 | for(int i = 1; i < PREC && N > EPS; i++) N -= p[i] * (N - p[i] > 0.); 21 | (N > EPS)? puts("NON-MEMBER") : puts("MEMBER"); 22 | } 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /uva/Volume VII/00729.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int n, need; 3 | char ans[32]; 4 | void bt(int k, int have){ 5 | if(k == n){ 6 | ans[k] = 0; 7 | for(char *p = ans; *p; p++) putc(*p, stdout); 8 | putc(10, stdout); 9 | return; 10 | } 11 | if(have + (n - k) > need) ans[k] = '0', bt(k + 1, have); 12 | if(have < need) ans[k] = '1', bt(k + 1, have + 1); 13 | } 14 | 15 | int main(void){ 16 | int t; scanf("%d", &t); 17 | for(;t-- && scanf("%d %d", &n, &need);){ 18 | bt(0, 0); 19 | if(t) putc(10, stdout); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /codeforces/053A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define SZ(x) ((int)(x.size())) 5 | int main(void){ 6 | string ans = "", page, prefix; 7 | char w[1024]; 8 | int n; scanf("%s %d", w, &n); 9 | prefix = w; 10 | for(int i = 0; i < n; ++i){ 11 | scanf("%s", w); 12 | page = w; 13 | if(SZ(page) >= SZ(prefix) && prefix == page.substr(0, SZ(prefix))) 14 | if(ans == "" || page < ans) ans = page; 15 | } 16 | if(SZ(ans)) printf("%s\n", ans.c_str()); 17 | else printf("%s\n", prefix.c_str()); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /spoj/classical/ANARC08G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define FOR(i,n) for(int i = 0; i < n; ++i) 3 | const int N = 1024; 4 | int b[N]; 5 | int main(void){ 6 | for(int cnum = 0, n, z; scanf("%d", &n) == 1 && n; ){ 7 | int sum = 0; 8 | FOR(i, n) b[i] = 0; 9 | FOR(i, n) FOR(j, n){ 10 | scanf("%d", &z); 11 | if(z){ 12 | b[j] += z; 13 | b[i] -= z; 14 | sum += z; 15 | } 16 | } 17 | printf("%d. %d ", ++cnum, sum); 18 | sum = 0; 19 | FOR(i, n) if(b[i] > 0) sum += b[i]; 20 | printf("%d\n", sum); 21 | } 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /spoj/sulamericana/IMPEDIDO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int a, d, i, pa[11],pd[11]; 4 | bool offside; 5 | int main(void){ 6 | while(scanf("%d%d",&a,&d) && (a || d)){ 7 | for(i = 0; i < a; i++) 8 | scanf("%d",&pa[i]); 9 | for(i = 0; i < d; i++) 10 | scanf("%d",&pd[i]); 11 | std::sort(pa,pa+a); 12 | std::sort(pd,pd+d); 13 | for(offside = i = 0; !offside && i < a; i++) 14 | if(pa[i] < pd[1]) offside = 1; 15 | if(offside) putchar('Y'); 16 | else putchar('N'); 17 | putchar('\n'); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /la/3708.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const double EPS = 1e-5; 5 | 6 | bool red = 0; 7 | int main(void){ 8 | int k, m, n; 9 | double a1, a2, a3, move; 10 | while(scanf("%d%d",&n,&m) == 2){ 11 | a1 = 10000/(double)(n + m); 12 | a2 = 10000/(double)(n); 13 | a3 = move = 0; 14 | for(int i = 0; i < n; ++i){ 15 | a3 += a2; 16 | k = floor(a3 / a1); 17 | a3 -= k * a1; 18 | double sp1 = a1 - a3; 19 | if(a3 < sp1) sp1 = a3; 20 | move += sp1; 21 | } 22 | printf("%.4lf\n", move); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /la/4806.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool cando[128]; 4 | int b[128]; 5 | int abs(int x){ return x < 0 ? -x : x; } 6 | int main(void){ 7 | for(int B, N; scanf("%d %d", &N, &B) == 2 && (N || B); ){ 8 | int s = 1; 9 | for(int i = 0; i <= N; ++i) cando[i] = 0; 10 | for(int i = 0; i < B; ++i){ 11 | scanf("%d", &b[i]); 12 | for(int j = 0; j < i; ++j){ 13 | int diff = abs(b[i] - b[j]); 14 | s += !cando[diff]; 15 | cando[diff] |= 1; 16 | } 17 | } 18 | if(s == N + 1) puts("Y"); 19 | else puts("N"); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /regional/2008/dama.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int abs(int x) { return x < 0 ? -x : x; } 4 | 5 | int main(void) { 6 | int ax, ay, bx, by; 7 | 8 | while(scanf("%d %d %d %d", &ax, &ay, &bx, &by)) { 9 | if(ax == 0 && ay == 0 && bx == 0 && by == 0) 10 | break; 11 | 12 | if(ax == bx) { 13 | if(ay == by) printf("0\n"); 14 | else printf("1\n"); 15 | } else { 16 | if(ay == by) printf("1\n"); 17 | else { 18 | if(abs(ax - bx) == abs(ay - by)) printf("1\n"); 19 | else printf("2\n"); 20 | } 21 | } 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /spoj/classical/SBANK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | map < string, int > m; 6 | char line[64]; 7 | int main(void){ 8 | int n, t; 9 | for(scanf("%d", &t); t--; ){ 10 | m.clear(); 11 | for(scanf("%d\n", &n); n--; ){ 12 | fgets(line, 64, stdin); 13 | line[32] = 0; 14 | ++m[line]; 15 | } 16 | map < string, int >::iterator it; 17 | for(it = m.begin(); it != m.end(); it++) 18 | printf("%s%d\n", it->first.c_str(), it->second); 19 | if(t) puts(""); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /timus/T1081.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int dp[44][2]; 4 | int main(void){ 5 | dp[1][0] = dp[1][1] = 1; 6 | for(int i = 2; i < 44; ++i){ 7 | dp[i][0] = dp[i-1][0] + dp[i-1][1]; 8 | dp[i][1] = dp[i-1][0]; 9 | } 10 | for(int n, k; scanf("%d %d", &n, &k) == 2; ){ 11 | if(k > dp[n][0] + dp[n][1]) puts("-1"); 12 | else { 13 | for(int i = n; i; --i){ 14 | if(dp[i][0] >= k) putc('0', stdout); 15 | else { 16 | k -= dp[i][0]; 17 | putc('1', stdout); 18 | } 19 | } 20 | puts(""); 21 | } 22 | } 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /uva/Volume CXIV/11498.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11498 - Division of Nlogonia 3 | ///////////////////////////////// 4 | #include 5 | int k,cx,cy,x,y; 6 | char c[3]; 7 | int main(void){ 8 | while(scanf("%d",&k)){ 9 | if(k==0) break; 10 | scanf("%d %d",&cx,&cy); 11 | for(int i = 0; i < k; i++){ 12 | scanf("%d %d",&x,&y); 13 | if(x == cx || y == cy) printf("divisa\n"); 14 | else{ 15 | c[0] = (y > cy? 'N':'S'); 16 | c[1] = (x > cx? 'E':'O'); 17 | printf("%s\n",c); 18 | } 19 | } 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11687.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | char digits(unsigned int x){ 4 | register char ret = 0; 5 | while(x) x/=10, ret++; 6 | return ret; 7 | } 8 | int main(void){ 9 | register char c; 10 | unsigned int cnt; 11 | bool mark; 12 | while((c = getc( stdin )) != 'E'){ 13 | cnt = mark = 1; 14 | if(c == '1') mark = 0; 15 | while((c = getc( stdin )) != '\n') cnt++; 16 | if(cnt == 1) printf("%d\n",cnt+mark); 17 | else if(cnt < 10) printf("%d\n",3); 18 | else printf("%d\n",4); 19 | } 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /codeforces/056B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void){ 3 | int k = -1, l = -1, r = - 1, z = -1; 4 | int n; scanf("%d", &n); 5 | bool valid = true; 6 | for(int i = 1; i <= n; ++i){ 7 | scanf("%d", &k); 8 | if(k != i){ 9 | valid &= (k > i && l == -1); 10 | l = i; 11 | int j; 12 | for(j = 1; i + j <= k; ++j){ 13 | scanf("%d", &z); 14 | if(k - z != j) valid = false; 15 | } 16 | if(valid) r = k; 17 | i += --j; 18 | } 19 | } 20 | if(valid && l != r) printf("%d %d\n", l, r); 21 | else printf("0 0\n"); 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /codeforces/066D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int p[51] = 3 | {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71, 4 | 73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151, 5 | 157,163,167,173,179,181,191,193,197,199,211,223,227,229,233}; 6 | int v[50]; 7 | int gcd(int a, int b){ return b ? gcd(b, a % b) : a; } 8 | int main(void){ 9 | int n; scanf("%d", &n); 10 | v[0] = 6; v[1] = 15; 11 | for(int i = 2; i < 50; ++i) v[i] = 10 * p[i + 1]; 12 | if(n == 2) puts("-1"); 13 | else for(int i = 0; i < n; ++i) printf("%d\n", v[i]); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /la/3571.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int t[1001]; 4 | int main(void){ 5 | for(int i = 1; i < 1001; ++i) t[i] = 1; 6 | for(int i = 2; i < 1001; ++i){ 7 | if(t[i] == 1){ 8 | int k, x = i - 1; 9 | for(int j = i; j < 1001; j+=i){ 10 | t[j] *= x; 11 | k = j / i; 12 | while(!(k % i)) t[j] *= i, k /= i; 13 | } 14 | } 15 | t[i] += t[i-1]; 16 | } 17 | int T, N; 18 | scanf("%d",&T); 19 | for(int cnum = 0; cnum < T; ){ 20 | scanf("%d",&N); 21 | printf("%d %d %d\n",++cnum,N,(t[N] << 1) + 1); 22 | } 23 | return 0; 24 | } 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /uva/Volume CI/10107.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10107 - What is the Median? 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | #define MAX 10000 7 | int i,n = 0,x; 8 | int v[MAX]; 9 | int main(void){ 10 | while(scanf("%d",&v[n])!=EOF){ 11 | n++; 12 | if(n > 1) if(v[n-1] < v[n-2]) std::sort(v,v+n); 13 | /*printf("Vector: "); 14 | for(i = 0; i < n; i++) printf("%d,",v[i]); 15 | printf("\n");*/ 16 | if(n%2) printf("%d\n",v[n/2]); 17 | else printf("%d\n",(v[n/2-1]+v[n/2])/2); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11712.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char *m = "0 0 0 0 L\n0 1 1 0 R\n1 0 0 0 L\n1 1 2 1 R\n\ 4 | 2 0 6 0 R\n2 1 3 1 R\n3 1 3 1 R\n3 0 4 1 L\n\ 5 | 4 1 4 1 L\n4 0 5 1 R\n6 0 2 0 L"; 6 | int main(void){ 7 | char v[16]; 8 | for(int n; scanf("%d", &n) && n; ){ 9 | printf("11 %d\n%s\n", n, m); 10 | for(int i = 0; i < n; ++i){ 11 | scanf("%s", v); 12 | if(*v == 'A') puts("3 4"); 13 | else if(*v == 'M') puts("1 1"); 14 | else if(*v == 'T') puts("2 2"); 15 | else if(*v == 'W') puts("3 5"); 16 | } 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /uva/Volume I/00151.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00151 - Power Crisis 3 | ///////////////////////////////// 4 | #include 5 | int n; 6 | unsigned char ans[] = 7 | {1,18,10,11,7,17,11,15,29,5,21,13,26,14,11,23,22,9,73,17,42,7,98,15, 8 | 61,22,84,24,30,9,38,15,54,27,9,61,38,22,19,178,38,53,79,68,166,20,9, 9 | 22,7,21,72,133,41,10,82,92,64,129,86,73,67,19,66,115,52,24,22,176,10, 10 | 57,137,239,41,70,60,116,81,79,55,102,49,5,22,54,52,113,15}; 11 | int main(void){ 12 | while(scanf("%d",&n) && n) printf("%d\n",ans[n-13]); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /codeforces/048A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | char a, b, c; 3 | char play[16]; 4 | bool beat(char x, char y){ 5 | if(x == 'p' && y == 'r') return true; 6 | if(x == 'r' && y == 's') return true; 7 | if(x == 's' && y == 'p') return true; 8 | return false; 9 | } 10 | int main(void){ 11 | scanf("%s", play); a = *play; 12 | scanf("%s", play); b = *play; 13 | scanf("%s", play); c = *play; 14 | if(beat(a,b) && beat(a,c)) puts("F"); 15 | else if(beat(b,a) && beat(b,c)) puts("M"); 16 | else if(beat(c,a) && beat(c,b)) puts("S"); 17 | else puts("?"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /codeforces/059A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | char w[128]; 3 | char lower(char x){ return x + ((x >= 'A' && x <= 'Z') << 5); } 4 | char upper(char x){ return x - ((x >= 'a' && x <= 'z') << 5); } 5 | int main(void){ 6 | int l = 0, u = 0; 7 | while(1){ 8 | char c = w[u + l] = getc(stdin); 9 | if(!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) break; 10 | l += c >= 'a' && c <= 'z'; 11 | u += c >= 'A' && c <= 'Z'; 12 | } 13 | w[u + l] = 0; 14 | for(char *p = w; *p; ++p) putc(u > l ? upper(*p) : lower(*p), stdout); 15 | putc(10, stdout); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /euler/e007.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 1000000 5 | bool p[MAX]; 6 | int main(void){ 7 | clock_t ini = clock(); 8 | memset(p, 1, sizeof(p)); 9 | int ans = 1000, c = 1; 10 | for(int i = 4; i < MAX; i += 2) p[i] = 0; 11 | for(int i = 3; i < 1001; i += 2) 12 | if(p[i]){ 13 | ++c; 14 | for(int j = i * i; j < MAX; j += i) p[j] = 0; 15 | } 16 | while(c < 10001) c += p[++ans]; 17 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 18 | printf("Answer: %d\n", ans); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /euler/e104.py: -------------------------------------------------------------------------------- 1 | def pandigital(x): 2 | seen, s = [0 for i in range(10)], 0 3 | while(x): 4 | k = x % 10 5 | seen[k] = 1 6 | s += k 7 | x //= 10 8 | return s == 45 and sum(seen[1:]) == 9 9 | 10 | p9, p15 = 10 ** 9, 10 ** 15 11 | lim = 1000000 12 | f, l, pf, pl = 1, 1, 0, 0 13 | for i in range(2, lim): 14 | f, l, pf, pl = (f + pf), (l + pl) % p9, f, l 15 | while(f > p15): 16 | f //= 10 17 | pf //= 10 18 | x = f 19 | while x > p9: x //= 10 20 | if(pandigital(x) and pandigital(l)): 21 | print("Answer: {}\nF[{}] = {}...{}".format(i, i, x, l)) 22 | break 23 | -------------------------------------------------------------------------------- /euler/e173.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | long long sqr(long long x){ return x * x; } 6 | bool is_sqr(long long x){ return sqr(floor(sqrt(x+.5))) == x; } 7 | int main(void){ 8 | clock_t ini = clock(); 9 | int ans = 0; 10 | for(long long i = 1; i < 500000; ++i) 11 | for(long long j = i + 2; ; j += 2){ 12 | long long d = j * j - i * i; 13 | if(d > 1000000) break; 14 | ++ans; 15 | } 16 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 17 | printf("Answer: %d\n", ans); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /spoj/classical/YAPP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | const LL M = 1000000007; 4 | int main(void){ 5 | int t; scanf("%d", &t); 6 | while(t--){ 7 | int n; scanf("%d", &n); --n; 8 | LL r = 1, mult = 2; 9 | while(n){ 10 | if(n & 1) r = (r * mult) % M; 11 | mult = (mult * mult) % M; 12 | n >>= 1; 13 | } 14 | printf("%lld\n", r); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CXI/11121.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11121 - Base -2 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | char q[100], top; 7 | unsigned int i,t; 8 | int n, res; 9 | int main(void){ 10 | scanf("%u",&t); 11 | for(i = 1; t--; i++){ 12 | scanf("%d",&n); top = 0; 13 | do{ 14 | res = n/-2; 15 | if(n<0 && n&1) res++; 16 | q[top++] = (n-(res*-2))+'0'; 17 | n = res; 18 | }while(n); 19 | q[top] = 0; 20 | std::reverse(q,q+top); 21 | printf("Case #%u: %s\n",i,q); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11743.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int v[2][10]; 4 | char n[16]; 5 | const char *answer[2] = {"Invalid", "Valid"}; 6 | int main(void){ 7 | int t; 8 | for(int i = 0; i < 5; ++i) v[0][i] = i, v[1][i] = (i << 1); 9 | for(int i = 5; i < 10; ++i) v[0][i] = i, v[1][i] = (i << 1) - 9; 10 | scanf("%d", &t); 11 | while(t--){ 12 | int s = 0; 13 | bool t = 1; 14 | for(int i = 0; i < 4; ++i){ 15 | scanf("%s", n); 16 | for(char *p = n; *p; ++p, t = 1 - t) s += v[t][*p - '0']; 17 | } 18 | puts(answer[!(s % 10)]); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /euler/e121.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | long long x = 0; 6 | long long fact(int x){ return x ? x * fact(x - 1) : 1; } 7 | void bt(long long w, int b, int r){ 8 | if(b + r == 15){ 9 | x += w; 10 | return; 11 | } 12 | if(r < 7) bt(w * (b + r + 1), b, r + 1); 13 | bt(w, b + 1, r); 14 | } 15 | int main(void){ 16 | clock_t ini = clock(); 17 | bt(1, 0, 0); 18 | long long ans = fact(16) / x; 19 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 20 | printf("Answer: %lld\n", ans); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /spoj/classical/DSUBSEQ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int M = 1000000007, N = 1048576; 4 | char s[N]; 5 | int dp[N], last[128]; 6 | int main(void){ 7 | int i, t; scanf("%d", &t); 8 | dp[0] = 1; 9 | while(t--){ 10 | for(int c = 0; c < 128; ++c) last[c] = 0; 11 | scanf("%s", s + 1); 12 | for(i = 1; s[i]; ++i){ 13 | dp[i] = dp[i - 1] << 1; 14 | if(last[s[i]]) dp[i] -= dp[last[s[i]] - 1]; 15 | if(dp[i] < 0) dp[i] += M; 16 | else if(dp[i] >= M) dp[i] -= M; 17 | last[s[i]] = i; 18 | } 19 | printf("%d\n", dp[--i]); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /spoj/classical/TTREAT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct interval { 4 | int b, e; 5 | bool intersect(interval &i){ return !(b >= i.e || e <= i.b); } 6 | } d[10048]; 7 | int main(void){ 8 | for(int cnt, i, j, m, n; scanf("%d %d", &n, &m) && (n || m); ){ 9 | for(i = 0; i < n; ++i){ scanf("%*d %*d %d %d", &d[i].b, &d[i].e); d[i].e += d[i].b; } 10 | for(i = 0; i < m; ++i){ 11 | scanf("%d %d", &d[n].b, &d[n].e); d[n].e += d[n].b; 12 | for(cnt = j = 0; j < n; ++j) cnt += d[j].intersect(d[n]); 13 | printf("%d\n", cnt); 14 | } 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /uva/Volume CXV/11500.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11500 - Vampires 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | double ev1,ev2,at,d,p,res; 7 | int main(void){ 8 | double p; 9 | scanf("%d",&ev1); 10 | while(ev1){ 11 | scanf("%lf %lf %lf", &ev2,&at,&d); 12 | ev1 = ceil(ev1/d); 13 | ev2 = ceil(ev2/d); 14 | d = 1; 15 | p = at/6; 16 | if(at == 3) res = ev1/(ev1+ev2); 17 | else res = (1 - pow((1-p)/p,ev1)) / (1 - pow((1-p)/p,ev1+ev2)); 18 | printf("%.1lf\n",res*100); 19 | scanf("%lf",&ev1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11683.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define NN 10032 4 | 5 | int v[NN]; 6 | int main(void){ 7 | int menor, H, L; 8 | int s, i; 9 | while(scanf("%d%d",&H,&L) && (H||L)){ 10 | s =0; 11 | for(i = 0; i < L; i++){ 12 | scanf("%d",&v[i]); 13 | if(i && v[i] == v[i-1]) i--, L--; 14 | } 15 | v[L] = H; 16 | menor = v[0]; 17 | for(i = 1; i < L; i++) 18 | if(v[i] > v[i-1] && v[i] > v[i+1]){ 19 | s += v[i] - menor; 20 | menor = H; 21 | } else if(v[i] < menor) menor = v[i]; 22 | printf("%d\n",s+H-menor); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /euler/e026.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | clock_t ini = clock(); 6 | int ans, len, max_len = 0, r, r0; 7 | for(int i = 2; i < 1000; ++i){ 8 | r = 1; 9 | for(int j = 0; j < i; ++j) r = (r * 10) % i; 10 | r0 = r; 11 | len = 0; 12 | do { 13 | r = (r * 10) % i; 14 | len++; 15 | } while(r != r0); 16 | if(len > max_len){ 17 | ans = i; 18 | max_len = len; 19 | } 20 | } 21 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 22 | printf("Answer: %d [%d]\n", ans, max_len); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /spoj/classical/QUALITY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int a, b, c, d, e, f, tc = 1; 5 | int mask[2] = {0xFFFFFFFF, 0}; 6 | while(scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f) == 6){ 7 | int total = 0; 8 | total += mask[!a] & (a + 1200 * (d - 1)); 9 | total += mask[!b] & (b + 1200 * (e - 1)); 10 | total += mask[!c] & (c + 1200 * (f - 1)); 11 | printf("team %d: %d, %d\n", tc++, (!!a)+(!!b)+(!!c), total); 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /uva/Volume CII/10221.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const double pi = 2 * acos(0); 5 | const double conv = pi/(double)180; 6 | int main(void){ 7 | int a, s; 8 | double angr, x1, x2, y1, y2; 9 | char t[8]; 10 | while(scanf("%d %d %s", &s, &a, t) == 3){ 11 | angr = a * conv; 12 | s = s + 6440; 13 | if(t[0] == 'm') angr /= 60.000000000; 14 | if(angr > pi) angr = 2 * pi - angr; 15 | x1 = s; y1 = 0; 16 | x2 = s * cos(angr); y2 = s * sin(angr); 17 | printf("%.6lf %.6lf\n", angr * s, hypot(x2 - x1, y2 - y1)); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume CXVI/11650.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ABS(a) ((a)< 0?-(a):(a)) 4 | #define MIN(a,b) ((a)<(b)?(a):(b)) 5 | #define MAX(a,b) ((a)>(b)?(a):(b)) 6 | 7 | int main(void){ 8 | int H, M, N; 9 | scanf("%d\n",&N); 10 | while(N--){ 11 | char a, b, c, d; 12 | scanf("%c%c:%c%c\n",&a,&b,&c,&d); 13 | a -= '0'; b -= '0'; c -= '0'; d -= '0'; 14 | H = 10 * a + b; 15 | M = 10 * c + d; 16 | int RM = (60 - M)%60; 17 | int RH = 12 - H - (RM%30 != 0) - (M == 30); 18 | if(RH <= 0) RH += 12; 19 | printf("%02d:%02d\n",RH,RM); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /uva/Volume IV/00424.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00424 - Integer Inquiry 3 | ///////////////////////////////// 4 | #include 5 | int i, cr, pos; 6 | int res[200]; 7 | char str[105]; 8 | int main(void){ 9 | while(gets(str)){ 10 | if(str[0] == '0') break; 11 | for(i = 0; str[i] != '\0'; i++); 12 | pos = 199; 13 | for(i = i-1; i > -1; i--){ 14 | cr = res[pos] + str[i] - '0'; 15 | res[pos--] = cr%10; 16 | res[pos] += cr/10; 17 | } 18 | } 19 | for(i = 0; res[i] == 0; i++); 20 | for(; i < 200; i++) printf("%d",res[i]); 21 | printf("\n"); 22 | } -------------------------------------------------------------------------------- /la/3651.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int oc[101]; 4 | int main(void){ 5 | int c, i, j, idx, k, min, n, v; 6 | while(scanf("%d%d%d",&n,&c,&k) && n){ 7 | memset(oc,0,(k+1)<<2); 8 | for(i = 0; i < n; i++) 9 | for(j = 0; j < c; j++) 10 | scanf("%d",&v), oc[v]++; 11 | min = 1<<14; 12 | for(i = 1; i <= k; i++) 13 | if(min > oc[i]) min = oc[i], idx = i; 14 | bool first = 1; 15 | for(i = idx; i <= k; i++) 16 | if(first) printf("%d",idx), first = 0; 17 | else if(oc[i] == min) printf(" %d",i); 18 | putchar('\n'); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /regional/2007/bolhas.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int E, i, swaps, T; 4 | int c[100001], v[100001]; 5 | int main(void){ 6 | while(scanf("%d%d",&E,&T) && E){ 7 | memset(c,0,sizeof(c)); 8 | i = 0; 9 | while(i < T){ 10 | scanf("%d",&swaps); 11 | v[swaps+c[swaps]++] = ++i; 12 | if(c[swaps+c[swaps]]) c[swaps]+= c[swaps+c[swaps]]; 13 | } 14 | while(i < E){ 15 | v[c[0]++] = ++i; 16 | if(c[c[0]]) c[0] += c[c[0]]; 17 | } 18 | printf("%d",v[0]); 19 | for(i = 1; i < E; i++) 20 | printf(" %d",v[i]); 21 | putchar('\n'); 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /spoj/obi/SALDO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int a, b, f, i, l, max, n, pmax, saldo, s; 3 | int main(void){ 4 | for(int t = 1; scanf("%d",&n) && n; t++){ 5 | saldo = max = f = l = pmax = -1; 6 | for(i = 1; i <= n; i++){ 7 | scanf("%d%d",&a,&b); 8 | if(saldo < 0) s = i, saldo = 0; 9 | saldo += (a - b); 10 | if(saldo > max || (saldo == max && i - s > pmax)){ 11 | max = saldo; 12 | f = s, l = i; 13 | pmax = l - f; 14 | } 15 | } 16 | if(max > 0) printf("Teste %d\n%d %d\n\n",t,f,l); 17 | else printf("Teste %d\nnenhum\n\n",t); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /spoj/obi/TRILHAS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MIN(a,b) ((a p) costR += (c-p); 15 | else if(c < p) costL += (p-c); 16 | } 17 | } 18 | cost = MIN(costL,costR); 19 | if(min > cost) min = cost, idx = i+1; 20 | } 21 | printf("%d\n",idx); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /spoj/regional/BOLHAS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int E, i, swaps, T; 4 | int c[100001], v[100001]; 5 | int main(void){ 6 | while(scanf("%d%d",&E,&T) && E){ 7 | memset(c,0,sizeof(c)); 8 | i = 0; 9 | while(i < T){ 10 | scanf("%d",&swaps); 11 | v[swaps+c[swaps]++] = ++i; 12 | if(c[swaps+c[swaps]]) c[swaps]+= c[swaps+c[swaps]]; 13 | } 14 | while(i < E){ 15 | v[c[0]++] = ++i; 16 | if(c[c[0]]) c[0] += c[c[0]]; 17 | } 18 | printf("%d",v[0]); 19 | for(i = 1; i < E; i++) 20 | printf(" %d",v[i]); 21 | putchar('\n'); 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /uva/Volume CXVII/11777.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(void){ 5 | int t, t1, t2, f, a, ct1, ct2, ct3; 6 | scanf("%d", &t); 7 | for(int cnum = 0; cnum++ < t; ){ 8 | scanf("%d %d %d %d %d %d %d", &t1, &t2, &f, &a, &ct1, &ct2, &ct3); 9 | float n = t1 + t2 + f + a + (ct1 + ct2 + ct3 - min(ct1, min(ct2, ct3)))/2.; 10 | printf("Case %d: ", cnum); 11 | if(n >= 90) puts("A"); 12 | else if(n >= 80) puts("B"); 13 | else if(n >= 70) puts("C"); 14 | else if(n >= 60) puts("D"); 15 | else puts("F"); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /uva/Volume IV/00408.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int STEP, MOD; 3 | const char *good = "Good Choice", 4 | *bad = "Bad Choice"; 5 | int f(int k) { return (k + STEP) % MOD; } 6 | int main(void){ 7 | int steps, tortoise, hare; 8 | while(scanf("%d%d",&STEP,&MOD)!= EOF){ 9 | tortoise = f(0); 10 | hare = f(f(0)); 11 | for(steps = 1; hare != tortoise; steps++){ 12 | tortoise = f(tortoise); 13 | hare = f(f(hare)); 14 | } 15 | printf("%10d%10d ",STEP,MOD); 16 | if(steps == MOD) puts(good); 17 | else puts(bad); 18 | putc(10,stdout); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /euler/e094.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(void){ 5 | clock_t ini = clock(); 6 | long long ans = 0; 7 | int nx, ny, r; 8 | for(int x = 7, y = 4; x <= 500000001; x = nx, y = ny){ 9 | // Solving Pell's equation x^2 - 3y^2 = 1 10 | // perimeter = 6 * a +/- 2 (- if c = 2 * a - 1, + if c = 2 * a + 1) 11 | ans += (x + 1 - ((x & 1) << 1)) << 1; 12 | nx = (x << 1) + 3 * y; 13 | ny = (y << 1) + x; 14 | } 15 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 16 | printf("Answer: %lld\n", ans); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /spoj/obi/LUA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int T[10000], sum, tmin, tmax; 3 | int main(void){ 4 | int i, j, n, ts; 5 | for(int t = 1; scanf("%d %d",&n,&ts) && n; t++){ 6 | tmin = (1<<25); tmax = -1<<25; 7 | for(i = 0; i < n; i++) scanf("%d",&T[i]); 8 | for(sum = i = 0; i < ts; i++) sum += T[i]; 9 | for(i = 0, j = ts; j <= n; i++, j++){ 10 | int mean = sum/ts; 11 | if(mean < tmin) tmin = mean; 12 | if(mean > tmax) tmax = mean; 13 | if(j == n) break; 14 | sum = sum + T[j] - T[i]; 15 | } 16 | printf("Teste %d\n%d %d\n\n",t,tmin,tmax); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /spoj/obi/VIVO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | vector < int > v; 5 | vector < int >::iterator it; 6 | int main(void){ 7 | int i, j, mov, N, ord, P, R; 8 | for(int t = 1; scanf("%d%d",&P,&R) && P; t++){ 9 | v.clear(); 10 | for(i = 0; i < P; i++) scanf("%d",&j), v.push_back(j); 11 | for(i = 0; i < R; i++){ 12 | scanf("%d%d",&N,&ord); 13 | for(it = v.begin(); it != v.end(); it++){ 14 | scanf("%d",&mov); 15 | if(mov != ord) v.erase(it--); 16 | } 17 | } 18 | printf("Teste %d\n%d\n\n",t,*v.begin()); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /la/4216.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int p[1024]; 4 | int main(void){ 5 | bool poss; 6 | int dpos, i, n, pos, who; 7 | while(scanf("%d",&n) && n){ 8 | memset(p,0,n<<2); 9 | poss = 1; 10 | for(i = 0; i < n; i++){ 11 | scanf("%d%d",&who,&dpos); 12 | pos = i + dpos; 13 | if(pos < 0 || pos >= n || p[i+dpos]) poss = 0; 14 | if(poss) p[pos] = who; 15 | } 16 | if(poss){ 17 | for(i = 0; i < n; i++){ 18 | if(i) putchar(' '); 19 | printf("%d",p[i]); 20 | } 21 | putchar('\n'); 22 | } 23 | else printf("-1\n"); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /spoj/classical/PARTY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int INF = 4096; 3 | int dp[2][1024]; 4 | int main(void){ 5 | for(int b, *c, f, m, *o, p, w; scanf("%d %d", &b, &p) == 2 && (b || p); ){ 6 | dp[0][0] = m = 0; 7 | for(int i = 1; i <= p; ++i){ 8 | c = dp[i&1], o = dp[!(i&1)]; 9 | scanf("%d %d", &w, &f); 10 | m += f; 11 | for(int j = 0; j <= m; ++j){ 12 | c[j] = j > m - f ? INF : o[j]; 13 | c[j] = j < f || o[j - f] + w > c[j] ? c[j] : o[j - f] + w; 14 | } 15 | } 16 | for(f = m; c[f] > b; f--); 17 | printf("%d %d\n", c[f], f); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume CXIII/11398.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int masks[32], keep[2]; 5 | char word[32]; 6 | int main(void){ 7 | masks[0] = 0; 8 | keep[0] = 0; keep[1] = 0xffffffff; 9 | for(int i = 1; i < 32; ++i) masks[i] = 1 + (masks[i - 1] << 1); 10 | while(scanf("%s", word) == 1 && word[0] != '~'){ 11 | int L, v = 0; bool flag = 0; 12 | while(word[0] != '#'){ 13 | L = strlen(word) - 2; 14 | if(L > 0) v = (v << L) + (masks[L] & keep[flag]); 15 | else flag = L & 1; 16 | scanf("%s", word); 17 | } 18 | printf("%d\n", v); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /uva/Volume CXV/11541.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11541 - Decoding 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | char line[500]; 7 | char curr; 8 | int c,i,j,n, cnumb = 1; 9 | int main(void){ 10 | scanf("%d\n",&c); 11 | while(c){ 12 | c--; 13 | gets(line); 14 | printf("Case %d: ",cnumb); 15 | for(i = 0; line[i] != '\0'; ){ 16 | curr = line[i++]; 17 | n = line[i++]-'0'; 18 | while(isdigit(line[i])) n = 10*n + line[i++] - '0'; 19 | for(j = 0; j < n; j++) putchar(curr); 20 | } 21 | putchar('\n'); 22 | cnumb++; 23 | } 24 | } -------------------------------------------------------------------------------- /uva/Volume IV/00483.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00483 - Word Scramble 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | char stack[100]; 7 | int top=-1; 8 | 9 | void push(char c){ 10 | stack[++top]=c; 11 | } 12 | 13 | char pop(){ 14 | return stack[top--]; 15 | } 16 | 17 | 18 | int main(void){ 19 | char ch; 20 | while((ch=getchar())!=EOF){ 21 | if(isspace(ch)){ 22 | while(top > -1)putchar(pop()); 23 | putchar(ch); 24 | 25 | } 26 | else push(ch); 27 | } 28 | while(top > -1) 29 | putchar(pop()); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /spoj/seletiva/DVORONOI.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int i, t, x[4],y[4],z[4]; 4 | double misto_quente; 5 | int main(void){ 6 | scanf("%d",&t); 7 | while(t--){ 8 | scanf("%d%d%d%d%d%d%d%d%d%d%d%d",&x[0],&y[0],&z[0], 9 | &x[1],&y[1],&z[1],&x[2],&y[2],&z[2],&x[3],&y[3],&z[3]); 10 | for(i = 0; i < 3; i++){ 11 | x[i] -= x[3]; y[i] -= y[3]; z[i] -= z[3]; 12 | } 13 | misto_quente = ( 14 | (x[0]*y[1]*z[2]+y[0]*z[1]*x[2]+z[0]*x[1]*y[2])- 15 | (z[0]*y[1]*x[2]+x[0]*z[1]*y[2]+y[0]*x[1]*z[2]) 16 | )/6.000000; 17 | printf("%.6lf\n",fabs(misto_quente)); 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /spoj/sulamericana/LOTERIA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int oc[101]; 4 | int main(void){ 5 | int c, i, j, idx, k, min, n, v; 6 | while(scanf("%d%d%d",&n,&c,&k) && n){ 7 | memset(oc,0,(k+1)<<2); 8 | for(i = 0; i < n; i++) 9 | for(j = 0; j < c; j++) 10 | scanf("%d",&v), oc[v]++; 11 | min = 1<<14; 12 | for(i = 1; i <= k; i++) 13 | if(min > oc[i]) min = oc[i], idx = i; 14 | bool first = 1; 15 | for(i = idx; i <= k; i++) 16 | if(first) printf("%d",idx), first = 0; 17 | else if(oc[i] == min) printf(" %d",i); 18 | putchar('\n'); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /sulamericana/2006/lottery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int oc[101]; 4 | int main(void){ 5 | int c, i, j, idx, k, min, n, v; 6 | while(scanf("%d%d%d",&n,&c,&k) && n){ 7 | memset(oc,0,(k+1)<<2); 8 | for(i = 0; i < n; i++) 9 | for(j = 0; j < c; j++) 10 | scanf("%d",&v), oc[v]++; 11 | min = 1<<14; 12 | for(i = 1; i <= k; i++) 13 | if(min > oc[i]) min = oc[i], idx = i; 14 | bool first = 1; 15 | for(i = idx; i <= k; i++) 16 | if(first) printf("%d",idx), first = 0; 17 | else if(oc[i] == min) printf(" %d",i); 18 | putchar('\n'); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /uva/Volume CXIV/11461.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11461 - Square Numbers 3 | ///////////////////////////////// 4 | #include 5 | #define MAX 318 6 | int squares[318]; 7 | int main(void){ 8 | int i,j, sq = 0, prev_square, a,b,count; 9 | for(i = 1; i < MAX;i++) squares[i] = i*i; 10 | while(scanf("%d %d",&a,&b)){ 11 | if(a == b && b == 0) break; 12 | count = 0; 13 | for(i = 0; squares[i] < a; i++); 14 | for(;squares[i] <= b; count++,i++); 15 | printf("%d\n",count); 16 | } 17 | //for(i = 1; i < MAX; i++) printf("ate %d = %d\n",i,squaresuntil[i]); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /codeforces/031A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int got[2048][2]; 3 | int v[2048]; 4 | int main(void){ 5 | for(int i = 0; i < 2048; ++i) got[i][0] = got[i][1] = -1; 6 | int n; scanf("%d", &n); 7 | for(int i = 0; i < n; ++i){ 8 | scanf("%d", &v[i]); 9 | for(int j = 0; j < i; ++j){ 10 | int sum = v[i] + v[j]; 11 | got[sum][0] = i + 1; 12 | got[sum][1] = j + 1; 13 | } 14 | } 15 | int i; 16 | for(i = 0; i < n; ++i) 17 | if(got[v[i]][0] != got[v[i]][1]){ 18 | printf("%d %d %d\n", 1+i, got[v[i]][0], got[v[i]][1]); 19 | break; 20 | } 21 | if(i == n) puts("-1"); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /tc/SRM412/ForbiddenStrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | long long a[32], b[32]; 11 | class ForbiddenStrings { 12 | public: 13 | long long countNotForbidden( int n ) { 14 | a[0] = 1; 15 | a[1] = 3; 16 | a[2] = 6; 17 | b[1] = 0; 18 | b[2] = 3; 19 | for(int i = 3; i <= n; ++i){ 20 | a[i] = (b[i - 1] << 1) + a[i - 1]; 21 | b[i] = b[i - 1] + a[i - 1]; 22 | } 23 | return a[n] + b[n]; 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /uva/Volume CX/11068.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int a, b, c, d, e, f; 5 | int den, n1, n2; 6 | double x, y; 7 | while(scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f)){ 8 | if(!(a || b || c || d || e || f)) break; 9 | den = a * e - b * d; 10 | if(den){ 11 | n1 = c * e - b * f; 12 | n2 = a * f - c * d; 13 | if(n1) x = n1 / (double) den; 14 | else x = 0.0; 15 | if(n2) y = n2 / (double) den; 16 | else y = 0.0; 17 | printf("The fixed point is at %.2lf %.2lf.\n", x, y); 18 | } else printf("No fixed point exists.\n"); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /uva/Volume CXVII/11713.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char *o[2] = {"No", "Yes"}; 4 | char a[32], b[32]; 5 | int na, nb; 6 | 7 | bool vowel[128]; 8 | int main(void){ 9 | int t; 10 | for(int i = 0; i < 128; ++i) vowel[i] = 0; 11 | vowel['a'] = vowel['e'] = vowel['i'] = vowel['o'] = vowel['u'] = 1; 12 | scanf("%d", &t); 13 | while(t--){ 14 | scanf("%s%n", a, &na); scanf("%s%n", b, &nb); 15 | bool mutation = na == nb; 16 | for(char *p = a, *q = b; *p && mutation; ++p, ++q) 17 | mutation &= *p == *q || (vowel[*p] && vowel[*q]); 18 | puts(o[mutation]); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /uva/Volume IV/00499.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00499 - What's The Frequency, Kenneth? 3 | ///////////////////////////////// 4 | #include 5 | char s[1000]; 6 | int fmax,freq[127],i; 7 | int main(void){ 8 | while(gets(s)){ 9 | for(fmax = 0,i = 0; i < 127; freq[i] = 0,i++); 10 | for(i = 0; s[i] != '\0'; i++){ 11 | if(s[i] < 65 || s[i] > 122 || (s[i] > 90 && s[i] < 97)) continue; 12 | freq[s[i]]++; 13 | if(freq[s[i]] > fmax) fmax++; 14 | } 15 | for(i = 0; i < 127; i++) 16 | if(freq[i] == fmax) putchar(i); 17 | printf(" %d\n",fmax); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /euler/e072.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int MAX = 1000001; 5 | int phi[MAX]; 6 | int main(void){ 7 | clock_t ini = clock(); 8 | long long ans = 0; 9 | for(int i = 2; i < MAX; ++i) phi[i] = i >> (!(i & 1)); 10 | for(int i = 3; i < MAX; i += 2){ 11 | if(phi[i] == i){ 12 | for(int j = i; j < MAX; j += i){ 13 | phi[j] /= i; 14 | phi[j] *= (i - 1); 15 | } 16 | } 17 | ans += phi[i] + phi[i - 1]; 18 | } 19 | printf("Time spent: %.3lfs\n", ((double)(clock() - ini))/CLOCKS_PER_SEC); 20 | printf("Answer: %lld\n", ans); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /seletiva/IME-USP2008-2/voronoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int i, t, x[4],y[4],z[4]; 4 | double misto_quente; 5 | int main(void){ 6 | scanf("%d",&t); 7 | while(t--){ 8 | scanf("%d%d%d%d%d%d%d%d%d%d%d%d",&x[0],&y[0],&z[0], 9 | &x[1],&y[1],&z[1],&x[2],&y[2],&z[2],&x[3],&y[3],&z[3]); 10 | for(i = 0; i < 3; i++){ 11 | x[i] -= x[3]; y[i] -= y[3]; z[i] -= z[3]; 12 | } 13 | misto_quente = ( 14 | (x[0]*y[1]*z[2]+y[0]*z[1]*x[2]+z[0]*x[1]*y[2])- 15 | (z[0]*y[1]*x[2]+x[0]*z[1]*y[2]+y[0]*x[1]*z[2]) 16 | )/6.000000; 17 | printf("%.6lf\n",fabs(misto_quente)); 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /spoj/sulamericana/POLEPOS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int p[1024]; 4 | int main(void){ 5 | bool poss; 6 | int dpos, i, n, pos, who; 7 | while(scanf("%d",&n) && n){ 8 | memset(p,0,n<<2); 9 | poss = 1; 10 | for(i = 0; i < n; i++){ 11 | scanf("%d%d",&who,&dpos); 12 | pos = i + dpos; 13 | if(pos < 0 || pos >= n || p[i+dpos]) poss = 0; 14 | if(poss) p[pos] = who; 15 | } 16 | if(poss){ 17 | for(i = 0; i < n; i++){ 18 | if(i) putchar(' '); 19 | printf("%d",p[i]); 20 | } 21 | putchar('\n'); 22 | } 23 | else printf("-1\n"); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /tc/SRM340/ProblemsToSolve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class ProblemsToSolve { 11 | public: 12 | int minNumber( vector pleasantness, int variety ) { 13 | int ans = pleasantness.size(), n = ans; 14 | for(int i = 0; i < n; ++i) 15 | for(int j = i + 1; j < n; ++j) 16 | if(abs(pleasantness[j] - pleasantness[i]) >= variety) 17 | ans = min(ans, 1 + ((i + 1) >> 1) + ((j - i + 1) >> 1)); 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /tc/SRM495/CarrotBoxesEasy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | class CarrotBoxesEasy { 12 | public: 13 | int theIndex( vector carrots, int K ) { 14 | int n = carrots.size(); 15 | while(1){ 16 | int idx = -1, max = 0; 17 | for(int i = 0; i < n; ++i) 18 | if(carrots[i] > max){ 19 | max = carrots[i]; 20 | idx = i; 21 | } 22 | carrots[idx]--; 23 | if(--K == 0) return idx; 24 | } 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /uva/Volume C/10019.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10019 - Funny Encryption Method 3 | ///////////////////////////////// 4 | #include 5 | int c,b1,b2,n; 6 | int howmanyones[] = {0,1,1,2,1,2,2,3,1,2}; 7 | void findb1(){ 8 | for(b1 = 0; n!=0; n = n>>1) 9 | b1 += n%2; 10 | } 11 | void findb2(int k){ 12 | int temp = k; 13 | for(b2 = 0; temp != 0; temp /= 10) 14 | b2+= howmanyones[temp%10]; 15 | } 16 | int main(void){ 17 | scanf("%d",&c); 18 | while(c){ 19 | scanf("%d",&n); 20 | findb2(n); 21 | findb1(); 22 | printf("%d %d\n",b1,b2); 23 | c--; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /uva/Volume C/10050.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10050 - Hartals 3 | ///////////////////////////////// 4 | #include 5 | bool h[3655]; 6 | int cases,hp,hi,i,j,N,hartals; 7 | int main(void){ 8 | scanf("%d",&cases); 9 | while(cases){ 10 | cases--; 11 | scanf("%d",&N); 12 | N++; 13 | scanf("%d",&hp); 14 | for(i = 0; i < N; h[i] = 0,i++); 15 | for(hartals = i = 0; i < hp; i++){ 16 | scanf("%d",&hi); 17 | for(j = hi; j < N; j+=hi) 18 | if(!h[j] && j%7 != 6 && j%7){ 19 | h[j] = 1; 20 | hartals++; 21 | } 22 | } 23 | printf("%d\n",hartals); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /uva/Volume CIII/10370.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 10370 - Above Average 3 | ///////////////////////////////// 4 | #include 5 | #define MAX 1000 6 | int grades[MAX]; 7 | float m,n,above; 8 | float ans; 9 | int c,i; 10 | int main(void){ 11 | scanf("%d",&c); 12 | while(c){ 13 | c--; 14 | m = above = 0.0; 15 | scanf("%f",&n); 16 | for(i = 0; i < n; scanf("%d",&grades[i]),m+=grades[i],i++); 17 | m/=n; 18 | //printf("m = %.3f\n",m); 19 | for(i = 0; i < n; i++) if(grades[i] > m) above++; 20 | ans = 100*above/n; 21 | printf("%.3f%\n",ans); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /uva/Volume CXII/11207.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 11207 - The easiest way 3 | ///////////////////////////////// 4 | #include 5 | #define MAX(a,b) (a>b?a:b) 6 | unsigned int i,n,paper,x,y; 7 | float side, biggest; 8 | int main(void){ 9 | while(scanf("%u\n",&n) && n){ 10 | biggest = 0; 11 | for(i = 0; i != n ; i++){ 12 | scanf("%u %u\n",&x,&y); 13 | if(x>y) x ^= y ^= x ^= y; 14 | if(y >= x*4) side = x; 15 | else side = MAX(y/4.00,x/2.00); 16 | if(side > biggest) biggest = side, paper = i+1; 17 | } 18 | printf("%u\n",paper); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /uva/Volume CXVIII/11889.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int factors(int c, int a){ 4 | int k = 1, r = 1; 5 | while(!(c & 1)) c >>= 1, k <<= 1; 6 | if(a % k) r = k; 7 | for(int i = 3; i * i <= c; i += 2){ 8 | if(c % i) continue; 9 | k = 1; 10 | while(!(c % i)) c /= i, k *= i; 11 | if(a % k) r *= k; 12 | } 13 | if(c > 1 && (a % c)) r *= c; 14 | return r; 15 | } 16 | 17 | int main(void){ 18 | int t; scanf("%d", &t); 19 | while(t--){ 20 | int a, c; 21 | scanf("%d %d", &a, &c); 22 | if(c % a) puts("NO SOLUTION"); 23 | else printf("%d\n", factors(c, a)); 24 | } 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /uva/Volume VI/00621.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////// 2 | // 00621 - Secret Research 3 | ///////////////////////////////// 4 | #include 5 | #include 6 | char line[1500]; 7 | unsigned int cnum,len; 8 | const char *pos = "+",*neg = "-",*fail = "*", *inc = "?"; 9 | int main(void){ 10 | scanf("%u\n",&cnum); 11 | while(cnum--){ 12 | gets(line); 13 | len = strlen(line); 14 | if(len < 3) puts(pos); 15 | else if(line[0] == '9' && line[len-1] == '4') puts(fail); 16 | else if(line[len-1] == '5' && line[len-2] == '3') puts(neg); 17 | else puts(inc); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /uva/Volume VI/00644.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char nnn[1000][1000]; 4 | 5 | bool match(const char *a, const char *b){ 6 | while(*a && *b) if(*a++ != *b++) return 0; 7 | return 1; 8 | } 9 | 10 | int main(void){ 11 | int snum = 0; 12 | while(scanf("%s",nnn[0])==1){ 13 | int k = 1; 14 | bool d = 1; 15 | while(scanf("%s",nnn[k]) && nnn[k][0] != '9'){ 16 | for(int i = 0; d && i < k; ++i) 17 | if(match(nnn[i],nnn[k])) d = 0; 18 | k++; 19 | } 20 | printf("Set %d is ",++snum); 21 | if(!d) fputs("not ",stdout); 22 | puts("immediately decodable"); 23 | } 24 | return 0; 25 | } 26 | 27 | --------------------------------------------------------------------------------