├── leetcode ├── .gitignore ├── Cargo.toml ├── Cargo.lock └── src │ └── bin │ └── 2384.rs ├── .gitignore ├── gcj ├── gcj 2010 Africa │ └── Qualification Round │ │ ├── B │ │ ├── B-small-practice.in │ │ └── B-small-practice.out │ │ └── A │ │ ├── A-small-practice.out │ │ └── A-large-practice.out └── gcj 2008 │ └── Round 1A │ └── A │ └── A-large-practice.out ├── project-euler ├── overview │ ├── 002_overview.pdf │ └── 005_overview.pdf ├── 0018.txt ├── 0001.cpp ├── 0002.cpp └── 0005.cpp ├── UVa ├── 012461.cpp ├── 012952.cpp ├── 010499.cpp ├── 000113.cpp ├── 012917.cpp ├── 013178.cpp ├── 012708.cpp ├── 011805.cpp ├── 012992.cpp ├── 013187.cpp ├── 011044.cpp ├── 000900.cpp ├── 010550.cpp ├── 000651.cpp ├── 010222.cpp ├── 010784.cpp ├── 000272.cpp ├── 010056.cpp ├── 010170.cpp ├── 013034.cpp ├── 010491.cpp ├── 013130.cpp ├── 010219.cpp ├── 000408.cpp ├── 012908.cpp ├── 010106.java ├── 010107.cpp ├── 012578.cpp ├── 011054.cpp ├── 011310.cpp ├── 000991.cpp ├── 010450.cpp ├── 011461.cpp ├── 012279.cpp ├── 010223.cpp ├── 011115.java ├── 010368.cpp ├── 012243.cpp ├── 000424.java ├── 012024.cpp ├── 001230.java ├── 010494.java ├── 010551.java ├── 010579.java ├── 011879.java ├── 011830.java ├── 011247.cpp ├── 010940.cpp ├── 010910-2.cpp ├── 013131.cpp ├── 000536.cpp ├── 010814.java ├── 000580.cpp ├── 010994.cpp ├── 010295.cpp ├── 013148.cpp ├── 012946.cpp ├── 000623.java ├── 013025.cpp ├── 000748.java ├── 010104.cpp ├── 010281.cpp ├── 010773.cpp ├── 010963.cpp ├── 012854.cpp ├── 000389.java ├── 010063.cpp ├── 012955.cpp ├── 010763.cpp ├── 000713.java ├── 011389.cpp ├── 011962.cpp ├── 011679.cpp ├── 010252.cpp ├── 010473.java ├── 012959.cpp ├── 012592.cpp ├── 000495.java ├── 011462.cpp ├── 011496.cpp ├── 012554.cpp ├── 013018.cpp ├── 012930.java ├── 010924.cpp ├── 012527.cpp ├── 000105.cpp ├── 010523.java ├── 000278.cpp ├── 010910.cpp ├── 011991.cpp ├── 012157.cpp ├── 001583.cpp ├── 000673.cpp ├── 011371.cpp ├── 010739.cpp ├── 011219.cpp ├── 000412.cpp ├── 010324.cpp ├── 000350.cpp ├── 000355.java ├── 010190.cpp ├── 011917.cpp ├── 011291.cpp ├── 012049.cpp ├── 000414.cpp ├── 010925.java ├── 012996.cpp ├── 000257.cpp ├── 012970.cpp ├── 011282.cpp ├── 013171.cpp ├── 012459.java ├── 000263.cpp ├── 000417.cpp ├── 000834.cpp ├── 011753.cpp ├── 012575.cpp ├── 000443.cpp ├── 011549.cpp ├── 011776.cpp ├── 001644.cpp ├── 000138.cpp ├── 010394.cpp ├── 011362.cpp ├── 000686.cpp ├── 001210.cpp ├── 011059.cpp ├── 011961.cpp ├── 001590.cpp ├── 010928.cpp ├── 011063.cpp ├── 011628.cpp ├── 012541.cpp ├── 001727.cpp ├── 010408.cpp ├── 000661.cpp ├── 011068.cpp ├── 010071.cpp ├── 011121.cpp ├── 012700.cpp ├── 011151.cpp ├── 000371.cpp ├── 010055.cpp ├── 011062.cpp ├── 010141.cpp ├── 011629.cpp ├── 001239.cpp ├── 000260.cpp ├── 010183.java ├── 000458.cpp ├── 000755.cpp ├── 010374.cpp ├── 010079.cpp ├── 001757.cpp ├── 010699.cpp ├── 011332.cpp ├── 012470.cpp ├── 010533.cpp ├── 011181.cpp ├── 012160.cpp ├── 000406.cpp ├── 010703.cpp ├── 000884.cpp ├── 001112.cpp ├── 010300.cpp ├── 000465.java ├── 000496.cpp ├── 010127.cpp ├── 011239.cpp ├── 000352.cpp ├── 010346.cpp ├── 012403.cpp ├── 000535.cpp ├── 012704.cpp ├── 012614.cpp ├── 012832.cpp ├── 000579.cpp ├── 010029.cpp ├── 012398.cpp ├── 012658.cpp ├── 010759.cpp ├── 011172.cpp ├── 010696.cpp ├── 013012.cpp ├── 011955.cpp ├── 000200.cpp ├── 010340.cpp ├── 013185.cpp ├── 013194.cpp ├── 000483.cpp ├── 011799.cpp ├── 000306.cpp ├── 000494.cpp ├── 011727.cpp ├── 000152.cpp ├── 010812.cpp ├── 012289.cpp ├── 010110.cpp ├── 011876.cpp ├── 013071.cpp ├── 000100.cpp ├── 000136.cpp ├── 001124.cpp ├── 012149.cpp ├── 000439.cpp ├── 000294.cpp ├── 010235.cpp ├── 010469.cpp └── 012405.cpp ├── acepta-el-reto ├── 0371.cpp ├── 0217.cpp ├── 0350.cpp ├── 0120.cpp ├── 0155.cpp ├── 0369.cpp ├── 0373.cpp ├── 0335.cpp ├── 0368.cpp ├── 0170.cpp ├── 0355.cpp ├── 0370.cpp ├── 0219.cpp ├── 0372.cpp ├── 0112.cpp ├── 0337.cpp ├── 0304.cpp ├── 0111.cpp ├── 0374.cpp ├── 0376.cpp ├── 0375.cpp ├── 0119.cpp ├── 0377.java ├── 0379.cpp ├── 0122.cpp ├── 0104.cpp ├── 0378.cpp ├── 0207.cpp ├── 0129.cpp ├── 0107.cpp ├── 0313.cpp └── 0321.cpp └── codeforces ├── 810A.cpp ├── 825A.cpp ├── 825C.cpp ├── 813A.cpp └── 825B.cpp /leetcode/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | *.zip 4 | CMakeLists.txt 5 | cmake-build-debug 6 | Makefile -------------------------------------------------------------------------------- /gcj/gcj 2010 Africa/Qualification Round/B/B-small-practice.in: -------------------------------------------------------------------------------- 1 | 5 2 | this is a test 3 | foobar 4 | all your base 5 | class 6 | pony along 7 | -------------------------------------------------------------------------------- /leetcode/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "leetcode" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | [dependencies] 7 | itertools = "=0.14" 8 | -------------------------------------------------------------------------------- /project-euler/overview/002_overview.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/david-perez/competitive-programming/HEAD/project-euler/overview/002_overview.pdf -------------------------------------------------------------------------------- /project-euler/overview/005_overview.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/david-perez/competitive-programming/HEAD/project-euler/overview/005_overview.pdf -------------------------------------------------------------------------------- /gcj/gcj 2010 Africa/Qualification Round/B/B-small-practice.out: -------------------------------------------------------------------------------- 1 | Case #1: test a is this 2 | Case #2: foobar 3 | Case #3: base your all 4 | Case #4: class 5 | Case #5: along pony 6 | -------------------------------------------------------------------------------- /UVa/012461.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | while (cin >> n && n != 0) 8 | cout << "1/2" << endl; 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /UVa/012952.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int A, B; 7 | while (cin >> A >> B) 8 | cout << (A == B ? A : max(A, B)) << endl; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /acepta-el-reto/0371.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | while (cin >> n && n != 0) 8 | cout << n*(n+1) / 2 * 3 << endl; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /UVa/010499.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | ll N; 9 | while (cin >> N && N > 0) 10 | cout << (N == 1 ? 0 : N*25) << '%' << endl; 11 | } -------------------------------------------------------------------------------- /UVa/000113.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | double n, p; 8 | while (cin >> n >> p) 9 | printf("%.0lf\n", pow(p, 1/n)); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /acepta-el-reto/0217.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | while (cin >> n && n != 0) 8 | cout << (n % 2 == 1 ? "IZQUIERDA" : "DERECHA") << endl; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /acepta-el-reto/0350.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b; 7 | while (cin >> a >> b && !(a == 0 && b == 0)) 8 | printf("%.1f\n", a * (double) b/2); 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /UVa/012917.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int P, H, O; 7 | while (cin >> P >> H >> O) 8 | cout << (H > O - P ? "Hunters win!" : "Props win!") << endl; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /gcj/gcj 2010 Africa/Qualification Round/A/A-small-practice.out: -------------------------------------------------------------------------------- 1 | Case #1: 2 3 2 | Case #2: 1 4 3 | Case #3: 4 5 4 | Case #4: 29 46 5 | Case #5: 11 56 6 | Case #6: 4 5 7 | Case #7: 40 46 8 | Case #8: 16 35 9 | Case #9: 55 74 10 | Case #10: 7 9 11 | -------------------------------------------------------------------------------- /acepta-el-reto/0120.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n, k; 7 | while (cin >> n >> k && !(n == 0 && k == 0)) 8 | cout << (n*n*n + n) / 2 + (k-1) * n << endl; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /acepta-el-reto/0155.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int a, b; 8 | while (cin >> a >> b && !(a < 0 || b < 0)) 9 | cout << 2*(a+b) << endl; 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /UVa/013178.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin >> t; 8 | while (t--) { 9 | int n; 10 | cin >> n; 11 | cout << (n % 3 == 1 ? "NO" : "YES") << endl; 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /acepta-el-reto/0369.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int N; 7 | while (cin >> N && N != 0) { 8 | for (int i = 0; i < N; i++) 9 | cout << 1; 10 | cout << endl; 11 | } 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /UVa/012708.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | int T; 9 | cin >> T; 10 | while (T--) { 11 | ll N; 12 | cin >> N; 13 | cout << N/2 << endl; 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /UVa/011805.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | for (int t = 1; t <= T; t++) { 9 | int N, K, P; 10 | cin >> N >> K >> P; 11 | 12 | printf("Case %d: %d\n", t, ((K-1+P) % N) + 1); 13 | } 14 | } -------------------------------------------------------------------------------- /UVa/012992.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | for (int t = 1; t <= T; t++) { 9 | int N; 10 | cin >> N; 11 | printf("Case #%d: %d\n", t, 2*(N-1) + 1); 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /UVa/013187.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | ll S; 12 | cin >> S; 13 | cout << S*(S+2) << endl; 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /acepta-el-reto/0373.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | int T; 9 | cin >> T; 10 | while (T--) { 11 | ll N; 12 | cin >> N; 13 | cout << 6*N*N - 12*N + 8 << endl; 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /acepta-el-reto/0335.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | int T; 9 | cin >> T; 10 | while (T--) { 11 | ll n; 12 | cin >> n; 13 | cout << n*(n+1)*(n+2)/6 << endl; 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /acepta-el-reto/0368.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int h, c; 7 | while (cin >> h >> c && !(h == 0 && c == 0)) { 8 | int d = h / c; 9 | if (h % c > 0) 10 | d++; 11 | cout << 10*d << endl; 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /gcj/gcj 2008/Round 1A/A/A-large-practice.out: -------------------------------------------------------------------------------- 1 | Case #1: -7839202227936 2 | Case #2: 7999201712083 3 | Case #3: -1313429236847 4 | Case #4: -3710387739618 5 | Case #5: -3414920765916 6 | Case #6: -1271937742993 7 | Case #7: -1964394407029 8 | Case #8: -1884282427866 9 | Case #9: -4044533757860 10 | Case #10: -838783451371 11 | -------------------------------------------------------------------------------- /acepta-el-reto/0170.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | double Tn; 8 | while (cin >> Tn && Tn != 0) { 9 | int n = (-1 + sqrt(1 + 8*Tn)) / 2; 10 | printf("%d %d\n", n, (int) Tn - n*(n+1) / 2); 11 | } 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /acepta-el-reto/0355.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | int N; 10 | cin >> N; 11 | if ((N % 4 == 0 && N % 100 != 0) || (N % 100 == 0 && N % 400 == 0)) cout << 29 << endl; 12 | else cout << 28 << endl; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /UVa/011044.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, m; 11 | cin >> n >> m; 12 | cout << (((n-2)/3) + ((n-2)%3 == 0 ? 0 : 1)) * (((m-2)/3) + ((m-2)%3 == 0 ? 0 : 1)) << endl; 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /acepta-el-reto/0370.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; cin.get(); 8 | while (T--) { 9 | int a, b; 10 | scanf("%d-%d", &a, &b); 11 | int x = min(a, b), y = max(a, b); 12 | cout << (x % 2 == 0 && x+1 == y ? "SI" : "NO") << endl; 13 | } 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /UVa/000900.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | ll f[51]; 9 | f[1] = 1; 10 | f[2] = 2; 11 | for (int n = 3; n <= 50; n++) 12 | f[n] = f[n-1] + f[n-2]; 13 | 14 | int n; 15 | while (cin >> n && n != 0) 16 | cout << f[n] << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /UVa/010550.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int dist(int u, int v) { 6 | return (u - v + 40) % 40; 7 | } 8 | 9 | int main() { 10 | int p, x, y, z; 11 | while (cin >> p >> x >> y >> z && !(p == 0 && x == 0 && y == 0 && z == 0)) 12 | cout << 9 * (120 + dist(p, x) + dist(y, x) + dist(y, z)) << endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /UVa/000651.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | double sol[100000]; 7 | sol[1] = 1; 8 | for (int n = 2; n < 100000; n++) sol[n] = sol[n-1] + 1.0/n; 9 | 10 | cout << "# Cards Overhang" << endl; 11 | int n; 12 | while (cin >> n) 13 | printf("%*d %.3f\n", 5, n, 0.5 * sol[n]); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /UVa/010222.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | string s = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"; 7 | 8 | char c; 9 | while (scanf("%c", &c) != EOF) { 10 | if (isalpha(c)) c = tolower(c); 11 | if (isspace(c)) cout << c; 12 | else cout << s[s.find(c) - 2]; 13 | } 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /UVa/010784.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | 8 | int main() { 9 | ll N; 10 | for (int t = 1; cin >> N && N != 0; t++) { 11 | ll n = (3 + sqrt(9 + 8*N)) / 2; 12 | if (N != (n*(n-3)) / 2) 13 | n++; 14 | 15 | printf("Case %d: %lld\n", t, n); 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /UVa/000272.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | string s; 7 | bool b = false; 8 | while (getline(cin, s)) { 9 | for (char &c : s) 10 | if (c == '"') { 11 | cout << (b ? "''" : "``"); 12 | b = !b; 13 | } else cout << c; 14 | cout << endl; 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /UVa/010056.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int S; 8 | cin >> S; 9 | while (S--) { 10 | int N, I; 11 | double p; 12 | cin >> N >> p >> I; 13 | if (p == 0) cout << "0.0000" << endl; 14 | else printf("%.4f\n", p * pow(1-p, I-1) / (1 - pow(1-p, N))); 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /UVa/010170.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | 8 | int main() { 9 | ll S, D; 10 | while (cin >> S >> D) { 11 | ll t = (int) (-S + 0.5 * (sqrt(4*S*S + 8*D - 4*S + 1) -1)); 12 | if (D == (t+1)*S + (t*(t+1))/2) cout << S + t << endl; 13 | else cout << S + t + 1 << endl; 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /UVa/013034.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int S; 7 | cin >> S; 8 | for (int t = 1; t <= S; t++) { 9 | bool b = true; 10 | for (int i = 0; i < 13; i++) { 11 | int x; 12 | cin >> x; 13 | if (x == 0) b = false; 14 | } 15 | 16 | printf("Set #%d: %s\n", t, b ? "Yes" : "No"); 17 | } 18 | } -------------------------------------------------------------------------------- /UVa/010491.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int cows, cars, show; 7 | while (cin >> cows >> cars >> show) { 8 | int N = cows + cars; 9 | double pcar = (double) cars / N, pcow = (double) cows / N; 10 | printf("%.5f\n", pcar * (((double) cars-1) / (N-show-1)) + pcow * (double) cars / (N-show-1)); 11 | } 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /UVa/013130.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | int a[5]; 9 | while (T--) { 10 | for (int i = 0; i < 5; i++) 11 | cin >> a[i]; 12 | 13 | bool scala = true; 14 | for (int i = 0; i < 4; i++) 15 | scala &= a[i] + 1 == a[i+1]; 16 | 17 | cout << (scala ? 'Y' : 'N') << endl; 18 | } 19 | } -------------------------------------------------------------------------------- /codeforces/810A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n, k; 8 | cin >> n >> k; 9 | int s = 0; 10 | for (int i = 0; i < n; i++) { 11 | int ai; 12 | cin >> ai; 13 | s += ai; 14 | } 15 | 16 | double m = (n*(k - 0.5) - s) / (0.5); 17 | cout << max(0, (int) ceil(m)) << endl; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /UVa/010219.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | double log10(double n) { 7 | return log(n) / log(10); 8 | } 9 | 10 | int main() { 11 | long long n, k; 12 | while (cin >> n >> k) { 13 | double d = 0; 14 | for (int i = 0; i < k; i++) 15 | d += log10(n-i) - log10(i+1); 16 | 17 | cout << (int) floor(d) + 1 << endl; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /acepta-el-reto/0219.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | int n; 10 | cin >> n; 11 | int evens = 0; 12 | while (n--) { 13 | int x; 14 | cin >> x; 15 | if (x % 2 == 0) evens++; 16 | } 17 | 18 | cout << evens << endl; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /UVa/000408.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int gcd(int a, int b) { 6 | while (b) { 7 | int t = b; 8 | b = a % b; 9 | a = t; 10 | } 11 | return a; 12 | } 13 | 14 | int main() { 15 | int STEP, MOD; 16 | while (cin >> STEP >> MOD) 17 | printf("%10d%10d %s\n\n", STEP, MOD, gcd(STEP, MOD) == 1 ? "Good Choice" : "Bad Choice"); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /UVa/012908.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int s; 8 | while (cin >> s && s != 0) { 9 | int n = (int) (-1 + sqrt(1 + 8*s)) / 2; 10 | if (n*(n+1) == 2*s) 11 | printf("%d %d\n", n+1, n+1); 12 | else { 13 | n++; 14 | printf("%d %d\n", n*(n+1)/2 - s, n); 15 | } 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /codeforces/825A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n, o = 0, m = 0; 7 | cin >> n; cin.get(); 8 | for (int i = 0; i < n; i++) { 9 | char c; 10 | cin >> c; 11 | if (c == '1') m++; 12 | else { 13 | o = 10*o + m; 14 | m = 0; 15 | } 16 | } 17 | 18 | cout << 10*o + m << endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /UVa/010106.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | 9 | while (in.hasNextBigInteger()) { 10 | BigInteger a = in.nextBigInteger(); 11 | BigInteger b = in.nextBigInteger(); 12 | System.out.println(a.multiply(b)); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /UVa/010107.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef vector vi; 8 | 9 | int main() { 10 | vi v; 11 | int x; 12 | while (cin >> x) { 13 | v.push_back(x); 14 | sort(v.begin(), v.end()); 15 | cout << (v.size() % 2 == 0 ? (v[v.size()/2 - 1] + v[v.size()/2]) / 2 : v[v.size()/2]) << endl; 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /UVa/012578.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // abs, atan... 3 | 4 | using namespace std; 5 | 6 | #define PI acos(-1) 7 | 8 | int main() { 9 | int T; 10 | cin >> T; 11 | while (T--) { 12 | double L; 13 | cin >> L; 14 | double W = L/10 * 6; 15 | double R = L/10 * 2; 16 | double A = PI*R*R; 17 | printf("%.2f %.2f\n", A, W*L - A); 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /project-euler/0018.txt: -------------------------------------------------------------------------------- 1 | 75 2 | 95 64 3 | 17 47 82 4 | 18 35 87 10 5 | 20 04 82 47 65 6 | 19 01 23 75 03 34 7 | 88 02 77 73 07 63 67 8 | 99 65 04 28 06 16 70 92 9 | 41 41 26 56 83 40 80 70 33 10 | 41 48 72 33 47 32 37 16 94 29 11 | 53 71 44 65 25 43 91 52 97 51 14 12 | 70 11 33 28 77 73 17 78 39 68 17 57 13 | 91 71 52 38 17 14 91 43 58 50 27 29 48 14 | 63 66 04 68 89 53 67 30 73 16 69 87 40 31 15 | 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 16 | -------------------------------------------------------------------------------- /UVa/011054.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | int n; 9 | while (cin >> n && n != 0) { 10 | ll s = 0, r; 11 | cin >> r; 12 | while (n-- > 1) { 13 | s += abs(r); 14 | ll x; 15 | cin >> x; 16 | r += x; 17 | } 18 | 19 | cout << s << endl; 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /UVa/011310.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | ll dp[41]; 9 | dp[1] = 1; dp[2] = 5; dp[3] = 11; 10 | for (int n = 4; n <= 40; n++) 11 | dp[n] = dp[n-1] + 4*dp[n-2] + 2*dp[n-3]; 12 | 13 | int t; 14 | cin >> t; 15 | while (t--) { 16 | int n; 17 | cin >> n; 18 | cout << dp[n] << endl; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /UVa/000991.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int cat[11] = {1, 1}; 7 | for (int n = 2; n <= 10; n++) { 8 | cat[n] = 0; 9 | for (int i = 0; i <= n-1; i++) 10 | cat[n] += cat[n-1-i]*cat[i]; 11 | } 12 | 13 | int n; bool nl = false; 14 | while (cin >> n) { 15 | if (nl) cout << endl; 16 | cout << cat[n] << endl; 17 | nl = true; 18 | } 19 | } -------------------------------------------------------------------------------- /UVa/010450.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | ll fib[51]; 9 | fib[1] = 2; fib[2] = 3; 10 | for (int i = 3; i < 51; i++) 11 | fib[i] = fib[i-2] + fib[i-1]; 12 | 13 | int T; 14 | cin >> T; 15 | for (int t = 1; t <= T; t++) { 16 | ll n; 17 | cin >> n; 18 | printf("Scenario #%d:\n%lld\n\n", t, fib[n]); 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /UVa/011461.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef vector vi; 8 | 9 | int main() { 10 | vi sq; 11 | for (int n = 1; n*n <= 100000; n++) 12 | sq.push_back(n*n); 13 | 14 | int a, b; 15 | while (cin >> a >> b && !(a == 0 && b == 0)) 16 | cout << distance(lower_bound(sq.begin(), sq.end(), a), upper_bound(sq.begin(), sq.end(), b)) << endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /UVa/012279.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int N; 7 | for (int t = 1; cin >> N && N != 0; t++) { 8 | int reasons = 0, treats = 0; 9 | for (int i = 0; i < N; i++) { 10 | int x; 11 | cin >> x; 12 | if (x == 0) treats++; 13 | else reasons++; 14 | } 15 | 16 | printf("Case %d: %d\n", t, reasons - treats); 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /UVa/010223.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int cat[20]; 8 | cat[0] = cat[1] = 1; 9 | for (int n = 2; n < 20; n++) { 10 | cat[n] = 0; 11 | for (int i = 0; i <= n-1; i++) 12 | cat[n] += cat[i]*cat[n-1-i]; 13 | } 14 | 15 | unsigned int n; 16 | while (cin >> n) 17 | cout << distance(cat, find(cat + 1, cat + 20, n)) << endl; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /UVa/011115.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | while (sc.hasNext()) { 9 | BigInteger N = sc.nextBigInteger(); 10 | int D = sc.nextInt(); 11 | 12 | if (BigInteger.ZERO.equals(N) && D == 0) break; 13 | 14 | System.out.println(N.pow(D)); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /UVa/010368.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | bool minmax(ll a, ll b) { 8 | if (b == 0) return 0; 9 | bool ret = false; 10 | if (a / b > 1) ret = !minmax(b + a % b, b); 11 | return ret | !minmax(b, a % b); 12 | } 13 | 14 | int main() { 15 | ll x, y; 16 | while (cin >> x >> y && !(x == 0 && y == 0)) 17 | printf("%s wins\n", minmax(max(x, y), min(x, y)) ? "Stan" : "Ollie"); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /UVa/012243.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | string s; 8 | while (getline(cin, s) && s != "*") { 9 | stringstream ss(s); 10 | string t; 11 | ss >> t; 12 | char c = tolower(t[0]); 13 | bool taut = true; 14 | while (taut && ss >> t) 15 | taut = tolower(t[0]) == c; 16 | 17 | cout << (taut ? "Y" : "N") << endl; 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /acepta-el-reto/0372.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | string s; 10 | cin >> s; 11 | 12 | for (int i = s.length()-1; i >= 0; i--) 13 | if (isupper(s[s.length() - 1 - i])) 14 | cout << char(toupper(s[i])); 15 | else 16 | cout << char(tolower(s[i])); 17 | 18 | cout << endl; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /UVa/000424.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | BigInteger num = BigInteger.ZERO; 9 | while (in.hasNext()) { 10 | BigInteger buff = in.nextBigInteger(); 11 | if (buff.equals(BigInteger.ZERO)) break; 12 | num = num.add(buff); 13 | } 14 | System.out.println(num); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /UVa/012024.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int d[13], f[13]; 7 | d[0] = 1, d[1] = 0; 8 | f[0] = 1, f[1] = 1; 9 | for (int n = 2; n <= 12; n++) { 10 | d[n] = (n-1) * (d[n-1] + d[n-2]); 11 | f[n] = n * f[n-1]; 12 | } 13 | 14 | int t; 15 | cin >> t; 16 | while (t--) { 17 | int n; 18 | cin >> n; 19 | printf("%d/%d\n", d[n], f[n]); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /UVa/001230.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int c = sc.nextInt(); 9 | while (c-- > 0) { 10 | BigInteger x = sc.nextBigInteger(); 11 | BigInteger y = sc.nextBigInteger(); 12 | BigInteger n = sc.nextBigInteger(); 13 | System.out.println(x.modPow(y, n)); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /UVa/010494.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | 9 | while (in.hasNextBigInteger()) { 10 | BigInteger a = in.nextBigInteger(); 11 | String op = in.next(); 12 | BigInteger b = in.nextBigInteger(); 13 | System.out.println("/".equals(op) ? a.divide(b) : a.mod(b)); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /UVa/010551.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int b = sc.nextInt(); 9 | while (b != 0) { 10 | BigInteger p = new BigInteger(sc.next(), b); 11 | BigInteger m = new BigInteger(sc.next(), b); 12 | System.out.println(p.mod(m).toString(b)); 13 | 14 | b = sc.nextInt(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /UVa/010579.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | BigInteger[] sol = new BigInteger[4787]; 10 | sol[0] = BigInteger.ZERO; 11 | sol[1] = BigInteger.ONE; 12 | for (int i = 2; i < 4787; i++) sol[i] = sol[i-2].add(sol[i-1]); 13 | 14 | while (sc.hasNext()) System.out.println(sol[sc.nextInt()]); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /UVa/011879.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | BigInteger sv = new BigInteger("17"); 9 | BigInteger b = new BigInteger(sc.next()); 10 | while (!b.equals(BigInteger.ZERO)) { 11 | System.out.println(b.mod(sv).equals(BigInteger.ZERO) ? "1" : "0"); 12 | 13 | b = new BigInteger(sc.next()); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /UVa/011830.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | while (sc.hasNext()) { 10 | String s = sc.next(), t = sc.next(); 11 | if ("0".equals(s) && "0".equals(t)) break; 12 | t = t.replace(s, ""); 13 | System.out.println(t.isEmpty() ? BigInteger.ZERO : new BigInteger(t)); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /acepta-el-reto/0112.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int d, v, t; 7 | while (cin >> d >> v >> t && !(d == 0 && v == 0 && t == 0)) { 8 | if (d <= 0 || v <= 0 || t <= 0) { 9 | cout << "ERROR" << endl; 10 | continue; 11 | } 12 | 13 | if (18*d <= 5*t*v) cout << "OK" << endl; 14 | else if (3*d < t*v) cout << "MULTA" << endl; 15 | else cout << "PUNTOS" << endl; 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /UVa/011247.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | ll m, x; 9 | while (cin >> m >> x && !(m == 0 && x == 0)) { 10 | if (x == 100) cout << "Not found" << endl; 11 | else { 12 | ll v = (100 * (m - 1)) / (100 - x); 13 | if (v * (100 - x) == 100 * (m - 1)) v--; 14 | if (m <= v) cout << v << endl; 15 | else cout << "Not found" << endl; 16 | } 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /UVa/010940.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | uint clear_msb(uint n) { 6 | uint mask = n; 7 | mask |= mask >> 1; 8 | mask |= mask >> 2; 9 | mask |= mask >> 4; 10 | mask |= mask >> 8; 11 | mask |= mask >> 16; 12 | mask = mask >> 1; 13 | 14 | return n & mask; 15 | } 16 | 17 | int main() { 18 | int n; 19 | while (cin >> n && n != 0) 20 | if (n == 1 || n == 2) cout << n << endl; 21 | else cout << 2 * clear_msb(n) << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /UVa/010910-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int nCm[71][71]; 7 | for (int n = 0; n <= 70; n++) 8 | for (int m = 0; m <= n; m++) 9 | if (m == 0 || n == m) nCm[n][m] = 1; 10 | else nCm[n][m] = nCm[n-1][m-1] + nCm[n-1][m]; 11 | 12 | int K; 13 | cin >> K; 14 | while (K--) { 15 | int N, T, P; 16 | cin >> N >> T >> P; 17 | cout << (P*N > T ? 0 : nCm[T-(P-1)*N-1][N-1]) << endl; 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /UVa/013131.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin >> T; 9 | while (T--) { 10 | int N, K; 11 | cin >> N >> K; 12 | 13 | int sum = 0; 14 | for (int i = 1; i <= sqrt(N); i++) 15 | if (N % i == 0) { 16 | if (i % K != 0) sum += i; 17 | if (i*i != N && N/i % K != 0) sum += N/i; 18 | } 19 | 20 | cout << sum << endl; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /UVa/000536.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string pre, in; 6 | 7 | void post(int i1, int j1, int i2, int j2) { 8 | char root = pre[i1]; 9 | int k = i2; 10 | while (in[k] != root) k++; 11 | 12 | if (k != i2) post(i1+1, i1+k-i2, i2, k-1); 13 | if (k != j2) post(i1+k-i2+1, j1, k+1, j2); 14 | cout << root; 15 | } 16 | 17 | int main() { 18 | while (cin >> pre >> in) { 19 | post(0, pre.length()-1, 0, in.length()-1); 20 | cout << endl; 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /UVa/010814.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int N = sc.nextInt(); 9 | while (N-- > 0) { 10 | BigInteger p = sc.nextBigInteger(); 11 | sc.next(); 12 | BigInteger q = sc.nextBigInteger(); 13 | BigInteger g = p.gcd(q); 14 | System.out.println(p.divide(g) + " / " + q.divide(g)); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /UVa/000580.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int L[31], U[31], n; 7 | L[1] = U[1] = 1; // L[n] = number of safe stacks of size n where the top block is lead. 8 | L[2] = U[2] = 2; // U[n] = number of safe stacks of size n where the top block is uranium. 9 | for (n = 3; n <= 30; n++) { 10 | L[n] = U[n-1] + L[n-1]; 11 | U[n] = L[n-1] + L[n-2]; 12 | } 13 | 14 | while (cin >> n && n != 0) 15 | cout << (1 << n) - U[n] - L[n] << endl; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /UVa/010994.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | ll h(ll n) { 8 | if (n <= 0) return 0; 9 | ll ret = 0; 10 | while (n != 0) { 11 | ret += 45 * (n / 10); 12 | for (ll m = ((n / 10) * 10) + 1; m <= n; m++) 13 | ret += m % 10; 14 | n /= 10; 15 | } 16 | 17 | return ret; 18 | } 19 | 20 | int main() { 21 | ll p, q; 22 | while (cin >> p >> q && !(p < 0 && q < 0)) 23 | cout << h(q) - h(p-1) << endl; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /UVa/010295.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int m, n; 8 | cin >> m >> n; 9 | map f; 10 | for (int i = 0; i < m; i++) { 11 | string s; 12 | int d; 13 | cin >> s >> d; 14 | f[s] = d; 15 | } 16 | 17 | for (int i = 0; i < n; i++) { 18 | int sum = 0; 19 | string s; 20 | while (cin >> s && s != ".") 21 | sum += f[s]; 22 | 23 | cout << sum << endl; 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /UVa/013148.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int pow6(int x) { 7 | int pow3 = x*x*x; 8 | return pow3 * pow3; 9 | } 10 | 11 | int main() { 12 | // I refuse to write an if / switch statement to solve the problem. 13 | set s; 14 | for (int x = 1; pow6(x) < 100000000; x++) // Hopefully the compiler will cache the call to pow6() 15 | s.insert(pow6(x)); 16 | 17 | int n; 18 | while (cin >> n && n != 0) 19 | cout << (s.count(n) > 0 ? "Special" : "Ordinary") << endl; 20 | } -------------------------------------------------------------------------------- /UVa/012946.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int p; 9 | while (cin >> p) { 10 | bitset<32> b = bitset<32>(p); 11 | 12 | complex g = 0; 13 | complex cf = complex(0, 1) - 1; 14 | complex pow = 1; 15 | for (int k = 0; k < 32; k++) { 16 | g += ((int) b[k]) * pow; 17 | pow *= cf; 18 | } 19 | 20 | printf("%d %d\n", g.real(), g.imag()); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /UVa/000623.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | while (sc.hasNext()) { 9 | int n = sc.nextInt(); 10 | BigInteger fact = BigInteger.ONE; 11 | for (int i = 1; i <= n; i++) 12 | fact = fact.multiply(BigInteger.valueOf(i)); 13 | 14 | System.out.println(n + "!"); 15 | System.out.println(fact.toString()); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /UVa/013025.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | tm timeStruct = {}; 7 | timeStruct.tm_year = 2013 - 1900; 8 | timeStruct.tm_mon = 5 - 1; 9 | timeStruct.tm_mday = 29; 10 | timeStruct.tm_hour = 12; // To avoid any doubts about summer time, etc. 11 | mktime(&timeStruct); 12 | 13 | string weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; 14 | printf("May 29, 2013 %s\n", weekdays[timeStruct.tm_wday].c_str()); // 0...6 for Sunday...Saturday 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /codeforces/825C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n, k; 8 | cin >> n >> k; 9 | int a[n]; 10 | for (int i = 0; i < n; i++) cin >> a[i]; 11 | 12 | sort(a, a + n); 13 | int cnt = 0; 14 | for (int i = 0; i < n; i++) { 15 | double d = ceil((double) a[i] / 2); 16 | while (k < d) { 17 | cnt++; 18 | k *= 2; 19 | } 20 | 21 | k = max(k, a[i]); 22 | } 23 | 24 | cout << cnt << endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /UVa/000748.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | 9 | while (in.hasNextBigDecimal()) { 10 | BigDecimal R = in.nextBigDecimal(); 11 | int n = in.nextInt(); 12 | String res = R.pow(n).stripTrailingZeros().toPlainString(); 13 | if (res.charAt(0) == '0') 14 | res = res.substring(1); 15 | System.out.println(res); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /UVa/010104.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | ll eea(ll a, ll b, ll &x, ll &y) { 8 | ll xx = y = 0, yy = x = 1; 9 | while (b) { 10 | ll q = a/b, t = b; b = a%b; a = t; 11 | t = xx; xx = x-q*xx; x = t; 12 | t = yy; yy = y-q*yy; y = t; 13 | } 14 | return a; 15 | } 16 | 17 | int main() { 18 | ll a, b; 19 | while (cin >> a >> b) { 20 | ll x, y; 21 | ll d = eea(a, b, x, y); 22 | printf("%lld %lld %lld\n", x, y, d); 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /UVa/010281.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | char buffer[1000]; 7 | int n, t = 0, now, hh, mm, ss, v = 0, u; 8 | double d = 0; 9 | 10 | while (cin.getline(buffer, sizeof(buffer))){ 11 | n = sscanf(buffer, "%d:%d:%d %d", &hh, &mm, &ss, &u); 12 | now = hh*60*60 + mm*60 + ss; 13 | 14 | d += (now - t) * v; 15 | t = now; 16 | 17 | if (n == 3) printf("%02d:%02d:%02d %.2f km\n", hh, mm, ss, d/(60*60)); 18 | else if (n == 4) v = u; 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /UVa/010773.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin >> T; 9 | for (int t = 1; t <= T; t++) { 10 | printf("Case %d: ", t); 11 | 12 | double d, v, u; 13 | cin >> d >> v >> u; 14 | if (u == 0 || v == 0 || u <= v) { 15 | cout << "can't determine" << endl; 16 | continue; 17 | } 18 | 19 | double t1 = d/u; 20 | double t2 = d/sqrt(u*u - v*v); 21 | printf("%.3f\n", t2 - t1); 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /UVa/010963.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | int n; 10 | cin >> n; 11 | bool ok = true; 12 | int x, y, d; 13 | cin >> x >> y; 14 | d = abs(x - y); 15 | n--; 16 | while (n--) { 17 | cin >> x >> y; 18 | if (d != abs(x - y)) ok = false; 19 | } 20 | 21 | cout << (ok ? "yes" : "no") << endl; 22 | 23 | if (T > 0) cout << endl; 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /UVa/012854.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | string s, t; 8 | while (getline(cin, s) && getline(cin, t)) { 9 | stringstream ss(s), st(t); 10 | bool b = true; 11 | for (int i = 0; i < 5; i++) { 12 | int x, y; 13 | ss >> x; 14 | st >> y; 15 | if (!(x ^ y)) { 16 | b = false; 17 | break; 18 | } 19 | } 20 | 21 | cout << (b ? 'Y' : 'N') << endl; 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /UVa/000389.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | while (sc.hasNext()) { 10 | String s = sc.next(); 11 | int b1 = sc.nextInt(); 12 | int b2 = sc.nextInt(); 13 | BigInteger x = new BigInteger(s, b1); 14 | String t = x.toString(b2).toUpperCase(); 15 | System.out.println(String.format("%7s", t.length() > 7 ? "ERROR" : t)); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /UVa/010063.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string s, t; 6 | 7 | void f(int i) { 8 | if (i == s.length()) { 9 | cout << t << endl; 10 | return; 11 | } 12 | 13 | for (int j = 0; j <= t.length(); j++) { 14 | t = t.insert(j, 1, s[i]); 15 | f(i+1); 16 | t = t.erase(j, 1); 17 | } 18 | } 19 | 20 | int main() { 21 | bool b = false; 22 | while (cin >> s) { 23 | if (b) cout << endl; 24 | b = true; 25 | t = ""; 26 | f(0); 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /UVa/012955.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | 8 | int main() { 9 | vi f; 10 | for (int i = 2, x = 1; x < 100000; i++) { 11 | f.push_back(x); 12 | x *= i; 13 | } 14 | 15 | int N; 16 | while (cin >> N) { 17 | int k = 0, i = f.size()-1; 18 | while (N != 0) { 19 | if (f[i] <= N) { 20 | N -= f[i]; 21 | k++; 22 | } else i--; 23 | } 24 | 25 | cout << k << endl; 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /UVa/010763.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef pair ii; 7 | 8 | int main() { 9 | int n; 10 | while (cin >> n && n != 0) { 11 | multiset s; 12 | while (n--) { 13 | int u, v; 14 | cin >> u >> v; 15 | auto it = s.find(ii(v, u)); 16 | if (it != s.end()) 17 | s.erase(it); 18 | else 19 | s.insert(ii(u, v)); 20 | } 21 | 22 | cout << (s.empty() ? "YES" : "NO") << endl; 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /UVa/000713.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int N = sc.nextInt(); 9 | while (N-- > 0) { 10 | BigInteger b1 = new BigInteger((new StringBuilder(sc.next())).reverse().toString()); 11 | BigInteger b2 = new BigInteger((new StringBuilder(sc.next())).reverse().toString()); 12 | System.out.println((new BigInteger(new StringBuilder(b1.add(b2).toString()).reverse().toString()))); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /UVa/011389.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n, d, r; 8 | while (cin >> n >> d >> r && !(n == 0 && d == 0 && r == 0)) { 9 | int m[n], e[n]; 10 | for (int i = 0; i < n; i++) cin >> m[i]; 11 | for (int i = 0; i < n; i++) cin >> e[i]; 12 | 13 | sort(m, m + n, greater()); 14 | sort(e, e + n); 15 | int over = 0; 16 | for (int i = 0; i < n; i++) over += max(0, m[i] + e[i] - d); 17 | 18 | cout << over * r << endl; 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /UVa/011962.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | int T; 9 | cin >> T; 10 | for (int t = 1; t <= T; t++) { 11 | string S; 12 | cin >> S; 13 | ll B = 0, p = 1; 14 | for (int i = S.length()-1; i >= 0; i--) { 15 | if (S[i] == 'C') B += p; 16 | else if (S[i] == 'G') B += 2*p; 17 | else if (S[i] == 'T') B += 3*p; 18 | p *= 4; 19 | } 20 | 21 | printf("Case %d: (%lu:%lld)\n", t, S.length(), B); 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /UVa/011679.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int B, N; 7 | while (cin >> B >> N && !(B == 0 && N == 0)) { 8 | int b[B]; 9 | for (int i = 0; i < B; i++) cin >> b[i]; 10 | while (N--) { 11 | int D, C, V; 12 | cin >> D >> C >> V; 13 | D--, C--; 14 | b[D] -= V; 15 | b[C] += V; 16 | } 17 | 18 | bool ok = true; 19 | for (int i = 0; i < B; i++) if (b[i] < 0) ok = false; 20 | cout << (ok ? "S" : "N") << endl; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /UVa/010252.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | string s, t; 8 | while (getline(cin, s) && getline(cin, t)) { 9 | sort(s.begin(), s.end()); 10 | sort(t.begin(), t.end()); 11 | 12 | int i = 0, j = 0; 13 | string c; 14 | while (i < s.length() && j < t.length()) 15 | if (s[i] == t[j]) { 16 | c += s[i]; 17 | i++, j++; 18 | } else if (s[i] < t[j]) i++; 19 | else j++; 20 | 21 | cout << c << endl; 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /UVa/010473.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | while (sc.hasNext()) { 8 | String s = sc.next(); 9 | if (s.charAt(0) == '-') break; 10 | if (s.length() >= 2 && s.charAt(1) == 'x') { 11 | System.out.println(Integer.parseInt(s.substring(2, s.length()), 16)); 12 | } else { 13 | System.out.println("0x" + (Integer.toHexString(Integer.parseInt(s))).toUpperCase()); 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /UVa/012959.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int J, R, pts[500]; 7 | while (cin >> J >> R) { 8 | for (int i = 0; i < J; i++) pts[i] = 0; 9 | int m = 0, j = 0; 10 | while (R--) 11 | for (int i = 0; i < J; i++) { 12 | int x; 13 | cin >> x; 14 | pts[i] += x; 15 | if (pts[i] >= m) { 16 | m = pts[i]; 17 | j = i; 18 | } 19 | } 20 | 21 | cout << j+1 << endl; 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /UVa/012592.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n; 8 | while (cin >> n) { 9 | cin.get(); 10 | map m; 11 | for (int i = 0; i < n; i++) { 12 | string s, t; 13 | getline(cin, s); 14 | getline(cin, t); 15 | m[s] = t; 16 | } 17 | 18 | int Q; 19 | cin >> Q; cin.get(); 20 | while (Q--) { 21 | string s; 22 | getline(cin, s); 23 | cout << m[s] << endl; 24 | } 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /UVa/000495.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | 9 | BigInteger[] fib = new BigInteger[5001]; 10 | fib[0] = BigInteger.ZERO; 11 | fib[1] = BigInteger.ONE; 12 | for (int i = 2; i <= 5000; i++) 13 | fib[i] = fib[i-1].add(fib[i-2]); 14 | 15 | while (in.hasNextInt()) { 16 | int n = in.nextInt(); 17 | System.out.println("The Fibonacci number for " + n + " is " + fib[n]); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /UVa/011462.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | while (cin >> n && n != 0) { 8 | int a[101]; 9 | for (int i = 0; i < 101; i++) a[i] = 0; 10 | while (n--) { 11 | int x; 12 | cin >> x; 13 | a[x]++; 14 | } 15 | 16 | bool first = true; 17 | for (int i = 1; i <= 100; i++) 18 | while (a[i]--) { 19 | printf("%s%d", first ? "" : " ", i); 20 | first = false; 21 | } 22 | cout << endl; 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /UVa/011496.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int isPeak(int i, int j, int k) { 6 | return (i < j && j > k) || (i > j && j < k) ? 1 : 0; 7 | } 8 | 9 | int main() { 10 | int N; 11 | while (cin >> N && N != 0) { 12 | int i0, i1, i, j, k, cnt = 0; 13 | cin >> i0 >> i1; 14 | i = i0, j = i1; 15 | N -= 2; 16 | while (N--) { 17 | cin >> k; 18 | cnt += isPeak(i, j, k); 19 | i = j, j = k; 20 | } 21 | 22 | cout << cnt + isPeak(i, j, i0) + isPeak(j, i0, i1) << endl; 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /acepta-el-reto/0337.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a[6]; 7 | int b[6]; 8 | int T; 9 | cin >> T; 10 | while (T--) { 11 | for (int i = 0; i < 6; i++) 12 | cin >> a[i]; 13 | for (int i = 0; i < 6; i++) 14 | cin >> b[i]; 15 | 16 | bool ok = true; 17 | int sum = a[0] + b[0]; 18 | for (int i = 1; i < 6; i++) 19 | if (a[i] + b[i] != sum) { 20 | ok = false; 21 | break; 22 | } 23 | 24 | cout << (ok ? "SI" : "NO") << endl; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /UVa/012554.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | string p[n]; 9 | for (int i = 0; i < n; i++) cin >> p[i]; 10 | 11 | string s[4] = {"Happy", "birthday", "to", "you"}; 12 | int i = 0, k = 0; 13 | while (i < n) { 14 | printf("%s: %s\n", p[i++].c_str(), (k % 16) == 11 ? "Rujia" : s[k % 4].c_str()); 15 | k++; 16 | } 17 | 18 | while ((k % 16) != 0) { 19 | printf("%s: %s\n", p[i % n].c_str(), (k % 16) == 11 ? "Rujia" : s[k % 4].c_str()); 20 | k++; 21 | i++; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /codeforces/813A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | int S = 0; 9 | for (int i = 0; i < n; i++) { 10 | int ai; 11 | cin >> ai; 12 | S += ai; 13 | } 14 | 15 | int m; 16 | cin >> m; 17 | bool ok = false; 18 | int T = 100000; 19 | for (int j = 0; j < m; j++) { 20 | int lj, rj; 21 | cin >> lj >> rj; 22 | if (!ok && S <= rj) { 23 | ok = true; 24 | T = max(S, lj); 25 | } 26 | } 27 | 28 | cout << (ok ? T : -1) << endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /UVa/013018.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef vector vi; 8 | 9 | int main() { 10 | int N, M; 11 | for (int t = 0; cin >> N >> M; t++) { 12 | if (t != 0) cout << endl; 13 | 14 | vi sums(N + M + 1, 0); 15 | 16 | for (int i = 1; i <= N; i++) 17 | for (int j = 1; j <= M; j++) 18 | sums[i+j]++; 19 | 20 | int M = *max_element(sums.begin(), sums.end()); 21 | for (int i = 0; i < sums.size(); i++) 22 | if (sums[i] == M) cout << i << endl; 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /UVa/012930.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | import java.util.Scanner; 3 | 4 | class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | for (int t = 1; in.hasNextBigDecimal(); t++) { 9 | BigDecimal a = in.nextBigDecimal(); 10 | BigDecimal b = in.nextBigDecimal(); 11 | int res = a.compareTo(b); 12 | if (res == 1) System.out.println("Case " + t + ": Bigger"); 13 | else if (res == -1) System.out.println("Case " + t + ": Smaller"); 14 | else System.out.println("Case " + t + ": Same"); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /UVa/010924.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | bitset<52*20 + 1> bs; 7 | 8 | void sieve() { 9 | bs.set(); 10 | bs[0] = 0; bs[1] = 1; 11 | for (int i = 2; i < bs.size(); i++) 12 | if (bs[i]) 13 | for (int j = i * i; j < bs.size(); j += i) bs[j] = 0; 14 | } 15 | 16 | int main() { 17 | sieve(); 18 | string t = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", s; 19 | while (cin >> s) { 20 | int n = s.length(); 21 | for (char &c : s) n += t.find(c); 22 | printf("It is %sa prime word.\n", bs[n] ? "" : "not "); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /UVa/012527.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int r[5001]; 8 | for (int n = 1; n <= 5000; n++) { 9 | bitset<10> b; 10 | r[n] = 1; 11 | int m = n; 12 | while (m) { 13 | if (b[(m % 10) - 1]) { 14 | r[n] = 0; 15 | break; 16 | } else { 17 | b[(m % 10) - 1] = 1; 18 | m /= 10; 19 | } 20 | } 21 | 22 | r[n] += r[n-1]; 23 | } 24 | 25 | int N, M; 26 | while (cin >> N >> M) 27 | cout << r[M] - r[N-1] << endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /acepta-el-reto/0304.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | div_t euc_div(int a, int d) { 8 | div_t divT = div(a, d); 9 | int I = divT.rem >= 0 ? 0 : (d > 0 ? 1 : -1); 10 | int qE = divT.quot - I; 11 | int rE = divT.rem + I * d; 12 | return div_t{qE, rE}; 13 | } 14 | 15 | int main() { 16 | int T; 17 | cin >> T; 18 | while (T--) { 19 | int a, b; 20 | cin >> a >> b; 21 | if (b == 0) cout << "DIV0" << endl; 22 | else { 23 | div_t res = euc_div(a, b); 24 | printf("%d %d\n", res.quot, res.rem); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /UVa/000105.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int x1, h, x2; 8 | 9 | int skyline[10000] = {0}; 10 | 11 | while (scanf("%d %d %d", &x1, &h, &x2) == 3) 12 | for (int i = x1; i < x2; i++) 13 | skyline[i] = max(skyline[i], h); 14 | 15 | bool e = false; 16 | h = 0; 17 | for (int i = 0; i < 10000; i++) { 18 | if (skyline[i] != h) { 19 | if (e) cout << ' '; 20 | cout << i << " " << skyline[i]; 21 | e = true; 22 | h = skyline[i]; 23 | } 24 | } 25 | cout << endl; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /UVa/010523.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | 9 | while (in.hasNextInt()) { 10 | int N = in.nextInt(); 11 | BigInteger A = in.nextBigInteger(); 12 | BigInteger sum = BigInteger.ZERO; 13 | 14 | BigInteger Ai = A; 15 | for (int i = 1; i <= N; i++) { 16 | sum = sum.add(BigInteger.valueOf(i).multiply(Ai)); 17 | Ai = Ai.multiply(A); 18 | } 19 | 20 | System.out.println(sum); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /UVa/000278.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | char c; 10 | int n, m, p; 11 | cin >> c >> n >> m; 12 | if (c == 'r' || c == 'Q') p = min(n, m); 13 | else if (c == 'k') { 14 | p = n*m / 2; 15 | if (n * m % 2 != 0) p++; 16 | } else { 17 | int a = min(n, m) / 2; 18 | if (min(n, m) % 2 != 0) a++; 19 | 20 | int b = max(n, m) / 2; 21 | if (max(n, m) % 2 != 0) b++; 22 | 23 | p = a * b; 24 | } 25 | cout << p << endl; 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /UVa/010910.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int memo[71][71]; 7 | 8 | int F(int n, int t, int P) { 9 | if (n == 0 && t == 0) return 1; 10 | if (n == 0) return 0; 11 | 12 | if (memo[n][t] != -1) return memo[n][t]; 13 | 14 | int sum = 0; 15 | for (int p = P; p <= t; p++) 16 | sum += F(n - 1, t - p, P); 17 | 18 | return memo[n][t] = sum; 19 | } 20 | 21 | int main() { 22 | int K; 23 | cin >> K; 24 | while (K--) { 25 | int N, T, P; 26 | cin >> N >> T >> P; 27 | memset(memo, -1, sizeof memo); 28 | cout << F(N, T, P) << endl; 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /UVa/011991.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | typedef vector> vvi; 8 | 9 | int main() { 10 | int n, m; 11 | while (cin >> n >> m) { 12 | vvi adjList(1000001, vi()); 13 | for (int pos = 1; pos <= n; pos++) { 14 | int x; 15 | cin >> x; 16 | adjList[x].push_back(pos); 17 | } 18 | 19 | while (m--) { 20 | int k, v; 21 | cin >> k >> v; 22 | if (adjList[v].size() < k) cout << 0 << endl; 23 | else cout << adjList[v][k-1] << endl; 24 | } 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /UVa/012157.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | for (int t = 1; t <= T; t++) { 9 | int N; 10 | cin >> N; 11 | int mile = 0, juice = 0; 12 | while (N--) { 13 | int m; 14 | cin >> m; 15 | mile += 1 + m/30; 16 | juice += 1 + m/60; 17 | } 18 | 19 | mile *= 10, juice *= 15; 20 | printf("Case %d: ", t); 21 | if (mile == juice) printf("Mile Juice %d\n", mile); 22 | else if (mile < juice) printf("Mile %d\n", mile); 23 | else printf("Juice %d\n", juice); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /acepta-el-reto/0111.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int c[19] = {2, 2, 6, 2, 6, 2, 10, 6, 2, 10, 6, 2, 14, 10, 6, 2, 14, 10, 6}; 7 | string n[19] = {"1s", "2s", "2p", "3s", "3p", "4s", "3d", "4p", "5s", "4d", "5p", "6s", "4f", "5d", "6p", "7s", "5f", "6d", "7p"}; 8 | 9 | string s; 10 | while (cin >> s && s != "Exit") { 11 | int Z; 12 | cin >> Z; 13 | 14 | int i = 0; 15 | while (Z > c[i]) { 16 | printf("%s%d ", n[i].c_str(), c[i]); 17 | Z -= c[i]; 18 | i++; 19 | } 20 | 21 | printf("%s%d\n", n[i].c_str(), Z); 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /UVa/001583.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | char s[1024]; 9 | int A[100001]; 10 | for (int i = 0; i < 100001; i++) 11 | A[i] = INT_MAX; 12 | 13 | for (int n = 0; n < 100001; n++) { 14 | sprintf(s, "%d", n); 15 | int sum = n; 16 | for (int i = 0; i < strlen(s); i++) 17 | sum += s[i] - '0'; 18 | A[sum] = min(A[sum], n); // n is a generator of sum 19 | } 20 | 21 | int T; 22 | cin >> T; 23 | while (T--) { 24 | int N; 25 | cin >> N; 26 | cout << (A[N] == INT_MAX ? 0 : A[N]) << endl; 27 | } 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /acepta-el-reto/0374.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int main() { 8 | int T; 9 | cin >> T; 10 | while (T--) { 11 | ll x; 12 | cin >> x; 13 | 14 | ll M = x, m = x; 15 | ll MM = 1, mm = 1; 16 | 17 | while (cin >> x && x != 0) { 18 | if (x > M) { 19 | M = x; 20 | MM = 1; 21 | } else if (x == M) MM++; 22 | 23 | if (x < m) { 24 | m = x; 25 | mm = 1; 26 | } else if (x == m) mm++; 27 | } 28 | 29 | printf("%lld %lld %lld %lld\n", m, mm, M, MM); 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /UVa/000673.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n; 8 | string s; 9 | 10 | cin >> n; cin.get(); 11 | while (n--) { 12 | stack st; 13 | bool ok = true; 14 | 15 | getline(cin, s); 16 | for (char &c : s) { 17 | if (c == '(' || c == '[') st.push(c); 18 | else if (st.empty() || (st.top() == '(' && c == ']') || (st.top() == '[' && c == ')')) { 19 | ok = false; 20 | break; 21 | } else st.pop(); 22 | } 23 | 24 | cout << (ok && st.empty() ? "Yes" : "No") << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /UVa/011371.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | 8 | int main() { 9 | string s; 10 | while (cin >> s) { 11 | string t = s; 12 | sort(s.begin(), s.end(), greater()); 13 | sort(t.begin(), t.end()); 14 | 15 | int z = 0; 16 | for (int i = 0; i < t.length() && t[i] == '0'; t.erase(i, 1), z++); 17 | 18 | ll a = stoll(s), b; 19 | if (t.empty()) b = 0; 20 | else { 21 | t.insert(1, z, '0'); 22 | b = stoll(t); 23 | } 24 | 25 | printf("%lld - %lld = %lld = 9 * %lld\n", a, b, a - b, (a - b) / 9); 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "either" 7 | version = "1.15.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" 10 | 11 | [[package]] 12 | name = "itertools" 13 | version = "0.14.0" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" 16 | dependencies = [ 17 | "either", 18 | ] 19 | 20 | [[package]] 21 | name = "leetcode" 22 | version = "0.1.0" 23 | dependencies = [ 24 | "itertools", 25 | ] 26 | -------------------------------------------------------------------------------- /UVa/010739.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string s; 6 | int memo[1000][1000]; 7 | 8 | int f(int i, int j) { 9 | if (i >= j) return 0; 10 | if (memo[i][j] != -1) return memo[i][j]; 11 | 12 | if (s[i] == s[j]) return memo[i][j] = f(i+1, j-1); 13 | return memo[i][j] = 1 + min(f(i+1, j-1), min(f(i+1, j), f(i, j-1))); 14 | } 15 | 16 | int main() { 17 | int T; 18 | cin >> T; 19 | for (int t = 1; t <= T; t++) { 20 | cin >> s; 21 | for (int i = 0; i < s.length(); i++) 22 | for (int j = 0; j < s.length(); j++) 23 | memo[i][j] = -1; 24 | printf("Case %d: %d\n", t, f(0, s.length()-1)); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /UVa/011219.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | for (int t = 1; t <= T; t++) { 9 | int d2, m2, y2, d1, m1, y1; 10 | scanf("%d/%d/%d", &d2, &m2, &y2); 11 | scanf("%d/%d/%d", &d1, &m1, &y1); 12 | 13 | printf("Case #%d: ", t); 14 | if (y1 > y2 || (y1 == y2 && m1 > m2) || y1 == y2 && m1 == m2 && d1 > d2) cout << "Invalid birth date" << endl; 15 | else { 16 | int s = y2 - y1; 17 | if (m2 < m1 || (m2 == m1 && d2 < d1)) s--; 18 | if (s > 130) cout << "Check birth date" << endl; 19 | else cout << s << endl; 20 | } 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /UVa/000412.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int gcd(int a, int b) { 7 | while (b) { 8 | int t = b; 9 | b = a % b; 10 | a = t; 11 | } 12 | return a; 13 | } 14 | 15 | int main() { 16 | int N; 17 | while (cin >> N && N != 0) { 18 | int a[N]; 19 | for (int i = 0; i < N; i++) cin >> a[i]; 20 | int cnt = 0; 21 | for (int i = 0; i < N; i++) 22 | for (int j = i+1; j < N; j++) 23 | if (gcd(a[i], a[j]) == 1) cnt++; 24 | 25 | if (cnt == 0) cout << "No estimate for this data set." << endl; 26 | else printf("%.6f\n", sqrt(3.0*N*(N-1)/cnt)); 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /UVa/010324.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | string s; 7 | for (int t = 1; cin >> s; t++) { 8 | int n; 9 | cin >> n; 10 | 11 | printf("Case %d:\n", t); 12 | while (n--) { 13 | int i, j; 14 | cin >> i >> j; 15 | 16 | bool b = true; 17 | int k = min(i, j); 18 | char c = s[k]; 19 | while (k <= max(i, j)) { 20 | if (s[k] != c) { 21 | b = false; 22 | break; 23 | } 24 | k++; 25 | } 26 | 27 | cout << (b ? "Yes" : "No") << endl; 28 | } 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /acepta-el-reto/0376.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | 8 | int main() { 9 | int n; 10 | while (cin >> n && n != 0) { 11 | vi v; 12 | for (int i = 0; i < n; i++) { 13 | int x; 14 | cin >> x; 15 | v.push_back(x); 16 | } 17 | 18 | int p = 0; 19 | for (int i = 1; i <= n - 2; i++) 20 | if (v[i-1] < v[i] && v[i] > v[i+1]) 21 | p++; 22 | 23 | if (v[0] > v[1] && v[n-1] < v[0]) 24 | p++; 25 | 26 | if (v[n-1] > v[n-2] && v[n-1] > v[0]) 27 | p++; 28 | 29 | cout << p << endl; 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /UVa/000350.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int Z, I, M, L, lambda; 6 | 7 | int f(int x) { 8 | return (Z * x + I) % M; 9 | } 10 | 11 | void brent(int x0) { 12 | int power = lambda = 1; 13 | int tortoise = x0, hare = f(x0); 14 | while (tortoise != hare) { 15 | if (power == lambda) { 16 | tortoise = hare; 17 | power *= 2; 18 | lambda = 0; 19 | } 20 | hare = f(hare); 21 | lambda++; 22 | } 23 | } 24 | 25 | int main() { 26 | for (int t = 1; cin >> Z >> I >> M >> L && !(Z == 0 && I == 0 && M == 0 && L == 0); t++) { 27 | brent(L); 28 | printf("Case %d: %d\n", t, lambda); 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /UVa/000355.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | while (sc.hasNextInt()) { 10 | int b1 = sc.nextInt(); 11 | int b2 = sc.nextInt(); 12 | String s = sc.next(); 13 | try { 14 | BigInteger x = new BigInteger(s, b1); 15 | System.out.println(s + " base " + b1 + " = " + x.toString(b2).toUpperCase() + " base " + b2); 16 | } catch (NumberFormatException e) { 17 | System.out.println(s + " is an illegal base " + b1 + " number"); 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /UVa/010190.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | 8 | int main() { 9 | int n, m; 10 | while (cin >> n >> m) { 11 | vi v; 12 | v.push_back(n); 13 | bool boring = n < 2 || m <= 1; 14 | while (!boring && n != 1) 15 | if (n % m != 0) boring = true; 16 | else { 17 | n /= m; 18 | v.push_back(n); 19 | } 20 | 21 | if (boring) cout << "Boring!" << endl; 22 | else { 23 | cout << v[0]; 24 | for (int i = 1; i < v.size(); i++) printf(" %d", v[i]); 25 | cout << endl; 26 | } 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /UVa/011917.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin >> T; 9 | for (int t = 1; t <= T; t++) { 10 | map m; 11 | int n; 12 | cin >> n; 13 | while (n--) { 14 | string s; 15 | int x; 16 | cin >> s >> x; 17 | m[s] = x; 18 | } 19 | 20 | int D; 21 | cin >> D; 22 | string s; 23 | cin >> s; 24 | if (m.count(s) == 0 || D + 5 < m[s]) printf("Case %d: Do your own homework!\n", t); 25 | else if (m[s] <= D) printf("Case %d: Yesss\n", t); 26 | else printf("Case %d: Late\n", t); 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /UVa/011291.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | stringstream ss; 7 | 8 | double smeech() { 9 | while (isspace(ss.peek())) ss.get(); 10 | if (ss.peek() == '(') { 11 | char par; 12 | ss >> par; 13 | double p; 14 | ss >> p; 15 | double e1 = smeech(), e2 = smeech(); 16 | ss >> par; 17 | return p*(e1 + e2) + (1-p)*(e1 - e2); 18 | } else { 19 | int x; 20 | ss >> x; 21 | return x; 22 | } 23 | } 24 | 25 | int main() { 26 | string s; 27 | while (getline(cin, s) && s != "()") { 28 | ss.clear(); 29 | ss << s; 30 | printf("%.2f\n", smeech()); 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /UVa/012049.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin >> T; 9 | while (T--) { 10 | int N, M; 11 | cin >> N >> M; 12 | map m; 13 | while (N--) { 14 | int x; 15 | cin >> x; 16 | m[x]++; 17 | } 18 | 19 | int cnt = 0; 20 | while (M--) { 21 | int x; 22 | cin >> x; 23 | 24 | if (m.count(x) == 0 || m[x] == 0) cnt++; 25 | else m[x]--; 26 | } 27 | 28 | for (const auto &p: m) 29 | if (p.second > 0) cnt += p.second; 30 | 31 | cout << cnt << endl; 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /UVa/000414.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | 8 | int main() { 9 | int N; 10 | while (cin >> N && N != 0) { 11 | cin.get(); 12 | vi v; 13 | string s; 14 | 15 | int minSpaces = 25; 16 | while (N--) { 17 | int spaces = 0; 18 | getline(cin, s); 19 | for (char &c : s) 20 | if (c == ' ') spaces++; 21 | 22 | v.push_back(spaces); 23 | minSpaces = min(minSpaces, spaces); 24 | } 25 | 26 | int ans = 0; 27 | for (int i : v) 28 | ans += (i - minSpaces); 29 | 30 | cout << ans << endl; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /acepta-el-reto/0375.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | string s; 8 | while (getline(cin, s)) { 9 | stringstream ss(s); 10 | int p; 11 | char o; 12 | bool ob = false; 13 | if (s.length() == 3) { 14 | o = s[s.length()-1]; 15 | ob = true; 16 | } 17 | ss >> p; 18 | 19 | p = (18 + p) % 36; 20 | if (p == 0) p = 36; 21 | 22 | printf("%02d", p); 23 | if (ob) { 24 | if (o == 'L') o = 'R'; 25 | else if (o == 'C') o = 'C'; 26 | else o = 'L'; 27 | cout << o; 28 | } 29 | 30 | cout << endl; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /UVa/010925.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | int t = 1; 10 | while (true) { 11 | int N = sc.nextInt(); 12 | BigInteger F = sc.nextBigInteger(); 13 | if (N == 0 && BigInteger.ZERO.equals(F)) break; 14 | BigInteger S = BigInteger.ZERO; 15 | for (int n = 0; n < N; n++) 16 | S = S.add(sc.nextBigInteger()); 17 | 18 | System.out.println("Bill #" + t + " costs " + S + ": each friend should pay " + S.divide(F)); 19 | System.out.println(); 20 | 21 | t++; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /UVa/012996.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | 8 | int main() { 9 | int T; 10 | cin >> T; 11 | for (int X = 1; X <= T; X++) { 12 | int N, L; 13 | cin >> N >> L; 14 | vi mangoes; 15 | int sum = 0; 16 | for (int i = 0; i < N; i++) { 17 | int x; 18 | cin >> x; 19 | mangoes.push_back(x); 20 | sum += x; 21 | } 22 | 23 | bool b = true; 24 | for (int i = 0; i < N; i++) { 25 | int x; 26 | cin >> x; 27 | b &= x >= mangoes[i]; 28 | } 29 | 30 | printf("Case %d: %s\n", X, sum <= L && b ? "Yes" : "No"); 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /acepta-el-reto/0119.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | 8 | int main() { 9 | vi squares; 10 | for (int i = 1; i*i <= 10000000; i++) 11 | squares.push_back(i*i); 12 | 13 | int n; 14 | while (cin >> n && n != 0) { 15 | int escudos = 0; 16 | int i = squares.size() - 1; 17 | while (n != 0) { 18 | if (n >= squares[i]) { 19 | int sq_root = i+1; 20 | if (sq_root == 1) escudos += 5; 21 | else escudos += squares[i] + (sq_root - 2) * 4 + 8; 22 | 23 | n -= squares[i]; 24 | } else i--; 25 | } 26 | 27 | cout << escudos << endl; 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /UVa/000257.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include ; 3 | 4 | using namespace std; 5 | 6 | bool palinword(string &s) { 7 | set st; 8 | for (int i = 1; i < s.length()-1; i++) { 9 | if (s[i-1] == s[i+1]) st.insert(s.substr(i-1, 3)); 10 | int j = i+1; 11 | if (i < s.length()-2 && s[i] == s[j] && s[i-1] == s[j+1]) { 12 | string t = s.substr(i-1, 4); 13 | if (string(4, t[0]) == t) { 14 | if (st.count(string(3, t[0])) == 0) st.insert(t); 15 | } else st.insert(s.substr(i-1, 4)); 16 | } 17 | } 18 | 19 | return st.size() >= 2; 20 | } 21 | 22 | int main() { 23 | string s; 24 | while (cin >> s) 25 | if (palinword(s)) 26 | cout << s << endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /UVa/012970.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | ll gcd(ll a, ll b) { 8 | if (b == 0) return a; 9 | else return gcd(b, a % b); 10 | } 11 | 12 | int main() { 13 | ll v1, d1, v2, d2; 14 | for (int t = 1; cin >> v1 >> d1 >> v2 >> d2 && !(v1 == 0 && d1 == 0 && v2 == 0 && d2 == 0); t++) { 15 | printf("Case #%d: %s\n", t, (v2*d1 < v1*d2 ? "You owe me a beer!" : "No beer for the captain.")); 16 | 17 | ll num = v1*d2 + v2*d1; 18 | ll den = 2*v1*v2; 19 | ll _gcd = gcd(max(num, den), min(num, den)); 20 | num /= _gcd, den /= _gcd; 21 | printf("Avg. arrival time: %lu", num); 22 | if (den == 1) cout << endl; 23 | else printf("/%lu\n", den); 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /leetcode/src/bin/2384.rs: -------------------------------------------------------------------------------- 1 | struct Solution; 2 | 3 | impl Solution { 4 | pub fn zero_filled_subarray(nums: Vec) -> i64 { 5 | let mut ret = 0; 6 | let mut cur = 0; 7 | 8 | for x in nums { 9 | if x == 0 { 10 | cur += 1; 11 | } else { 12 | ret += (cur * (cur + 1)) / 2; 13 | cur = 0; 14 | } 15 | } 16 | 17 | ret += (cur * (cur + 1)) / 2; 18 | ret 19 | } 20 | } 21 | 22 | fn main() { 23 | assert_eq!( 24 | 6, 25 | Solution::zero_filled_subarray(vec![1, 3, 0, 0, 2, 0, 0, 4]) 26 | ); 27 | assert_eq!(9, Solution::zero_filled_subarray(vec![0, 0, 0, 2, 0, 0])); 28 | assert_eq!(0, Solution::zero_filled_subarray(vec![2, 10, 2019])); 29 | } 30 | -------------------------------------------------------------------------------- /UVa/011282.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | ll dp[21][21]; 8 | 9 | ll nCm(int n, int m) { 10 | if (n == m || m == 0) return 1; 11 | if (dp[n][m] != -1) return dp[n][m]; 12 | return dp[n][m] = nCm(n-1, m-1) + nCm(n-1, m); 13 | } 14 | 15 | int main() { 16 | for (int n = 0; n <= 20; n++) 17 | for (int m = 0; m <= n; m++) 18 | dp[n][m] = -1; 19 | 20 | ll D[21]; 21 | D[0] = 1; D[1] = 0; 22 | for (int n = 2; n <= 20; n++) 23 | D[n] = (n-1) * (D[n-1] + D[n-2]); 24 | 25 | int N, M; 26 | while (cin >> N >> M) { 27 | ll res = 0; 28 | for (int m = 0; m <= M; m++) 29 | res += nCm(N, m) * D[N-m]; 30 | cout << res << endl; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /UVa/013171.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | int m, y, c; 10 | string s; 11 | cin >> m >> y >> c >> s; 12 | 13 | for (char &ch : s) { 14 | if (ch == 'M') m--; 15 | else if (ch == 'Y') y--; 16 | else if (ch == 'C') c--; 17 | else if (ch == 'R') y--, m--; 18 | else if (ch == 'B') y--, m--, c--; 19 | else if (ch == 'G') y--, c--; 20 | else if (ch == 'V') m--, c--; 21 | 22 | if (m < 0 || y < 0 || c < 0) break; 23 | } 24 | 25 | if (m < 0 || y < 0 || c < 0) cout << "NO" << endl; 26 | else printf("YES %d %d %d\n", m, y, c); 27 | } 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /UVa/012459.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | import java.util.Scanner; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | Scanner sc = new Scanner(System.in); 10 | 11 | List v = new ArrayList<>(); 12 | BigInteger m = BigInteger.ZERO; 13 | BigInteger f = BigInteger.ONE; 14 | v.add(m.add(f)); 15 | for (int g = 2; g <= 80; g++) { 16 | BigInteger t = f; 17 | f = m.add(f); 18 | m = t; 19 | v.add(f.add(m)); 20 | } 21 | 22 | while (sc.hasNextInt()) { 23 | int g = sc.nextInt(); 24 | if (g == 0) break; 25 | 26 | System.out.println(v.get(g-1)); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /acepta-el-reto/0377.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | BigInteger SIXTEEN = BigInteger.valueOf(16); 9 | BigInteger FOUR = BigInteger.valueOf(4); 10 | BigInteger NINE = BigInteger.valueOf(9); 11 | while (in.hasNextBigInteger()) { 12 | BigInteger x = in.nextBigInteger(); 13 | BigInteger mod = x.mod(SIXTEEN); 14 | if (mod.equals(BigInteger.ZERO) || mod.equals(FOUR) || mod.equals(BigInteger.ONE) || mod.equals(NINE)) { 15 | System.out.println("NO SE"); 16 | } else { 17 | System.out.println("IMPERFECTO"); 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /UVa/000263.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | int main() { 10 | ll u; 11 | while (cin >> u && u != 0) { 12 | set s; 13 | int n = 0; 14 | printf("Original number was %lld\n", u); 15 | while (s.count(u) == 0) { 16 | s.insert(u); 17 | string t2 = to_string(u); 18 | sort(t2.begin(), t2.end()); 19 | string t1 = t2; 20 | reverse(t1.begin(), t1.end()); 21 | ll t2n = stoll(t2), t1n = stoll(t1); 22 | u = t1n - t2n; 23 | printf("%lld - %lld = %lld\n", t1n, t2n, u); 24 | n++; 25 | } 26 | printf("Chain length %d\n\n", n); 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /UVa/000417.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | map m; 8 | 9 | void preprocess() { 10 | queue q; 11 | for (char c = 'a'; c <= 'z'; c++) 12 | q.push(string(1, c)); 13 | 14 | int cnt = 1; 15 | while (!q.empty()) { 16 | string s = q.front(); 17 | q.pop(); 18 | 19 | m[s] = cnt++; 20 | if (s.length() < 5) 21 | for (char c = s[s.length()-1] + 1; c <= 'z'; c++) 22 | q.push(s + c); 23 | } 24 | } 25 | 26 | int main() { 27 | preprocess(); 28 | 29 | string s; 30 | while (cin >> s) { 31 | auto it = m.find(s); 32 | if (it != m.end()) 33 | cout << (*it).second << endl; 34 | else cout << 0 << endl; 35 | } 36 | } -------------------------------------------------------------------------------- /UVa/000834.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | 8 | int n, d; 9 | vi v; 10 | 11 | void f() { 12 | if (n % d == 0) { 13 | v.push_back(n / d); 14 | return; 15 | } 16 | int i = n / d; 17 | v.push_back(i); 18 | 19 | n -= i*d; 20 | int aux = n; 21 | n = d; 22 | d = aux; 23 | 24 | f(); 25 | } 26 | 27 | int main() { 28 | while (cin >> n >> d) { 29 | v.clear(); 30 | f(); 31 | 32 | printf("[%d", v[0]); 33 | if (v.size() > 1) { 34 | cout << ';'; 35 | for (int i = 1; i < v.size()-1; i++) 36 | printf("%d,", v[i]); 37 | cout << v[v.size()-1]; 38 | } 39 | cout << ']' << endl; 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /acepta-el-reto/0379.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | 8 | int main() { 9 | int N; 10 | while (cin >> N && N != 0) { 11 | vi v; 12 | for (int i = 0; i < N; i++) { 13 | int x; 14 | cin >> x; 15 | v.push_back(x); 16 | } 17 | 18 | int j = 0; 19 | for (int i = v.size() - 1; i >= 1; i--) 20 | if (v[i] == v[i-1]) { 21 | j = i; 22 | break; 23 | } 24 | 25 | v[j]++; 26 | for (int k = j+1; k < v.size(); k++) 27 | v[k] = v[j]; 28 | 29 | for (int i = 0; i < v.size()-1; i++) 30 | cout << v[i] << ' '; 31 | cout << v[v.size()-1] << endl; 32 | } 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /UVa/011753.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int c, K, a[10000]; 6 | 7 | int f(int i, int j) { 8 | if (c > K) return c; 9 | if (i >= j) return 0; 10 | 11 | if (a[i] == a[j]) return f(i+1, j-1); 12 | c++; 13 | int D = 1 + min(f(i+1, j), f(i, j-1)); 14 | c--; 15 | return D; 16 | } 17 | 18 | int main() { 19 | int T; 20 | cin >> T; 21 | for (int t = 1; t <= T; t++) { 22 | int N; 23 | cin >> N >> K; 24 | for (int i = 0; i < N; i++) 25 | cin >> a[i]; 26 | 27 | c = 0; 28 | int D = f(0, N-1); 29 | printf("Case %d: ", t); 30 | if (D == 0) cout << "Too easy" << endl; 31 | else if (D > K) cout << "Too difficult" << endl; 32 | else cout << D << endl; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /UVa/012575.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | #define PI acos(-1) 7 | 8 | int A, B; 9 | double F(double theta) { 10 | return A*sin(theta) + B*cos(theta); 11 | } 12 | 13 | int main() { 14 | int T; 15 | cin >> T; 16 | while (T--) { 17 | cin >> A >> B; 18 | 19 | double x, C = (double) A / B; 20 | if (A == 0 && B == 0) 21 | x = 0; 22 | else if (B == 0) 23 | if (F(PI/2) > 0) x = PI/2; 24 | else x = PI + PI/2; 25 | else if (atan(C) >= 0 && F(atan(C)) > 0) 26 | x = atan(C); 27 | else if (F(atan(C) + PI) > 0) 28 | x = atan(C) + PI; 29 | else 30 | x = atan(C) + 2*PI; 31 | 32 | printf("%.2f %.2f\n", x, F(x)); 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /UVa/000443.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int humble[5842]; 7 | int i2, i3, i5, i7; 8 | 9 | i2 = i3 = i5 = i7 = 0; 10 | humble[0] = 1; 11 | for (int i = 1; i < 5842; i++) { 12 | humble[i] = min(min(2*humble[i2], 3*humble[i3]), min(5*humble[i5], 7*humble[i7])); 13 | 14 | if (humble[i] == 2*humble[i2]) i2++; 15 | if (humble[i] == 3*humble[i3]) i3++; 16 | if (humble[i] == 5*humble[i5]) i5++; 17 | if (humble[i] == 7*humble[i7]) i7++; 18 | } 19 | 20 | string s[] = {"st", "nd", "rd"}; 21 | int n; 22 | while (cin >> n && n != 0) 23 | printf("The %d%s humble number is %d.\n", n, (1 <= n % 10 && n % 10 <= 3 && !(11 <= n % 100 && n % 100 <= 13) ? s[n % 10 - 1].c_str() : "th"), humble[n-1]); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /UVa/011549.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int lambda, n; 8 | 9 | int f(int x) { 10 | return stoi(to_string((ll) x*x).substr(0, n)); 11 | } 12 | 13 | int brent(int x0) { 14 | int power = lambda = 1; 15 | int tortoise = x0, hare = f(x0), M = max(tortoise, hare); 16 | while (tortoise != hare) { 17 | if (power == lambda) { 18 | tortoise = hare; 19 | power *= 2; 20 | lambda = 0; 21 | } 22 | hare = f(hare); 23 | lambda++; 24 | M = max(M, hare); 25 | } 26 | 27 | return M; 28 | } 29 | 30 | int main() { 31 | int T; 32 | cin >> T; 33 | while (T--) { 34 | int k; 35 | cin >> n >> k; 36 | cout << brent(k) << endl; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /acepta-el-reto/0122.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int prev; 7 | while (cin >> prev && prev != -1) { 8 | int KM = 0, km = 0; 9 | int M = 0, m = 0, next; 10 | while (cin >> next && next != -1) { 11 | km++; 12 | if (next == prev) m++; 13 | else { 14 | if (m > M) { 15 | M = max(M, m); 16 | KM = km - 1 - m; 17 | } 18 | m = 0; 19 | prev = next; 20 | } 21 | } 22 | 23 | if (m > M) { 24 | M = max(M, m); 25 | KM = km - m; 26 | } 27 | 28 | if (M == 0) cout << "HOY NO COMEN" << endl; 29 | else printf("%d %d\n", KM, M); 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /UVa/011776.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef pair ii; 8 | typedef vector vii; 9 | 10 | int main() { 11 | int N; 12 | for (int t = 1; cin >> N && N != -1; t++) { 13 | vii intervals; 14 | while (N--) { 15 | int a, b; 16 | cin >> a >> b; 17 | intervals.push_back(ii(a, -1)); 18 | intervals.push_back(ii(b, +1)); 19 | } 20 | 21 | sort(intervals.begin(), intervals.end()); 22 | 23 | int enforcers = 0, maxEnforcers = 0; 24 | for (ii &p : intervals) { 25 | enforcers -= p.second; 26 | maxEnforcers = max(maxEnforcers, enforcers); 27 | } 28 | 29 | printf("Case %d: %d\n", t, maxEnforcers); 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /acepta-el-reto/0104.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool isBalanced(int pi, int di, int pd, int dd, int &w) { 6 | bool left = true; 7 | if (pi == 0) { 8 | int pii, dii, pdi, ddi; 9 | cin >> pii >> dii >> pdi >> ddi; 10 | left = isBalanced(pii, dii, pdi, ddi, pi); 11 | } 12 | 13 | bool right = true; 14 | if (pd == 0) { 15 | int pid, did, pdd, ddd; 16 | cin >> pid >> did >> pdd >> ddd; 17 | right = isBalanced(pid, did, pdd, ddd, pd); 18 | } 19 | 20 | w = pi + pd; 21 | return left && right && pi*di == pd*dd; 22 | } 23 | 24 | int main() { 25 | int pi, di, pd, dd, w; 26 | while (cin >> pi >> di >> pd >> dd && !(pi == 0 && di == 0 && pd == 0 && dd == 0)) 27 | cout << (isBalanced(pi, di, pd, dd, w) ? "SI" : "NO") << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /UVa/001644.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | 10 | bitset<10000000> bs; // 10^7 11 | vector primes; 12 | 13 | void sieve() { 14 | bs.set(); 15 | bs[0] = bs[1] = 0; 16 | for (ll i = 2; i < bs.size(); i++) 17 | if (bs[i]) { 18 | for (ll j = i * i; j < bs.size(); j += i) bs[j] = 0; 19 | primes.push_back(i); 20 | } 21 | } 22 | 23 | int main() { 24 | sieve(); 25 | int k; 26 | while (cin >> k && k != 0) { 27 | auto hi = lower_bound(primes.begin(), primes.end(), k); 28 | if (*hi == k) cout << 0 << endl; 29 | else { 30 | auto lo = hi; 31 | --lo; 32 | cout << *hi - *lo << endl; 33 | } 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /UVa/000138.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | 8 | void preprocess() { 9 | for (ll N = 2, cnt = 0; cnt < 10; N++) { 10 | ll n = (int) sqrt((N*N + N) / 2); 11 | if (n*n == (N*N + N) / 2) { 12 | cnt++; 13 | printf("%10lld%10lld\n", n, N); 14 | } 15 | } 16 | } 17 | 18 | int main() { 19 | // preprocess(); 20 | 21 | cout << " 6 8\n" 22 | " 35 49\n" 23 | " 204 288\n" 24 | " 1189 1681\n" 25 | " 6930 9800\n" 26 | " 40391 57121\n" 27 | " 235416 332928\n" 28 | " 1372105 1940449\n" 29 | " 7997214 11309768\n" 30 | " 46611179 65918161\n"; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /UVa/010394.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | typedef pair ii; 9 | typedef vector vi; 10 | 11 | bitset<20000001> bs; // 2*10^7 + 1 12 | vector primes; 13 | 14 | void sieve() { 15 | bs.set(); 16 | bs[0] = bs[1] = 0; 17 | for (ll i = 2; i < bs.size(); i++) if (bs[i]) { 18 | for (ll j = i * i; j < bs.size(); j += i) bs[j] = 0; 19 | primes.push_back(i); 20 | } 21 | } 22 | 23 | int main() { 24 | sieve(); 25 | vector twins; 26 | for (int i = 0; i < primes.size()-1; i++) 27 | if (primes[i+1] - primes[i] == 2) 28 | twins.push_back(ii(primes[i], primes[i+1])); 29 | 30 | int S; 31 | while (cin >> S) 32 | printf("(%d, %d)\n", twins[S-1].first, twins[S-1].second); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /UVa/011362.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | vector phones; 12 | int n; 13 | cin >> n; 14 | while (n--) { 15 | string s; 16 | cin >> s; 17 | phones.push_back(s); 18 | } 19 | 20 | sort(phones.begin(), phones.end()); 21 | 22 | bool consistent = true; 23 | for (int i = 0; i < phones.size() - 1; i++) { 24 | string p1 = phones[i]; 25 | string p2 = phones[i+1]; 26 | 27 | if (p2.find(p1) == 0) { 28 | consistent = false; 29 | break; 30 | } 31 | } 32 | 33 | cout << (consistent ? "YES" : "NO") << endl; 34 | } 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /UVa/000686.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | bitset<100000> bs; // 10^5 10 | vector primes; 11 | 12 | void sieve() { 13 | bs.set(); 14 | bs[0] = bs[1] = 0; 15 | for (ll i = 2; i < bs.size(); i++) 16 | if (bs[i]) { 17 | for (ll j = i * i; j < bs.size(); j += i) bs[j] = 0; 18 | primes.push_back(i); 19 | } 20 | } 21 | 22 | int main() { 23 | sieve(); 24 | int n; 25 | while (cin >> n && n != 0) { 26 | int cnt = 0; 27 | for (int i = 0; i < primes.size() && 2*primes[i] <= n; i++) 28 | for (int j = i; j < primes.size() && (primes[i] + primes[j] <= n); j++) 29 | if (primes[i] + primes[j] == n) cnt++; 30 | 31 | cout << cnt << endl; 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /UVa/001210.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef vector vi; 8 | 9 | bitset<10001> bs; 10 | vi primes; 11 | 12 | void sieve() { 13 | bs.set(); 14 | bs[0] = bs[1] = 0; 15 | for (int i = 2; i < bs.size(); i++) if (bs[i]) { 16 | for (int j = i * i; j < bs.size(); j += i) bs[j] = 0; 17 | primes.push_back(i); 18 | } 19 | } 20 | 21 | int main() { 22 | sieve(); 23 | int n; 24 | while (cin >> n && n != 0) { 25 | int cnt = 0; 26 | for (auto it = primes.begin(); it != primes.end() && *it <= n; ++it) { 27 | int sum = 0; 28 | for (auto i = it; i != primes.end() && sum < n; ++i) 29 | sum += *i; 30 | if (sum == n) cnt++; 31 | } 32 | cout << cnt << endl; 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /UVa/011059.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | typedef long long ll; 8 | 9 | int main() { 10 | int N; 11 | for (int M = 1; cin >> N; M++) { 12 | vi v; 13 | for (int i = 0; i < N; i++) { 14 | int x; 15 | cin >> x; 16 | v.push_back(x); 17 | } 18 | 19 | ll P = 1; 20 | bool pos = false; 21 | for (int i = 0; i < N; i++) { 22 | ll p = 1; 23 | for (int j = i; j < N; j++) { 24 | p *= v[j]; 25 | P = max(P, p); 26 | pos |= p > 0; 27 | } 28 | } 29 | 30 | if (pos) printf("Case #%d: The maximum product is %lld.\n\n", M, P); 31 | else printf("Case #%d: The maximum product is 0.\n\n", M); 32 | } 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /UVa/011961.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | char d[4] = {'A', 'C', 'G', 'T'}; 7 | set s; 8 | int N, K; 9 | 10 | void f(string &t, int k, int i) { 11 | if (k == K) { 12 | s.insert(t); 13 | return; 14 | } 15 | 16 | if (i == N) return; 17 | 18 | char c = t[i]; 19 | for (int j = 0; j < 4; j++) { 20 | t[i] = d[j]; 21 | f(t, k + 1, i + 1); 22 | } 23 | 24 | t[i] = c; 25 | f(t, k, i + 1); 26 | } 27 | 28 | int main() { 29 | int T; 30 | cin >> T; 31 | while (T--) { 32 | cin >> N >> K; 33 | s.clear(); 34 | string t; 35 | cin >> t; 36 | f(t, 0, 0); 37 | 38 | cout << s.size() << endl; 39 | for (const string &mutation : s) 40 | cout << mutation << endl; 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /UVa/001590.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | uint readIP() { 6 | uint d3, d2, d1, d0; 7 | scanf("%u.%u.%u.%u", &d3, &d2, &d1, &d0); 8 | return (d3 << 24) | (d2 << 16) | (d1 << 8) | d0; 9 | } 10 | 11 | void printIP(uint n) { 12 | printf("%u.%u.%u.%u\n", (n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff); 13 | } 14 | 15 | int main() { 16 | int m; 17 | while (cin >> m) { 18 | uint mask = -1, a = readIP(); 19 | m--; 20 | while (m--) { 21 | uint b = readIP(); 22 | b ^= a; 23 | if (b == 0) continue; // a == b, no need to expand subnet 24 | b = __builtin_clz(b); 25 | uint n = b == 0 ? -1 : (1 << (32 - b)) - 1; 26 | mask &= ~n; 27 | } 28 | 29 | printIP(a & mask); 30 | printIP(mask); 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /UVa/010928.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef vector vi; 8 | 9 | int main() { 10 | int N; 11 | cin >> N; 12 | while (N--) { 13 | vi v; 14 | int P; 15 | cin >> P; cin.get(); 16 | int n = 1000; 17 | for (int p = 1; p <= P; p++) { 18 | string s; 19 | getline(cin, s); 20 | stringstream ss(s); 21 | int x, cnt = 0; 22 | while (ss >> x) cnt++; 23 | if (cnt < n) { 24 | v.clear(); 25 | v.push_back(p); 26 | n = cnt; 27 | } else if (cnt == n) v.push_back(p); 28 | } 29 | 30 | printf("%d", v[0]); 31 | for (int i = 1; i < v.size(); i++) printf(" %d", v[i]); 32 | cout << endl; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /UVa/011063.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef vector vi; 8 | 9 | int main() { 10 | int N, t = 1; 11 | while (cin >> N) { 12 | vi v; 13 | set s; 14 | int x; 15 | cin >> x; 16 | bool b2 = 1 <= x; 17 | v.push_back(x); 18 | while (N-- > 1) { 19 | cin >> x; 20 | b2 &= (v[v.size()-1] < x); 21 | v.push_back(x); 22 | if (b2) 23 | for (int &y : v) { 24 | if (s.count(x + y)) { 25 | b2 = false; 26 | break; 27 | } 28 | s.insert(x + y); 29 | } 30 | } 31 | 32 | printf("Case #%d: It is %sa B2-Sequence.\n\n", t++, b2 ? "" : "not "); 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /UVa/011628.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | ll gcd(ll a, ll b) { 8 | while (b) { 9 | ll t = b; 10 | b = a % b; 11 | a = t; 12 | } 13 | return a; 14 | } 15 | 16 | int main() { 17 | int n, m; 18 | while (cin >> n >> m && !(n == 0 && m == 0)) { 19 | ll sum = 0, p[n]; 20 | for (int j = 0; j < n; j++) 21 | for (int i = 1; i <= m; i++) 22 | if (i == m) { 23 | cin >> p[j]; 24 | sum += p[j]; 25 | } else { 26 | int x; 27 | cin >> x; 28 | } 29 | 30 | for (int j = 0; j < n; j++) { 31 | ll g = gcd(p[j], sum); 32 | printf("%lld / %lld\n", p[j] / g, sum / g); 33 | } 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /UVa/012541.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int dateToInt(int m, int d, int y) { 7 | return 8 | 1461*(y+4800+(m-14)/12)/4+ 9 | 367*(m -2-(m-14)/12*12)/12- 10 | 3*((y+4900+(m-14)/12)/100)/4+ 11 | d-32075; 12 | } 13 | 14 | int main() { 15 | int n; 16 | cin >> n; 17 | 18 | string oldest, youngest; 19 | int o = INT_MAX, y = 0; 20 | 21 | while (n--) { 22 | string s; 23 | int dd, mm, yyyy; 24 | cin >> s >> dd >> mm >> yyyy; 25 | 26 | int j = dateToInt(mm, dd, yyyy); 27 | if (j > y) { 28 | y = j; 29 | youngest = s; 30 | } else if (j < o) { 31 | o = j; 32 | oldest = s; 33 | } 34 | } 35 | 36 | cout << youngest << endl << oldest << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /acepta-el-reto/0378.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef pair ii; 8 | typedef vector vii; 9 | 10 | int main() { 11 | int n; 12 | while (cin >> n && n != 0) { 13 | vii v; 14 | for (int i = 0; i < n; i++) { 15 | int x, y; 16 | cin >> x >> y; 17 | v.push_back(ii(x, y)); 18 | } 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | bool ok = true; 23 | for (int i = 1; i < n; i++) { 24 | ii q = v[i]; 25 | ii p = v[i-1]; 26 | 27 | // q ha gastado más que p 28 | if (q.first > p.first && q.second <= p.second) { 29 | ok = false; 30 | break; 31 | } 32 | } 33 | 34 | cout << (ok ? "SI" : "NO") << endl; 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /UVa/001727.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | while (T--) { 9 | string s1, s2; 10 | cin >> s1 >> s2; 11 | int mdays = 31, wday; 12 | 13 | if (s1 == "APR" || s1 == "JUN" || s1 == "SEP" || s1 == "NOV") mdays = 30; 14 | else if (s1 == "FEB") mdays = 28; 15 | 16 | if (s2 == "SUN") wday = 0; 17 | else if (s2 == "MON") wday = 1; 18 | else if (s2 == "TUE") wday = 2; 19 | else if (s2 == "WED") wday = 3; 20 | else if (s2 == "THU") wday = 4; 21 | else if (s2 == "FRI") wday = 5; 22 | else if (s2 == "SAT") wday = 6; 23 | 24 | int wkendays = 0; 25 | for (int i = 1; i <= mdays; i++, wday = (wday + 1) % 7) 26 | if (wday == 5 || wday == 6) wkendays++; 27 | 28 | cout << wkendays << endl; 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /UVa/010408.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef pair ii; 7 | 8 | struct comp { 9 | bool operator() (const ii &x, const ii &y) { 10 | return x.first * y.second < x.second * y.first; 11 | } 12 | }; 13 | 14 | set Fn; 15 | 16 | int gcd(int a, int b) { 17 | while (b) { 18 | int t = b; 19 | b = a % b; 20 | a = t; 21 | } 22 | return a; 23 | } 24 | 25 | int main() { 26 | for (int b = 1; b <= 1000; b++) 27 | for (int a = 1; a <= b; a++) { 28 | int g = gcd(a, b); 29 | Fn.insert(ii(a / g, b / g)); 30 | } 31 | 32 | int n, k; 33 | while (cin >> n >> k) { 34 | auto it = Fn.begin(); 35 | for (int i = 0; !(((*it).second <= n && ++i == k)); ++it) { } 36 | printf("%d/%d\n", (*it).first, (*it).second); 37 | } 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /UVa/000661.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n, m, cap, c[20]; 8 | bitset<20> b; 9 | for (int t = 1; cin >> n >> m >> cap && !(n == 0 && m == 0 && cap == 0); t++) { 10 | b.reset(); 11 | for (int i = 0; i < n; i++) cin >> c[i]; 12 | int x = 0, M = 0; 13 | while (m--) { 14 | int i; 15 | cin >> i; i--; 16 | if (b[i]) { 17 | b[i] = 0; 18 | x -= c[i]; 19 | } else { 20 | b[i] = 1; 21 | x += c[i]; 22 | M = max(M, x); 23 | } 24 | } 25 | 26 | printf("Sequence %d\n", t); 27 | if (M > cap) cout << "Fuse was blown." << endl << endl; 28 | else printf("Fuse was not blown.\nMaximal power consumption was %d amperes.\n\n", M); 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /UVa/011068.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct point { double x, y; }; 6 | struct line { int a, b, c; }; 7 | 8 | bool areParallel(line l1, line l2) { 9 | return l1.a == l2.a && l1.b == l2.b; 10 | } 11 | 12 | bool areIntersect(line l1, line l2, point &p) { 13 | if (areParallel(l1, l2)) return false; 14 | int det = l1.a*l2.b - l1.b*l2.a; 15 | p.x = (double) (l1.c*l2.b - l1.b*l2.c) / det + 0.0; // get rid of signed zero 16 | p.y = (double) (l1.a*l2.c - l1.c*l2.a) / det + 0.0; 17 | } 18 | 19 | int main() { 20 | line l1, l2; 21 | while (cin >> l1.a >> l1.b >> l1.c && !(l1.a == 0 && l1.b == 0 && l1.c == 0)) { 22 | cin >> l2.a >> l2.b >> l2.c; 23 | 24 | point p; 25 | if (areIntersect(l1, l2, p)) 26 | printf("The fixed point is at %.2f %.2f.\n", p.x, p.y); 27 | else cout << "No fixed point exists." << endl; 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /UVa/010071.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | int v, t; 34 | while (scanf("%d %d", &v, &t) == 2) 35 | cout << v*2*t << endl; 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /UVa/011121.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | div_t euc_div(int a, int d) { 7 | div_t divT = div(a, d); 8 | int I = divT.rem >= 0 ? 0 : (d > 0 ? 1 : -1); 9 | int qE = divT.quot - I; 10 | int rE = divT.rem + I * d; 11 | return div_t{qE, rE}; 12 | } 13 | 14 | int main() { 15 | int N; 16 | cin >> N; 17 | for (int t = 1; t <= N; t++) { 18 | printf("Case #%d: ", t); 19 | int n; 20 | cin >> n; 21 | stack s; 22 | if (n == 0) { 23 | cout << 0 << endl; 24 | continue; 25 | } 26 | 27 | while (n != 0) { 28 | div_t q = euc_div(n, -2); 29 | s.push(q.rem); 30 | n = (q.quot); 31 | } 32 | 33 | while (!s.empty()) { 34 | cout << s.top(); 35 | s.pop(); 36 | } 37 | cout << endl; 38 | } 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /UVa/012700.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | for (int t = 1; t <= T; t++) { 9 | int N; 10 | cin >> N; 11 | string s; 12 | cin >> s; 13 | int B = 0, W = 0, tie = 0, A = 0; 14 | for (char &c : s) { 15 | if (c == 'B') B++; 16 | else if (c == 'W') W++; 17 | else if (c == 'T') tie++; 18 | else A++; 19 | } 20 | 21 | printf("Case %d: ", t); 22 | if (B > 0 && W == 0 && tie == 0) cout << "BANGLAWASH" << endl; 23 | else if (W > 0 && B == 0 && tie == 0) cout << "WHITEWASH" << endl; 24 | else if (A == N) cout << "ABANDONED" << endl; 25 | else if (B == W) printf("DRAW %d %d\n", B, tie); 26 | else if (B > W) printf("BANGLADESH %d - %d\n", B, W); 27 | else printf("WWW %d - %d\n", W, B); 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /UVa/011151.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int N, memo[1000][1000]; 6 | string s; 7 | 8 | int len(int l, int r) { 9 | if (l == r) return 1; 10 | if (l+1 == r) { 11 | if (s[l] == s[r]) return 2; 12 | return 1; 13 | } 14 | 15 | if (memo[l][r] != 0) return memo[l][r]; 16 | 17 | if (s[l] == s[r]) return memo[l][r] = 2 + len(l+1, r-1); 18 | return memo[l][r] = max(len(l+1, r), len(l, r-1)); 19 | } 20 | 21 | int main() { 22 | int T; 23 | cin >> T; cin.get(); 24 | while (T--) { 25 | getline(cin, s); 26 | N = s.length(); 27 | 28 | if (N == 0) { 29 | cout << 0 << endl; 30 | continue; 31 | } 32 | 33 | for (int l = 0; l < N; l++) 34 | for (int r = l; r < N; r++) 35 | memo[l][r] = 0; 36 | 37 | cout << len(0, N-1) << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /UVa/000371.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | 8 | ll f(ll n) { 9 | if (n % 2 == 0) return n / 2; 10 | else return 3*n + 1; 11 | } 12 | 13 | map m; // cache 14 | 15 | ll len(ll n) { 16 | if (n == 1) return 0; 17 | if (m.count(n) > 0) return m[n]; 18 | return m[n] = len(f(n)) + 1; 19 | } 20 | 21 | int main() { 22 | ll A, B; 23 | while (cin >> A >> B && !(A == 0 && B == 0)) { 24 | ll L = min(A, B), H = max(A, B); 25 | ll V = 0, S = -1; 26 | for (ll n = L; n <= H; n++) { 27 | ll l; 28 | if (n == 1) l = 3; 29 | else l = len(n); 30 | 31 | if (l > S) { 32 | V = n; 33 | S = l; 34 | } 35 | } 36 | 37 | printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n", L, H, V, S); 38 | } 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /UVa/010055.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | unsigned long x, y; 34 | while(scanf("%ld %ld", &x, &y) == 2) cout << max(x, y) - min(x, y) << endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /UVa/011062.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | set st; 10 | string s, prep = ""; 11 | while (getline(cin, s)) { 12 | for (char &c : s) 13 | if (!isalpha(c) && c != '-') c = ' '; 14 | 15 | stringstream ss(s); 16 | string word; 17 | while (ss >> word) { 18 | transform(word.begin(), word.end(), word.begin(), ::tolower); 19 | if (!prep.empty()) { 20 | word.insert(0, prep); 21 | prep = ""; 22 | } 23 | if (ss.eof() && word[word.length()-1] == '-') { 24 | word.pop_back(); 25 | prep = word; 26 | } else st.insert(word); 27 | } 28 | } 29 | 30 | if (!prep.empty()) st.insert(prep); 31 | for (const string &word : st) cout << word << endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /UVa/010141.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n, p; 8 | for (int t = 1; cin >> n >> p && !(n == 0 && p == 0); t++) { 9 | cin.get(); 10 | if (t > 1) cout << endl; 11 | 12 | string s; 13 | for (int i = 0; i < n; i++) 14 | getline(cin, s); 15 | 16 | string prop; 17 | int compliance = 0; 18 | double price = DBL_MAX; 19 | for (int i = 0; i < p; i++) { 20 | double d; 21 | int r; 22 | getline(cin, s); 23 | cin >> d >> r; cin.get(); 24 | if (r > compliance || r == compliance && d < price) { 25 | prop = s; 26 | compliance = r; 27 | price = d; 28 | } 29 | 30 | for (int j = 0; j < r; j++) getline(cin, s); 31 | } 32 | 33 | printf("RFP #%d\n%s\n", t, prop.c_str()); 34 | } 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /UVa/011629.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int p, g; 8 | cin >> p >> g; 9 | map m; 10 | while (p--) { 11 | char s[100]; 12 | int i, d; 13 | scanf("%s %d.%d", s, &i, &d); 14 | m[s] = i*10 + d; 15 | } 16 | 17 | for (int t = 1; t <= g; t++) { 18 | int cnt = 0; 19 | string s; 20 | while (cin >> s && !(s == "<" || s == "<=" || s == "=" || s == ">=" || s == ">")) 21 | if (s != "+") cnt += m[s]; 22 | 23 | int res; 24 | cin >> res; 25 | res *= 10; 26 | bool c = (s == "<" && cnt < res) || 27 | (s == "<=" && cnt <= res) || 28 | (s == "=" && cnt == res) || 29 | (s == ">=" && cnt >= res) || 30 | (s == ">" && cnt > res); 31 | 32 | printf("Guess #%d was %scorrect.\n", t, c ? "" : "in"); 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /acepta-el-reto/0207.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int dr[] = {1,1,0,-1,-1,-1,0,1}; 6 | int dc[] = {0,1,1,1,0,-1,-1,-1}; 7 | 8 | int main() { 9 | int c, f, g[50][50]; 10 | while (cin >> c >> f && !(c == 0 && f == 0)) { 11 | for (int i = 0; i < f; i++) 12 | for (int j = 0; j < c; j++) 13 | g[i][j] = 0; 14 | 15 | int a, cnt = 0; 16 | cin >> a; 17 | while (a--) { 18 | int x, y; 19 | cin >> y >> x; 20 | x--, y--; 21 | if (g[x][y] == 1) cnt--; 22 | g[x][y] = 2; 23 | for (int d = 0; d < 8; d++) { 24 | int i = x + dr[d], j = y + dc[d]; 25 | if (0 <= i && i < f && 0 <= j && j < c && g[i][j] == 0) { 26 | g[i][j] = 1; 27 | cnt++; 28 | } 29 | } 30 | } 31 | 32 | cout << cnt << endl; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /project-euler/0001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | int sum = 0; 34 | int M = 1000; 35 | while (--M) 36 | sum += (M % 5 == 0 || M % 3 == 0 ? M : 0); 37 | cout << sum << endl; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /UVa/001239.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string s; 6 | int memo[1000][1000]; 7 | 8 | int kpalindrome (int i, int j) { 9 | if (i >= j) return 0; 10 | 11 | if (memo[i][j] != -1) return memo[i][j]; 12 | 13 | return memo[i][j] = (s[i] == s[j] ? 0 : 1) + kpalindrome(i+1, j-1); 14 | } 15 | 16 | int main() { 17 | int T; 18 | cin >> T; 19 | while (T--) { 20 | int K; 21 | cin >> s >> K; 22 | 23 | for (int i = 0; i < s.length(); i++) 24 | for (int j = i + 1; j < s.length(); j++) 25 | memo[i][j] = -1; 26 | 27 | for (int l = s.length(); ; l--) 28 | // iterate over all substrings of length l 29 | for (int i = 0; i + l <= s.length(); i++) { 30 | int j = i + l - 1; 31 | if (kpalindrome(i, j) <= K) { 32 | cout << l << endl; 33 | goto next; 34 | } 35 | } 36 | next:; 37 | } 38 | } -------------------------------------------------------------------------------- /UVa/000260.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | char grid[200][200]; 6 | int N; 7 | 8 | int dr[] = {-1,-1,0,0,1,1}; 9 | int dc[] = {-1,0,-1,1,0,1}; 10 | 11 | bool white(int r, int c) { 12 | if (r < 0 || r >= N || c < 0 || c >= N) return false; 13 | if (grid[r][c] != 'w') return false; 14 | if (c == N-1) return true; 15 | grid[r][c] = 'x'; 16 | for (int d = 0; d < 6; d++) 17 | if (white(r + dr[d], c + dc[d])) 18 | return true; 19 | return false; 20 | } 21 | 22 | int main() { 23 | for (int t = 1; cin >> N && N != 0; t++) { 24 | for (int i = 0; i < N; i++) 25 | for (int j = 0; j < N; j++) 26 | cin >> grid[i][j]; 27 | 28 | bool white_wins = false; 29 | for (int i = 0; i < N; i++) 30 | if (white(i, 0)) { 31 | white_wins = true; 32 | break; 33 | } 34 | 35 | printf("%d %c\n", t, (white_wins ? 'W' : 'B')); 36 | } 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /UVa/010183.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.NavigableSet; 3 | import java.util.Scanner; 4 | import java.util.TreeSet; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | BigInteger L = new BigInteger("10").pow(100); 10 | NavigableSet s = new TreeSet<>(); 11 | BigInteger f = BigInteger.ONE; 12 | BigInteger g = f.add(BigInteger.ONE); 13 | s.add(f); 14 | while (g.compareTo(L) <= 0) { 15 | s.add(g); 16 | BigInteger aux = g; 17 | g = f.add(g); 18 | f = aux; 19 | } 20 | 21 | Scanner sc = new Scanner(System.in); 22 | while (sc.hasNextBigInteger()) { 23 | BigInteger a = sc.nextBigInteger(); 24 | BigInteger b = sc.nextBigInteger(); 25 | if (BigInteger.ZERO.equals(a) && BigInteger.ZERO.equals(b)) break; 26 | 27 | System.out.println(s.tailSet(a, true).headSet(b, true).size()); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /UVa/000458.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | string s; 34 | while (getline(cin, s)) { 35 | for (int i = 0; i < s.length(); i++) 36 | cout << char(s[i] - 7); 37 | cout << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /UVa/000755.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | string r = "2223334445556667Q77888999"; 9 | int T; 10 | cin >> T; 11 | while (T--) { 12 | int n; 13 | cin >> n; 14 | set s; 15 | map dup; 16 | while (n--) { 17 | string t; 18 | cin >> t; 19 | for (int i = 0; i < t.length(); i++) { 20 | if (isdigit(t[i])) continue; 21 | if (t[i] == '-') { t.erase(i, 1); i--; } 22 | else t[i] = r[t[i] - 'A']; 23 | } 24 | if (t[3] != '-') t.insert(3, "-"); 25 | 26 | if (s.count(t) > 0) dup[t]++; 27 | else s.insert(t); 28 | } 29 | 30 | if (dup.empty()) cout << "No duplicates." << endl; 31 | else for (auto &p : dup) printf("%s %d\n", p.first.c_str(), 1 + p.second); 32 | 33 | if (T > 0) cout << endl; 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /UVa/010374.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin >> T; 9 | while (T--) { 10 | map ctp; 11 | map votes; 12 | int n; 13 | string c; 14 | cin >> n; getline(cin, c); 15 | while (n--) { 16 | string p; 17 | getline(cin, c); 18 | getline(cin, p); 19 | ctp[c] = p; 20 | votes[c] = 0; 21 | } 22 | 23 | int m, M = 0; 24 | cin >> m; getline(cin, c); 25 | string winner = "tie"; 26 | while (m--) { 27 | getline(cin, c); 28 | if (votes.count(c) > 0) { 29 | votes[c]++; 30 | if (votes[c] > M) M = votes[c], winner = ctp[c]; 31 | else if (votes[c] == M) winner = "tie"; 32 | } 33 | } 34 | 35 | cout << winner << endl; 36 | if (T > 0) cout << endl; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /UVa/010079.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | long N; 37 | while (scanf("%ld", &N), N >= 0) 38 | printf("%ld\n", N*(N+1)/2+1); 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /UVa/001757.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool adjMat[26][26]; 6 | 7 | void floyd() { 8 | for (int k = 0; k < 26; k++) 9 | for (int i = 0; i < 26; i++) 10 | for (int j = 0; j < 26; j++) 11 | adjMat[i][j] |= adjMat[i][k] && adjMat[k][j]; 12 | } 13 | 14 | int main() { 15 | int m, n; 16 | while (cin >> m >> n) { 17 | for (int i = 0; i < 26; i++) 18 | for (int j = 0; j < 26; j++) 19 | adjMat[i][j] = i == j; 20 | 21 | while (m--) { 22 | char x, y; 23 | cin >> x >> y; 24 | adjMat[x - 'a'][y - 'a'] = true; 25 | } 26 | 27 | floyd(); 28 | 29 | while (n--) { 30 | string s, t; 31 | cin >> s >> t; 32 | bool ok = s.length() == t.length(); 33 | if (ok) for (int k = 0; ok && k < s.length(); k++) ok &= adjMat[s[k] - 'a'][t[k] - 'a']; 34 | cout << (ok ? "yes" : "no") << endl; 35 | } 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /UVa/010699.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | 10 | bitset<1000001> bs; // 10^6 + 1 11 | vector primes; 12 | 13 | void sieve() { 14 | bs.set(); 15 | bs[0] = bs[1] = 0; 16 | for (ll i = 2; i < bs.size(); i++) 17 | if (bs[i]) { 18 | for (ll j = i * i; j < bs.size(); j += i) bs[j] = 0; 19 | primes.push_back(i); 20 | } 21 | } 22 | 23 | set primeFactors(int N) { 24 | set factors; 25 | int PF_idx = 0; ll PF = primes[PF_idx]; 26 | while (PF * PF <= N) { 27 | while (N % PF == 0) { 28 | N /= PF; 29 | factors.insert(PF); 30 | } 31 | PF = primes[++PF_idx]; 32 | } 33 | if (N != 1) factors.insert((ll) N); // N is prime 34 | return factors; 35 | } 36 | 37 | int main() { 38 | sieve(); 39 | int N; 40 | while (cin >> N && N != 0) 41 | printf("%d : %d\n", N, primeFactors(N).size()); 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /UVa/011332.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | long n; 37 | 38 | while (scanf("%ld", &n) && n != 0) 39 | cout << (n % 9 == 0 ? 9 : n % 9) << endl; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /UVa/012470.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | struct Matrix { ll mat[3][3]; }; 8 | 9 | int mod = 1000000009; 10 | 11 | Matrix matMul(Matrix &a, Matrix &b) { 12 | Matrix ans; 13 | for (int i = 0; i < 3; i++) 14 | for (int j = 0; j < 3; j++) 15 | for (int k = ans.mat[i][j] = 0; k < 3; k++) 16 | ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % mod; 17 | return ans; 18 | } 19 | 20 | int T(ll n) { 21 | Matrix ans; 22 | for (int i = 0; i < 3; i++) 23 | for (int j = 0; j < 3; j++) 24 | ans.mat[i][j] = i == j; 25 | 26 | Matrix base = {{{0,1,0}, {0,0,1}, {1,1,1}}}; 27 | n--; 28 | while (n) { 29 | if (n % 2 == 1) ans = matMul(ans, base); 30 | base = matMul(base, base); 31 | n /= 2; 32 | } 33 | 34 | return (ans.mat[0][1] + ans.mat[0][2]*2) % mod; 35 | } 36 | 37 | int main() { 38 | ll n; 39 | while (cin >> n && n != 0) 40 | cout << T(n) << endl; 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /project-euler/0002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | long a = 1, b = 2, c, sum = 0; 34 | while (a < 4000000) { 35 | if (a % 2 == 0) sum += a; 36 | c = b; 37 | b += a; 38 | a = c; 39 | } 40 | cout << sum << endl; 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /UVa/010533.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | 10 | bitset<1000000> bs; // 10^6 11 | vector dprimes; 12 | 13 | void sieve() { 14 | bs.set(); 15 | bs[0] = bs[1] = 0; 16 | for (ll i = 2; i < bs.size(); i++) 17 | if (bs[i]) { 18 | for (ll j = i * i; j < bs.size(); j += i) bs[j] = 0; 19 | ll k = i; 20 | int sum = 0; 21 | while (k) { 22 | sum += k % 10; 23 | k /= 10; 24 | } 25 | 26 | if (bs[sum]) dprimes.push_back(i); 27 | } 28 | } 29 | 30 | int main() { 31 | sieve(); 32 | int N; 33 | cin >> N; 34 | while (N--) { 35 | int t1, t2; 36 | cin >> t1 >> t2; 37 | auto it1 = lower_bound(dprimes.begin(), dprimes.end(), t1); 38 | auto it2 = upper_bound(dprimes.begin(), dprimes.end(), t2); 39 | cout << distance(it1, it2) << endl; 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /UVa/011181.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef vector vd; 8 | 9 | int main() { 10 | int N, r; 11 | for (int t = 1; cin >> N >> r && !(N == 0 && r == 0); t++) { 12 | vd p, q; 13 | vector v; 14 | for (int i = 0; i < N; i++) { 15 | double x; 16 | cin >> x; 17 | q.push_back(0); 18 | p.push_back(x); 19 | v.push_back(i >= (N - r)); 20 | } 21 | 22 | double P = 0; 23 | do { 24 | double prob = 1; 25 | for (int i = 0; i < N; i++) 26 | prob *= v[i] ? p[i] : (1 - p[i]); 27 | 28 | for (int i = 0; i < N; i++) 29 | if (v[i]) q[i] += prob; 30 | 31 | P += prob; 32 | } while (next_permutation(v.begin(), v.end())); 33 | 34 | printf("Case %d:\n", t); 35 | for (int i = 0; i < N; i++) 36 | printf("%.6f\n", q[i] / P); 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /UVa/012160.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int r[10], d[10000], R; 7 | 8 | int bfs(int u, int U) { 9 | fill(d, d + 10000, -1); 10 | d[u] = 0; 11 | queue q; 12 | q.push(u); 13 | while (!q.empty()) { 14 | u = q.front(); q.pop(); 15 | if (u == U) return d[u]; 16 | else { 17 | for (int i = 0; i < R; i++) { 18 | int v = (u + r[i]) % 10000; 19 | if (d[v] == -1) { 20 | d[v] = d[u] + 1; 21 | q.push(v); 22 | } 23 | } 24 | } 25 | } 26 | 27 | return -1; 28 | } 29 | 30 | int main() { 31 | int L, U, t = 1; 32 | while (cin >> L >> U >> R && !(L == 0 && U == 0 && R == 0)) { 33 | for (int i = 0; i < R; i++) cin >> r[i]; 34 | 35 | printf("Case %d: ", t++); 36 | int moves = bfs(L, U); 37 | if (moves == -1) cout << "Permanently Locked" << endl; 38 | else cout << moves << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /project-euler/0005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | int i = 2520; 34 | while (i += 20) 35 | if (i % 20 == 0 && i % 19 == 0 && i % 18 == 0 && i % 17 == 0 && i % 16 == 0 && i % 13 == 0 && i % 11 == 0 && i % 7 == 0) break; 36 | cout << i << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /UVa/000406.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | typedef vector vi; 10 | 11 | bitset<1001> bs; 12 | vi primes; 13 | 14 | void sieve() { 15 | bs.set(); 16 | bs[0] = 0; 17 | bs[1] = 1; // in this problem, 1 is considered to be a prime number 18 | primes.push_back(1); 19 | for (int i = 2; i < bs.size(); i++) if (bs[i]) { 20 | for (int j = i * i; j < bs.size(); j += i) bs[j] = 0; 21 | primes.push_back(i); 22 | } 23 | } 24 | 25 | int main() { 26 | sieve(); 27 | int N, C; 28 | while (cin >> N >> C) { 29 | int L = distance(primes.begin(), upper_bound(primes.begin(), primes.end(), N)); 30 | int j = L / 2; 31 | j = (L % 2 == 0 ? max(0, j - 1 - (C-1)) : max(0, j - (C - 1))); 32 | printf("%d %d:", N, C); 33 | for (int i = (L % 2 == 0 ? 2*C : 2*C - 1); i > 0 && j < L; i--, j++) 34 | cout << ' ' << primes[j]; 35 | cout << endl << endl; 36 | } 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /UVa/010703.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int a[500][500]; 7 | 8 | int main() { 9 | int W, H, N; 10 | while (cin >> W >> H >> N && !(W == 0 && H == 0 && N == 0)) { 11 | memset(a, 0, sizeof a); 12 | while (N--) { 13 | int u1, v1, u2, v2; 14 | cin >> u1 >> v1 >> u2 >> v2; 15 | u1--, v1--, u2--, v2--; 16 | int x1 = min(u1, u2), x2 = max(u1, u2); 17 | int y1 = min(v1, v2), y2 = max(v1, v2); 18 | 19 | for (int i = x1; i <= x2; i++) 20 | for (int j = y1; j <= y2; j++) 21 | a[i][j] = 1; 22 | } 23 | 24 | int s = 0; 25 | for (int i = 0; i < W; i++) 26 | for (int j = 0; j < H; j++) 27 | if (a[i][j] == 0) 28 | s++; 29 | 30 | if (s == 0) cout << "There is no empty spots." << endl; 31 | else if (s == 1) cout << "There is one empty spot." << endl; 32 | else printf("There are %d empty spots.\n", s); 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /UVa/000884.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | bitset<1000001> bs; // 10^6 10 | vector primes; 11 | 12 | void sieve() { 13 | bs.set(); 14 | bs[0] = bs[1] = 0; 15 | for (ll i = 2; i < bs.size(); i++) 16 | if (bs[i]) { 17 | for (ll j = i * i; j < bs.size(); j += i) bs[j] = 0; 18 | primes.push_back(i); 19 | } 20 | } 21 | 22 | int numPF(ll N) { 23 | int PF_idx = 0, ans = 0; 24 | ll PF = primes[PF_idx]; 25 | while (PF * PF <= N) { 26 | while (N % PF == 0) { 27 | N /= PF; 28 | ans++; 29 | } 30 | PF = primes[++PF_idx]; 31 | } 32 | if (N != 1) ans++; // N is prime 33 | return ans; 34 | } 35 | 36 | int main() { 37 | sieve(); 38 | int sols[1000001]; 39 | sols[2] = 1; 40 | for (int n = 2; n <= 1000000; n++) 41 | sols[n] = sols[n-1] + numPF(n); 42 | 43 | int n; 44 | while (cin >> n) 45 | cout << sols[n] << endl; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /UVa/001112.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define INF 1000000000 // 10^9 6 | 7 | int N, adjMat[100][100]; 8 | 9 | void floyd() { 10 | for (int k = 0; k < N; k++) 11 | for (int i = 0; i < N; i++) 12 | for (int j = 0; j < N; j++) 13 | adjMat[i][j] = min(adjMat[i][j], adjMat[i][k] + adjMat[k][j]); 14 | } 15 | 16 | int main() { 17 | int nc; 18 | cin >> nc; 19 | while (nc--) { 20 | int E, T, M; 21 | cin >> N >> E >> T >> M; 22 | for (int u = 0; u < N; u++) 23 | for (int v = 0; v < N; v++) 24 | adjMat[u][v] = u == v ? 0 : INF; 25 | 26 | while (M--) { 27 | int a, b, e; 28 | cin >> a >> b >> e; 29 | a--, b--; 30 | adjMat[a][b] = e; 31 | } 32 | 33 | floyd(); 34 | 35 | E--; 36 | int cnt = 0; 37 | for (int u = 0; u < N; u++) 38 | if (adjMat[u][E] <= T) 39 | cnt++; 40 | 41 | cout << cnt << endl; 42 | if (nc > 0) cout << endl; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /UVa/010300.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | int t, f, a, b, c, s; 34 | cin >> t; 35 | while (t--) { 36 | s = 0; 37 | cin >> f; 38 | while (f--) { 39 | cin >> a >> b >> c; 40 | s += a * c; 41 | } 42 | cout << s << endl; 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /UVa/000465.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | 9 | BigInteger max = new BigInteger("2147483647"); 10 | while (in.hasNext()) { 11 | String s1 = in.next(); 12 | String op = in.next(); 13 | String s2 = in.next(); 14 | BigInteger a = new BigInteger(s1); 15 | BigInteger b = new BigInteger(s2); 16 | System.out.println(s1 + " " + op + " " + s2); 17 | if (a.compareTo(max) > 0) 18 | System.out.println("first number too big"); 19 | if (b.compareTo(max) > 0) 20 | System.out.println("second number too big"); 21 | 22 | BigInteger res; 23 | if (op.equals("+")) 24 | res = a.add(b); 25 | else 26 | res = a.multiply(b); 27 | if (res.compareTo(max) > 0) { 28 | System.out.println("result too big"); 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /UVa/000496.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | string s, t; 11 | while (getline(cin, s) && getline(cin, t)) { 12 | stringstream ss(s), st(t); 13 | int a, b; 14 | set A, B; 15 | while (ss >> a) A.insert(a); 16 | while (st >> b) B.insert(b); 17 | 18 | if (A == B) { 19 | cout << "A equals B" << endl; 20 | continue; 21 | } 22 | 23 | vector AB(min(A.size(), B.size())); 24 | auto it = set_intersection(A.begin(), A.end(), B.begin(), B.end(), AB.begin()); 25 | AB.resize(it - AB.begin()); 26 | 27 | if (AB.empty()) cout << "A and B are disjoint" << endl; 28 | else if (includes(A.begin(), A.end(), B.begin(), B.end())) cout << "B is a proper subset of A" << endl; 29 | else if (includes(B.begin(), B.end(), A.begin(), A.end())) cout << "A is a proper subset of B" << endl; 30 | else cout << "I'm confused!" << endl; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /codeforces/825B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int dr[] = {1,1,1,0}; 6 | int dc[] = {-1,0,1,1}; 7 | 8 | int main() { 9 | char g[10][10]; 10 | for (int i = 0; i < 10; i++) 11 | for (int j = 0; j < 10; j++) 12 | cin >> g[i][j]; 13 | 14 | bool wins = false; 15 | for (int i = 0; i < 10; i++) 16 | for (int j = 0; j < 10; j++) 17 | for (int d = 0; d < 4; d++) { 18 | int crosses = 0, empty = 0, fi = i + 4*dr[d], fj = j + 4*dc[d]; 19 | if (fi >= 10 || fj < 0 || fj >= 10) continue; 20 | for (int ni = i, nj = j; !(ni == fi + dr[d] && nj == fj + dc[d]); ni = ni + dr[d], nj = nj + dc[d]) { 21 | if (g[ni][nj] == 'X') crosses++; 22 | else if (g[ni][nj] == '.') empty++; 23 | } 24 | 25 | if (crosses == 4 && empty == 1) { 26 | wins = true; 27 | goto done; 28 | } 29 | } 30 | 31 | done: 32 | cout << (wins ? "YES" : "NO") << endl; 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /UVa/010127.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | int n; 34 | while (cin >> n) { 35 | int t = 1; 36 | unsigned long long x = 1; 37 | while (t % n != 0) { 38 | t = t * 10 + 1; 39 | t = t % n; 40 | x++; 41 | } 42 | cout << x << endl; 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /gcj/gcj 2010 Africa/Qualification Round/A/A-large-practice.out: -------------------------------------------------------------------------------- 1 | Case #1: 2 3 2 | Case #2: 1 4 3 | Case #3: 4 5 4 | Case #4: 29 46 5 | Case #5: 11 56 6 | Case #6: 84 240 7 | Case #7: 413 584 8 | Case #8: 28 80 9 | Case #9: 380 633 10 | Case #10: 190 242 11 | Case #11: 450 667 12 | Case #12: 7 126 13 | Case #13: 208 636 14 | Case #14: 301 831 15 | Case #15: 243 649 16 | Case #16: 258 429 17 | Case #17: 18 49 18 | Case #18: 7 32 19 | Case #19: 750 965 20 | Case #20: 196 718 21 | Case #21: 292 1236 22 | Case #22: 539 935 23 | Case #23: 20 263 24 | Case #24: 30 107 25 | Case #25: 3 94 26 | Case #26: 425 484 27 | Case #27: 623 923 28 | Case #28: 219 656 29 | Case #29: 133 242 30 | Case #30: 691 800 31 | Case #31: 317 338 32 | Case #32: 207 485 33 | Case #33: 63 64 34 | Case #34: 13 206 35 | Case #35: 407 421 36 | Case #36: 654 972 37 | Case #37: 205 275 38 | Case #38: 499 883 39 | Case #39: 471 633 40 | Case #40: 371 393 41 | Case #41: 25 170 42 | Case #42: 64 521 43 | Case #43: 28 78 44 | Case #44: 949 1590 45 | Case #45: 1 1043 46 | Case #46: 636 773 47 | Case #47: 240 908 48 | Case #48: 6 47 49 | Case #49: 81 288 50 | Case #50: 274 983 51 | -------------------------------------------------------------------------------- /UVa/011239.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | while (true) { 10 | string s; 11 | map> m; 12 | set st; 13 | getline(cin, s); 14 | while (!(s == "1" || s == "0")) { 15 | m[s]; 16 | string t; 17 | while (getline(cin, t) && !(t == "1" || t == "0") && all_of(t.begin(), t.end(), [](char ch) { return islower(ch) || isdigit(ch); })) { 18 | if (m[s].count(t) == 0 && st.count(t) > 0) 19 | for (auto &p : m) p.second.erase(t); 20 | else { 21 | m[s].insert(t); 22 | st.insert(t); 23 | } 24 | } 25 | s = t; 26 | } 27 | 28 | set> sol; 29 | for (auto &p : m) sol.insert(make_pair(-p.second.size(), p.first)); 30 | 31 | for (auto &p : sol) printf("%s %d\n", p.second.c_str(), -p.first); 32 | if (s == "0") break; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /UVa/000352.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int N; 6 | int grid[25][25]; 7 | 8 | int dr[] = {1,1,0,-1,-1,-1,0,1}; 9 | int dc[] = {0,1,1,1,0,-1,-1,-1}; 10 | 11 | int floodfill(int r, int c) { 12 | if (r < 0 || r >= N || c < 0 || c >= N) return 0; 13 | if (grid[r][c] != 1) return 0; 14 | int ans = 1; 15 | grid[r][c] = 0; 16 | for (int d = 0; d < 8; d++) 17 | ans += floodfill(r + dr[d], c + dc[d]); 18 | return ans; 19 | } 20 | 21 | int main() { 22 | for (int t = 1; cin >> N; t++) { 23 | cin.get(); 24 | for (int i = 0; i < N; i++) { 25 | string s; 26 | getline(cin, s); 27 | for (int j = 0; j < N; j++) 28 | grid[i][j] = s[j] - '0'; 29 | } 30 | 31 | int eagles = 0; 32 | for (int i = 0; i < N; i++) 33 | for (int j = 0; j < N; j++) 34 | if (grid[i][j] == 1) { 35 | floodfill(i, j); 36 | eagles++; 37 | } 38 | 39 | printf ("Image number %d contains %d war eagles.\n", t, eagles); 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /UVa/010346.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int f(int n, int k) { 36 | if (n < k) return n; 37 | return (n - n%k) + f(n/k + n%k, k); 38 | } 39 | 40 | int main() { 41 | int n, k; 42 | while (scanf("%d %d", &n, &k) == 2) 43 | cout << f(n, k) << endl; 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /UVa/012403.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | int n; 34 | cin >> n; 35 | string word; int buff; int total = 0; 36 | while (n--) { 37 | cin >> word; 38 | if (word == "report") cout << total << endl; 39 | if (word == "donate") { 40 | cin >> buff; 41 | total += buff; 42 | } 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /UVa/000535.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define PI acos(-1) 8 | 9 | double toRad(double deg) { 10 | return deg / 180 * PI; 11 | } 12 | 13 | double dist(double phi1, double theta1, double phi2, double theta2) { 14 | double dot = sin(phi1) * sin(phi2) * cos(theta1 - theta2) + cos(phi1) * cos(phi2); 15 | if (dot > 1) dot = 1; 16 | if (dot < -1) dot = -1; 17 | return acos(dot); 18 | } 19 | 20 | int main() { 21 | map> m; 22 | string s; 23 | while (cin >> s && s != "#") { 24 | double lat, lng; 25 | cin >> lat >> lng; 26 | double phi = toRad(90 - lat), theta = toRad(lng); 27 | m[s] = make_pair(phi, theta); 28 | } 29 | 30 | string A, B; 31 | while (cin >> A >> B && !(A == "#" && B == "#")) { 32 | printf("%s - %s\n", A.c_str(), B.c_str()); 33 | if (m.count(A) > 0 && m.count(B) > 0) 34 | printf("%d km\n", (int) round(6378 * dist(m[A].first, m[A].second, m[B].first, m[B].second))); 35 | else cout << "Unknown" << endl; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /UVa/012704.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | int t; 37 | double x, y, r; 38 | 39 | cin >> t; 40 | while (t--) { 41 | cin >> x >> y >> r; 42 | double d = hypot(x, y); 43 | printf("%.2f %.2f\n", r - d, 2*r-(r-d)); 44 | } 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /UVa/012614.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | int n; 34 | cin >> n; 35 | int cards; int a[100000]; 36 | for (int i = 0; i < n; i++) { 37 | cin >> cards; 38 | for (int j = 0; j < cards; j++) 39 | cin >> a[j]; 40 | sort(a, a+cards); 41 | cout << "Case " << i+1 << ": " << a[cards-1] << endl; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /UVa/012832.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | ll gcd(ll a, ll b) { 8 | while (b) { 9 | ll t = b; 10 | b = a % b; 11 | a = t; 12 | } 13 | return a; 14 | } 15 | 16 | struct frac { 17 | ll n, d; 18 | 19 | frac() : n(0), d(1) { }; 20 | 21 | frac(ll _n, ll _d) : n(_n), d(_d) { 22 | simplify(); 23 | } 24 | 25 | void add(struct frac &o) { 26 | n = n*o.d + o.n*d; 27 | d *= o.d; 28 | simplify(); 29 | } 30 | 31 | void simplify() { 32 | ll g = gcd(n, d); 33 | n /= g, d /= g; 34 | } 35 | }; 36 | 37 | // https://en.wikipedia.org/wiki/Poisson_binomial_distribution 38 | int main() { 39 | int T; 40 | cin >> T; 41 | for (int t = 1; t <= T; t++) { 42 | int m; 43 | cin >> m; 44 | frac sol; 45 | for (int i = 0; i < m; i++) { 46 | int ni, ki; 47 | cin >> ni >> ki; 48 | frac f(ki, ni); 49 | sol.add(f); 50 | } 51 | 52 | printf("Case %d: %lld/%lld\n", t, sol.n, sol.d); 53 | } 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /acepta-el-reto/0129.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a[11][7] = { 7 | {1,1,1,1,1,1,0}, 8 | {0,1,1,0,0,0,0}, 9 | {1,1,0,1,1,0,1}, 10 | {1,1,1,1,0,0,1}, 11 | {0,1,1,0,0,1,1}, 12 | {1,0,1,1,0,1,1}, 13 | {1,0,1,1,1,1,1}, 14 | {1,1,1,0,0,0,0}, 15 | {1,1,1,1,1,1,1}, 16 | {1,1,1,1,0,1,1}, 17 | {0,0,0,0,0,0,0} // blank 18 | }; 19 | 20 | int adjMat[11][11]; 21 | for (int u = 0; u < 11; u++) 22 | for (int v = u; v < 11; v++) { 23 | int cnt = 0; 24 | for (int k = 0; k < 7; k++) 25 | if (a[u][k] != a[v][k]) cnt++; 26 | adjMat[u][v] = adjMat[v][u] = cnt; 27 | } 28 | 29 | int n; 30 | while (cin >> n && n != -1) { 31 | int N = 1, cnt = adjMat[10][n], m; 32 | while (cin >> m && m != -1) { 33 | N++; 34 | cnt += adjMat[n][m]; 35 | n = m; 36 | } 37 | cnt += adjMat[n][10]; 38 | 39 | cout << N*cnt << endl; 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /UVa/000579.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | int h, m; 37 | while (scanf("%d:%d", &h, &m) && !(h == 0 && m == 0)) { 38 | double a1 = max(h*30+m*0.5, (double)6*m), a2 = min(h*30+m*0.5, (double)6*m); 39 | printf("%.3f\n", min(a1-a2, 360-a1+a2)); 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /UVa/010029.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | map m; 8 | string t; 9 | int M = 0; 10 | while (cin >> t) { 11 | int l = 1; 12 | for (int i = 0; i <= t.size(); i++) 13 | for (char c = 'a'; c <= 'z'; c++) { 14 | string s = t; 15 | s.insert(i, 1, c); 16 | if (s > t) break; 17 | if (m.count(s) > 0) l = max(l, m[s] + 1); 18 | } 19 | 20 | for (int i = 0; i < t.size(); i++) { 21 | string s = t; 22 | s.erase(i, 1); 23 | if (s < t && m.count(s) > 0) l = max(l, m[s] + 1); 24 | } 25 | 26 | for (int i = 0; i < t.size(); i++) 27 | for (char c = 'a'; c <= 'z'; c++) { 28 | if (t[i] == c) continue; 29 | string s = t; 30 | s[i] = c; 31 | if (s > t) break; 32 | if (m.count(s) > 0) l = max(l, m[s] + 1); 33 | } 34 | 35 | m[t] = l; 36 | M = max(M, l); 37 | }; 38 | 39 | printf("%d\n", M); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /UVa/012398.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef pair ii; 6 | 7 | int m[3][3]; 8 | 9 | ii coords(char c) { 10 | int cell = (c - 'a'); 11 | int row = cell / 3; 12 | int col = cell - 3*row; 13 | return ii(row, col); 14 | } 15 | 16 | int dr[] = {-1,0,1,0}; 17 | int dc[] = {0,1,0,-1}; 18 | 19 | void addOne(ii p) { 20 | int i = p.first, j = p.second; 21 | m[i][j] = (m[i][j] + 1) % 10; 22 | for (int d = 0; d < 4; d++) 23 | if (0 <= i + dr[d] && i + dr[d] < 3 && 0 <= j + dc[d] && j + dc[d] < 3) 24 | m[i + dr[d]][j + dc[d]] = (m[i + dr[d]][j + dc[d]] + 1) % 10; 25 | } 26 | 27 | int main() { 28 | string s; 29 | for (int t = 1; getline(cin, s); t++) { 30 | for (int i = 0; i < 3; i++) 31 | for (int j = 0; j < 3; j++) 32 | m[i][j] = 0; 33 | 34 | if (!s.empty()) 35 | for (int i = s.length()-1; i >= 0; i--) 36 | addOne(coords(s[i])); 37 | 38 | printf("Case #%d:\n", t); 39 | for (int i = 0; i < 3; i++) 40 | printf("%d %d %d\n", m[i][0], m[i][1], m[i][2]); 41 | } 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /UVa/012658.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | int n, i, j; 34 | string s; 35 | string dummy; 36 | cin >> n >> dummy >> dummy >> dummy >> s >> dummy; 37 | 38 | for (i = 0, j = 0; j < n; i += 4, j++) { 39 | if (s[i] == '*') cout << "2"; 40 | else if (s[i+1] == '*') cout << "1"; 41 | else cout << "3"; 42 | } 43 | cout << endl; 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /acepta-el-reto/0107.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | typedef vector vi; 10 | 11 | bitset<100000> bs; // 10^5 12 | vi primes; 13 | 14 | void sieve() { 15 | bs.set(); 16 | bs[0] = bs[1] = 0; 17 | for (int i = 2; i < bs.size(); i++) 18 | if (bs[i]) { 19 | for (ll j = (ll) i * i; j < bs.size(); j += i) bs[j] = 0; 20 | primes.push_back(i); 21 | } 22 | } 23 | 24 | int main() { 25 | double delta[6] = {1, .1, .01, .001, .0001, .00001}; 26 | sieve(); 27 | int n, m; 28 | while (cin >> n >> m && !(n == 0 && m == 0)) { 29 | if (n == 1) { 30 | cout << "Menor" << endl; 31 | continue; 32 | } 33 | 34 | int pi = distance(primes.begin(), upper_bound(primes.begin(), primes.end(), n)); 35 | double error = abs((double) pi / n - (double) 1 / log(n)); 36 | 37 | if (error < delta[m]) cout << "Menor" << endl; 38 | else if (error == delta[m]) cout << "Igual" << endl; 39 | else cout << "Mayor" << endl; 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /UVa/010759.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | 8 | int x; 9 | ll memo[25][145]; 10 | 11 | ll gcd(ll a, ll b) { 12 | while (b) { 13 | ll t = b; 14 | b = a % b; 15 | a = t; 16 | } 17 | return a; 18 | } 19 | 20 | ll f(int dl, int score) { 21 | if (dl == 0) { 22 | if (score >= x) return 1; 23 | else return 0; 24 | } 25 | 26 | if (memo[dl][score] != -1) return memo[dl][score]; 27 | 28 | memo[dl][score] = 0; 29 | for (int v = 1; v <= 6; v++) 30 | memo[dl][score] += f(dl - 1, score + v); 31 | 32 | return memo[dl][score]; 33 | } 34 | 35 | int main() { 36 | int n; 37 | while (cin >> n >> x && !(n == 0 && x == 0)) { 38 | for (int dl = 0; dl <= n; dl++) 39 | for (int score = 0; score <= 6*n; score++) 40 | memo[dl][score] = -1; 41 | 42 | ll num = f(n, 0); 43 | ll den = pow(6, n); 44 | ll g = gcd(num, den); 45 | num /= g, den /= g; 46 | if (den == 1) cout << num << endl; 47 | else printf("%lld/%lld\n", num, den); 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /UVa/011172.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | //freopen("input.txt", "r", stdin); 34 | //freopen("output.txt", "w", stdout); 35 | 36 | int t; cin >> t; 37 | long a, b; 38 | while (t--) { 39 | cin >> a >> b; 40 | if (a < b) cout << '<'; 41 | else if (a == b) cout << '='; 42 | else cout << '>'; 43 | cout << endl; 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /UVa/010696.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | /* 36 | int f91(int N) { 37 | return (N <= 100 ? f91(f91(N+11)) : N-10); 38 | } 39 | */ 40 | 41 | int main() { 42 | int N; 43 | cin >> N; 44 | while (N != 0) { 45 | printf("f91(%d) = %d\n", N, (N > 100 ? N-10 : 91)); 46 | cin >> N; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa/013012.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | int t, u; 37 | while(scanf("%d", &t) == 1) { 38 | int count = 0; 39 | for (int i = 0; i < 5; i++) { 40 | cin >> u; 41 | if (u == t) count++; 42 | } 43 | 44 | cout << count << endl; 45 | } 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /UVa/011955.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | 8 | ll memo[51][51]; 9 | 10 | ll binom(int k, int j) { 11 | if (j == 0 || j == k) return 1; 12 | if (memo[k][j] != -1) return memo[k][j]; 13 | return memo[k][j] = binom(k-1, j-1) + binom(k-1, j); 14 | } 15 | 16 | int main() { 17 | memset(memo, -1, sizeof memo); 18 | 19 | int T; 20 | cin >> T; cin.get(); 21 | for (int t = 1; t <= T; t++) { 22 | printf("Case %d: ", t); 23 | 24 | int k; 25 | char x[100], y[100]; 26 | scanf("(%[a-z]+%[a-z])^%d\n", x, y, &k); 27 | 28 | for (int j = 0; j <= k; j++) { 29 | if (j != 0) cout <<'+'; 30 | 31 | ll coef = binom(k, j); 32 | if (coef > 1) cout << coef << '*'; 33 | if (k-j > 0) { 34 | cout << x; 35 | if (k-j > 1) cout << '^' << k-j; 36 | } 37 | if (j > 0) { 38 | if (k-j > 0) cout << '*'; 39 | cout << y; 40 | if (j > 1) cout << '^' << j; 41 | } 42 | } 43 | 44 | cout << endl; 45 | } 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /UVa/000200.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef vector vi; 7 | typedef vector vvi; 8 | 9 | void dfs(int u, vvi &adjList, vi &dfs_num, vi &topo) { 10 | dfs_num[u] = 1; 11 | for (int v = 0; v < adjList[u].size(); v++) 12 | if (dfs_num[adjList[u][v]] == 0) 13 | dfs(adjList[u][v], adjList, dfs_num, topo); 14 | topo.push_back(u); // read topo in reverse order 15 | } 16 | 17 | int main() { 18 | string s; 19 | while (cin >> s) { 20 | vvi adjList(26, vi()); 21 | int root = s[0] - 'A'; 22 | string t; 23 | while (cin >> t && t != "#") { 24 | int i = 0; 25 | while (i < s.length() && i < t.length() && s[i] == t[i]) i++; 26 | 27 | if (i < s.length()) // if i >= s.length(), then t is longer than s and no conclusion can be drawn. 28 | adjList[s[i] - 'A'].push_back(t[i] - 'A'); 29 | 30 | s = t; 31 | } 32 | 33 | vi topo, dfs_num(26); 34 | dfs(root, adjList, dfs_num, topo); 35 | 36 | for (int i = topo.size()-1; i >= 0; i--) 37 | cout << char(topo[i] + 'A'); 38 | cout << endl; 39 | } 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /UVa/010340.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | string s, t; 37 | while (cin >> s && cin >> t) { 38 | int i = 0, j = 0; 39 | while (i < s.length() && j < t.length()) { 40 | if (s[i] == t[j]) i++, j++; 41 | else j++; 42 | } 43 | cout << (i == s.length() ? "Yes" : "No") << endl; 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /UVa/013185.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef vector vi; 8 | 9 | bitset<100> bs; // 10^2 10 | vi primes; 11 | 12 | void sieve() { 13 | bs.set(); 14 | bs[0] = bs[1] = 0; 15 | for (int i = 2; i < bs.size(); i++) 16 | if (bs[i]) { 17 | for (int j = i * i; j < bs.size(); j += i) bs[j] = 0; 18 | primes.push_back(i); 19 | } 20 | } 21 | 22 | int sumDiv(int N) { 23 | int PF_idx = 0, PF = primes[PF_idx], sum = 1; 24 | while (PF * PF <= N) { 25 | int p = 1; 26 | while (N % PF == 0) { 27 | N /= PF; 28 | p *= PF; 29 | } 30 | if (p != 1) sum *= ((p * PF) - 1) / (PF - 1); 31 | PF = primes[++PF_idx]; 32 | } 33 | 34 | if (N != 1) sum *= N + 1; 35 | return sum; 36 | } 37 | 38 | int main() { 39 | sieve(); 40 | int t; 41 | cin >> t; 42 | while (t--) { 43 | int n; 44 | cin >> n; 45 | int sum = sumDiv(n) - n; 46 | if (sum < n) cout << "deficient" << endl; 47 | else if (sum == n) cout << "perfect" << endl; 48 | else cout << "abundant" << endl; 49 | } 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /UVa/013194.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | bitset<1000000> bs; // 10^6 10 | vector primes; 11 | 12 | void sieve() { 13 | bs.set(); 14 | bs[0] = bs[1] = 0; 15 | for (ll i = 2; i < bs.size(); i++) 16 | if (bs[i]) { 17 | for (ll j = i * i; j < bs.size(); j += i) bs[j] = 0; 18 | primes.push_back(i); 19 | } 20 | } 21 | 22 | ll sumDiv(ll N) { 23 | ll PF_idx = 0, PF = primes[PF_idx], sum = 1; 24 | while (PF * PF <= N) { 25 | ll p = 1; 26 | while (N % PF == 0) { 27 | N /= PF; 28 | p *= PF; 29 | } 30 | if (p != 1) sum *= ((p * PF) - 1) / (PF - 1); 31 | PF = primes[++PF_idx]; 32 | } 33 | 34 | if (N != 1) sum *= N + 1; 35 | return sum; 36 | } 37 | 38 | int main() { 39 | sieve(); 40 | int t; 41 | cin >> t; 42 | while (t--) { 43 | ll n; 44 | cin >> n; 45 | ll sum = sumDiv(n) - n; 46 | if (sum < n) cout << "deficient" << endl; 47 | else if (sum == n) cout << "perfect" << endl; 48 | else cout << "abundant" << endl; 49 | } 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /UVa/000483.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | string s; 37 | while (getline(cin, s)) { 38 | stringstream ss(s); 39 | ss >> s; 40 | while (true) { 41 | for (int i = s.size()-1; i >= 0; i--) cout << s[i]; 42 | if (ss >> s) cout << ' '; 43 | else break; 44 | } 45 | cout << endl; 46 | } 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /UVa/011799.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | int T, N, c, speed; 37 | 38 | cin >> T; 39 | for (int i = 1; i <= T; i++) { 40 | speed = 0; 41 | cin >> N; 42 | while (N--) { 43 | cin >> c; 44 | speed = max(speed, c); 45 | } 46 | 47 | printf("Case %d: %d\n", i, speed); 48 | } 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /UVa/000306.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n, a[200], cycle[200]; 7 | while (cin >> n && n != 0) { 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | a[i]--; 11 | } 12 | 13 | for (int i = 0; i < n; i++) { 14 | int t = 1; 15 | int p = a[i]; 16 | 17 | while (p != i) { 18 | t++; 19 | p = a[p]; 20 | } 21 | 22 | cycle[i] = t; // after t iterations, a[i] returns to pos i 23 | } 24 | 25 | int k; 26 | while (cin >> k && k != 0) { 27 | cin.get(); 28 | string in; 29 | getline(cin, in); 30 | 31 | if (in.size() < n) { 32 | string spaces(n - in.size(), ' '); 33 | in += spaces; 34 | } 35 | 36 | char out[200]; 37 | for (int i = 0; i < n; i++) { 38 | int c = k % cycle[i], p = i; 39 | while (c--) 40 | p = a[p]; 41 | out[p] = in[i]; 42 | } 43 | 44 | cout << string(out, n) << endl; 45 | } 46 | cout << endl; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa/000494.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | string s, buff; 34 | int c; 35 | while (getline(cin, s)) { 36 | c = 0; 37 | int p = s.size() - 1; 38 | for (int i = 0; i < p; i++) { 39 | while (i < p && !isalpha(s[i])) i++; 40 | while (i < p && isalpha(s[i])) { 41 | if (!isalpha(s[i+1])) c++; 42 | i++; 43 | } 44 | } 45 | cout << c << endl; 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /UVa/011727.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | int T, a, b, c, k; 37 | cin >> T; 38 | for (int i = 1; i <= T; i++) { 39 | cin >> a >> b >> c; 40 | if ((a <= b && b <= c) || (c <= b && b <= a)) k = b; 41 | else if (a <= c && c <= b || (b <= c && c <= a)) k = c; 42 | else k = a; 43 | printf("Case %d: %d\n", i, k); 44 | } 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /UVa/000152.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef vector vi; 9 | 10 | struct point { 11 | point(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {} 12 | int x, y, z; 13 | }; 14 | 15 | double dist(point &p1, point &p2) { 16 | int dx = p1.x - p2.x; 17 | int dy = p1.y - p2.y; 18 | int dz = p1.z - p2.z; 19 | return sqrt(dx*dx + dy*dy + dz*dz); 20 | } 21 | 22 | int main() { 23 | vector pts; 24 | int x, y, z; 25 | while (cin >> x >> y >> z && !(x == 0 && y == 0 && z == 0)) 26 | pts.push_back(point(x, y, z)); 27 | 28 | vi h(10, 0); 29 | for (int u = 0; u < pts.size(); u++) { 30 | point p1 = pts[u]; 31 | 32 | // find nearest neighbor of p1 33 | double minDist = DBL_MAX; 34 | for (int v = 0; v < pts.size(); v++) { 35 | point p2 = pts[v]; 36 | if (u != v && dist(p1, p2) < minDist) 37 | minDist = dist(p1, p2); 38 | } 39 | 40 | if (minDist < 10) { 41 | int i = (int) (minDist); 42 | h[i]++; 43 | } 44 | } 45 | 46 | for (int &s : h) 47 | printf("%4d", s); 48 | cout << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /UVa/010812.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef pair dd; 28 | typedef vector vi; 29 | typedef map mii; 30 | typedef vector vii; 31 | typedef vector
vdd; 32 | typedef vector vll; 33 | typedef vector vvi; 34 | 35 | int main() { 36 | //freopen("input.txt", "r", stdin); 37 | //freopen("output.txt", "w", stdout); 38 | int n, s, d; 39 | cin >> n; 40 | while (n--) { 41 | cin >> s >> d; 42 | if (d > s || (s+d) % 2 != 0 || (s-d) % 2 != 0) cout << "impossible" << endl; 43 | else printf("%d %d\n", (s+d)/2, (s-d)/2); 44 | } 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /acepta-el-reto/0313.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair l_l; 27 | typedef pair ll_ll; 28 | typedef pair dd; 29 | typedef vector vi; 30 | typedef vector vl; 31 | typedef vector vll; 32 | typedef vector vii; 33 | typedef vector
vdd; 34 | typedef vector vl_l; 35 | typedef vector vvi; 36 | typedef map mii; 37 | typedef map mil; 38 | typedef map mll; 39 | 40 | int main() { 41 | int t, a, b; 42 | 43 | cin >> t; 44 | while (t--) { 45 | cin >> a >> b; 46 | cout << (a + b >= 0 ? "SI" : "NO") << endl; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa/012289.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int onetwothree(string word) { 33 | if (word.length() != 3) return 3; 34 | if ((word[0] == 'o' && word[1] == 'n') || (word[0] == 'o' && word[2] == 'e') || (word[1] == 'n' && word[2] == 'e')) return 1; 35 | return 2; 36 | } 37 | 38 | int main() { 39 | int n; 40 | cin >> n; 41 | 42 | string word; 43 | for (int i = 0; i < n; i++) { 44 | cin >> word; 45 | cout << onetwothree(word) << endl; 46 | } 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /UVa/010110.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair id; 27 | typedef pair di; 28 | typedef pair ll_ll; 29 | typedef pair dd; 30 | typedef vector vi; 31 | typedef vector vd; 32 | typedef vector vll; 33 | typedef vector vii; 34 | typedef vector
vdd; 35 | typedef vector vid; 36 | typedef vector vvi; 37 | typedef map mii; 38 | typedef map mil; 39 | typedef map mll; 40 | 41 | int main() { 42 | unsigned int n; 43 | while (cin >> n && n != 0) { 44 | unsigned int sq = sqrt(n); 45 | cout << (sq*sq == n ? "yes" : "no") << endl; 46 | } 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /UVa/011876.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | typedef vector vi; 10 | 11 | vi primes; 12 | bitset<1000001> bs; 13 | 14 | void sieve() { 15 | bs.set(); 16 | bs[0] = bs[1] = 0; 17 | for (ll i = 2; i < bs.size(); i++) 18 | if (bs[i]) { 19 | for (ll j = i*i; j < bs.size(); j += i) bs[j] = 0; 20 | primes.push_back(i); 21 | } 22 | } 23 | 24 | int nod(int n) { 25 | int d = 1; 26 | for (int idx = 0, p = primes[idx]; p*p <= n; p = primes[++idx]) { 27 | int m = 0; 28 | while (n % p == 0) { 29 | n /= p; 30 | m++; 31 | } 32 | d *= m + 1; 33 | } 34 | if (n != 1) d *= 2; 35 | return d; 36 | } 37 | 38 | int main() { 39 | sieve(); 40 | vi seq; 41 | for (int ni = 1; ni < 1e6; ni += nod(ni)) seq.push_back(ni); 42 | 43 | int T; 44 | cin >> T; 45 | for (int t = 1; t <= T; t++) { 46 | int A, B; 47 | cin >> A >> B; 48 | auto lo = lower_bound(seq.begin(), seq.end(), A), hi = upper_bound(seq.begin(), seq.end(), B); 49 | printf("Case %d: %d\n", t, distance(lo, hi)); 50 | } 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /UVa/013071.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair id; 27 | typedef pair di; 28 | typedef pair ll_ll; 29 | typedef pair dd; 30 | typedef vector vi; 31 | typedef vector vd; 32 | typedef vector vll; 33 | typedef vector vii; 34 | typedef vector
vdd; 35 | typedef vector vid; 36 | typedef vector vvi; 37 | typedef map mii; 38 | typedef map mil; 39 | typedef map mll; 40 | 41 | int main() { 42 | int A; 43 | cin >> A; 44 | while (A--) { 45 | int N, M; cin >> N >> M; 46 | cout << (N + 1 + ((N + M)*(N + M + 1))/2) << endl; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa/000100.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | unsigned long collatz_cycle(unsigned long n) { 33 | if (n == 1) return 1; 34 | return 1 + (n % 2 == 0 ? collatz_cycle(n/2) : collatz_cycle(3*n+1)); 35 | } 36 | 37 | int main() { 38 | unsigned long i, j, ans; 39 | while (scanf("%ld %ld", &i, &j) == 2) { 40 | ans = 0; 41 | for (unsigned long k = min(i, j); k <= max(i, j); k++) 42 | ans = max(ans, collatz_cycle(k)); 43 | printf("%ld %ld %ld\n", i, j, ans); 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /acepta-el-reto/0321.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair id; 27 | typedef pair di; 28 | typedef pair ll_ll; 29 | typedef pair dd; 30 | typedef vector vi; 31 | typedef vector vd; 32 | typedef vector vll; 33 | typedef vector vii; 34 | typedef vector
vdd; 35 | typedef vector vid; 36 | typedef vector vvi; 37 | typedef map mii; 38 | typedef map mil; 39 | typedef map mll; 40 | 41 | int main() { 42 | int A; 43 | cin >> A; 44 | while (A--) { 45 | int N, M; cin >> N >> M; 46 | cout << (N + 1 + ((N + M)*(N + M + 1))/2) << endl; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa/000136.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair ll_ll; 27 | typedef vector vi; 28 | typedef map mii; 29 | typedef vector vii; 30 | typedef vector vll; 31 | typedef vector vvi; 32 | 33 | bool ugly(long i) { 34 | while (i % 5 == 0) i /= 5; 35 | while (i % 3 == 0) i /= 3; 36 | while (i % 2 == 0) i /= 2; 37 | return i == 1; 38 | } 39 | 40 | int main() { 41 | /* 42 | int c = 0; long i = 1; 43 | while (c != 1500) { 44 | if (ugly(i)) c++; 45 | i++; 46 | } 47 | printf("The 1500'th ugly number is %d.\n", i-1); 48 | */ 49 | printf("The 1500'th ugly number is 859963392.\n"); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /UVa/001124.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair id; 27 | typedef pair di; 28 | typedef pair l_l; 29 | typedef pair ll_ll; 30 | typedef pair dd; 31 | typedef vector vi; 32 | typedef vector vl; 33 | typedef vector vd; 34 | typedef vector vll; 35 | typedef vector vii; 36 | typedef vector
vdd; 37 | typedef vector vid; 38 | typedef vector vl_l; 39 | typedef vector vvi; 40 | typedef map mii; 41 | typedef map mil; 42 | typedef map mll; 43 | 44 | int main() { 45 | string s; 46 | while (getline(cin, s)) 47 | cout << s << endl; 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa/012149.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include // max... 14 | #include // pair 15 | #include 16 | #include // int, ll... 17 | #include // double... 18 | #include // abs, atan... 19 | #include // memset 20 | #include 21 | #include // greater, less... 22 | #include 23 | 24 | using namespace std; 25 | 26 | typedef long long ll; 27 | typedef unsigned long long ull; 28 | typedef pair ii; 29 | typedef pair id; 30 | typedef pair di; 31 | typedef pair ll_ll; 32 | typedef pair dd; 33 | typedef vector vi; 34 | typedef vector vd; 35 | typedef vector vll; 36 | typedef vector vii; 37 | typedef vector
vdd; 38 | typedef vector vid; 39 | typedef vector vvi; 40 | typedef map mii; 41 | typedef map mil; 42 | typedef map mll; 43 | 44 | int main() { 45 | int N; 46 | while (cin >> N && N != 0) 47 | cout << N*(N+1)*(2*N+1)/6 << endl; 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa/000439.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef pair ii; 7 | 8 | int grid[8][8], si, sj, ti, tj; 9 | int dr[8] = {-2,-1,1,2,2,1,-1,-2}; 10 | int dc[8] = {1,2,2,1,-1,-2,-2,-1}; 11 | 12 | void bfs(int i, int j) { 13 | queue q; 14 | grid[i][j] = 0; 15 | q.push(ii(i, j)); 16 | while (!q.empty()) { 17 | ii u = q.front(); q.pop(); 18 | int i = u.first, j = u.second; 19 | for (int d = 0; d < 8; d++) { 20 | int ni = i + dr[d], nj = j + dc[d]; 21 | if (0 <= ni && ni < 8 && 0 <= nj && nj < 8 && grid[ni][nj] == -1) { 22 | grid[ni][nj] = grid[i][j] + 1; 23 | if (ni == ti && nj == tj) return; 24 | q.push(ii(ni, nj)); 25 | } 26 | } 27 | } 28 | } 29 | 30 | int main() { 31 | string s, t; 32 | while (cin >> s >> t) { 33 | for (int i = 0; i < 8; i++) 34 | for (int j = 0; j < 8; j++) 35 | grid[i][j] = -1; 36 | 37 | si = s[0] - 'a'; 38 | sj = s[1] - '1'; 39 | ti = t[0] - 'a'; 40 | tj = t[1] - '1'; 41 | 42 | bfs(si, sj); 43 | 44 | printf("To get from %s to %s takes %d knight moves.\n", s.c_str(), t.c_str(), grid[ti][tj]); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /UVa/000294.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | bitset<10000000> bs; // 10^7 10 | vector primes; 11 | 12 | void sieve() { 13 | bs.set(); 14 | bs[0] = bs[1] = 0; 15 | for (ll i = 2; i < bs.size(); i++) 16 | if (bs[i]) { 17 | for (ll j = i * i; j < bs.size(); j += i) bs[j] = 0; 18 | primes.push_back(i); 19 | } 20 | } 21 | 22 | int nod(int n) { 23 | int d = 1; 24 | for (int idx = 0, p = primes[idx]; p*p <= n; p = primes[++idx]) { 25 | int m = 0; 26 | while (n % p == 0) { 27 | n /= p; 28 | m++; 29 | } 30 | d *= m + 1; 31 | } 32 | if (n != 1) d *= 2; 33 | return d; 34 | } 35 | 36 | int main() { 37 | sieve(); 38 | 39 | int N; 40 | cin >> N; 41 | while (N--) { 42 | int L, U; 43 | cin >> L >> U; 44 | int P = L, D = nod(L); 45 | for (int n = L + 1; n <= U; n++) { 46 | int x = nod(n); 47 | if (x > D) { 48 | D = x; 49 | P = n; 50 | } 51 | } 52 | 53 | printf("Between %d and %d, %d has a maximum of %d divisors.\n", L, U, P, D); 54 | } 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /UVa/010235.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | typedef vector vi; 9 | 10 | ll _sieve_size; 11 | bitset<1000000> bs; // 10^6 12 | vi primes; 13 | 14 | void sieve(ll upperbound) { // finds primes in [0...upperbound] 15 | _sieve_size = upperbound + 1; 16 | bs.set(); 17 | bs[0] = bs[1] = 0; 18 | for (ll i = 2; i <= _sieve_size; i++) if (bs[i]) { 19 | for (ll j = i * i; j <= _sieve_size; j += i) bs[j] = 0; 20 | primes.push_back(i); 21 | } 22 | } 23 | 24 | bool isPrime(ll N) { // only works for 0 <= N <= primes[primes.size()-1]^2 25 | if (N <= _sieve_size) return bs[N]; 26 | for (int i = 0; i < primes.size(); i++) 27 | if (N % primes[i] == 0) return false; 28 | return true; 29 | } 30 | 31 | int reverse(int n) { 32 | int ret = 0; 33 | while (n) { 34 | ret = ret*10 + n % 10; 35 | n /= 10; 36 | } 37 | return ret; 38 | } 39 | 40 | int main() { 41 | sieve(1000000); 42 | int N; 43 | while (cin >> N) { 44 | if (!isPrime(N)) printf("%d is not prime.\n", N); 45 | else if (N != reverse(N) && isPrime(reverse(N))) printf("%d is emirp.\n", N); 46 | else printf("%d is prime.\n", N); 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa/010469.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | #include 21 | 22 | using namespace std; 23 | 24 | typedef long long ll; 25 | typedef pair ii; 26 | typedef pair id; 27 | typedef pair di; 28 | typedef pair l_l; 29 | typedef pair ll_ll; 30 | typedef pair dd; 31 | typedef vector vi; 32 | typedef vector vl; 33 | typedef vector vd; 34 | typedef vector vll; 35 | typedef vector vii; 36 | typedef vector
vdd; 37 | typedef vector vid; 38 | typedef vector vl_l; 39 | typedef vector vvi; 40 | typedef map mii; 41 | typedef map mil; 42 | typedef map mll; 43 | 44 | int main() { 45 | unsigned int x, y; 46 | while (cin >> x >> y) 47 | cout << (x ^ y) << endl; 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /UVa/012405.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // max... 12 | #include // pair 13 | #include 14 | #include // int, ll... 15 | #include // double... 16 | #include // abs, atan... 17 | #include // memset 18 | #include 19 | #include // greater, less... 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef pair ii; 25 | typedef pair ll_ll; 26 | typedef vector vi; 27 | typedef map mii; 28 | typedef vector vii; 29 | typedef vector vll; 30 | typedef vector vvi; 31 | 32 | int main() { 33 | int testcases, n; 34 | char arr[100]; 35 | 36 | scanf("%d", &testcases); 37 | 38 | for (int tc = 1; tc <= testcases; tc++) { 39 | scanf("%d %s", &n, arr); 40 | int ans = 0; 41 | 42 | for (int i = 0; i < n; ) { 43 | if (arr[i] == '#') i++; 44 | else { 45 | ans++; 46 | i += 3; 47 | } 48 | } 49 | printf("Case %d: %d\n", tc, ans); 50 | } 51 | 52 | return 0; 53 | } 54 | --------------------------------------------------------------------------------