├── .gitignore ├── 1000-Greetings-from-LightOJ.cpp ├── 1001-Opposite-Task.cpp ├── 1002-Country-Roads.cpp ├── 1003-Drunk.cpp ├── 1004-Monkey-Banana-Problem.cpp ├── 1005-Rooks.cpp ├── 1006-Hex-a-bonacci.cpp ├── 1007-Mathematically-Hard.cpp ├── 1008-Fibsieves-Fantabulous-Birthday.cpp ├── 1009-Back-to-Underworld.cpp ├── 1010-Knights-In-Chessboard.cpp ├── 1011-Marriage-Ceremonies.cpp ├── 1012-Guilty-Prince.cpp ├── 1013-Love-Calculator.cpp ├── 1014-Ifter-Party.cpp ├── 1016-Brush-2.cpp ├── 1017-Brush-III.cpp ├── 1018-Brush-4.cpp ├── 1019-Brush-5.cpp ├── 1020-A-Childhood-Game.cpp ├── 1021-Painful-Bases.cpp ├── 1022-Circle-in-Square.cpp ├── 1023-Discovering-Perumutations.cpp ├── 1024-Eid.cpp ├── 1025-The-Specials-Menu.cpp ├── 1026-Critical-Links.cpp ├── 1027-A-Dangerous-Maze.cpp ├── 1028-Trailing-Zeroes-2.cpp ├── 1029-Civil-and-Evil-Engineer.cpp ├── 1030-Discovering-Gold.cpp ├── 1031-Easy-Game.cpp ├── 1033-Generating-Palindromes.cpp ├── 1034-Hit-the-Light-Switches.cpp ├── 1035-Intelligent-Factorial-Factorization.cpp ├── 1037-Agent-47.cpp ├── 1038-Race-to-1-Again.cpp ├── 1040-Donation.cpp ├── 1041-Road-Construction.cpp ├── 1042-Secret-Origins.cpp ├── 1044-Paliandrome-Partitioning.cpp ├── 1045-Digits-Of-A-Factorial.cpp ├── 1046-Rider.cpp ├── 1047-Neighbour-House ├── 1049-One-Way-Roads.cpp ├── 1050-Marbles.cpp ├── 1051-Good-Or-Bad.cpp ├── 1053-Higher-Math.cpp ├── 1054-Efficient-Pseudo-Code.cpp ├── 1057-Collecting-Gold.cpp ├── 1058-Parallelogram-Counting.cpp ├── 1063-Ant-Hills.cpp ├── 1065-Number-Sequence.cpp ├── 1066-Gathering-Food.cpp ├── 1076-Get-the-Container.cpp ├── 1077-How-Many-Points.cpp ├── 1079-Just-Another-Robbery.cpp ├── 1080-Binary-Simulation.cpp ├── 1082-Array-Queries.cpp ├── 1084-Winter.cpp ├── 1085-All-Possible-Increasing-Subsequences.cpp ├── 1086-Jogging-Trails.cpp ├── 1087-Diablo.cpp ├── 1088-Points-in-Segements.cpp ├── 1089-Points-in-Segments-II.cpp ├── 1090-Trailing-Zeroes-II.cpp ├── 1093-Ghajini.cpp ├── 1094-Farthest-Nodes-In-Tree.cpp ├── 1104-Birthday-Paradox.cpp ├── 1105-Fi-Binary-Number.cpp ├── 1107-How-Cow.cpp ├── 1109-False-Ordering.cpp ├── 1110-An-Easy-LCS.cpp ├── 1111-Best-Picnic-Ever.cpp ├── 1112-Curious-Robin-Hood.cpp ├── 1113-Dsicover-The-Web.cpp ├── 1114-Easily-Readable.cpp ├── 1116-Ekka-Dokka.cpp ├── 1119-Pimp-My-Ride.cpp ├── 1122-Digit-Count.cpp ├── 1127-Funny-Knapsack.cpp ├── 1133-Array-Simulation.cpp ├── 1134-Be-Efficient.cpp ├── 1138-Trailing-Zeroes-3.cpp ├── 1141-Number-Tranformation.cpp ├── 1148-Mad-Counting.cpp ├── 1149-Factors-and-Multiples.cpp ├── 1153-Internet-Bandwidth.cpp ├── 1159-Batman.cpp ├── 1163-Bank-Robbery.cpp ├── 1164-Horrible-Queries.cpp ├── 1166-Old-Sorting.cpp ├── 1175-Jane-and-the-Frost-Giants.cpp ├── 1176-Getting-a-T-shirt.cpp ├── 1179-Josephus-Problem.cpp ├── 1182-Parity.cpp ├── 1184-Marriage-Media.cpp ├── 1186-Incredible-Chess.cpp ├── 1188-Fast-Queries.cpp ├── 1189-Sum-of-Factorials.cpp ├── 1198-Karate-Competetion.cpp ├── 1200-Thief.cpp ├── 1212-Double-Ended-Queue.cpp ├── 1215-Finding-LCM.cpp ├── 1217-Neighbor-House-II.cpp ├── 1225-Palindromic-Numbers-II.cpp ├── 1227-Boiled-Eggs.cpp ├── 1231-Coin-Change-1.cpp ├── 1232-Coin-Exchange-2.cpp ├── 1234-Harmonic-Number.cpp ├── 1241-Pinocchio.cpp ├── 1249-Chocolate-Thief.cpp ├── 1257-Farthest-Nodes-in-a-Tree-II.cpp ├── 1259-Goldbachs-Conjecture.cpp ├── 1275-Internet-Service-Providers.cpp ├── 1289-LCM-from-1-to-n.cpp ├── 1294-Positive-Negative-Sign.cpp ├── 1297-Largest-Box.cpp ├── 1305-Area-of-a-Parallelogram.cpp ├── 1311-Unlucky-Bird.cpp ├── 1326-Race.cpp ├── 1338-Hidden-Secret.cpp ├── 1349-Aladdin-and-the-Optimal-Invitation.cpp ├── 1369-Answering-Queries.cpp ├── 1387-Setu.cpp ├── 1389-Scarecrow.cpp ├── 1425-The-Monkey-and-the-Oiled-Bamboo.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | #binary files 2 | 3 | *.out 4 | 5 | #input files 6 | 7 | *.in 8 | 9 | #test files 10 | 11 | test* 12 | -------------------------------------------------------------------------------- /1000-Greetings-from-LightOJ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | int t; 9 | int a; 10 | int b; 11 | cin >> t; 12 | for (int i = 1; i <= t; i++) { 13 | cin >> a; 14 | cin >> b; 15 | 16 | a = a + b; 17 | cout <<"Case "< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | int t; 9 | int a; 10 | int b; 11 | cin >> t; 12 | for (int i = 1; i <= t; i++) { 13 | cin >> a; 14 | 15 | b = a / 2; 16 | a = a % 2 + a / 2; 17 | cout << a << " "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define ii pair 8 | using namespace std; 9 | 10 | 11 | int dijkstra(vector < vector > > x, int n, int my){ 12 | 13 | pair temp; 14 | vector dis(n, INT_MAX); 15 | priority_queue < ii, vector , greater > q; 16 | int bro; 17 | temp.first = 0; 18 | temp.second = my; 19 | ii top; 20 | q.push(temp); 21 | int distance; 22 | int index; 23 | int d; 24 | int j; 25 | while (!q.empty()) { 26 | top = q.top(); 27 | q.pop(); 28 | 29 | index = top.second; 30 | distance = top.first; 31 | 32 | if(distance <= dis[index]) { 33 | dis[index] = distance; 34 | for (int i = 0; i < x[index].size(); i++) { 35 | temp = x[index][i]; 36 | d = temp.first; 37 | j = temp.second; 38 | 39 | bro = max(d,distance); 40 | if(bro < dis[j]) { 41 | dis[j] = bro; 42 | q.push(ii(dis[j], j)); 43 | } 44 | 45 | 46 | } 47 | 48 | 49 | } 50 | 51 | 52 | } 53 | 54 | for (int i = 0; i < n; i++) { 55 | if(dis[i] == INT_MAX) { 56 | cout << "Impossible\n"; 57 | } 58 | else { 59 | cout << dis[i] << endl; 60 | } 61 | } 62 | 63 | 64 | 65 | } 66 | 67 | 68 | int main() 69 | { 70 | 71 | int a; 72 | int b; 73 | int w; 74 | 75 | int m; 76 | int n; 77 | int t; 78 | int k; 79 | int my; 80 | pair temp; 81 | //fstream cin; 82 | //cin.open("1002.in", ios::in); 83 | 84 | cin >> t; 85 | 86 | for (int j = 1; j <= t; j++) { 87 | cin >> n; 88 | vector < vector > > x(n); 89 | cin >> m; 90 | 91 | for (int i = 1; i <= m; i++) { 92 | cin >> a; 93 | cin >> b; 94 | cin >> w; 95 | 96 | temp.first = w; 97 | temp.second = b; 98 | int k; 99 | int kk; 100 | 101 | for(k = 0; k < x[a].size(); k++) { 102 | if(x[a][k].second == b) 103 | break; 104 | } 105 | 106 | for(kk = 0; kk < x[b].size(); kk++) { 107 | if(x[b][kk].second == a) 108 | break; 109 | } 110 | 111 | if(k == x[a].size()) { 112 | x[a].push_back(temp); 113 | x[b].push_back(ii(w, a)); 114 | } 115 | 116 | else { 117 | if(temp.first < x[a][k].first ) { 118 | x[a][k] = temp; 119 | x[b][kk] = ii(w, a); 120 | } 121 | 122 | } 123 | 124 | } 125 | 126 | cin >> my; 127 | cout << "Case "< 2 | #include 3 | #include 4 | #include 5 | #define white 0 6 | #define gray 1 7 | #define black 2 8 | using namespace std; 9 | 10 | vector < vector > a; 11 | map hash; 12 | vector visi; 13 | int ok; 14 | 15 | int count; 16 | int wow(string x) 17 | { 18 | int t; 19 | int k; 20 | string temp; 21 | t = hash[x]; 22 | visi[t] = gray; 23 | for (unsigned int i = 0; i < a[t].size(); i++) { 24 | temp = a[t][i]; 25 | if(visi[hash[temp]] == gray) { 26 | ok = 0; 27 | return 0; 28 | } else { 29 | wow(temp); 30 | } 31 | } 32 | visi[t] = black; 33 | } 34 | int main() 35 | { 36 | 37 | int t; 38 | int m; 39 | int yes; 40 | int tot; 41 | int c; 42 | c = 1; 43 | tot = 0; 44 | string x; 45 | string y; 46 | cin >> t; 47 | for (int i = 1; i <= t; i++) { 48 | cin >> m; 49 | vector backup; 50 | vector < vector > aa(10005); 51 | map has; 52 | 53 | swap(aa, a); 54 | swap(has, hash); 55 | count = -1; 56 | ok = 1; 57 | 58 | for (int j = 0; j < m; j++) { 59 | cin >> x; 60 | cin >> y; 61 | 62 | if(hash.count(x) == 0) { 63 | count++; 64 | hash[x] = count; 65 | backup.push_back(x); 66 | tot++; 67 | } else { 68 | count = hash[x]; 69 | } 70 | 71 | yes = 1; 72 | 73 | for (unsigned int g = 0; g < a[count].size(); g++) { 74 | if(a[count][g] == y) { 75 | yes = 0; 76 | } 77 | } 78 | if(yes) { 79 | a[count].push_back(y); 80 | } 81 | 82 | if(hash.count(y) == 0) { 83 | count++; 84 | hash[y] = count; 85 | backup.push_back(y); 86 | tot++; 87 | } 88 | 89 | } 90 | 91 | vector yo(tot+5, 0); 92 | swap(visi, yo); 93 | 94 | for (unsigned int j = 0; j < backup.size(); j++) { 95 | if(visi[j] == white) { 96 | wow(backup[j]); 97 | } 98 | 99 | if(ok == 0) { 100 | break; 101 | } 102 | } 103 | 104 | if(ok) { 105 | cout << "Case "< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | int jmax; 10 | int n; 11 | //fstream cin; 12 | //cin.open("1004.in", ios::in); 13 | unsigned long long a[300][300]; 14 | unsigned long long inp[300][300]; 15 | n = 4; 16 | cin >> t; 17 | for (int k = 1; k <= t; k++) { 18 | 19 | cin >> n; 20 | memset(a, 0, sizeof(a)); 21 | memset(inp, 0, sizeof(inp)); 22 | 23 | for (int i = 0; i <= ((2 * n) - 1); i++) { 24 | if(i < n) 25 | jmax = i; 26 | else 27 | jmax = 2 * n - i; 28 | 29 | for(int j = 0; j < jmax; j++) { 30 | cin >> inp[i][j]; 31 | } 32 | 33 | } 34 | 35 | for (int i = ((2 * n) - 1); i >= 0; i--) { 36 | 37 | if(i < n) { 38 | jmax = i+1; 39 | } 40 | 41 | else { 42 | jmax = 2 * n - i; 43 | } 44 | 45 | if(i < n) { 46 | 47 | for(int j = 0; j < jmax; j++) { 48 | 49 | if(i == ((2 * n) - 1)) { 50 | a[i][j] = inp[i][j]; 51 | continue; 52 | } 53 | 54 | a[i][j] = max((inp[i][j] + a[i+1][j]), (inp[i][j] + a[i+1][j+1])); 55 | } 56 | } 57 | 58 | else { 59 | 60 | for (int j = jmax - 1; j >= 0; j--) { 61 | if(i == ((2 * n) - 1)) { 62 | a[i][j] = inp[i][j]; 63 | continue; 64 | } 65 | 66 | if(j -1 >= 0) { 67 | a[i][j] = max((inp[i][j]+a[i+1][j]), (inp[i][j]+ a[i+1][j-1])); 68 | } 69 | 70 | else { 71 | a[i][j] = a[i+1][j] + inp[i][j]; 72 | } 73 | 74 | } 75 | 76 | } 77 | 78 | } 79 | 80 | cout <<"Case "< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int a[100]; 9 | int n; 10 | int k; 11 | 12 | long long ans[40][40]; 13 | 14 | int ok(int x, int y) 15 | { 16 | 17 | for (int i = 0; i < x; i++) { 18 | 19 | if(a[i] == y) { 20 | return 0; 21 | } 22 | 23 | } 24 | 25 | 26 | return 1; 27 | 28 | } 29 | 30 | long long explore(int i, int count) 31 | { 32 | 33 | long long x; 34 | long long y; 35 | long long sum = 0; 36 | 37 | 38 | if(i == n) { 39 | if(count == k) { 40 | return 1; 41 | } 42 | 43 | else { 44 | return 0; 45 | } 46 | 47 | } 48 | if(ans[i][count] != -1) { 49 | return ans[i][count]; 50 | } 51 | 52 | for (int j = 0; j < n; j++) { 53 | 54 | y = 0; 55 | x = 0; 56 | if(ok(i, j)) { 57 | a[i] = j; 58 | x = explore(i+1, count+1); 59 | } 60 | 61 | sum += x; 62 | 63 | } 64 | 65 | a[i] = -1; 66 | y = explore(i+1, count); 67 | 68 | sum += y; 69 | ans[i][count] = sum; 70 | return sum; 71 | 72 | 73 | 74 | } 75 | 76 | 77 | int main() 78 | { 79 | 80 | int t; 81 | 82 | scanf("%d", &t); 83 | 84 | for (int cs = 1; cs <= t; cs++) { 85 | scanf("%d", &n); 86 | scanf("%d", &k); 87 | 88 | memset(a, -1, sizeof(a)); 89 | memset(ans, -1, sizeof(ans)); 90 | 91 | printf("Case %d: %lld\n",cs, explore(0, 0)); 92 | 93 | } 94 | 95 | 96 | } 97 | 98 | -------------------------------------------------------------------------------- /1006-Hex-a-bonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | long long t; 5 | long long a; 6 | long long b; 7 | long long c; 8 | long long d; 9 | long long e; 10 | long long f; 11 | long long n; 12 | long long x[10005]; 13 | 14 | int fn( int n ) { 15 | for (int i = 0; i <= n; i++) { 16 | 17 | if( i == 0 ) { 18 | x[i] = a; 19 | continue; 20 | } 21 | if( i == 1 ) { 22 | x[i] = b; 23 | continue; 24 | } 25 | if( i == 2 ) { 26 | x[i] = c; 27 | continue; 28 | } 29 | if( i == 3 ) { 30 | x[i] = d; 31 | continue; 32 | } 33 | if( i == 4 ) { 34 | x[i] = e; 35 | continue; 36 | } 37 | if( i == 5 ) { 38 | x[i] = f; 39 | continue; 40 | } 41 | x[i] = x[i-1] + x[i-2] + x[i-3] + x[i-4] + x[i-5] + x[i-6]; 42 | x[i] = x[i] % 10000007; 43 | 44 | } 45 | 46 | return x[n]; 47 | } 48 | 49 | int main() 50 | { 51 | 52 | 53 | long long ans; 54 | 55 | 56 | cin >> t; 57 | 58 | for (long long i = 1; i <= t; i++) { 59 | cin >> a; 60 | cin >> b; 61 | cin >> c; 62 | cin >> d; 63 | cin >> e; 64 | cin >> f; 65 | cin >> n; 66 | 67 | ans = fn(n); 68 | ans = ans % 10000007; 69 | 70 | cout << "Case "<< i <<": "<< ans << endl; 71 | } 72 | 73 | 74 | } 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /1007-Mathematically-Hard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define N 5000000 5 | #include 6 | using namespace std; 7 | unsigned long long ans[N+5]; 8 | bool isprime[N+5]; 9 | 10 | int prime() 11 | { 12 | memset(isprime, 1, sizeof(isprime)); 13 | 14 | for(int i = 2; i <= N; i++) { 15 | if(isprime[i] == 1) { 16 | for (int j = i + i; j <= N; j = j + i) { 17 | isprime[j] = 0; 18 | } 19 | 20 | } 21 | 22 | } 23 | 24 | 25 | } 26 | 27 | 28 | int main() 29 | { 30 | 31 | vector x; 32 | ans[0] = 0; 33 | ans[1] = 0; 34 | 35 | int count = 0; 36 | unsigned long long a; 37 | unsigned long long b; 38 | unsigned long long temp; 39 | unsigned long long pr; 40 | unsigned long long t; 41 | unsigned long long j; 42 | unsigned long long div; 43 | prime(); 44 | for (int i = 2; i <= N; i++) { 45 | if(isprime[i]) { 46 | x.push_back(i); 47 | } 48 | 49 | } 50 | 51 | for (int i = 0; i <= N; i++) { 52 | ans[i] = i; 53 | } 54 | for (int i = 0; i < x.size(); i++) { 55 | pr = x[i]; 56 | div = 1; 57 | for (int j = pr; j <= N; j = j + pr) { 58 | ans[j] =(((ans[j] * (pr - 1)))/ pr); 59 | } 60 | 61 | 62 | 63 | 64 | } 65 | 66 | for (int i = 1; i <= N; i++) { 67 | ans[i] = ans[i] * ans[i]; 68 | ans[i] += ans[i-1]; 69 | } 70 | cin >> t; 71 | 72 | for (int i =1 ; i <= t; i++) { 73 | scanf("%lld", &a); 74 | scanf("%lld", &b); 75 | temp = ans[b] - ans[a-1]; 76 | printf("Case %d: %llu\n",i, temp); 77 | } 78 | 79 | } 80 | 81 | 82 | 83 | 84 | &t); 85 | ll cases = t; 86 | 87 | pregenerate(); 88 | 89 | 90 | 91 | 92 | while(t--){ 93 | 94 | ll a,b; 95 | 96 | // cin >> a >> b; 97 | scanf("%lld %lld",&a,&b); 98 | 99 | ll ans = dp[b] - dp[a-1]; 100 | 101 | // cout << "Case " << cases - t << ": " << ans << endl; 102 | printf("Case "); 103 | printf("%lld: %llu\n",cases-t,ans); 104 | 105 | 106 | } 107 | 108 | 109 | 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /1008-Fibsieves-Fantabulous-Birthday.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | unsigned long long k; 9 | unsigned long long t; 10 | unsigned long long m; 11 | unsigned long long x; 12 | unsigned long long f; 13 | 14 | cin >> t; 15 | 16 | for (int i = 1; i <= t; i++) { 17 | cin >> k; 18 | x = ceil(sqrt(k)); 19 | switch(k) { 20 | case 1: 21 | cout <<"Case "<= k) { 42 | f = k - m; 43 | cout <<"Case "<= k) { 58 | f = k - m; 59 | cout <<"Case "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define N 20004 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | 12 | int x; 13 | int y; 14 | int ok; 15 | int count; 16 | int k; 17 | int t; 18 | int n; 19 | int total; 20 | int maxi; 21 | int next; 22 | int c0; 23 | int c1; 24 | int tt; 25 | cin >> tt; 26 | 27 | for (int i = 1; i <= tt; i++) { 28 | vector < vector > g(N); 29 | int vis[N]; 30 | vector < stack > a(2); 31 | stack ny; 32 | memset(vis, -1, sizeof(vis)); 33 | cin >> n; 34 | total = 0; 35 | count = 0; 36 | 37 | 38 | for (int j = 0; j < n; j++) { 39 | scanf("%d", &x); 40 | scanf("%d", &y); 41 | g[x].push_back(y); 42 | g[y].push_back(x); 43 | 44 | if(vis[x] == -1) { 45 | total++; 46 | vis[x] = 0; 47 | ny.push(x); 48 | } 49 | 50 | if(vis[y] == -1) { 51 | total++; 52 | vis[y] = 0; 53 | ny.push(y); 54 | } 55 | 56 | } 57 | 58 | next = x; 59 | vis[next] = 1; 60 | count = 1; 61 | maxi = 0; 62 | 63 | while(count < total) { 64 | c0 = 1; 65 | c1 = 0; 66 | //cout << " here " << endl; 67 | for (int j = 0; j < g[next].size(); j++) { 68 | t = g[next][j]; 69 | if(vis[t] == 0) { 70 | c1++; 71 | a[1].push(t); 72 | //cout << " a1 pushed from start "< 2 | #include 3 | using namespace std; 4 | 5 | int cal(int row, int col) 6 | { 7 | int col1; 8 | int col2; 9 | int row1; 10 | int row2; 11 | int ans; 12 | 13 | int temp; 14 | 15 | 16 | 17 | row1 = (row / 2) + (row % 2); 18 | row2 = (row / 2); 19 | 20 | col1 = (col / 2) + (col % 2); 21 | col2 = col / 2; 22 | 23 | ans = row1 * col1 + row2 * col2; 24 | 25 | 26 | return ans; 27 | 28 | } 29 | 30 | 31 | int two(int row, int col) 32 | { 33 | int ans; 34 | int temp; 35 | 36 | temp = cal(row, col); 37 | 38 | row = max(row, col); 39 | 40 | ans = (row / 4) * 4 + min(2, row % 4) * 2; 41 | 42 | ans = max(ans, temp); 43 | 44 | return ans; 45 | 46 | } 47 | 48 | int main() 49 | { 50 | 51 | int t; 52 | 53 | int row; 54 | int col; 55 | scanf("%d", &t); 56 | 57 | for (int cs = 1; cs <= t; cs++) { 58 | scanf("%d", &row); 59 | scanf("%d", &col); 60 | 61 | 62 | if(row == 1 or col == 1) { 63 | 64 | printf("Case %d: %d\n", cs, max(row, col)); 65 | continue; 66 | 67 | } 68 | 69 | if(row == 2 or col == 2) { 70 | printf("Case %d: %d\n", cs, two(row, col)); 71 | continue; 72 | } 73 | 74 | if(min(row, col) == 2 and max(row, col) == 3) { 75 | 76 | printf("Case %d: 4\n", cs); 77 | continue; 78 | } 79 | 80 | 81 | printf("Case %d: %d\n", cs, cal(row, col)); 82 | 83 | } 84 | 85 | 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /1011-Marriage-Ceremonies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define check(n, pos) (n & (1< 2 | #include 3 | #include 4 | #include 5 | char mm[21][21]; 6 | int a[21][21]; 7 | int h; 8 | int w; 9 | int count; 10 | using namespace std; 11 | 12 | int wow(int i, int j) 13 | { 14 | 15 | if(i + 1 < h and !a[i+1][j] and mm[i+1][j] == '.') { 16 | a[i+1][j] = 1; 17 | count++; 18 | wow(i+1, j); 19 | } 20 | 21 | if(i - 1 >= 0 and !a[i-1][j] and mm[i-1][j] == '.') { 22 | a[i-1][j] = 1; 23 | count++; 24 | wow(i-1, j); 25 | } 26 | 27 | if(j + 1 < w and !a[i][j+1] and mm[i][j+1] == '.') { 28 | a[i][j+1] = 1; 29 | count++; 30 | wow(i, j+1); 31 | } 32 | 33 | if(j - 1 >= 0 and !a[i][j-1] and mm[i][j-1] == '.') { 34 | a[i][j-1] = 1; 35 | count++; 36 | wow(i, j -1); 37 | } 38 | 39 | } 40 | 41 | 42 | 43 | int main() 44 | { 45 | 46 | int x; 47 | int p; 48 | int r; 49 | int t; 50 | // fstream cin; 51 | // cin.open("input/1012.in", ios::in); 52 | 53 | 54 | // scanf("%d", &t); 55 | 56 | cin >> t; 57 | for (int k = 1; k <= t; k++) { 58 | //scanf("%d", &w); 59 | //scanf("%d", &h); 60 | cin >> w; 61 | cin >> h; 62 | count = 0; 63 | for (int i = 0; i < h; i++) { 64 | for (int j = 0; j < w; j++) { 65 | //scanf("%c", &mm[i][j]); 66 | cin >> mm[i][j]; 67 | if(mm[i][j] == '@') { 68 | p = i; 69 | r = j; 70 | count = 1; 71 | } 72 | 73 | } 74 | 75 | } 76 | 77 | memset(a, 0, sizeof(a)); 78 | a[p][r] = 1; 79 | wow(p, r); 80 | printf("Case %d: %d\n",k, count); 81 | } 82 | 83 | } 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /1013-Love-Calculator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int n; 7 | int m; 8 | 9 | char a[100]; 10 | char b[100]; 11 | long long count; 12 | unsigned long long ans2[40][40][40]; 13 | long long ans[40][40]; 14 | int temp; 15 | 16 | unsigned long long explore2(int i, int j, int l) 17 | { 18 | 19 | if(i == n and j != m) { 20 | return explore2(i, j+1, l-1); 21 | } 22 | 23 | if(j == m and i != n) { 24 | return explore2(i+1, j, l-1); 25 | } 26 | 27 | 28 | if(i == n and j == m and l == 0) { 29 | return 1; 30 | } 31 | 32 | if(i == n and j == m and l != 0) { 33 | return 0; 34 | } 35 | 36 | if(ans2[i][j][l] != -1) { 37 | return ans2[i][j][l]; 38 | } 39 | 40 | if(a[i] == b[j]) { 41 | return ans2[i][j][l] = explore2(i+1, j+1, l -1); 42 | } 43 | 44 | return ans2[i][j][l] = explore2(i+1, j, l -1) + explore2(i, j+1, l -1); 45 | 46 | } 47 | int explore(int i, int j) 48 | { 49 | if(i == n) { 50 | count++; 51 | return m - j; 52 | 53 | } 54 | 55 | if(j == m) { 56 | count++; 57 | return n - i; 58 | } 59 | 60 | if(ans[i][j] != -1) { 61 | return ans[i][j]; 62 | } 63 | 64 | if(a[i] == b[j]) { 65 | return ans[i][j] = 1 + explore(i+1, j+1); 66 | } 67 | 68 | return ans[i][j] = 1 + min(explore(i+1, j),explore(i, j+1)); 69 | 70 | } 71 | int main() 72 | { 73 | int t; 74 | long long epic; 75 | scanf("%d", &t); 76 | 77 | for (int cs = 1; cs <= t ; cs++) { 78 | scanf("%s", a); 79 | scanf("%s", b); 80 | 81 | n = strlen(a); 82 | m = strlen(b); 83 | 84 | memset(ans, -1, sizeof(ans)); 85 | 86 | temp = explore(0, 0); 87 | memset(ans2, -1, sizeof(ans2)); 88 | epic = explore2(0, 0, temp); 89 | 90 | printf("Case %d: %d %lld\n",cs,temp,epic); 91 | 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /1014-Ifter-Party.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | long long p; 10 | long long q; 11 | long long l; 12 | long long i; 13 | 14 | cin >> t; 15 | 16 | for (int j = 1; j <= t; j++) { 17 | scanf("%lld", &p); 18 | scanf("%lld", &l); 19 | p = p - l; 20 | vector a; 21 | vector b; 22 | 23 | 24 | for (i = 1; i * i < p; i++) { 25 | if(p % i == 0) { 26 | if(i > l) { 27 | a.push_back(i); 28 | } 29 | 30 | if((p / i) > l) { 31 | b.push_back(p / i); 32 | } 33 | } 34 | 35 | } 36 | 37 | if(i * i == p and i > l) { 38 | a.push_back(i); 39 | } 40 | 41 | 42 | 43 | 44 | printf("Case %d: ", j); 45 | 46 | for (int i = 0; i < a.size(); i++) { 47 | printf("%lld", a[i]); 48 | if(!(i == a.size() - 1 and b.size() == 0)) { 49 | printf(" "); 50 | } 51 | } 52 | 53 | for (int i = b.size() - 1; i >= 0; i--) { 54 | printf("%lld", b[i]); 55 | if(i != 0) { 56 | printf(" "); 57 | } 58 | } 59 | 60 | if(a.size() + b.size()) { 61 | printf("\n"); 62 | } 63 | 64 | else { 65 | printf("impossible\n"); 66 | } 67 | 68 | } 69 | 70 | 71 | } 72 | 73 | -------------------------------------------------------------------------------- /1016-Brush-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | int t; 11 | int n; 12 | int x; 13 | int y; 14 | int w; 15 | int temp; 16 | int count; 17 | 18 | scanf("%d", &t); 19 | 20 | for (int j = 1; j <= t; j++) { 21 | scanf("%d", &n); 22 | scanf("%d", &w); 23 | vector a; 24 | 25 | 26 | for (int i = 0; i < n; i++) { 27 | scanf("%d %d", &x, &y); 28 | a.push_back(y); 29 | } 30 | 31 | set s(a.begin(), a.end()); 32 | a.assign(s.begin(), s.end()); 33 | 34 | temp = a[0]; 35 | count = 1; 36 | 37 | for (int i = 0; i < a.size(); i++) { 38 | if(temp + w < a[i]) { 39 | 40 | temp = a[i]; 41 | 42 | count++; 43 | } 44 | 45 | } 46 | 47 | printf("Case %d: %d\n", j, count); 48 | 49 | 50 | } 51 | 52 | 53 | } 54 | 55 | -------------------------------------------------------------------------------- /1017-Brush-III.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int n; 9 | int k; 10 | int w; 11 | int ans[110][110][110]; 12 | 13 | struct node 14 | { 15 | int val; 16 | int count; 17 | }; 18 | 19 | 20 | node a[200]; 21 | 22 | bool epic(node x, node y) 23 | { 24 | if(x.val < y.val) { 25 | return 1; 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | 32 | int explore(int start, int end, int count) 33 | { 34 | 35 | 36 | if(end == n) { 37 | return 0; 38 | } 39 | 40 | if(count == k) { 41 | return 0; 42 | } 43 | 44 | if(ans[start][end][count] != -1) { 45 | return ans[start][end][count]; 46 | } 47 | 48 | if(a[end].val - a[start].val <= w) { 49 | if(start == end) { 50 | 51 | return ans[start][end][count] = max(a[end].count + explore(start, end+1, count), explore(end+1, end+1, count)); 52 | } 53 | 54 | else { 55 | return ans[start][end][count] = a[end].count + explore(start, end+1, count); 56 | } 57 | } 58 | 59 | else { 60 | return ans[start][end][count] = explore(end, end, count + 1); 61 | } 62 | 63 | } 64 | 65 | 66 | int main() 67 | { 68 | int t; 69 | int x; 70 | int y; 71 | int tot; 72 | 73 | scanf("%d", &t); 74 | 75 | for (int cs = 1; cs <= t; cs++) { 76 | 77 | scanf("%d", &tot); 78 | scanf("%d", &w); 79 | scanf("%d", &k); 80 | 81 | map q; 82 | memset(ans, -1, sizeof(ans)); 83 | n = 0; 84 | 85 | for (int i = 0; i < tot; i++) { 86 | scanf("%d", &x); 87 | scanf("%d", &y); 88 | 89 | if(q.count(y) == 0) { 90 | node temp; 91 | temp.val = y; 92 | temp.count = 1; 93 | q[y] = n; 94 | a[n] = temp; 95 | n++; 96 | } 97 | 98 | else { 99 | a[q[y]].count++; 100 | } 101 | 102 | 103 | } 104 | 105 | 106 | sort(a, a + n, epic); 107 | printf("Case %d: %d\n", cs,explore(0, 0, 0)); 108 | 109 | 110 | } 111 | 112 | } 113 | 114 | 115 | -------------------------------------------------------------------------------- /1018-Brush-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define set(x, i) (x | (1 << i)) 6 | #define check(x, i) (x & (1 << i)) 7 | using namespace std; 8 | 9 | enum CONST {N = 16}; 10 | 11 | typedef struct node { 12 | int x; 13 | int y; 14 | } node; 15 | 16 | int n; 17 | int collinear[N+3][N+3]; 18 | int dp[1 << N+2]; 19 | 20 | node a[N + 5]; 21 | 22 | int cal(int state) 23 | { 24 | int count; 25 | count = 0; 26 | 27 | for (int i = 1; i <= n; i++) 28 | if(check(state, i)) 29 | count++; 30 | 31 | return count; 32 | } 33 | 34 | int explore(int status) 35 | { 36 | int count; 37 | int temp; 38 | int mini; 39 | 40 | mini = INT_MAX; 41 | 42 | if(dp[status] != -1) { 43 | return dp[status]; 44 | } 45 | count = cal(status); 46 | 47 | if(count == n-1) { 48 | return 1; 49 | } 50 | 51 | if(count == n) { 52 | return 0; 53 | } 54 | 55 | 56 | 57 | for (int i = 1; i <= n; i++) { 58 | if(check(status,i) == 0) { 59 | for (int j = i+1; j <= n; j++) { 60 | if(check(status, j) == 0) { 61 | temp = set(status, i); 62 | temp = set(temp, j); 63 | 64 | temp = temp | collinear[i][j]; 65 | mini = min(mini, explore(temp)); 66 | } 67 | } 68 | break; 69 | } 70 | 71 | } 72 | 73 | return dp[status] = 1 + mini; 74 | 75 | } 76 | 77 | 78 | 79 | bool is_collinear(int i, int j, int k) 80 | { 81 | 82 | int x1; 83 | int y1; 84 | int x2; 85 | int y2; 86 | int x3; 87 | int y3; 88 | 89 | x1 = a[i].x; 90 | y1 = a[i].y; 91 | 92 | x2 = a[j].x; 93 | y2 = a[j].y; 94 | 95 | x3 = a[k].x; 96 | y3 = a[k].y; 97 | 98 | return (y2 - y1) * (x3 - x2) == (x2 - x1) * (y3 - y2); 99 | 100 | } 101 | 102 | void calculate_collinear_points() 103 | { 104 | 105 | for (int i = 1; i <= n; i++) { 106 | for (int j = i + 1; j <= n; j++) { 107 | for (int k = 1; k <= n; k++) { 108 | if(i != k and j != k and is_collinear(i, j, k)) { 109 | collinear[i][j] = set(collinear[i][j], k); 110 | collinear[j][i] = set(collinear[j][i], k); 111 | } 112 | } 113 | } 114 | } 115 | 116 | } 117 | 118 | 119 | int main() 120 | { 121 | 122 | int t; 123 | node temp; 124 | 125 | scanf("%d", &t); 126 | 127 | for(int cs = 1; cs <= t; cs++) { 128 | scanf("%d", &n); 129 | 130 | memset(collinear, 0, sizeof collinear); 131 | memset(dp, -1, sizeof dp); 132 | 133 | for (int i = 1; i <= n; i++) { 134 | scanf("%d", &temp.x); 135 | scanf("%d", &temp.y); 136 | a[i] = temp; 137 | } 138 | 139 | calculate_collinear_points(); 140 | 141 | printf("Case %d: %d\n", cs, explore(0)); 142 | 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /1019-Brush-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define INF 10000000 4 | using namespace std; 5 | int a[102][102]; 6 | void setup() 7 | { 8 | 9 | for (int i = 0; i < 101; i++) { 10 | for(int j = 0; j < 101; j++) { 11 | a[i][j] = INF; 12 | } 13 | 14 | } 15 | } 16 | int min(int x, int y) 17 | { 18 | if(x < y) { 19 | return x; 20 | } 21 | 22 | return y; 23 | } 24 | int main() 25 | { 26 | int n; 27 | int m; 28 | int x; 29 | int y; 30 | int w; 31 | int t; 32 | 33 | scanf("%d", &t); 34 | 35 | for(int p = 1; p <= t; p++) { 36 | setup(); 37 | scanf("%d", &n); 38 | scanf("%d", &m); 39 | 40 | for (int i = 0; i < m; i++) { 41 | scanf("%d", &x); 42 | scanf("%d", &y); 43 | scanf("%d", &w); 44 | 45 | a[x][y] = min(w, a[x][y]); 46 | } 47 | 48 | for (int k = 1; k <= n; k++) { 49 | for(int i = 0; i <= n; i++) { 50 | for (int j = 0; j <=n; j++) { 51 | if(a[i][j] > a[i][k] + a[k][j]) { 52 | a[i][j] = a[i][k] + a[k][j]; 53 | } 54 | 55 | } 56 | 57 | } 58 | 59 | } 60 | 61 | if(a[1][n] != INF) { 62 | printf("Case %d: %d\n",p, a[1][n]); 63 | } 64 | 65 | else { 66 | printf("Case %d: Impossible\n", p); 67 | } 68 | 69 | } 70 | 71 | 72 | } 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /1020-A-Childhood-Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | int t; 10 | long long n; 11 | char x[6]; 12 | 13 | scanf("%d", &t); 14 | 15 | for (int j = 1; j <= t; j++) { 16 | scanf("%lld", &n); 17 | scanf("%s", x); 18 | 19 | n = n % 3; 20 | if(strcmp(x, "Alice") == 0) { 21 | if(n == 0 or n == 2) { 22 | printf("Case %d: Alice\n", j); 23 | } 24 | 25 | else { 26 | printf("Case %d: Bob\n", j); 27 | } 28 | 29 | } 30 | 31 | else 32 | { 33 | if(n == 1 or n == 2) { 34 | printf("Case %d: Bob\n", j); 35 | } 36 | 37 | else { 38 | printf("Case %d: Alice\n", j); 39 | 40 | } 41 | 42 | } 43 | 44 | 45 | } 46 | 47 | 48 | 49 | } 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /1021-Painful-Bases.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define unset(x, i) (x & ~(1 << i)) 7 | #define set(x, i) (x | ((1 << i))) 8 | #define check(x, i) (x & (1 << i)) 9 | using namespace std; 10 | 11 | 12 | int base; 13 | int by; 14 | int n; 15 | int a[30]; 16 | int rem[25]; 17 | int l; 18 | unsigned long long dp[1 << 16][20]; 19 | unsigned long long pow[20]; 20 | bool vis[1 << 16]; 21 | 22 | unsigned long long explore(unsigned long long mask) 23 | { 24 | 25 | if(vis[mask] == 1 or mask == 0) { 26 | return 0; 27 | } 28 | 29 | 30 | vis[mask] = 1; 31 | 32 | for (int i = 0; i < by; i++) 33 | dp[mask][i] = 0; 34 | 35 | for (int i = 0; i < base; i++) { 36 | if(check(mask, i)) { 37 | l--; 38 | explore(unset(mask, i)); 39 | l++; 40 | for (int j = 0; j < by; j++) { 41 | dp[mask][(pow[l - 1] * i + j) % by] += dp[unset(mask, i)][j]; 42 | } 43 | } 44 | } 45 | } 46 | 47 | int main() 48 | { 49 | 50 | int t; 51 | char c; 52 | char temp[20]; 53 | unsigned long long mask; 54 | scanf("%d", &t); 55 | 56 | for (int cs = 1; cs <= t; cs++) { 57 | scanf("%d", &base); 58 | scanf("%d", &by); 59 | scanf("%s", temp); 60 | 61 | 62 | n = strlen(temp); 63 | l = n; 64 | pow[0] = 1; 65 | mask = 0; 66 | memset(dp, 0, sizeof dp); 67 | memset(vis, 0, sizeof vis); 68 | 69 | for (int i = 1; i <= n; i++) { 70 | pow[i] = pow[i-1] * base; 71 | pow[i] = pow[i] % by; 72 | } 73 | 74 | 75 | for (int i = 0; i < n; i++) { 76 | if(isalpha(temp[i])) { 77 | a[i] = (temp[i] - 'A') + 10; 78 | } 79 | else { 80 | a[i] = (temp[i] - '0'); 81 | } 82 | mask = set(mask, a[i]); 83 | } 84 | 85 | 86 | dp[0][0] = 1; 87 | explore(mask); 88 | printf("Case %d: %llu\n", cs, dp[mask][0]); 89 | 90 | } 91 | } 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /1022-Circle-in-Square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | double r; 9 | double sarea; 10 | double carea; 11 | double area; 12 | int t; 13 | 14 | scanf("%d", &t); 15 | 16 | for (int i = 1; i <= t; i++) { 17 | scanf("%lf", &r); 18 | carea = 2 * acos(0.0) * r * r; 19 | sarea = (2 * r) * (2 * r); 20 | area = sarea - carea; 21 | 22 | printf("Case %d: %.2lf\n",i, area); 23 | } 24 | 25 | 26 | } 27 | 28 | -------------------------------------------------------------------------------- /1023-Discovering-Perumutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define rint(x) scanf("%d", &x) 5 | using namespace std; 6 | 7 | int fact(int x) 8 | { 9 | int ans = 1; 10 | 11 | while(x) { 12 | ans = ans * x; 13 | x--; 14 | } 15 | 16 | return ans; 17 | 18 | } 19 | 20 | int main() 21 | { 22 | char c; 23 | int t; 24 | int k; 25 | int n; 26 | int tot; 27 | 28 | 29 | rint(t); 30 | 31 | for (int p = 1; p <= t; p++) { 32 | c = 'A'; 33 | string a; 34 | rint(n); 35 | rint(k); 36 | 37 | for (int i = 0; i < n; i++) { 38 | a = a + c; 39 | c = c + 1; 40 | } 41 | 42 | printf("Case %d:\n", p); 43 | 44 | 45 | if(n < 5) { 46 | 47 | tot = fact(n); 48 | 49 | if(k < tot) { 50 | 51 | for (int i = 0; i < k; i++) { 52 | cout << a << endl; 53 | next_permutation(a.begin(), a.end()); 54 | 55 | } 56 | 57 | } 58 | 59 | else { 60 | 61 | if(k >= tot) { 62 | 63 | for (int i = 0; i < tot; i++) { 64 | cout << a << endl; 65 | next_permutation(a.begin(), a.end()); 66 | 67 | } 68 | } 69 | } 70 | 71 | } 72 | 73 | else { 74 | 75 | for (int i = 0; i < k; i++) { 76 | cout << a << endl; 77 | next_permutation(a.begin(), a.end()); 78 | } 79 | 80 | } 81 | 82 | } 83 | 84 | } 85 | 86 | -------------------------------------------------------------------------------- /1024-Eid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define sz 10000 10 | 11 | using namespace std; 12 | int sv[sz]; 13 | 14 | 15 | 16 | string multiply( string a, long long b ) { 17 | // a contains the biginteger in reversed form 18 | int carry = 0; 19 | for( int i = 0; i < a.size(); i++ ) { 20 | carry += (a[i] - 48) * b; 21 | a[i] = ( carry % 10 + 48 ); 22 | carry /= 10; 23 | } 24 | while( carry ) { 25 | a += ( carry % 10 + 48 ); 26 | carry /= 10; 27 | } 28 | return a; 29 | } 30 | 31 | 32 | int main() { 33 | vector prime; 34 | prime.push_back(2); 35 | 36 | for (int i = 3; i * i <= sz; i++) { 37 | if(sv[i] == 0) { 38 | for (int j = i * i; j <= sz; j += 2 * i) { 39 | sv[j] = 1; 40 | } 41 | } 42 | } 43 | 44 | for (int i = 3; i <= sz; i += 2) { 45 | if(sv[i] == 0) { 46 | prime.push_back(i); 47 | } 48 | } 49 | 50 | int t; 51 | long long x; 52 | long long mul; 53 | int n; 54 | string lcm; 55 | 56 | string temp; 57 | 58 | scanf("%d", &t); 59 | 60 | for (int cs = 1; cs <= t; cs++) { 61 | long long fact[10000]; 62 | memset(fact, 0, sizeof fact); 63 | scanf("%d", &n); 64 | lcm = "1"; 65 | 66 | for(int j = 0; j < n; j++) { 67 | 68 | scanf("%lld", &x); 69 | // cout << "=== " << x << endl; 70 | for (int i = 0; i < prime.size() and prime[i] * prime[i] <= x ; i++) { 71 | mul = prime[i]; 72 | while(x % prime[i] == 0) { 73 | // cout << mul << "inside \n"; 74 | mul = mul * prime[i]; 75 | x = x / prime[i]; 76 | } 77 | 78 | mul = mul / prime[i]; 79 | 80 | fact[i] = max(fact[i], mul); 81 | } 82 | 83 | // cout <<"remains "<< x << endl; 84 | 85 | int k; 86 | if(x == 2 or ((x % 2) and sv[x] == 0)) { 87 | // cout << "inside " << x << endl; 88 | k = lower_bound(prime.begin(), prime.end(),x) - prime.begin(); 89 | fact[k] = max(fact[k], x); 90 | 91 | } 92 | 93 | } 94 | 95 | for (int i = 0; i < prime.size(); i++) { 96 | if(fact[i] != 0) { 97 | lcm = multiply(lcm, fact[i]); 98 | } 99 | 100 | } 101 | 102 | char ans[1000000]; 103 | string s; 104 | s = lcm; 105 | 106 | for (int i = 0, j = s.size() - 1; i < s.size(); i++, j--) { 107 | ans[i] = s[j]; 108 | } 109 | 110 | ans[s.size()] = '\0'; 111 | 112 | printf("Case %d: %s\n", cs, ans); 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /1025-The-Specials-Menu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char str[100]; 7 | long long dp[100][100]; 8 | 9 | long long wow(int i, int j) 10 | { 11 | if(j < i) { 12 | return 0; 13 | } 14 | 15 | if(i == j ) { 16 | return 1; 17 | } 18 | 19 | if(dp[i][j] != -1) { 20 | return dp[i][j]; 21 | } 22 | 23 | if(str[i] == str[j]) { 24 | return dp[i][j] = 1 + wow(i+1, j) + wow(i, j-1); 25 | } 26 | 27 | else { 28 | return dp[i][j] = wow(i+1, j) + wow(i, j-1) - wow(i+1, j-1); 29 | 30 | } 31 | 32 | } 33 | 34 | 35 | int main() 36 | { 37 | 38 | int t; 39 | int n; 40 | scanf("%d", &t); 41 | for (int i = 1; i <= t; i++) { 42 | scanf("%s", str); 43 | memset(dp, -1, sizeof(dp)); 44 | n = strlen(str) - 1; 45 | printf("Case %d: %lld\n", i, wow(0, n)); 46 | 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /1026-Critical-Links.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define SIZE 10006 8 | using namespace std; 9 | 10 | vector < vector > g; 11 | map < pair , int > mp; 12 | 13 | int vis[SIZE]; 14 | int dur[SIZE]; 15 | int low[SIZE]; 16 | int art[SIZE]; 17 | int counter; 18 | int parent[SIZE]; 19 | 20 | int find_articulation(int x) 21 | { 22 | int child; 23 | int y; 24 | 25 | counter++; 26 | vis[x] = 1; 27 | child = 0; 28 | 29 | dur[x] = low[x] = counter; 30 | 31 | for (int i = 0; i < g[x].size(); i++) { 32 | y = g[x][i]; 33 | child++; 34 | 35 | if(vis[y] == 0) { 36 | parent[y] = x; 37 | find_articulation(y); 38 | low[x] = min(low[x], low[y]); 39 | 40 | if(low[y] > dur[x]) { 41 | if(mp.count(make_pair(min(x, y), max(x, y))) == 0) { 42 | mp[make_pair(min(x, y), max(x, y))] = 1; 43 | } 44 | 45 | } 46 | } 47 | 48 | else { 49 | if(y != parent[x]) { 50 | low[x] = min(low[x], dur[y]); 51 | } 52 | } 53 | 54 | } 55 | 56 | 57 | } 58 | 59 | int main() 60 | { 61 | 62 | int t; 63 | int n; 64 | int m; 65 | int x; 66 | int y; 67 | char c; 68 | 69 | scanf("%d", &t); 70 | 71 | for (int cs = 1; cs <= t; cs++) { 72 | 73 | scanf("%d", &n); 74 | 75 | vector < vector > tg(n + 2); 76 | map < pair , int > tmp; 77 | swap(mp, tmp); 78 | 79 | swap(tg, g); 80 | memset(parent, -1, sizeof parent); 81 | memset(vis, 0, sizeof vis); 82 | memset(art, 0, sizeof art); 83 | counter = 0; 84 | 85 | for (int i = 0; i < n; i++) { 86 | scanf("%d (%d)", &x, &m); 87 | 88 | for (int j = 0; j < m; j++) { 89 | scanf("%d", &y); 90 | if(x < y) { 91 | g[x].push_back(y); 92 | g[y].push_back(x); 93 | } 94 | 95 | } 96 | 97 | } 98 | 99 | for (int i = 0; i <= n; i++) { 100 | if(!vis[i]) { 101 | find_articulation(i); 102 | } 103 | } 104 | 105 | map< pair , int >::iterator it; 106 | 107 | printf("Case %d:\n", cs); 108 | printf("%d critical links\n", mp.size()); 109 | for(it = mp.begin(); it != mp.end(); it++) { 110 | printf("%d - %d\n", it->first.first, it->first.second); 111 | } 112 | 113 | 114 | 115 | } 116 | 117 | } 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /1027-A-Dangerous-Maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | pair minify(pair temp) 8 | { 9 | int x; 10 | int y; 11 | int ok; 12 | x = temp.first; 13 | y = temp.second; 14 | 15 | ok = 1; 16 | 17 | while(ok) { 18 | ok = 0; 19 | for (int i = 2; i <= y; i++) { 20 | if(x % i == 0 and y % i == 0) { 21 | ok = 1; 22 | x = x / i; 23 | y = y / i; 24 | } 25 | 26 | } 27 | 28 | } 29 | 30 | 31 | temp.first = x; 32 | temp.second = y; 33 | 34 | return temp; 35 | 36 | } 37 | 38 | 39 | int main() 40 | { 41 | 42 | int t; 43 | int psum; 44 | int nsum; 45 | int n; 46 | int ncount; 47 | int x; 48 | int y; 49 | 50 | pair temp; 51 | scanf("%d", &t); 52 | 53 | for (int cases = 1; cases <= t; cases++) { 54 | scanf("%d", &n); 55 | nsum = 0; 56 | psum = 0; 57 | ncount = 0; 58 | for(int i = 0; i < n; i++) { 59 | scanf("%d", &x); 60 | if(x < 0) { 61 | ncount++; 62 | nsum += abs(x); 63 | } 64 | 65 | else { 66 | nsum += x; 67 | } 68 | 69 | } 70 | 71 | if(ncount == n) { 72 | printf("Case %d: inf\n", cases); 73 | } 74 | 75 | else { 76 | temp.first = nsum; 77 | temp.second = n - ncount; 78 | temp = minify(temp); 79 | 80 | printf("Case %d: %d/%d\n",cases, temp.first, temp.second); 81 | } 82 | 83 | } 84 | 85 | 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /1028-Trailing-Zeroes-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define sroot 1000007 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | vector primes; 10 | 11 | 12 | int sieve() 13 | { 14 | bool a[sroot]; 15 | memset(a, 0, sizeof(a)); 16 | 17 | for(int i = 2; i <= sroot; i++) { 18 | if(a[i] == 0) { 19 | for (int j = i + i; j <= sroot; j = j + i) { 20 | a[j] = 1; 21 | } 22 | 23 | } 24 | 25 | } 26 | 27 | for (int i = 2; i <= sroot; i++) { 28 | if(a[i] == 0) { 29 | primes.push_back(i); 30 | } 31 | 32 | } 33 | 34 | sort(primes.begin(), primes.end()); 35 | 36 | } 37 | 38 | 39 | 40 | 41 | int main() 42 | { 43 | sieve(); 44 | 45 | int t; 46 | long long x; 47 | long long sum; 48 | long long k; 49 | long long temp; 50 | long long n; 51 | 52 | scanf("%d", &t); 53 | 54 | for (int cases = 1; cases <= t; cases++) { 55 | scanf("%lld", &x); 56 | sum = 1; 57 | n = sqrt(x); 58 | 59 | for(int i = 0; i <= primes.size() - 1 and primes[i] <= n ; i++) { 60 | 61 | k = 0; 62 | if(x < primes[i]) { 63 | break; 64 | } 65 | 66 | while(x % primes[i] == 0) { 67 | 68 | x = x / primes[i]; 69 | k++; 70 | } 71 | 72 | sum = sum * (k + 1); 73 | 74 | } 75 | 76 | if(x > 1) sum *= 2; 77 | cout << x << endl; 78 | sum = sum - 1; 79 | 80 | printf("Case %d: %lld\n",cases, sum); 81 | 82 | } 83 | 84 | 85 | } 86 | 87 | -------------------------------------------------------------------------------- /1029-Civil-and-Evil-Engineer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | vector < vector < pair > > graph; 10 | int n; 11 | 12 | int prim() 13 | { 14 | 15 | priority_queue < pair < int, pair < int, int> >, vector < pair > > , greater < pair > > > q; 16 | bool a[10004]; 17 | memset(a, 0, sizeof(a)); 18 | 19 | pair > temp; 20 | pair node; 21 | int count = 0; 22 | int x; 23 | int y; 24 | int w; 25 | int ans; 26 | 27 | ans = 0; 28 | 29 | for (int i = 0; i < graph[0].size(); i++) { 30 | node = graph[0][i]; 31 | temp.first = node.second; 32 | temp.second.first = 0; 33 | temp.second.second = node.first; 34 | q.push(temp); 35 | temp.second.second = 0; 36 | temp.second.first = node.first; 37 | q.push(temp); 38 | count = 1; 39 | a[0] = 1; 40 | } 41 | 42 | 43 | while (count <= n and !q.empty()) { 44 | temp = q.top(); 45 | q.pop(); 46 | x = temp.second.first; 47 | y = temp.second.second; 48 | w = temp.first; 49 | if(a[x] and a[y]) 50 | continue; 51 | 52 | if(!a[x]) { 53 | ans += w; 54 | a[x] = 1; 55 | 56 | for (int i = 0; i < graph[x].size(); i++) { 57 | temp.first = graph[x][i].second; 58 | temp.second.first = x; 59 | temp.second.second = graph[x][i].first; 60 | q.push(temp); 61 | temp.second.second = x; 62 | temp.second.first = graph[x][i].first; 63 | q.push(temp); 64 | } 65 | 66 | count++; 67 | 68 | } 69 | 70 | else { 71 | ans += w; 72 | a[y] = 1; 73 | 74 | for (int i = 0; i < graph[y].size(); i++) { 75 | temp.first = graph[y][i].second; 76 | temp.second.first = y; 77 | temp.second.second = graph[y][i].first; 78 | q.push(temp); 79 | temp.second.second = y; 80 | temp.second.first = graph[y][i].first; 81 | q.push(temp); 82 | } 83 | count++; 84 | 85 | } 86 | 87 | 88 | 89 | } 90 | 91 | return ans; 92 | 93 | 94 | } 95 | 96 | int prim2() 97 | { 98 | 99 | priority_queue < pair < int, pair < int, int> > > q; 100 | bool a[10004]; 101 | memset(a, 0, sizeof(a)); 102 | 103 | pair > temp; 104 | pair node; 105 | int count = 0; 106 | int x; 107 | int y; 108 | int w; 109 | int ans; 110 | 111 | ans = 0; 112 | 113 | for (int i = 0; i < graph[0].size(); i++) { 114 | node = graph[0][i]; 115 | temp.first = node.second; 116 | temp.second.first = 0; 117 | temp.second.second = node.first; 118 | q.push(temp); 119 | temp.second.second = 0; 120 | temp.second.first = node.first; 121 | q.push(temp); 122 | count = 1; 123 | a[0] = 1; 124 | } 125 | 126 | 127 | while (count <= n and !q.empty()) { 128 | temp = q.top(); 129 | q.pop(); 130 | x = temp.second.first; 131 | y = temp.second.second; 132 | w = temp.first; 133 | if(a[x] and a[y]) 134 | continue; 135 | 136 | if(!a[x]) { 137 | ans += w; 138 | a[x] = 1; 139 | 140 | for (int i = 0; i < graph[x].size(); i++) { 141 | temp.first = graph[x][i].second; 142 | temp.second.first = x; 143 | temp.second.second = graph[x][i].first; 144 | q.push(temp); 145 | temp.second.second = x; 146 | temp.second.first = graph[x][i].first; 147 | q.push(temp); 148 | } 149 | 150 | count++; 151 | 152 | } 153 | 154 | else { 155 | ans += w; 156 | a[y] = 1; 157 | 158 | for (int i = 0; i < graph[y].size(); i++) { 159 | temp.first = graph[y][i].second; 160 | temp.second.first = y; 161 | temp.second.second = graph[y][i].first; 162 | q.push(temp); 163 | temp.second.second = y; 164 | temp.second.first = graph[y][i].first; 165 | q.push(temp); 166 | } 167 | count++; 168 | 169 | } 170 | 171 | 172 | 173 | } 174 | 175 | return ans; 176 | 177 | } 178 | 179 | 180 | 181 | int main() 182 | { 183 | 184 | int t; 185 | int x; 186 | int y; 187 | int w; 188 | int ok; 189 | 190 | pair to; 191 | scanf("%d", &t); 192 | 193 | for(int cases = 1; cases <= t; cases++) { 194 | scanf("%d", &n); 195 | vector < vector > > temp(n+1); 196 | swap(temp, graph); 197 | while (scanf("%d %d %d", &x, &y, &w)==3 && w) { 198 | 199 | 200 | to.first = y; 201 | to.second = w; 202 | 203 | graph[x].push_back(to); 204 | to.first = x; 205 | to.second = w; 206 | graph[y].push_back(to); 207 | } 208 | x = prim(); 209 | y = prim2(); 210 | 211 | y = y + x; 212 | if(y % 2 == 0) { 213 | y = y / 2; 214 | printf("Case %d: %d\n", cases, y); 215 | } 216 | 217 | else { 218 | printf("Case %d: %d/2\n", cases, y); 219 | } 220 | 221 | } 222 | 223 | 224 | } 225 | 226 | 227 | 228 | -------------------------------------------------------------------------------- /1030-Discovering-Gold.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | double pro[110]; 10 | int val[110]; 11 | int t; 12 | int n; 13 | int j; 14 | int count; 15 | double exp; 16 | 17 | scanf("%d", &t); 18 | 19 | for (int cs = 1; cs <= t; cs++) { 20 | scanf("%d", &n); 21 | memset(pro, 0, sizeof pro); 22 | pro[1] = 1; 23 | 24 | 25 | for (int i = 1; i <= n; i++) { 26 | scanf("%d", val + i); 27 | } 28 | 29 | exp = val[1]; 30 | 31 | for (int i = 2; i <= n; i++) { 32 | 33 | for (int k = 1; k <= 6 and i - k >= 1; k++) { 34 | j = i - k; 35 | count = min(6, n - j); 36 | pro[i] = pro[i] + (pro[j] * ( double(1) / double(count))); 37 | } 38 | 39 | exp = exp + pro[i] * val[i]; 40 | 41 | } 42 | 43 | printf("Case %d: %lf\n", cs, exp); 44 | 45 | } 46 | 47 | } 48 | 49 | -------------------------------------------------------------------------------- /1031-Easy-Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | long long dp[110][110][2]; 8 | int a[110]; 9 | int n; 10 | 11 | long long explore(int l, int r, int chance) 12 | { 13 | 14 | long long x; 15 | long long sum; 16 | 17 | if(l > r) { 18 | return 0; 19 | } 20 | 21 | if(dp[l][r][chance] != -1) { 22 | return dp[l][r][chance]; 23 | } 24 | 25 | if(chance == 0) { 26 | sum = 0; 27 | x = INT_MIN; 28 | 29 | for (int i = l; i <= r; i++) { 30 | sum = sum + a[i]; 31 | x = max(x, sum + explore(i+1, r, 1)); 32 | } 33 | sum = 0; 34 | 35 | for (int i = r; i >= l; i--) { 36 | sum = sum + a[i]; 37 | x = max(x, sum + explore(l, i-1, 1)); 38 | } 39 | 40 | dp[l][r][chance] = x; 41 | return x; 42 | 43 | } 44 | 45 | if(chance == 1) { 46 | sum = 0; 47 | x = INT_MAX; 48 | 49 | for (int i = l; i <= r; i++) { 50 | sum = sum + -a[i]; 51 | x = min(x, sum + explore(i+1, r, 0)); 52 | } 53 | sum = 0; 54 | 55 | for (int i = r; i >= l; i--) { 56 | sum = sum + -a[i]; 57 | x = min(x, sum + explore(l, i-1, 0)); 58 | } 59 | 60 | dp[l][r][chance] = x; 61 | return x; 62 | 63 | } 64 | 65 | } 66 | 67 | 68 | 69 | int main() 70 | { 71 | 72 | int t; 73 | 74 | scanf("%d", &t); 75 | 76 | for (int cs = 1; cs <= t; cs++) { 77 | 78 | memset(dp, -1 , sizeof dp); 79 | scanf("%d", &n); 80 | 81 | for (int i = 0; i < n; i++) { 82 | scanf("%d", a + i); 83 | } 84 | 85 | printf("Case %d: %lld\n", cs, explore(0, n-1, 0)); 86 | 87 | } 88 | 89 | } 90 | 91 | -------------------------------------------------------------------------------- /1033-Generating-Palindromes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int pali(string x, string y) 8 | { 9 | int a[110][110]; 10 | memset(a, 0, sizeof(a)); 11 | 12 | for (int i = x.length(); i >= 0; i--) { 13 | for (int j = y.length(); j >= 0; j--) { 14 | if(i == x.length() or j == y.length()) { 15 | a[i][j] = 0; 16 | continue; 17 | } 18 | 19 | if(x[i] == y[j]) { 20 | a[i][j] = 1 + a[i+1][j+1]; 21 | continue; 22 | } 23 | 24 | a[i][j] = max(a[i+1][j], a[i][j+1]); 25 | 26 | } 27 | 28 | } 29 | 30 | 31 | return x.length() - a[0][0]; 32 | 33 | 34 | } 35 | 36 | 37 | 38 | 39 | int main() 40 | { 41 | 42 | string x; 43 | string y; 44 | char temp[110]; 45 | int t; 46 | scanf("%d", &t); 47 | 48 | for (int cases = 1; cases <= t; cases++) { 49 | scanf("%s", temp); 50 | x = temp; 51 | for (int i = 0, j = x.length() - 1; i < j; i++, j--) { 52 | swap(temp[i], temp[j]); 53 | } 54 | y = temp; 55 | printf("Case %d: %d\n",cases, pali(x, y)); 56 | } 57 | 58 | } 59 | 60 | -------------------------------------------------------------------------------- /1034-Hit-the-Light-Switches.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | bool visited[10004]; 9 | int n; 10 | 11 | vector < vector > graph; 12 | vector < vector > transpose; 13 | stack track; 14 | 15 | int dfs(int j) 16 | { 17 | visited[j] = true; 18 | int t; 19 | 20 | for (int i = 0; i < graph[j].size(); i++) { 21 | t = graph[j][i]; 22 | 23 | if(visited[t] == false) { 24 | dfs(t); 25 | } 26 | 27 | } 28 | 29 | track.push(j); 30 | 31 | } 32 | 33 | int dfsbro(int j) 34 | { 35 | visited[j] = true; 36 | int t; 37 | for (int i = 0; i < transpose[j].size(); i++) { 38 | t = transpose[j][i]; 39 | if(visited[t] == false) { 40 | dfsbro(t); 41 | } 42 | 43 | } 44 | 45 | 46 | } 47 | 48 | int dfsbro2(int j) 49 | { 50 | visited[j] = true; 51 | int t; 52 | for (int i = 0; i < graph[j].size(); i++) { 53 | t = graph[j][i]; 54 | if(visited[t] == false) { 55 | dfsbro2(t); 56 | } 57 | 58 | } 59 | 60 | 61 | } 62 | 63 | 64 | int trans() 65 | { 66 | vector < vector > temp(n+5); 67 | swap(temp, transpose); 68 | int t; 69 | 70 | for (int i = 1; i <= n; i++) { 71 | for (int j = 0; j < graph[i].size(); j++) { 72 | t = graph[i][j]; 73 | transpose[t].push_back(i); 74 | } 75 | 76 | } 77 | 78 | } 79 | 80 | int main() 81 | { 82 | 83 | int x; 84 | int y; 85 | int m; 86 | int count; 87 | int tt; 88 | int t; 89 | scanf("%d", &t); 90 | 91 | for (int cases = 1; cases <= t; cases++) { 92 | scanf("%d", &n); 93 | scanf("%d", &m); 94 | vector < vector > temp(n+5); 95 | memset(visited, 0, sizeof(visited)); 96 | stack track2; 97 | swap(track, track2); 98 | swap(temp, graph); 99 | 100 | for (int i = 0; i < m; i++) { 101 | scanf("%d", &x); 102 | scanf("%d", &y); 103 | graph[x].push_back(y); 104 | } 105 | 106 | for (int i = 1; i <= n; i++) { 107 | if(visited[i] == false) { 108 | dfs(i); 109 | } 110 | 111 | } 112 | 113 | 114 | // trans(); 115 | 116 | memset(visited, 0, sizeof(visited)); 117 | count = 0; 118 | 119 | while(!track.empty()) { 120 | tt = track.top(); 121 | track.pop(); 122 | if(visited[tt] == false) { 123 | dfsbro2(tt); 124 | count++; 125 | } 126 | 127 | } 128 | 129 | printf("Case %d: %d\n", cases, count); 130 | 131 | } 132 | 133 | 134 | } 135 | 136 | 137 | -------------------------------------------------------------------------------- /1035-Intelligent-Factorial-Factorization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | vector primes; 7 | 8 | int sieve() 9 | { 10 | 11 | int a[102]; 12 | memset(a, 0, sizeof(a)); 13 | 14 | for (int i = 2; i <= 100; i++) { 15 | 16 | if(a[i] == 0) { 17 | primes.push_back(i); 18 | for (int j = 2 * i; j <= 100; j = j + i) { 19 | a[j] = 1; 20 | } 21 | } 22 | 23 | } 24 | 25 | 26 | 27 | } 28 | 29 | 30 | int main() 31 | { 32 | 33 | int t; 34 | int n; 35 | int a[105]; 36 | int ans[105]; 37 | int Cases; 38 | 39 | sieve(); 40 | 41 | scanf("%d", &Cases); 42 | 43 | for (int cases = 1; cases <= Cases; cases++) { 44 | scanf("%d", &n); 45 | memset(ans, 0, sizeof(ans)); 46 | for (int i = 1; i <= n; i++) { 47 | a[i] = i; 48 | } 49 | 50 | for (int j = 0; primes[j] <= n and j < primes.size(); j++) { 51 | t = primes[j]; 52 | for (int i = t; i <= n; i = i + t) { 53 | while(a[i] % t == 0) { 54 | a[i] = a[i] / t; 55 | ans[t]++; 56 | } 57 | 58 | } 59 | 60 | } 61 | printf("Case %d: %d = ", cases, n); 62 | for (int i = 0; primes[i] <= n and i < primes.size(); i++) { 63 | printf("%d (%d)",primes[i], ans[primes[i]]); 64 | if(i != primes.size() - 1 and primes[i+1] <= n) { 65 | printf(" * "); 66 | } 67 | } 68 | 69 | printf("\n"); 70 | 71 | 72 | 73 | } 74 | 75 | 76 | 77 | } 78 | 79 | 80 | -------------------------------------------------------------------------------- /1037-Agent-47.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define check(n, pos) (n & (1<> c; 85 | x[i][j] = (int) (c - '0'); 86 | } 87 | 88 | } 89 | 90 | for (int i = 0; i < n; i++) { 91 | x[n][i] = 1; 92 | } 93 | printf("Case %d: %d\n",cases, wow(0, a)); 94 | 95 | } 96 | 97 | } 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /1038-Race-to-1-Again.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define N 100000 4 | 5 | using namespace std; 6 | 7 | double pro[N + 4]; 8 | int count[N+ 4]; 9 | int main() 10 | { 11 | double sum; 12 | double temp; 13 | int x; 14 | int n; 15 | 16 | 17 | for (int i = 2; i <= N; i++) { 18 | sum = pro[i]; 19 | n = count[i]; 20 | n += 2; 21 | sum += n; 22 | 23 | 24 | sum = sum / (double) (n -1); 25 | pro[i] = sum; 26 | 27 | for (int j = 2 * i; j <= N; j = j + i) { 28 | count[j]++; 29 | pro[j] += sum; 30 | } 31 | 32 | } 33 | 34 | int t; 35 | scanf("%d", &t); 36 | 37 | for (int cs = 1; cs<= t; cs++) { 38 | scanf("%d", &x); 39 | 40 | printf("Case %d: %lf\n", cs, pro[x]); 41 | } 42 | 43 | } 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /1040-Donation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define ii pair 8 | #define pii pair 9 | using namespace std; 10 | 11 | 12 | int count = 0; 13 | int check[100]; 14 | int a[100][100]; 15 | 16 | int test(int i, int n) 17 | { 18 | for (int j = 0; j < n; j++) { 19 | if(i != j and (a[i][j] != INT_MAX or a[j][i] != INT_MAX) and check[j] == 0) { 20 | check[j] = 1; 21 | count++; 22 | test(j, n); 23 | } 24 | } 25 | 26 | } 27 | 28 | 29 | int prim() 30 | { 31 | 32 | int ans[100][100]; 33 | int already[100]; 34 | long long total; 35 | total = 0; 36 | 37 | 38 | 39 | priority_queue < pair, vector >, greater > > q; 40 | 41 | int n; 42 | int m; 43 | 44 | int x; 45 | int y; 46 | int w; 47 | 48 | int n1; 49 | int n2; 50 | 51 | int dist = 0; 52 | 53 | pair temp; 54 | 55 | 56 | cin >> n; 57 | m = n * n; 58 | for (int i = 0; i < n; i++) { 59 | already[i] = -1; 60 | } 61 | 62 | for (int i = 0; i < n; i++) { 63 | for (int j = 0; j < n; j++) { 64 | scanf("%d", &a[i][j]); 65 | total += a[i][j]; 66 | ans[i][j] = INT_MAX; 67 | if(a[i][j] == 0) { 68 | a[i][j] = INT_MAX; 69 | } 70 | } 71 | 72 | } 73 | + 74 | 75 | 76 | memset(check, 0, sizeof(check)); 77 | count = 1; 78 | check[0] = 1; 79 | test(0, n); 80 | 81 | if(count != n) { 82 | 83 | return -1; 84 | } 85 | 86 | for (int i = 0; i < n; i++) { 87 | for (int j = 0; j < n; j++) { 88 | a[i][j] = min(a[i][j], a[j][i]); 89 | } 90 | } 91 | 92 | 93 | 94 | already[0] = 1; 95 | 96 | for (int i = 0; i < n; i++) { 97 | q.push(pii (a[0][i],(ii ( 0, i)))); 98 | } 99 | 100 | for (int j = 1; j < n; j++) { 101 | 102 | do 103 | { 104 | temp = q.top(); 105 | q.pop(); 106 | w = temp.first; 107 | x = temp.second.first; 108 | y = temp.second.second; 109 | 110 | } while(already[x] != -1 and already[y] != -1 and !q.empty()); 111 | ans[x][y] = w; 112 | dist += w; 113 | 114 | if(already[x] == -1) { 115 | for (int i = 0; i < n; i++) { 116 | q.push(pii(a[x][i], ii(x, i))); 117 | already[x] = 1; 118 | } 119 | 120 | } 121 | 122 | else { 123 | for (int i = 0; i < n; i++) { 124 | q.push(pii(a[y][i], ii(y, i))); 125 | already[y] = 1; 126 | } 127 | 128 | } 129 | 130 | 131 | } 132 | 133 | return (total - dist); 134 | 135 | 136 | 137 | 138 | } 139 | 140 | 141 | 142 | int main() 143 | { 144 | int t; 145 | scanf("%d", &t); 146 | for (int cases = 1; cases <= t; cases++) { 147 | printf("Case %d: %d\n", cases, prim()); 148 | } 149 | } 150 | 151 | 152 | 153 | 154 | 155 | 156 | -------------------------------------------------------------------------------- /1041-Road-Construction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define ii pair 9 | #define pii pair 10 | using namespace std; 11 | 12 | 13 | int n; 14 | int a[100][100]; 15 | int count; 16 | int compo[100]; 17 | int marked[100]; 18 | int tested; 19 | 20 | int prims() 21 | { 22 | 23 | int distance_matrix[100][100]; 24 | int ans[100][100]; 25 | int already_visited[100]; 26 | 27 | 28 | 29 | priority_queue < pair, vector >, greater > > prims_prio_q; 30 | 31 | int number_nodes; 32 | int number_edges; 33 | 34 | int node_1; 35 | int node_2; 36 | int p; 37 | int r; 38 | int temp_weight; 39 | 40 | int final_distance = 0; 41 | 42 | pair pair_temp; 43 | 44 | number_nodes = count; 45 | 46 | 47 | for (int i = 0; i < number_nodes; i++) { 48 | already_visited[i] = -1; 49 | } 50 | 51 | for (int i = 0; i < number_nodes; i++) { 52 | for (int j = 0; j < number_nodes; j++) { 53 | distance_matrix[i][j] = INT_MAX; 54 | ans[i][j] = INT_MAX; 55 | } 56 | 57 | } 58 | 59 | 60 | for (int i = 0; i < n; ++i) 61 | { 62 | for (int j = 0; j < n; ++j) 63 | { 64 | p = compo[i]; 65 | r = compo[j]; 66 | 67 | if(a[i][j] != -1 and a[j][i] != -1 and a[i][j] != 0) { 68 | distance_matrix[p][r] = min(a[i][j], distance_matrix[p][r]); 69 | distance_matrix[r][p] = min(a[j][i], distance_matrix[r][p]); 70 | } 71 | 72 | } 73 | } 74 | 75 | 76 | already_visited[0] = 1; 77 | 78 | for (int i = 0; i < number_nodes; i++) { 79 | prims_prio_q.push(pii (distance_matrix[0][i],(ii ( 0, i)))); 80 | } 81 | 82 | for (int j = 1; j < number_nodes; j++) { 83 | 84 | do 85 | { 86 | pair_temp = prims_prio_q.top(); 87 | prims_prio_q.pop(); 88 | temp_weight = pair_temp.first; 89 | node_1 = pair_temp.second.first; 90 | node_2 = pair_temp.second.second; 91 | 92 | } while(already_visited[node_1] != -1 and already_visited[node_2] != -1 and !prims_prio_q.empty()); 93 | ans[node_1][node_2] = temp_weight; 94 | final_distance += temp_weight; 95 | 96 | if(already_visited[node_1] == -1) { 97 | for (int i = 0; i < number_nodes; i++) { 98 | prims_prio_q.push(pii(distance_matrix[node_1][i], ii(node_1, i))); 99 | already_visited[node_1] = 1; 100 | } 101 | 102 | } 103 | 104 | else { 105 | for (int i = 0; i < number_nodes; i++) { 106 | prims_prio_q.push(pii(distance_matrix[node_2][i], ii(node_2, i))); 107 | already_visited[node_2] = 1; 108 | } 109 | 110 | } 111 | 112 | 113 | } 114 | 115 | return final_distance; 116 | 117 | 118 | 119 | 120 | 121 | } 122 | 123 | 124 | 125 | 126 | int explore(int i) 127 | { 128 | for (int j = 0; j < n; j++) { 129 | if(a[i][j] == 0 and compo[j] == -1) { 130 | compo[j] = count; 131 | explore(j); 132 | } 133 | 134 | } 135 | 136 | } 137 | 138 | int goCheck(int i) 139 | { 140 | 141 | for (int j = 0; j < n; j++) { 142 | if(a[i][j] != -1 and marked[j] == 0) { 143 | marked[j] = 1; 144 | tested++; 145 | goCheck(j); 146 | } 147 | } 148 | 149 | } 150 | 151 | 152 | int main() 153 | { 154 | int t; 155 | int m; 156 | string x; 157 | string y; 158 | 159 | int xx; 160 | int yy; 161 | int w; 162 | 163 | cin >> t; 164 | 165 | for (int cases = 1; cases <=t; cases++) { 166 | n = 0; 167 | count = 0; 168 | map mapping; 169 | memset(a, -1, sizeof(a)); 170 | memset(compo, -1, sizeof(compo)); 171 | cin >> m; 172 | 173 | for (int i = 0; i < m; i++) { 174 | cin >> x; 175 | cin >> y; 176 | cin >> w; 177 | 178 | if(mapping.count(x) == 0) { 179 | mapping[x] = n; 180 | xx = n; 181 | n++; 182 | } 183 | 184 | else { 185 | xx = mapping[x]; 186 | } 187 | 188 | if(mapping.count(y)==0) { 189 | mapping[y] = n; 190 | yy = n; 191 | n++; 192 | } 193 | 194 | else { 195 | yy = mapping[y]; 196 | } 197 | 198 | if(a[xx][yy] == -1) { 199 | a[xx][yy] = w; 200 | a[yy][xx] = w; 201 | } 202 | 203 | else { 204 | a[xx][yy] = min(w, a[xx][yy]); 205 | a[yy][xx] = min(w, a[yy][xx]); 206 | } 207 | } 208 | 209 | 210 | for (int i = 0; i < n; i++) { 211 | if(compo[i] == -1) { 212 | compo[i] = count; 213 | explore(i); 214 | count++; 215 | 216 | } 217 | 218 | } 219 | 220 | tested = 1; 221 | memset(marked, 0, sizeof(marked)); 222 | marked[0] = 1; 223 | goCheck(0); 224 | 225 | 226 | if(tested != n) { 227 | printf("Case %d: Impossible\n", cases); 228 | 229 | } 230 | 231 | else { 232 | 233 | printf("Case %d: %d\n", cases, prims()); 234 | } 235 | 236 | 237 | 238 | } 239 | 240 | 241 | } 242 | 243 | 244 | 245 | 246 | -------------------------------------------------------------------------------- /1042-Secret-Origins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int t; 7 | bool a[40]; 8 | bool b[40]; 9 | long long x; 10 | long long ans; 11 | int n; 12 | int ones; 13 | long long mul; 14 | int i; 15 | int j; 16 | scanf("%d", &t); 17 | 18 | for (int cases = 1; cases <= t; cases++) { 19 | scanf("%lld", &x); 20 | memset(a, 0, sizeof(a)); 21 | memset(b, 0, sizeof(b)); 22 | n = 0; 23 | ones = 0; 24 | ans = 0; 25 | mul = 1; 26 | 27 | while(x) { 28 | if(x % 2 == 0) { 29 | a[n] = 0; 30 | } 31 | 32 | else { 33 | a[n] = 1; 34 | } 35 | 36 | n++; 37 | x = x / 2; 38 | 39 | } 40 | 41 | i = 0; 42 | j = 1; 43 | 44 | while(1) { 45 | if(a[i] == 1 and a[j] == 0) { 46 | break; 47 | } 48 | if(a[i] == 1) 49 | ones++; 50 | i++; 51 | j++; 52 | 53 | } 54 | 55 | for (int k = 0; k < ones; k++) { 56 | b[k] = 1; 57 | } 58 | 59 | b[j] = 1; 60 | 61 | for (int k = j + 1; k <= n+1; k++) { 62 | b[k] = a[k]; 63 | } 64 | 65 | for (int i = 0; i <= n + 1; i++, mul = mul * 2) { 66 | ans += b[i] * mul; 67 | } 68 | /* 69 | for (int i = n + 2; i >= 0; i--) { 70 | cout << a[i] ; 71 | } 72 | 73 | cout << endl; 74 | 75 | for (int i = n + 2; i >= 0; i--) { 76 | cout << b[i] ; 77 | } 78 | 79 | cout << endl; 80 | */ 81 | printf("Case %d: %lld\n", cases, ans); 82 | } 83 | 84 | } 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /1044-Paliandrome-Partitioning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int palin[1005][1005]; 8 | 9 | 10 | int generate(string x) 11 | { 12 | int n; 13 | int e; 14 | n = x.length(); 15 | 16 | for (int i = 0; i < n; i++) { 17 | palin[i][i] = 1; 18 | } 19 | 20 | for (int length = 2; length <= n; length++) { 21 | for (int s = 0; s <= n - length; s++) { 22 | e = s + length - 1; 23 | 24 | 25 | if(length == 2) { 26 | palin[s][e] = (x[s] == x[e]); 27 | 28 | } 29 | 30 | else { 31 | palin[s][e] = ((x[s] == x[e]) and palin[s+1][e-1]); 32 | } 33 | } 34 | } 35 | } 36 | 37 | 38 | 39 | 40 | 41 | int main() 42 | { 43 | 44 | int t; 45 | int explore[1001][1001]; 46 | int n; 47 | 48 | scanf("%d", &t); 49 | for (int cases = 1; cases <= t; cases++) { 50 | 51 | int l; 52 | char inp[1005]; 53 | string x; 54 | 55 | scanf("%s", inp); 56 | 57 | memset(explore, 0, sizeof(explore)); 58 | memset(palin, 0, sizeof(palin)); 59 | 60 | x = inp; 61 | n = x.length(); 62 | 63 | 64 | generate(x); 65 | 66 | for (int i = n; i >= 0; i--) { 67 | for (int j = n; j >= i; j--) { 68 | 69 | if(j == n) { 70 | explore[i][j] = j - i; 71 | continue; 72 | } 73 | 74 | 75 | 76 | if(palin[i][j]) { 77 | explore[i][j] = min( 1 + explore[j+1] [j+1], explore[i][ j+1]); 78 | continue; 79 | } 80 | 81 | else { 82 | explore[i][j] = explore[i][j+1]; 83 | continue; 84 | } 85 | 86 | } 87 | 88 | } 89 | 90 | 91 | printf("Case %d: %d\n", cases, explore[0][0]); 92 | } 93 | 94 | } 95 | 96 | 97 | -------------------------------------------------------------------------------- /1045-Digits-Of-A-Factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | int x; 10 | int base; 11 | double ans; 12 | int number_of_digits; 13 | double wow[1000005]; 14 | 15 | 16 | 17 | cin >> t; 18 | 19 | for(int i = 1; i <= 1000002; i++) { 20 | wow[i] = wow[i-1] + log(i); 21 | } 22 | 23 | 24 | for (int cases = 1; cases <= t; cases++) { 25 | cin >> x; 26 | cin >> base; 27 | ans = 0; 28 | 29 | if(x == 0 or x == 1) { 30 | printf("Case %d: %d\n", cases, 1 ); 31 | continue; 32 | } 33 | 34 | if(x == 2) { 35 | 36 | x = 4; 37 | number_of_digits = 0; 38 | 39 | while(x) { 40 | x = x / base; 41 | number_of_digits++; 42 | 43 | } 44 | printf("Case %d: %d\n", cases, number_of_digits ); 45 | 46 | continue; 47 | } 48 | 49 | 50 | ans = wow[x]; 51 | 52 | number_of_digits = ceil(ans/log(base)); 53 | printf("Case %d: %d\n", cases, number_of_digits ); 54 | 55 | 56 | 57 | 58 | 59 | } 60 | } -------------------------------------------------------------------------------- /1046-Rider.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int m; 8 | int n; 9 | int p; 10 | int r; 11 | int f; 12 | int d; 13 | char a[15][15]; 14 | bool vis[15][15]; 15 | int dis[110][110]; 16 | 17 | 18 | int explore(int i, int j,int sofar) 19 | { 20 | int temp; 21 | 22 | temp = INT_MAX; 23 | 24 | if(i < 0 or i >= m or j < 0 or j >= n) { 25 | return INT_MAX; 26 | } 27 | 28 | if(dis[p * n + r][i * n + j] > sofar) { 29 | dis[p * n + r][i * n + j] = sofar; 30 | 31 | } 32 | 33 | if(vis[i+1][j+2] == 0) { 34 | vis[i+1][j+2] = 1; 35 | temp = min(explore(i+1, j+2,sofar+1), temp); 36 | vis[i+1][j+2] = 0; 37 | } 38 | 39 | if(vis[i+1][j-2] == 0) { 40 | vis[i+1][j-2] = 1; 41 | temp = min(explore(i+1, j-2,sofar+1), temp); 42 | vis[i+1][j-2] =0; 43 | } 44 | 45 | if(vis[i-1][j+2] == 0) { 46 | vis[i-1][j+2] = 1; 47 | temp = min(explore(i-1, j+2,sofar+1), temp); 48 | vis[i-1][j+2] = 0; 49 | } 50 | 51 | if(vis[i-1][j-2] == 0) { 52 | vis[i-1][j-2] = 1; 53 | temp = min(explore(i-1, j-2,sofar+1), temp); 54 | vis[i-1][j-2] = 0; 55 | } 56 | 57 | if(vis[i+2][j+1] == 0) { 58 | vis[i+2][j+1] = 1; 59 | temp = min(explore(i+2, j+1,sofar+1), temp); 60 | vis[i+2][j+1] = 0; 61 | } 62 | 63 | if(vis[i+2][j-1] == 0) { 64 | vis[i+2][j-1] = 1; 65 | temp = min(explore(i+2, j-1,sofar+1), temp); 66 | vis[i+2][j-1] = 0; 67 | } 68 | 69 | if(vis[i-2][j+1] == 0) { 70 | vis[i-2][j+1] = 1; 71 | temp = min(explore(i-2, j+1,sofar+1), temp); 72 | vis[i-2][j+1] = 0; 73 | } 74 | 75 | if(vis[i-2][j-1] == 0) { 76 | vis[i-2][j-1] = 1; 77 | temp = min(explore(i-2, j-1,sofar+1), temp); 78 | vis[i-2][j-1] = 0; 79 | } 80 | 81 | 82 | } 83 | 84 | 85 | 86 | 87 | int main() 88 | { 89 | 90 | int t; 91 | int k; 92 | int temp; 93 | int ans; 94 | 95 | cin >> t; 96 | for (int cs = 1; cs <= t; cs++) { 97 | cin >> m; 98 | cin >> n; 99 | 100 | for (int i = 0; i < m; i++) { 101 | for (int j = 0; j < n; j++) { 102 | cin >> a[i][j]; 103 | } 104 | } 105 | 106 | memset(dis, 0, sizeof(dis)); 107 | 108 | for (int i = 0; i < m * n; i++) { 109 | for (int j = 0; j < n * m; j++) { 110 | dis[i][j] = INT_MAX; 111 | } 112 | } 113 | 114 | for (int i = 0; i < m; i++) { 115 | for (int j = 0; j < n; j++) { 116 | memset(vis, 0, sizeof(vis)); 117 | p = i; 118 | r = j; 119 | if(a[i][j] != '.') { 120 | vis[i][j] = true; 121 | explore(i, j, 0); 122 | vis[i][j] = false; 123 | } 124 | } 125 | } 126 | 127 | ans = INT_MAX; 128 | 129 | int p; 130 | int pp; 131 | 132 | for (int i = 0; i < m * n; i++) { 133 | temp = 0; 134 | for (int j = 0; j < m * n; j++) { 135 | p = j / n; 136 | pp = j % n; 137 | 138 | 139 | if(a[p][pp] != '.') { 140 | if(dis[j][i] != INT_MAX) { 141 | temp += dis[j][i]; 142 | } 143 | 144 | else { 145 | temp = INT_MAX; 146 | break; 147 | } 148 | 149 | } 150 | 151 | } 152 | 153 | ans = min(ans, temp); 154 | 155 | } 156 | 157 | cout << ans << endl; 158 | 159 | } 160 | 161 | 162 | 163 | 164 | } 165 | 166 | } 167 | -------------------------------------------------------------------------------- /1047-Neighbour-House: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main() 8 | { 9 | 10 | int n; 11 | int a[23][5]; 12 | int ans[23][5]; 13 | int x; 14 | int t; 15 | 16 | scanf("%d", &t); 17 | 18 | for (int cs = 1; cs <= t; cs++) { 19 | scanf("%d", &n); 20 | 21 | for (int i = 0; i < n; i++) { 22 | for (int j = 0; j < 3; j++) { 23 | scanf("%d", &a[i][j]); 24 | } 25 | } 26 | 27 | 28 | 29 | for(int i = n; i >= 0; i--) { 30 | for (int prev = 2; prev >= 0; prev--) { 31 | 32 | x = INT_MAX; 33 | 34 | if( i == n) { 35 | ans[i][prev] = 0; 36 | continue; 37 | } 38 | 39 | for (int j = 0; j < 3; j++) { 40 | if(j != prev) { 41 | x = min(x, a[i][j] + ans[i+1][j]); 42 | } 43 | 44 | } 45 | 46 | ans[i][prev] = x; 47 | 48 | 49 | 50 | } 51 | 52 | 53 | } 54 | 55 | printf("Case %d: %d\n",cs, min(ans[0][0], min(ans[0][1], ans[0][2]))); 56 | 57 | 58 | } 59 | 60 | 61 | } 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /1049-One-Way-Roads.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | 11 | int t; 12 | int x; 13 | int w; 14 | int y; 15 | int total; 16 | int sofar; 17 | bool visited[110]; 18 | int count; 19 | int n; 20 | 21 | 22 | scanf("%d", &t); 23 | for(int cs = 1; cs <= t; cs++) { 24 | cin >> n; 25 | 26 | vector < vector < pair > > a(n+3); 27 | memset(visited, 0, sizeof(visited)); 28 | total = 0; 29 | sofar = 0; 30 | 31 | for (int i = 0; i < n; i++) { 32 | scanf("%d", &x); 33 | scanf("%d", &y); 34 | scanf("%d", &w); 35 | 36 | a[x].push_back(make_pair(y, w)); 37 | a[y].push_back(make_pair(x, -w)); 38 | total += w; 39 | } 40 | 41 | int i; 42 | 43 | i = 1; 44 | count = 0; 45 | visited[1] = 1; 46 | while (count < n -1) { 47 | 48 | x = a[i][0].first; 49 | 50 | if(visited[x] == 0) { 51 | w = a[i][0].second; 52 | visited[x] = 1; 53 | 54 | if(w < 0) { 55 | w = w * -1; 56 | sofar += w; 57 | } 58 | 59 | i = x; 60 | 61 | } 62 | 63 | else { 64 | x = a[i][1].first; 65 | visited[x] = 1; 66 | w = a[i][1].second; 67 | 68 | if(w < 0) { 69 | w = w * -1; 70 | sofar += w; 71 | } 72 | 73 | i = x; 74 | } 75 | 76 | 77 | count++; 78 | 79 | } 80 | 81 | if(x = a[i][0].first == 1) { 82 | w = a[i][0].second; 83 | if(w < 0) { 84 | w = w * -1; 85 | sofar += w; 86 | } 87 | 88 | } 89 | 90 | else { 91 | w = a[i][1].second; 92 | if(w < 0) { 93 | w = w * -1; 94 | sofar += w; 95 | } 96 | 97 | } 98 | 99 | printf("Case %d: %d\n", cs, min(sofar, total - sofar)); 100 | 101 | } 102 | 103 | 104 | } 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /1050-Marbles.cpp: -------------------------------------------------------------------------------- 1 | // amiD 2 | #include 3 | #define ld long double 4 | #define ll long long int 5 | #define mod 1000000007 6 | #define ll_inf 1000000000000000 7 | #define int_inf 1000000000 8 | #define pb push_back 9 | #define endl '\n' 10 | #define Endl '\n' 11 | #define eps 1e-9 12 | #define PI acos(-1.0) 13 | #define ii pair 14 | #define se second 15 | #define fi first 16 | using namespace std; 17 | bool vis[505][505]; 18 | double dp[502][505]; 19 | double cal(int red, int blue){ 20 | if(red==0){ 21 | if(blue%2==0)return dp[red][blue]=0.0; 22 | return dp[red][blue]= 1.0; 23 | } 24 | if(blue==0)return dp[red][blue]=0.0; 25 | if(blue<=0)return 0.0; 26 | double &ret=dp[red][blue]; 27 | if(vis[red][blue])return dp[red][blue]; 28 | vis[red][blue]=1; 29 | ret=1.0; 30 | ret=((1.0*red)/(red+blue)*cal(red-1, blue-1)); 31 | if(blue>=2)ret+=((1.0*blue)/(red+blue)*cal(red, blue-2)); 32 | return ret; 33 | } 34 | int main(){ 35 | cout.precision(12); 36 | int t, ca=1; 37 | cin>>t; 38 | while(t--){ 39 | int red,blue; 40 | scanf("%d%d",&red, &blue); 41 | printf("Case %d: %.8f\n", ca++, cal(red, blue)); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /1051-Good-Or-Bad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int ab; 8 | int ok; 9 | bool ans[55][1000]; 10 | vector x; 11 | string a; 12 | 13 | int check(int n) 14 | { 15 | 16 | int acount = 0; 17 | int bcount = 0; 18 | int qcount = 0; 19 | int i; 20 | 21 | for ( i = 0; i < a.length(); i++) { 22 | 23 | 24 | if(a[i] == 'A' or a[i] == 'E' or a[i] == 'I' or a[i] == 'O' or a[i] == 'U') { 25 | acount++; 26 | bcount = 0; 27 | a[i] = 'A'; 28 | } 29 | else { 30 | acount = 0; 31 | if(a[i] != '?') { 32 | bcount++; 33 | a[i] = 'B'; 34 | } 35 | 36 | else { 37 | bcount = 0; 38 | qcount++; 39 | } 40 | } 41 | 42 | if(acount == 3 or bcount == 5) { 43 | 44 | break; 45 | } 46 | } 47 | 48 | if(acount == 3 or bcount == 5) { 49 | return 0; 50 | } 51 | 52 | else { 53 | return 1; 54 | } 55 | 56 | } 57 | 58 | int explore(int i) 59 | { 60 | 61 | int t; 62 | int temp; 63 | int mul = 1; 64 | 65 | 66 | temp = 0; 67 | 68 | if(i >= x.size()) { 69 | ok++; 70 | return 0; 71 | } 72 | 73 | t = x[i]; 74 | 75 | a[t] = 'A'; 76 | 77 | for (int j = 0; (t - j) >= 0 and j <= 5; j++) { 78 | temp = temp + ((a[t-j] - 'A') * mul); 79 | mul *= 2; 80 | } 81 | if(check(t)) { 82 | 83 | if(ans[i][temp] == 0) { 84 | explore(i+1); 85 | ans[i][temp] = 1; 86 | } 87 | 88 | } 89 | 90 | else { 91 | ab++; 92 | } 93 | 94 | mul = 1; 95 | 96 | a[t] = 'B'; 97 | 98 | temp = 0; 99 | 100 | for (int j = 0; (t - j) >= 0 and j <= 5; j++) { 101 | temp += (a[t-j] - 'A') * mul; 102 | mul *= 2; 103 | } 104 | 105 | if(check(t)) { 106 | 107 | if(ans[i][temp] == 0) { 108 | explore(i+1); 109 | ans[i][temp] = 1; 110 | } 111 | 112 | } 113 | 114 | else { 115 | ab++; 116 | } 117 | 118 | a[t] = '?'; 119 | 120 | } 121 | 122 | 123 | 124 | 125 | 126 | 127 | int main() 128 | { 129 | 130 | int t; 131 | int acount; 132 | int bcount; 133 | int qcount; 134 | int i; 135 | 136 | cin >> t; 137 | 138 | for (int cs = 1; cs <= t; cs++) { 139 | cin >> a; 140 | 141 | vector temp; 142 | swap(x, temp); 143 | acount = 0; 144 | bcount = 0; 145 | qcount = 0; 146 | 147 | for ( i = 0; i < a.length(); i++) { 148 | 149 | 150 | if(a[i] == 'A' or a[i] == 'E' or a[i] == 'I' or a[i] == 'O' or a[i] == 'U') { 151 | acount++; 152 | bcount = 0; 153 | a[i] = 'A'; 154 | } 155 | 156 | else { 157 | acount = 0; 158 | if(a[i] != '?') { 159 | bcount++; 160 | a[i] = 'B'; 161 | } 162 | 163 | else { 164 | bcount = 0; 165 | qcount++; 166 | } 167 | } 168 | 169 | if(acount == 3 or bcount == 5) { 170 | break; 171 | } 172 | 173 | } 174 | 175 | if(qcount == 0) { 176 | if(acount == 3 or bcount == 5) { 177 | printf("Case %d: BAD\n", cs); 178 | } 179 | 180 | else { 181 | printf("Case %d: GOOD\n", cs); 182 | } 183 | 184 | } 185 | 186 | else { 187 | for (int i = 0; i < a.length(); i++) { 188 | if(a[i] == '?') { 189 | x.push_back(i); 190 | } 191 | 192 | } 193 | 194 | ab = 0; 195 | ok = 0; 196 | memset(ans, 0, sizeof(ans)); 197 | explore(0); 198 | 199 | if(ab and ok) { 200 | printf("Case %d: MIXED\n", cs); 201 | } 202 | 203 | if(ab and ok == 0) { 204 | printf("Case %d: BAD\n", cs); 205 | } 206 | 207 | if(ab == 0 and ok) { 208 | printf("Case %d: GOOD\n", cs); 209 | } 210 | 211 | 212 | 213 | 214 | } 215 | 216 | } 217 | 218 | 219 | } 220 | -------------------------------------------------------------------------------- /1053-Higher-Math.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | 11 | int t; 12 | 13 | 14 | cin >> t; 15 | 16 | for (int cs = 1; cs <= t; cs++) { 17 | vector a(4); 18 | cin >> a[0]; 19 | cin >> a[1]; 20 | cin >> a[2]; 21 | 22 | sort(a.begin(), a.begin() + 3); 23 | 24 | if((a[0] * a[0] + a[1] * a[1]) == (a[2] * a[2])) { 25 | printf("Case %d: yes\n", cs); 26 | } 27 | 28 | else { 29 | printf("Case %d: no\n", cs); 30 | } 31 | 32 | } 33 | 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /1054-Efficient-Pseudo-Code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define sz 1000000 5 | using namespace std; 6 | 7 | vector pr; 8 | 9 | int sv[1000005]; 10 | 11 | long long MOD = 1000000007; 12 | long long mod = 1000000007; 13 | void extEuclid(int a, int b, int &x, int &y, int &gcd) { 14 | x = 0; y = 1; gcd = b; 15 | int m, n, q, r; 16 | for (int u=1, v=0; a != 0; gcd=a, a=r) { 17 | q = gcd / a; r = gcd % a; 18 | m = x-u*q; n = y-v*q; 19 | x=u; y=v; u=m; v=n; 20 | } 21 | } 22 | 23 | long long modInv(long long n, long long m) { 24 | int x, y, gcd; 25 | extEuclid(n, m, x, y, gcd); 26 | if (gcd == 1) return x % m; 27 | return 0; 28 | } 29 | 30 | long long mod_pow(long long x, long long y) 31 | { 32 | 33 | long long ans; 34 | 35 | ans = 1; 36 | while(y) { 37 | if(y & 1) { 38 | ans = (ans * x) % MOD; 39 | 40 | } 41 | 42 | x = (x * x) % MOD; 43 | y = y >> 1; 44 | } 45 | 46 | return ans; 47 | 48 | 49 | } 50 | int main() 51 | { 52 | pr.push_back(2); 53 | 54 | for (int i = 3; i * i <= sz; i += 2) { 55 | if(!sv[i]) { 56 | for (int j = i * i; j <= sz; j += 2 * i) { 57 | sv[j] = 1; 58 | } 59 | } 60 | } 61 | 62 | for (int i = 3; i <= sz; i += 2) { 63 | if(!sv[i]) { 64 | pr.push_back(i); 65 | } 66 | } 67 | int t; 68 | int n; 69 | int m; 70 | int k; 71 | long long ans; 72 | 73 | scanf("%d", &t); 74 | 75 | for (int cs = 1; cs <= t; cs++) { 76 | scanf("%d %d", &n, &m); 77 | vector < pair < int, int> > fact; 78 | 79 | for (int i = 0; i < pr.size() and pr[i] * pr[i] <= n ; i++) { 80 | k = 0; 81 | while(n % pr[i] == 0) { 82 | k++; 83 | n = n / pr[i]; 84 | } 85 | 86 | if(k != 0) { 87 | fact.push_back(make_pair(pr[i], k)); 88 | } 89 | } 90 | 91 | 92 | if(n != 1) { 93 | fact.push_back(make_pair(n, 1)); 94 | } 95 | 96 | ans = 1; 97 | long long p; 98 | long long e; 99 | 100 | 101 | 102 | for (int i = 0; i < fact.size(); i++) { 103 | p = fact[i].first; 104 | e = fact[i].second; 105 | ans = (ans * (mod_pow(p, e * m + 1LL) - 1LL + mod)) % mod; 106 | ans = (ans * mod_pow(p - 1LL, mod - 2)) % mod; 107 | 108 | } 109 | 110 | printf("Case %d: %lld\n", cs , ans); 111 | 112 | } 113 | 114 | } 115 | 116 | -------------------------------------------------------------------------------- /1057-Collecting-Gold.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | 11 | int n; 12 | int k; 13 | int vis[20]; 14 | int a[20][20]; 15 | 16 | int ans[20][(1 << 16) + 1]; 17 | int check(int x, int i) 18 | { 19 | return (x & (1 << i)); 20 | 21 | } 22 | 23 | int set(int x, int i) 24 | { 25 | x = x ^ (1 << i); 26 | 27 | return x; 28 | } 29 | 30 | int explore(int i, int x) 31 | { 32 | int k; 33 | int temp; 34 | 35 | k = 1 << (n + 1); 36 | k--; 37 | 38 | 39 | if(x == k) { 40 | return a[i][0]; 41 | } 42 | 43 | if(ans[i][x] != -1) { 44 | return ans[i][x]; 45 | } 46 | 47 | int mini = INT_MAX; 48 | 49 | for (int j = 1; j <= n; j++) { 50 | if(vis[j] == 0) { 51 | x = set(x, j); 52 | vis[j] = 1; 53 | temp = a[i][j] + explore(j, x); 54 | mini = min(mini, temp); 55 | x = set(x, j); 56 | vis[j] = 0; 57 | } 58 | 59 | 60 | } 61 | 62 | ans[i][x] = mini; 63 | return mini; 64 | 65 | } 66 | 67 | 68 | int main() 69 | { 70 | 71 | int r; 72 | int c; 73 | int t; 74 | char cc; 75 | 76 | 77 | scanf("%d", &t); 78 | 79 | for (int cs = 1; cs <= t; cs++) { 80 | scanf("%d", &r); 81 | scanf("%d", &c); 82 | vector > gr(20); 83 | n = 0; 84 | 85 | for (int i = 0; i < r; i++) { 86 | for (int j = 0; j < c; j++) { 87 | cin >> cc; 88 | 89 | if(cc == 'x') { 90 | gr[0] = make_pair(i, j); 91 | continue; 92 | } 93 | 94 | if(cc == 'g') { 95 | n++; 96 | gr[n] = make_pair(i, j); 97 | } 98 | 99 | } 100 | 101 | 102 | } 103 | for (int i = 0; i <= n; i++) { 104 | for (int j = i + 1; j <= n; j++) { 105 | a[i][j] = a[j][i] = max(abs(gr[i].first - gr[j].first) , abs(gr[i].second - gr[j].second)); 106 | } 107 | 108 | } 109 | 110 | memset(ans, -1, sizeof(ans)); 111 | printf("Case %d: %d\n",cs, explore(0, 1)); 112 | 113 | } 114 | 115 | 116 | } 117 | 118 | -------------------------------------------------------------------------------- /1058-Parallelogram-Counting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define pii pair 8 | using namespace std; 9 | 10 | vector < pii > v; 11 | vector < pii > m; 12 | 13 | int main() 14 | { 15 | 16 | int t; 17 | int x; 18 | int y; 19 | int n; 20 | 21 | scanf("%d", &t); 22 | 23 | for (int cs = 1; cs <= t; cs++) { 24 | 25 | scanf("%d", &n); 26 | 27 | m.clear(); 28 | v.clear(); 29 | 30 | 31 | pii temp; 32 | pii p1; 33 | pii p2; 34 | int ans; 35 | int top; 36 | 37 | ans = 0; 38 | 39 | 40 | for (int i = 0; i < n; i++) { 41 | scanf("%d", &temp.first); 42 | scanf("%d", &temp.second); 43 | v.push_back(temp); 44 | } 45 | 46 | for (int i = 0; i < v.size(); i++) { 47 | for (int j = i + 1; j < v.size(); j++) { 48 | p1 = v[i]; 49 | p2 = v[j]; 50 | 51 | x = v[i].first + v[j].first; 52 | y = v[i].second + v[j].second; 53 | 54 | temp.first = x; 55 | temp.second = y; 56 | 57 | m.push_back(temp); 58 | 59 | 60 | } 61 | 62 | } 63 | 64 | sort(m.begin(), m.end()); 65 | top = 0; 66 | 67 | 68 | for (int i = 1; i < m.size(); i++) { 69 | if((m[i].first == m[i-1].first) and (m[i].second == m[i-1].second)) { 70 | top++; 71 | } 72 | 73 | else { 74 | ans = ans + (top * (top + 1)) / 2; 75 | top = 0; 76 | } 77 | } 78 | 79 | 80 | printf("Case %d: %d\n",cs, ans); 81 | } 82 | 83 | } 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /1063-Ant-Hills.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define SIZE 10008 6 | using namespace std; 7 | 8 | vector < vector > g; 9 | int vis[SIZE]; 10 | int dur[SIZE]; 11 | int low[SIZE]; 12 | int art[SIZE]; 13 | int counter; 14 | int parent[SIZE]; 15 | 16 | int find_articulation(int x) 17 | { 18 | int child; 19 | int y; 20 | 21 | counter++; 22 | vis[x] = 1; 23 | child = 0; 24 | 25 | dur[x] = low[x] = counter; 26 | 27 | for (int i = 0; i < g[x].size(); i++) { 28 | y = g[x][i]; 29 | child++; 30 | 31 | if(vis[y] == 0) { 32 | parent[y] = x; 33 | find_articulation(y); 34 | low[x] = min(low[x], low[y]); 35 | 36 | if(child > 1 and parent[x] == -1) { 37 | art[x] = 1; 38 | } 39 | 40 | if(parent[x] != -1 and low[y] >= dur[x]) { 41 | art[x] = 1; 42 | } 43 | } 44 | 45 | else { 46 | if(y != parent[x]) { 47 | low[x] = min(low[x], dur[y]); 48 | } 49 | } 50 | 51 | } 52 | 53 | 54 | } 55 | 56 | int main() 57 | { 58 | 59 | int t; 60 | int n; 61 | int m; 62 | int x; 63 | int y; 64 | int count; 65 | scanf("%d", &t); 66 | 67 | for (int cs = 1; cs <= t; cs++) { 68 | scanf("%d", &n); 69 | scanf("%d", &m); 70 | 71 | vector < vector > tg(n + 2); 72 | swap(tg, g); 73 | memset(parent, -1, sizeof parent); 74 | memset(vis, 0, sizeof vis); 75 | memset(art, 0, sizeof art); 76 | counter = 0; 77 | count = 0; 78 | 79 | for (int i = 0; i < m; i++) { 80 | scanf("%d", &x); 81 | scanf("%d", &y); 82 | 83 | g[x].push_back(y); 84 | g[y].push_back(x); 85 | 86 | } 87 | 88 | for (int i = 1; i <= n; i++) { 89 | if(!vis[i]) { 90 | find_articulation(i); 91 | } 92 | } 93 | 94 | for (int i = 1; i <= n; i++) { 95 | if(art[i]) 96 | count++; 97 | } 98 | 99 | printf("Case %d: %d\n",cs, count ); 100 | 101 | } 102 | 103 | } 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /1065-Number-Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int mod; 7 | 8 | int multi(long long a[2][2], long long b[2][2], long long result[2][2]) 9 | { 10 | 11 | for (int i = 0; i < 2; i++) { 12 | for (int j = 0; j < 2; j++) { 13 | result[i][j] = 0; 14 | for (int k = 0; k < 2; k++) { 15 | result[i][j] = (result[i][j] % mod + (a[i][k] * b[k][j]) % mod ) % mod; 16 | } 17 | } 18 | } 19 | 20 | } 21 | long long exponentation(long long z[2][2], long long result[2][2], long long n) 22 | { 23 | long long temp[2][2]; 24 | 25 | result[0][0] = 1; 26 | result[0][1] = 0; 27 | result[1][0] = 0; 28 | result[1][1] = 1; 29 | 30 | while(n) { 31 | 32 | if(n % 2) { 33 | 34 | multi(result, z, temp); 35 | 36 | for (int i = 0; i < 2; i++) { 37 | for (int j = 0; j < 2; j++) { 38 | result[i][j] = temp[i][j]; 39 | } 40 | } 41 | } 42 | 43 | multi(z, z, temp); 44 | 45 | for (int i = 0; i < 2; i++) { 46 | for (int j = 0; j < 2; j++) { 47 | z[i][j] = temp[i][j]; 48 | } 49 | } 50 | 51 | n = n / 2; 52 | } 53 | } 54 | 55 | int main() 56 | { 57 | 58 | long long z[2][2]; 59 | long long inp[2][2]; 60 | long long result[2][2]; 61 | long long k; 62 | int t; 63 | int a; 64 | int b; 65 | int l; 66 | 67 | scanf("%d", &t); 68 | 69 | 70 | for (int cs = 1; cs <= t; cs++) { 71 | 72 | z[0][0] = 1; 73 | z[0][1] = 1; 74 | z[1][0] = 1; 75 | z[1][1] = 0; 76 | 77 | scanf("%d", &a); 78 | scanf("%d", &b); 79 | scanf("%lld", &k); 80 | scanf("%d", &l); 81 | mod = pow(10, l); 82 | 83 | 84 | 85 | k++; 86 | if(k == 1) { 87 | a = a % mod; 88 | 89 | printf("Case %d: %d\n", cs, a); 90 | continue; 91 | } 92 | 93 | if(k == 2) { 94 | b = b % mod; 95 | 96 | printf("Case %d: %d\n", cs, b); 97 | continue; 98 | } 99 | 100 | 101 | a = a % mod; 102 | b = b % mod; 103 | 104 | exponentation(z, result, k - 2); 105 | printf("Case %d: %lld\n", cs, ((result[0][0] * b) % mod + (result[0][1] * a) % mod) % mod); 106 | } 107 | 108 | 109 | } 110 | 111 | -------------------------------------------------------------------------------- /1066-Gathering-Food.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct node{ 9 | int i; 10 | int j; 11 | int count; 12 | }; 13 | 14 | int n; 15 | int k; 16 | char a[20][20]; 17 | int temp; 18 | 19 | node find_distance(char target, node start) 20 | { 21 | 22 | queue q; 23 | node temp; 24 | 25 | int b[4] = {+1, -1, 0, 0}; 26 | int c[4] = {0, 0, +1, -1}; 27 | int i; 28 | int j; 29 | int ii; 30 | int jj; 31 | int vis[20][20]; 32 | 33 | memset(vis, 0, sizeof(vis)); 34 | q.push(start); 35 | 36 | vis[start.i][start.j] = 1; 37 | 38 | while(!q.empty()) { 39 | temp = q.front(); 40 | q.pop(); 41 | 42 | i = temp.i; 43 | j = temp.j; 44 | 45 | if(a[i][j] == target) { 46 | return temp; 47 | } 48 | 49 | for (int cs = 0; cs < 4; cs++) { 50 | if((i + b[cs] < n and i + b[cs] >= 0) and (j + c[cs] < n and j + c[cs] >= 0)) { 51 | 52 | 53 | node epic; 54 | ii = i + b[cs]; 55 | jj = j + c[cs]; 56 | 57 | if((a[ii][jj] == '.' or a[ii][jj] == target) and vis[ii][jj] == 0) { 58 | epic.i = i + b[cs]; 59 | epic.j = j + c[cs]; 60 | epic.count = temp.count + 1; 61 | vis[ii][jj] = 1; 62 | q.push(epic); 63 | 64 | } 65 | } 66 | 67 | } 68 | 69 | 70 | } 71 | 72 | temp.count = INT_MAX; 73 | return temp; 74 | 75 | 76 | 77 | 78 | 79 | } 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | int main() 89 | { 90 | 91 | int t; 92 | int sum; 93 | int starti; 94 | int startj; 95 | char target; 96 | 97 | scanf("%d", &t); 98 | 99 | for (int cs = 1; cs <= t; cs++) { 100 | scanf("%d", &n); 101 | k = -999; 102 | sum = 0; 103 | 104 | 105 | 106 | for (int i = 0; i < n; i++) { 107 | for (int j = 0; j < n; j++) { 108 | cin >> a[i][j]; 109 | temp = a[i][j] - 'A' + 1; 110 | k = max(k, temp); 111 | if(a[i][j] == 'A') { 112 | starti = i; 113 | startj = j; 114 | } 115 | } 116 | 117 | } 118 | 119 | node temp; 120 | temp.i = starti; 121 | temp.j = startj; 122 | temp.count = 0; 123 | 124 | int i; 125 | 126 | for (i = 0; i < k; i++) { 127 | target = 'A' + i; 128 | temp.count = 0; 129 | temp = find_distance(target, temp); 130 | 131 | if(temp.count == INT_MAX) { 132 | break; 133 | } 134 | 135 | a[temp.i][temp.j] = '.'; 136 | 137 | sum += temp.count; 138 | 139 | } 140 | 141 | if(i == k) { 142 | printf("Case %d: %d\n", cs, sum); 143 | } 144 | 145 | else { 146 | printf("Case %d: Impossible\n", cs); 147 | 148 | } 149 | 150 | 151 | } 152 | 153 | 154 | } 155 | -------------------------------------------------------------------------------- /1076-Get-the-Container.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define N 10005 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | int t; 11 | int a[N]; 12 | long long bb; 13 | long long sum[N]; 14 | int m; 15 | long long maxi; 16 | int n; 17 | 18 | scanf("%d", &t); 19 | 20 | for (int cs = 1; cs <= t; cs++) { 21 | scanf("%d", &n); 22 | scanf("%d", &m); 23 | 24 | sum[n ] = 0; 25 | maxi = -1; 26 | bb = 0; 27 | int j; 28 | j = 0; 29 | 30 | for (int i = 0; i < n; i++) { 31 | scanf("%d", a + i); 32 | } 33 | 34 | for (int i = n - 1; i >= 0; i--) { 35 | sum[i] = sum[i + 1] + a[i ]; 36 | } 37 | 38 | for (int i = 0; i < n; i++) { 39 | // cout << sum[i] << " "; 40 | } 41 | 42 | // cout << endl; 43 | 44 | for (int i = 0; i < n; ) { 45 | if(bb + a[i] <= maxi) { 46 | bb = bb + a[i]; 47 | i++; 48 | } 49 | else { 50 | if((double)(bb + a[i]) <= (double) sum[i] / (m - (j + 1))) { 51 | bb = bb + a[i]; 52 | maxi = max(maxi, bb); 53 | i++; 54 | } 55 | else { 56 | bb = 0; 57 | } 58 | 59 | } 60 | } 61 | 62 | printf("Case %d: %lld\n", cs, maxi); 63 | 64 | } 65 | 66 | 67 | } 68 | 69 | -------------------------------------------------------------------------------- /1077-How-Many-Points.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | long long x1; 11 | long long y1; 12 | long long x2; 13 | long long y2; 14 | long long t; 15 | long long dx; 16 | long long dy; 17 | long long up; 18 | long long down; 19 | long long gcd; 20 | long long ans; 21 | 22 | 23 | scanf("%lld", &t); 24 | 25 | for (int cs = 1; cs <= t; cs++) { 26 | scanf("%lld", &x1); 27 | scanf("%lld", &y1); 28 | scanf("%lld", &x2); 29 | scanf("%lld", &y2); 30 | 31 | dx = abs(x2 - x1); 32 | dy = abs(y2 - y1); 33 | 34 | if(dx == 0 and dy == 0) { 35 | printf("Case %d: 1\n",cs ); 36 | continue; 37 | } 38 | 39 | if(dx == 0) { 40 | printf("Case %d: %lld\n",cs, dy+1 ); 41 | continue; 42 | } 43 | 44 | if(dy == 0) { 45 | printf("Case %d: %lld\n",cs, dx+1 ); 46 | continue; 47 | } 48 | 49 | gcd = __gcd(dx, dy); 50 | up = dx / gcd; 51 | down = dy / gcd; 52 | ans = 1 + (dy / down); 53 | 54 | printf("Case %d: %lld\n", cs, ans); 55 | 56 | } 57 | 58 | 59 | } 60 | 61 | -------------------------------------------------------------------------------- /1079-Just-Another-Robbery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | double p[110]; 8 | int million[110]; 9 | int n; 10 | long long count; 11 | double tol; 12 | 13 | int main() 14 | { 15 | 16 | int t; 17 | int sum; 18 | int ans; 19 | double val[10005]; 20 | 21 | scanf("%d", &t); 22 | 23 | for (int cs = 1; cs <= t; cs++) { 24 | scanf("%lf", &tol); 25 | scanf("%d", &n); 26 | sum = 0; 27 | 28 | for (int i = 0; i < n; i++) { 29 | scanf("%d", &million[i]); 30 | scanf("%lf", &p[i]); 31 | sum += million[i]; 32 | } 33 | 34 | for (int i = 1; i <= sum; i++) { 35 | val[i] = 1; 36 | } 37 | 38 | val[0] = 0; 39 | int index; 40 | 41 | for (int j = 0; j < n; j++) { 42 | for(int i = sum; i >= 1; i--) { 43 | 44 | index = i - million[j]; 45 | if(index >= 0) { 46 | val[i] = min(val[i], val[index] + (1 - val[index]) * p[j]); 47 | } 48 | 49 | } 50 | 51 | } 52 | 53 | for (int i = 0; i <= sum; i++) { 54 | if(val[i] < tol) { 55 | ans = i; 56 | } 57 | 58 | } 59 | 60 | printf("Case %d: %d\n", cs, ans); 61 | 62 | } 63 | 64 | } 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /1080-Binary-Simulation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int create_tree(int input[],int tree[], int n) 8 | { 9 | int x; 10 | 11 | for (int i = 1; i <= n; i++) { 12 | x = i; 13 | 14 | while(x <= n) { 15 | tree[x] += input[i]; 16 | x += x & -x; 17 | } 18 | } 19 | } 20 | 21 | 22 | int update(int tree[], int x, int val, int n) 23 | { 24 | if(x == 0) { 25 | return 0; 26 | } 27 | 28 | while(x <= n) { 29 | tree[x] += val; 30 | x += x & -x; 31 | } 32 | } 33 | 34 | int query(int tree[], int x) 35 | { 36 | int sum; 37 | sum = 0; 38 | 39 | while(x > 0) { 40 | sum += tree[x]; 41 | x -= x & -x; 42 | } 43 | 44 | return sum; 45 | } 46 | 47 | int alter(int tree[], int i, int j, int n) 48 | { 49 | update(tree, i, 1, n); 50 | update(tree, j+1, -1, n); 51 | 52 | } 53 | 54 | int main() 55 | { 56 | int N = 100009; 57 | int a[N]; 58 | int tree[N]; 59 | int n; 60 | int x; 61 | int t; 62 | int y; 63 | char temp; 64 | int q; 65 | char choice; 66 | char epic[100007]; 67 | int ans; 68 | 69 | ios_base::sync_with_stdio(false); 70 | 71 | 72 | 73 | cin >> t; 74 | 75 | for (int cs = 1; cs <= t; cs++) { 76 | 77 | cin >> epic; 78 | n = strlen(epic); 79 | for (int i = 0; i < n; i++) { 80 | a[i+1] = epic[i] - '0'; 81 | } 82 | 83 | // create_tree(a, tree, n); 84 | memset(tree, 0, sizeof(tree)); 85 | 86 | cin >> q; 87 | 88 | printf("Case %d:\n", cs); 89 | 90 | for (int i = 0; i < q; i++) { 91 | 92 | cin >> choice; 93 | switch(choice) { 94 | case 'I': 95 | cin >> x; 96 | cin >> y; 97 | alter(tree, x, y, n); 98 | break; 99 | 100 | case 'Q': 101 | cin >> x; 102 | ans = (a[x] ^ (query(tree, x) % 2)); 103 | printf("%d\n", ans); 104 | break; 105 | } 106 | 107 | } 108 | 109 | } 110 | 111 | 112 | 113 | 114 | } 115 | -------------------------------------------------------------------------------- /1082-Array-Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int a[100005]; 7 | int tree[400005]; 8 | 9 | int build_segment_tree(int l, int r, int node) 10 | { 11 | 12 | int x; 13 | int y; 14 | 15 | if(l == r) { 16 | tree[node] = a[l]; 17 | return a[l]; 18 | } 19 | 20 | x = build_segment_tree(l, (l + r) / 2, node * 2); 21 | y = build_segment_tree((l + r) / 2 + 1, r, node * 2 + 1); 22 | 23 | tree[node] = min(x, y); 24 | 25 | return tree[node]; 26 | } 27 | 28 | int query(int l, int r, int e, int f, int node) 29 | { 30 | int x; 31 | int y; 32 | 33 | if(e <= l and f >= r) { 34 | return tree[node]; 35 | } 36 | 37 | if(e > r or f < l) { 38 | return INT_MAX; 39 | } 40 | 41 | x = query(l, (l+r)/2, e, f, node * 2); 42 | y = query((l+r)/ 2 + 1, r, e, f, node * 2 + 1); 43 | 44 | return min(x, y); 45 | } 46 | 47 | 48 | int main() 49 | { 50 | int t; 51 | int n; 52 | int qs; 53 | int x; 54 | int y; 55 | 56 | scanf("%d", &t); 57 | 58 | for (int cs = 1; cs <= t; cs++) { 59 | 60 | scanf("%d", &n); 61 | scanf("%d", &qs); 62 | 63 | for (int i = 1; i <= n; i++) { 64 | scanf("%d", &a[i]); 65 | } 66 | 67 | build_segment_tree(1, n, 1); 68 | 69 | printf("Case %d:\n", cs); 70 | 71 | for(int i = 0; i < qs; i++) { 72 | scanf("%d", &x); 73 | scanf("%d", &y); 74 | 75 | printf("%d\n", query(1, n, x, y, 1)); 76 | } 77 | 78 | 79 | } 80 | 81 | 82 | } 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /1084-Winter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | long long a[100005]; 12 | bool vis[100005]; 13 | int n; 14 | long long k; 15 | 16 | 17 | int bfs() 18 | { 19 | 20 | pair temp; 21 | temp.first = 0; 22 | temp.second = 0; 23 | 24 | queue < pair > q; 25 | 26 | q.push(temp); 27 | vis[0] = 1; 28 | 29 | while(!q.empty()) { 30 | int i = q.front().first; 31 | int count = q.front().second; 32 | 33 | q.pop(); 34 | 35 | 36 | int j = i; 37 | int p; 38 | 39 | p = 0; 40 | 41 | if(j == n) { 42 | return count; 43 | } 44 | 45 | while((a[j] - a[i]) <= 2 * k and j < n) { 46 | j++; 47 | p++; 48 | } 49 | 50 | int k; 51 | k = 0; 52 | 53 | while((p - k) >= 3) { 54 | if(vis[j - k] == 0) { 55 | q.push(make_pair(j - k, count + 1)); 56 | vis[j-k] = 1; 57 | } 58 | k++; 59 | } 60 | 61 | 62 | 63 | } 64 | 65 | return -1; 66 | 67 | 68 | } 69 | int main() 70 | { 71 | int t; 72 | 73 | scanf("%d", &t); 74 | 75 | for (int cs = 1; cs <= t; cs++) { 76 | scanf("%d", &n); 77 | scanf("%lld", &k); 78 | 79 | memset(vis, 0, sizeof vis); 80 | 81 | for (int i = 0; i < n; i++) { 82 | scanf("%lld", &a[i]); 83 | } 84 | 85 | sort(a, a + n); 86 | 87 | printf("Case %d: %d\n", cs, bfs()); 88 | 89 | 90 | } 91 | 92 | } 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /1085-All-Possible-Increasing-Subsequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define Last(i) ( (i) & (-i) ) 8 | #define MOD 1000000007 9 | using namespace std; 10 | 11 | int tree[100050]; 12 | int a[100050]; 13 | int n; 14 | int cnt; 15 | 16 | 17 | int update(int i, int val) 18 | { 19 | 20 | while(i <= cnt) { 21 | tree[i] = (tree[i] + val) % MOD; 22 | i += Last(i); } 23 | 24 | } 25 | 26 | int query(int i) 27 | { 28 | int result; 29 | result = 0; 30 | 31 | while(i > 0) { 32 | result = (result + tree[i]) % MOD; 33 | i -= Last(i); } 34 | 35 | return result; 36 | 37 | } 38 | 39 | 40 | int main() 41 | { 42 | 43 | int t; 44 | int result; 45 | int ans; 46 | 47 | 48 | scanf("%d", &t); 49 | 50 | for (int cs = 1; cs <= t; cs++) { 51 | scanf("%d", &n); 52 | cnt = 0; 53 | ans = 0; 54 | memset(a, 0, sizeof a); 55 | for (int i = 0; i < n; i++) { 56 | scanf("%d", &a[i]); 57 | } 58 | 59 | vector b(a, a+n); 60 | 61 | memset(tree, 0, sizeof tree); 62 | 63 | map m; 64 | 65 | sort(b.begin(), b.end()); 66 | 67 | for (int i = 0; i < n; i++) { 68 | if(m.count(b[i]) == 0) { 69 | m[b[i]] = ++cnt; 70 | } 71 | } 72 | 73 | for (int i = 0; i < n; i++) { 74 | result = query(m[a[i]] - 1) + 1; 75 | ans = (result + ans) % MOD; 76 | update(m[a[i]], result); 77 | } 78 | 79 | printf("Case %d: %d\n", cs, ans); 80 | 81 | 82 | 83 | 84 | } 85 | return 0; 86 | 87 | } -------------------------------------------------------------------------------- /1086-Jogging-Trails.cpp: -------------------------------------------------------------------------------- 1 | //The great chinese postman problem 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define set(x, n) (x ^ (1 << n)) 12 | #define check(x, n) (x & (1 << n)) 13 | 14 | 15 | 16 | using namespace std; 17 | 18 | int n; 19 | int ind; 20 | int edge_count[1028]; 21 | long long floyd[1028][1028]; 22 | int ans; 23 | long long dp[ 1 << 17]; 24 | int floyd_warshal() 25 | { 26 | long long t; 27 | 28 | for (int i = 1; i <= n; i++) { 29 | for (int j = 1; j <= n; j++) { 30 | if(floyd[i][j] == 0) { 31 | floyd[i][j] = INT_MAX; 32 | } 33 | } 34 | } 35 | 36 | for (int k = 1; k <= n; k++) { 37 | for (int i = 1; i <= n; i++) { 38 | for (int j = 1; j <= n; j++) { 39 | if(floyd[i][k] != INT_MAX and floyd[k][j] != INT_MAX) { 40 | t = floyd[i][k] + floyd[k][j]; 41 | if(t < floyd[i][j]) { 42 | floyd[i][j] = t; 43 | } 44 | } 45 | 46 | } 47 | } 48 | } 49 | } 50 | 51 | 52 | 53 | long long chinese(int bit) 54 | { 55 | 56 | int x; 57 | long long mini; 58 | int i; 59 | 60 | 61 | 62 | mini = INT_MAX; 63 | 64 | if(bit == 0) { 65 | return 0; 66 | } 67 | 68 | if(dp[bit] != -1) { 69 | return dp[bit]; 70 | } 71 | 72 | for(i = 1; i <= n; i++) { 73 | if(check(bit, i)) { 74 | break; 75 | } 76 | } 77 | 78 | for (int j = i + 1; j <= n; j++) { 79 | int temp = bit; 80 | 81 | if(check(bit, i) and check(bit, j )) { 82 | temp = set(temp, i); 83 | temp = set(temp, j); 84 | 85 | mini = min(mini, floyd[i][j] + chinese(temp)); 86 | } 87 | } 88 | 89 | 90 | 91 | return dp[bit] = mini; 92 | } 93 | 94 | 95 | 96 | int travel(int cs) 97 | { 98 | int x; 99 | int y; 100 | int start; 101 | long long mini; 102 | long long w; 103 | int m; 104 | int bit; 105 | 106 | 107 | 108 | memset(edge_count, 0, sizeof edge_count); 109 | memset(dp, -1, sizeof dp); 110 | 111 | 112 | 113 | 114 | ans = 0; 115 | bit = 0; 116 | 117 | cin >> n; //no of joints 118 | cin >> m; // no of streets 119 | 120 | for (int i = 0; i <= n; i++) { 121 | for (int j = 0; j <= n; j++) { 122 | floyd[i][j] = INT_MAX; 123 | } 124 | } 125 | 126 | for (int i = 0; i < m; i++) { 127 | 128 | 129 | cin >> x; // street starting point 130 | cin >> y; // street ending point 131 | cin >> w; // street length 132 | 133 | ans += w; 134 | 135 | floyd[x][y] = min(w, floyd[x][y]); 136 | floyd[y][x] = min(w, floyd[y][x]); 137 | 138 | 139 | edge_count[x]++; 140 | edge_count[y]++; 141 | 142 | } 143 | 144 | 145 | 146 | floyd_warshal(); // calculate the shortest path between all the joints 147 | 148 | 149 | for (int i = 1; i <= n; i++) { 150 | if(edge_count[i] % 2 == 1) { 151 | bit = set(bit, i); // mark joints with odd degree 152 | } 153 | 154 | } 155 | ans += chinese(bit); // try all combination of odd vertices and choose the combination with minimum distance 156 | 157 | 158 | printf("Case %d: %d\n", cs, ans); 159 | } 160 | 161 | 162 | 163 | int main() 164 | { 165 | 166 | int t; 167 | 168 | cin >> t; // no of test cases 169 | 170 | for (int cs = 1; cs <= t; cs++) { 171 | travel(cs); 172 | } 173 | 174 | 175 | 176 | } 177 | -------------------------------------------------------------------------------- /1087-Diablo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int a[2000005]; 7 | int tree[2000005]; 8 | int vis[2000005]; 9 | int n; 10 | 11 | 12 | int update(int i, int val) 13 | { 14 | 15 | while(i <= n) { 16 | tree[i] += val; 17 | i = i + (i & (-i)); 18 | } 19 | 20 | } 21 | 22 | long long query(int i) 23 | { 24 | 25 | long long res = 0; 26 | 27 | while(i > 0) { 28 | res = res + tree[i]; 29 | i = i - (i & (-i)); 30 | } 31 | 32 | return res; 33 | 34 | } 35 | 36 | 37 | int search(int i, int j, int x) 38 | { 39 | int mid; 40 | int val; 41 | mid = (i + j) / 2; 42 | 43 | if(i > j ) { 44 | return -1; 45 | } 46 | 47 | val = query(mid); 48 | // cout <<"mid => " < " << val << "x = > " << x << endl; 49 | 50 | if(val > x) { 51 | return search(i, mid-1, x); 52 | } 53 | 54 | if(val < x) { 55 | return search(mid+1, j, x); 56 | } 57 | 58 | if(val == x) { 59 | if(vis[mid] == 1) { 60 | return mid; 61 | } 62 | 63 | else { 64 | return search(i, mid-1, x); 65 | } 66 | 67 | } 68 | 69 | 70 | } 71 | 72 | 73 | int main() 74 | { 75 | 76 | int t; 77 | int m; 78 | int count; 79 | char ch; 80 | int x; 81 | int k; 82 | int nn; 83 | 84 | 85 | scanf("%d", &t); 86 | n = 100000; 87 | 88 | for (int cs = 1; cs <= t;cs++) { 89 | scanf("%d %d", &nn, &m); 90 | n = nn + m + 1; 91 | 92 | count = 0; 93 | 94 | memset(a, 0, sizeof a); 95 | memset(tree, 0, sizeof tree); 96 | memset(vis, 0, sizeof vis); 97 | 98 | printf("Case %d:\n", cs); 99 | for (int i = 1; i <= nn; i++) { 100 | scanf("%d", a + i); 101 | vis[i] = 1; 102 | count++; 103 | update(i, 1); 104 | } 105 | 106 | 107 | for (int i = 0; i < m; i++) { 108 | getchar(); 109 | scanf("%c %d", &ch, &x); 110 | 111 | //cout << ch << " " << x << endl; 112 | 113 | if(ch == 'a') { 114 | nn++; 115 | a[nn] = x; 116 | vis[nn] = 1; 117 | update(nn, 1); 118 | count++; 119 | } 120 | 121 | if(ch == 'c') { 122 | if(x > count) { 123 | printf("none\n"); 124 | continue; 125 | } 126 | 127 | k = search(1, nn, x); 128 | 129 | update(k, -1); 130 | vis[k] = 0; 131 | count--; 132 | printf("%d\n", a[k]); 133 | } 134 | /* 135 | for (int i = 1; i <= nn; i++) { 136 | cout << query(i) << " "; 137 | } 138 | cout << endl; 139 | 140 | for (int i = 1; i <= nn; i++) { 141 | cout << a[i] << " "; 142 | } 143 | cout << endl; 144 | for (int i = 1; i <= nn; i++) { 145 | cout << vis[i] << " "; 146 | } 147 | 148 | cout << endl; 149 | */ 150 | } 151 | 152 | } 153 | 154 | } 155 | 156 | 157 | -------------------------------------------------------------------------------- /1088-Points-in-Segements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int a[100008]; 5 | int n; 6 | int q; 7 | int binary_left(int left, int right, int x) 8 | { 9 | int mid; 10 | 11 | mid = (left + right) / 2; 12 | 13 | if((mid+1 == n ||a[mid+1] > x) and (mid == 0 || a[mid-1] < x)) { 14 | if(a[mid] < x) { 15 | mid++; 16 | 17 | } 18 | return mid; 19 | } 20 | 21 | if(left > right) { 22 | return -9999; 23 | } 24 | 25 | if(a[mid] < x) { 26 | binary_left(mid +1, right, x); 27 | } 28 | 29 | else { 30 | binary_left(left, mid - 1, x); 31 | } 32 | 33 | } 34 | 35 | 36 | int binary_right(int left, int right, int x) 37 | { 38 | int mid; 39 | 40 | mid = (left + right) / 2; 41 | 42 | if((mid+1 == n ||a[mid+1] > x) and (mid == 0 || a[mid-1] < x)) { 43 | 44 | if(a[mid] > x) { 45 | mid--; 46 | } 47 | return mid; 48 | } 49 | 50 | if(left > right) { 51 | return -1; 52 | } 53 | 54 | if(a[mid] < x) { 55 | binary_right(mid +1, right, x); 56 | } 57 | 58 | else { 59 | binary_right(left, mid - 1, x); 60 | } 61 | 62 | } 63 | 64 | int main() 65 | { 66 | 67 | int t; 68 | int left; 69 | int right; 70 | 71 | int x; 72 | int y; 73 | int ans; 74 | 75 | scanf("%d", &t); 76 | 77 | for (int cs = 1; cs <= t; cs++) { 78 | scanf("%d", &n); 79 | scanf("%d", &q); 80 | 81 | for (int i = 0; i < n; i++) { 82 | scanf("%d", &a[i]); 83 | } 84 | printf("Case %d:\n", cs); 85 | 86 | for (int i = 0; i < q; i++) { 87 | scanf("%d", &x); 88 | scanf("%d", &y); 89 | 90 | left = binary_left(0, n-1, x); 91 | right = binary_right(0, n-1, y); 92 | 93 | ans = right - left + 1; 94 | 95 | printf("%d\n",ans); 96 | 97 | } 98 | 99 | } 100 | 101 | } 102 | 103 | 104 | -------------------------------------------------------------------------------- /1089-Points-in-Segments-II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | long long a[500000]; 11 | long long tree[500000]; 12 | long long cnt; 13 | 14 | 15 | 16 | long long update(long long x, int val) 17 | { 18 | if(x == 0) 19 | return 0; 20 | while (x <= cnt) { 21 | tree[x] += val; 22 | x = x + (x & (-x)); 23 | } 24 | 25 | } 26 | 27 | long long queryt(long long x) 28 | { 29 | long long ans; 30 | ans = 0; 31 | 32 | while (x > 0) { 33 | ans += tree[x]; 34 | x = x - (x & (-x)); 35 | } 36 | 37 | return ans; 38 | 39 | } 40 | 41 | 42 | 43 | int main() 44 | { 45 | int t; 46 | long long x; 47 | long long y; 48 | int q; 49 | int n; 50 | 51 | scanf("%d", &t); 52 | 53 | for (int cs = 1; cs <= t; cs++) { 54 | 55 | map < long long, long long > mp; 56 | map id; 57 | vector < pair > v; 58 | vector < long long > query; 59 | 60 | memset(tree, 0, sizeof tree); 61 | 62 | cnt = 0; 63 | 64 | scanf("%d", &n); 65 | scanf("%d", &q); 66 | 67 | for (int i = 0; i < n; i++) { 68 | scanf("%lld", &x); 69 | scanf("%lld", &y); 70 | 71 | if(mp.count(x) == 0) { 72 | mp[x] = 1; 73 | a[cnt] = x; 74 | cnt++; 75 | } 76 | 77 | if(mp.count(y) == 0) { 78 | mp[y] = 1; 79 | a[cnt] = y; 80 | cnt++; 81 | } 82 | 83 | v.push_back(make_pair(x, y)); 84 | 85 | } 86 | 87 | for (int i = 0; i < q; i++) { 88 | scanf("%lld", &x); 89 | query.push_back(x); 90 | 91 | if(mp.count(x) == 0) { 92 | mp[x] = 1; 93 | a[cnt] = x; 94 | cnt++; 95 | } 96 | } 97 | 98 | sort(a, a + cnt); 99 | 100 | for (int i = 0; i < cnt; i++) { 101 | id[a[i]] = i+1; 102 | } 103 | 104 | for (int i = 0; i < v.size(); i++) { 105 | x = v[i].first; 106 | y = v[i].second; 107 | 108 | x = id[x]; 109 | y = id[y]; 110 | 111 | update(x, 1); 112 | update(y + 1, -1); 113 | 114 | 115 | } 116 | 117 | printf("Case %d:\n", cs); 118 | 119 | 120 | 121 | for (int i = 0; i < query.size(); i++) { 122 | x = query[i]; 123 | x = id[x]; 124 | printf("%lld\n", queryt(x) ); 125 | } 126 | 127 | } 128 | 129 | 130 | } 131 | -------------------------------------------------------------------------------- /1090-Trailing-Zeroes-II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define sz 1000000 6 | 7 | long long dp2[sz + 5]; 8 | long long dp5[sz + 5]; 9 | 10 | int main() 11 | { 12 | int j; 13 | int x; 14 | int n; 15 | int r; 16 | int p; 17 | int q; 18 | int t; 19 | long long a; 20 | long long b; 21 | long long temp; 22 | long long two; 23 | long long five; 24 | 25 | dp2[1] = 0; 26 | dp5[1] = 0; 27 | for (int i = 2; i <= sz; i++) { 28 | j = i; 29 | 30 | temp = 0; 31 | 32 | while(j % 2 == 0) { 33 | temp++; 34 | j = j / 2; 35 | } 36 | 37 | dp2[i] = dp2[i-1] + temp; 38 | 39 | j = i; 40 | 41 | temp = 0; 42 | 43 | while(j % 5 == 0) { 44 | temp++; 45 | j = j / 5; 46 | } 47 | 48 | dp5[i] = dp5[i-1] + temp; 49 | 50 | } 51 | 52 | scanf("%d", &t); 53 | 54 | for (int cs = 1; cs <= t; cs++) { 55 | scanf("%d", &n); 56 | scanf("%d", &r); 57 | scanf("%d", &p); 58 | scanf("%d", &q); 59 | 60 | two = (dp2[p] - dp2[p-1]) * q; 61 | five = (dp5[p] - dp5[p-1]) * q; 62 | 63 | two += (dp2[n] - dp2[n-r]); 64 | five += (dp5[n] - dp5[n-r]); 65 | 66 | two = two - dp2[r]; 67 | five = five - dp5[r]; 68 | 69 | a = min(two, five); 70 | 71 | printf("Case %d: %lld\n", cs, a); 72 | } 73 | 74 | 75 | 76 | } 77 | 78 | -------------------------------------------------------------------------------- /1093-Ghajini.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | 11 | long long abs(long long x) 12 | { 13 | if(x < 0) 14 | return x * -1; 15 | 16 | return x; 17 | 18 | } 19 | 20 | int main() 21 | { 22 | 23 | int t; 24 | int n; 25 | int d; 26 | long long x; 27 | long long small; 28 | 29 | scanf("%d", &t); 30 | 31 | for (int cs = 1; cs <= t; cs++) { 32 | scanf("%d", &n); 33 | scanf("%d", &d); 34 | small = 0; 35 | 36 | priority_queue < pair, vector >, greater > > bigq; 37 | priority_queue > smallq; 38 | 39 | for (int i = 1; i <= n; i++) { 40 | cin >> x; 41 | //cout << x << "time -> :"<= d) { 46 | //cout << " poping bigq " << bigq.top().first << endl; 47 | bigq.pop(); 48 | } 49 | 50 | while(!smallq.empty() and(i - smallq.top().second) >= d) { 51 | //cout << " poping smallq " << smallq.top().first << " " << smallq.top().second<= 2 and smallq.size() >= 2) { 56 | small = max(small, abs(bigq.top().first - smallq.top().first)); 57 | //cout << "small updated "<< small < 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | vector < vector > > graph; 10 | bool vis[30005]; 11 | long long maxi; 12 | int start; 13 | int end; 14 | 15 | long long explore(int j, long long dis) 16 | { 17 | int x; 18 | int w; 19 | 20 | if(dis > maxi) { 21 | maxi = dis; 22 | start = j; 23 | } 24 | 25 | for (int i = 0; i < graph[j].size(); i++) { 26 | x = graph[j][i].first; 27 | w = graph[j][i].second; 28 | 29 | if(vis[x] == 0) { 30 | vis[x] = 1; 31 | explore(x, dis+w); 32 | } 33 | 34 | } 35 | 36 | 37 | } 38 | 39 | 40 | 41 | int main() 42 | { 43 | 44 | int x; 45 | int y; 46 | int w; 47 | int t; 48 | int temps; 49 | int n; 50 | 51 | scanf("%d", &t); 52 | 53 | for (int cs = 1; cs <= t; cs++) { 54 | scanf("%d", &n); 55 | 56 | vector < vector > > temp(n+2); 57 | swap(temp, graph); 58 | 59 | for (int i = 0; i < n - 1; i++) { 60 | cin >> x; 61 | cin >> y; 62 | cin >> w; 63 | graph[x].push_back(make_pair(y, w)); 64 | graph[y].push_back(make_pair(x, w)); 65 | } 66 | 67 | 68 | maxi = INT_MIN; 69 | memset(vis, 0, sizeof(vis)); 70 | vis[0] = 1; 71 | explore(0, 0); 72 | temps = start; 73 | 74 | 75 | maxi = INT_MIN; 76 | memset(vis, 0, sizeof(vis)); 77 | vis[start] = 1; 78 | explore(start, 0); 79 | printf("Case %d: %lld\n", cs, maxi); 80 | } 81 | 82 | } 83 | 84 | -------------------------------------------------------------------------------- /1104-Birthday-Paradox.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define LIM 0.5 3 | using namespace std; 4 | int days; 5 | 6 | double cal_prob(int people) 7 | { 8 | double ans; 9 | double fin; 10 | 11 | int x; 12 | 13 | ans = 1; 14 | x = days; 15 | 16 | for (int i = 0; i < people; i++) { 17 | ans = ans * ((double)x / days); 18 | x--; 19 | } 20 | 21 | fin = 1 - ans; 22 | return fin; 23 | } 24 | 25 | int explore(int l, int r) 26 | { 27 | int mid; 28 | mid = (l + r) / 2; 29 | 30 | if(cal_prob(mid) < LIM) { 31 | return explore(mid+1, r); 32 | } 33 | 34 | if(cal_prob(mid - 1) < LIM) { 35 | return mid; 36 | } 37 | 38 | return explore(l, mid-1); 39 | } 40 | 41 | int main() 42 | { 43 | 44 | int t; 45 | scanf("%d", &t); 46 | 47 | for (int cs = 1; cs <= t; cs++) { 48 | scanf("%d", &days); 49 | //days = t; 50 | 51 | if(days == 1) { 52 | printf("Case %d: %d\n", cs, 1); 53 | continue; 54 | } 55 | 56 | if(days == 2) { 57 | printf("Case %d: %d\n", cs, 1); 58 | continue; 59 | } 60 | 61 | 62 | printf("Case %d: %d\n", cs, explore(1, 373) - 1); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /1105-Fi-Binary-Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int a[100]; 6 | int dp[100]; 7 | int maxi; 8 | int n; 9 | 10 | int explore(int sum) 11 | { 12 | if(sum <= 0) { 13 | 14 | for (int j = maxi; j >= 0; j--) { 15 | printf("%d", a[j]); 16 | } 17 | 18 | printf("\n"); 19 | 20 | return 0; 21 | } 22 | 23 | if(sum == 1) { 24 | a[0] = 1; 25 | maxi = max(maxi, 0); 26 | explore(0); 27 | return 0; 28 | } 29 | 30 | for (int i = 0; 1; i++) { 31 | if(dp[i+1] >= sum and dp[i] < sum) { 32 | a[i+1] = 1; 33 | sum = sum - (dp[i]+1); 34 | maxi = max(maxi, i+1); 35 | explore(sum); 36 | break; 37 | } 38 | } 39 | 40 | 41 | } 42 | 43 | 44 | 45 | int main() 46 | { 47 | 48 | int j; 49 | int k; 50 | int t; 51 | int temp; 52 | 53 | j = 1; 54 | k = 2; 55 | 56 | dp[0] = 1; 57 | 58 | for (int i = 1; i < 44; i++) { 59 | dp[i] = dp[i-1] + j; 60 | temp = k; 61 | k = j + k; 62 | j = temp; 63 | 64 | } 65 | 66 | scanf("%d", &t); 67 | 68 | for (int cs = 1; cs <= t; cs++) { 69 | scanf("%d", &n); 70 | memset(a, 0, sizeof(a)); 71 | maxi = -1; 72 | printf("Case %d: ", cs); 73 | explore(n); 74 | } 75 | 76 | } 77 | 78 | -------------------------------------------------------------------------------- /1107-How-Cow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | bool between(int z, int x, int y) 7 | { 8 | if(x > y) { 9 | swap(x, y); 10 | } 11 | 12 | return z >= x and z <= y; 13 | 14 | } 15 | int main() 16 | { 17 | 18 | int x1; 19 | int x2; 20 | int y1; 21 | int y2; 22 | int n; 23 | int x; 24 | int y; 25 | 26 | int t; 27 | 28 | scanf("%d", &t); 29 | 30 | for (int cs = 1; cs <= t; cs++) { 31 | scanf("%d", &x1); 32 | scanf("%d", &y1); 33 | scanf("%d", &x2); 34 | scanf("%d", &y2); 35 | 36 | scanf("%d", &n); 37 | printf("Case %d:\n", cs); 38 | for (int i = 0; i < n; i++) { 39 | scanf("%d", &x); 40 | scanf("%d", &y); 41 | 42 | if(between(x, x1, x2) and between(y, y1, y2)) { 43 | printf("Yes\n"); 44 | } 45 | 46 | else { 47 | printf("No\n"); 48 | } 49 | 50 | 51 | } 52 | 53 | 54 | 55 | } 56 | 57 | 58 | } 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /1109-False-Ordering.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | bool cmp(pair a, pair b) 9 | { 10 | 11 | if(a.first < b.first) { 12 | return 1; 13 | } 14 | 15 | if(a.first > b.first) { 16 | return 0; 17 | } 18 | 19 | if(a.second > b.second) { 20 | return 1; 21 | } 22 | 23 | return 0; 24 | 25 | } 26 | 27 | 28 | 29 | 30 | int main() 31 | { 32 | 33 | 34 | int x; 35 | int t; 36 | 37 | vector < pair > a(1001); 38 | 39 | for (int i = 1; i <= 1000; i++) { 40 | a[i].second = i; 41 | a[i].first = 0; 42 | } 43 | 44 | for (int i = 1; i <= 1000; i++) { 45 | for (int j = i; j <= 1000; j = j + i) { 46 | a[j].first++; 47 | } 48 | 49 | 50 | } 51 | 52 | sort(a.begin(), a.end(), cmp); 53 | 54 | scanf("%d", &t); 55 | 56 | 57 | for (int cs = 1; cs <= t; cs++) { 58 | scanf("%d", &x); 59 | printf("Case %d: %d\n", cs, a[x].second); 60 | 61 | } 62 | 63 | 64 | } 65 | 66 | -------------------------------------------------------------------------------- /1110-An-Easy-LCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | string minim(string x, string y) 6 | { 7 | if(x.length() < y.length()) { 8 | return y; 9 | } 10 | 11 | if(x.length() > y.length()) { 12 | return x; 13 | } 14 | 15 | for (int i = 0; i < x.length(); i++) { 16 | if(x[i] < y[i]) { 17 | return x; 18 | } 19 | 20 | if(x[i] > y[i]) { 21 | return y; 22 | } 23 | 24 | } 25 | 26 | return x; 27 | 28 | } 29 | 30 | 31 | 32 | int main() 33 | { 34 | 35 | int t; 36 | string x; 37 | string y; 38 | string temp; 39 | char xx[110]; 40 | char yy[110]; 41 | char ans[110]; 42 | int n; 43 | int m; 44 | scanf("%d", &t); 45 | for (int cs = 1; cs <= t; cs++) { 46 | 47 | string a[110][110]; 48 | 49 | scanf("%s", xx); 50 | scanf("%s", yy); 51 | 52 | x = xx; 53 | y = yy; 54 | 55 | n = x.length(); 56 | m = y.length(); 57 | 58 | for (int i = n; i >= 0; i--) { 59 | for (int j = m; j >= 0; j--) { 60 | if(i == n or j == m) { 61 | a[i][j] = ""; 62 | continue; 63 | } 64 | 65 | if(x[i] == y[j]) { 66 | a[i][j] = x[i] + a[i+1][j+1]; 67 | continue; 68 | } 69 | 70 | a[i][j] = minim(a[i+1][j],a[i][j+1]); 71 | } 72 | 73 | 74 | } 75 | 76 | for (int i = 0; i < a[0][0].length(); i++) { 77 | ans[i] = a[0][0][i]; 78 | } 79 | 80 | ans[a[0][0].length()] = '\0'; 81 | 82 | if(a[0][0].length() != 0) { 83 | printf("Case %d: %s\n", cs, ans); 84 | 85 | } 86 | 87 | else { 88 | printf("Case %d: :(\n", cs); 89 | } 90 | 91 | } 92 | 93 | } 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /1111-Best-Picnic-Ever.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | enum consts { N = 1005}; 8 | 9 | vector < vector > a; 10 | 11 | int n; 12 | int m; 13 | int k; 14 | int city[N]; 15 | int vis[N]; 16 | int count[N]; 17 | 18 | int explore(int j, int inc) 19 | { 20 | 21 | count[j] += inc; 22 | 23 | for (int i = 0; i < a[j].size(); i++) { 24 | int x; 25 | x = a[j][i]; 26 | 27 | if(vis[x] == 0) { 28 | vis[x] = 1; 29 | explore(x, inc); 30 | } 31 | } 32 | 33 | } 34 | 35 | int main() 36 | { 37 | 38 | int t; 39 | int x; 40 | int y; 41 | int inc; 42 | scanf("%d", &t); 43 | 44 | for (int cs = 1; cs <= t; cs++) { 45 | scanf("%d", &k); 46 | scanf("%d", &n); 47 | scanf("%d", &m); 48 | 49 | vector < vector > temp(N + 5); 50 | swap(temp, a); 51 | 52 | memset(city, 0, sizeof city); 53 | memset(count, 0, sizeof count); 54 | 55 | for (int i = 0; i < k; i++) { 56 | scanf("%d", &x); 57 | city[x]++; 58 | } 59 | 60 | for (int i = 0; i < m; i++) { 61 | scanf("%d", &x); 62 | scanf("%d", &y); 63 | a[x].push_back(y); 64 | } 65 | 66 | for (int i = 1; i <= n; i++) { 67 | memset(vis, 0, sizeof vis); 68 | if(city[i]) { 69 | vis[i] = 1; 70 | inc = city[i]; 71 | explore(i, inc); 72 | } 73 | } 74 | 75 | int ans; 76 | ans = 0; 77 | 78 | for (int i = 1; i <= n; i++) { 79 | if(count[i] >= k) 80 | ans++; 81 | } 82 | 83 | printf("Case %d: %d\n", cs, ans); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /1112-Curious-Robin-Hood.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 100005 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | int a[SIZE]; 8 | int n; 9 | 10 | 11 | long long update(int i, int val) 12 | { 13 | 14 | while(i <= n) { 15 | a[i] = a[i] + val; 16 | i = i + (i & (-i)); 17 | } 18 | 19 | } 20 | 21 | long long query(int i) 22 | { 23 | long long ans; 24 | ans = 0; 25 | 26 | while(i > 0) { 27 | ans = ans + a[i]; 28 | i = i - (i & (-i)); 29 | } 30 | 31 | return ans; 32 | } 33 | 34 | int main() 35 | { 36 | int t; 37 | int x; 38 | int y; 39 | int w; 40 | int choice; 41 | long long ans; 42 | int q; 43 | int ch; 44 | 45 | scanf("%d", &t); 46 | 47 | for (int cs = 1; cs <= t; cs++) { 48 | scanf("%d", &n); 49 | scanf("%d", &q); 50 | 51 | memset(a, 0, sizeof a); 52 | 53 | for (int i = 1; i <= n; i++) { 54 | scanf("%d", &x); 55 | update(i, x); 56 | } 57 | 58 | 59 | 60 | printf("Case %d:\n", cs); 61 | for (int i = 0; i < q; i++) { 62 | scanf("%d", &ch); 63 | 64 | switch(ch) { 65 | case 1: 66 | scanf("%d", &x); 67 | x++; 68 | ans = query(x) - query(x-1); 69 | update(x, -ans); 70 | printf("%lld\n", ans); 71 | break; 72 | case 2: 73 | scanf("%d", &x); 74 | x++; 75 | scanf("%d", &w); 76 | update(x, w); 77 | break; 78 | 79 | case 3: 80 | scanf("%d", &x); 81 | scanf("%d", &y); 82 | x++; 83 | y++; 84 | ans = query(y) - query(x -1); 85 | printf("%lld\n", ans); 86 | break; 87 | 88 | } 89 | } 90 | } 91 | } 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /1113-Dsicover-The-Web.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | string choice; 7 | int turn; 8 | 9 | int nextturn() 10 | { 11 | 12 | if(choice == "VISIT") { 13 | turn = 0; 14 | } 15 | 16 | if(choice == "FORWARD") { 17 | turn = 1; 18 | } 19 | 20 | if(choice == "BACK") { 21 | turn = 2; 22 | } 23 | 24 | } 25 | 26 | int main() 27 | { 28 | ios_base::sync_with_stdio(false); 29 | 30 | int t; 31 | string url; 32 | stack f; 33 | stack b; 34 | 35 | cin >> t; 36 | 37 | for (int cs = 1; cs <= t; cs++) { 38 | cin >> choice; 39 | while(!b.empty()) { 40 | b.pop(); 41 | } 42 | 43 | while(!f.empty()) { 44 | f.pop(); 45 | } 46 | 47 | url = "http://www.lightoj.com/"; 48 | cout << "Case "<> url; 57 | cout << url << "\n"; 58 | while(!f.empty()) { 59 | f.pop(); 60 | } 61 | break; 62 | 63 | case 1: 64 | if(f.empty()) { 65 | cout << "Ignored\n"; 66 | } 67 | 68 | else { 69 | b.push(url); 70 | url = f.top(); 71 | f.pop(); 72 | cout << url << "\n"; 73 | } 74 | break; 75 | 76 | 77 | case 2: 78 | 79 | if(b.empty()) { 80 | cout << "Ignored\n"; 81 | } 82 | 83 | else { 84 | f.push(url); 85 | url = b.top(); 86 | b.pop(); 87 | cout << url << "\n"; 88 | } 89 | 90 | break; 91 | 92 | } 93 | 94 | 95 | cin >> choice; 96 | } 97 | 98 | 99 | 100 | } 101 | 102 | 103 | } 104 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /1114-Easily-Readable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | map > q; 9 | int mod = 16127; 10 | 11 | int getHash(string line) 12 | { 13 | int hash; 14 | 15 | hash = 1; 16 | for (int j = 0; j < line.length(); j++) { 17 | hash = hash * ( 1 +line[j] - 'A'); 18 | hash = hash % mod; 19 | } 20 | //cout << line << " " << hash << endl; 21 | return hash; 22 | 23 | } 24 | 25 | bool check(string a, string b) 26 | { 27 | int n; 28 | int acount[70]; 29 | int bcount[70]; 30 | 31 | 32 | memset(acount, 0, sizeof(acount)); 33 | memset(bcount, 0, sizeof(bcount)); 34 | 35 | n = a.length(); 36 | 37 | if(a.length() != b.length()) { 38 | return 0; 39 | } 40 | 41 | if(a.length() == 1) { 42 | return a[0] == b[0]; 43 | } 44 | 45 | if(a.length() == 2) { 46 | return a[0] == b[0] and a[1] == b[1]; 47 | } 48 | 49 | if(a[0] != b[0] or a[n-1] != b[n-1]) { 50 | return 0; 51 | } 52 | for (int i = 1; i < n-1; i++) { 53 | acount[a[i] - 'A']++; 54 | bcount[b[i] - 'A']++; 55 | } 56 | 57 | for (int i = 0; i < 56; i++) { 58 | if(acount[i] != bcount[i]) { 59 | return 0; 60 | } 61 | 62 | } 63 | 64 | return 1; 65 | 66 | } 67 | 68 | int getCount(string a) 69 | { 70 | int hash; 71 | int count; 72 | 73 | count = 0; 74 | hash = getHash(a); 75 | 76 | for (int i = 0; i < q[hash].size(); i++) { 77 | if(check(a, q[hash][i])) { 78 | count++; 79 | } 80 | 81 | } 82 | 83 | return count; 84 | 85 | } 86 | 87 | 88 | bool calculate(string line) 89 | { 90 | istringstream iss(line); 91 | string a; 92 | long long ans; 93 | long long count; 94 | ans = 1; 95 | 96 | while(iss >> a) { 97 | count = getCount(a); 98 | ans = ans * count; 99 | 100 | } 101 | 102 | printf("%lld\n", ans); 103 | 104 | } 105 | 106 | int mmain() 107 | { 108 | string x; 109 | int hash; 110 | x = "AZbcdZ abczzz lfdpPSlslaSPApSWlwLWWWsssss lfdpPSlslaSAPpSWlwLWWWsssss "; 111 | 112 | string temp[] = {"abczzz", "AbcdZZ","lfdpPSlslaSPApSWlwLWWWsssss"}; 113 | for (int i = 0; i < 3; i++) { 114 | hash = getHash(temp[i]); 115 | q[hash].push_back(temp[i]); 116 | } 117 | 118 | calculate(x); 119 | } 120 | int main() 121 | { 122 | 123 | int m; 124 | char temp[100008]; 125 | int t; 126 | int dic_count; 127 | long long hash; 128 | long long ans; 129 | int count; 130 | string line; 131 | 132 | 133 | scanf("%d", &t); 134 | 135 | for (int cs = 1; cs <= t; cs++) { 136 | scanf("%d",&dic_count); 137 | map > tq; 138 | swap(q, tq); 139 | 140 | gets(temp); 141 | for (int i = 0; i < dic_count; i++) { 142 | gets(temp); 143 | line = temp; 144 | hash = getHash(line); 145 | q[hash].push_back(line); 146 | 147 | } 148 | 149 | scanf("%d", &m); 150 | gets(temp); 151 | printf("Case %d:\n", cs); 152 | for (int i = 0; i < m; i++) { 153 | ans = 1; 154 | 155 | gets(temp); 156 | line = temp; 157 | calculate(line); 158 | 159 | 160 | } 161 | 162 | 163 | } 164 | 165 | 166 | } 167 | 168 | 169 | -------------------------------------------------------------------------------- /1116-Ekka-Dokka.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | int t; 9 | 10 | long long w; 11 | long long odd; 12 | long long even; 13 | 14 | scanf("%d", &t); 15 | 16 | for (int cs = 1; cs <= t; cs++) { 17 | scanf("%lld", &w); 18 | if(w % 2 != 0) { 19 | printf("Case %d: Impossible\n", cs); 20 | continue; 21 | } 22 | 23 | odd = w / 2; 24 | even = 2; 25 | 26 | while(odd % 2 == 0) { 27 | odd = odd / 2; 28 | even = even * 2; 29 | } 30 | 31 | printf("Case %d: %lld %lld\n",cs, odd, even); 32 | 33 | } 34 | 35 | 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /1119-Pimp-My-Ride.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define set(x, i) (x | (1 << i)) 8 | #define check(x, i) (x & (1 << i)) 9 | 10 | int a[20][20]; 11 | long long dp[20][2 << 16]; 12 | int n; 13 | 14 | int explore(int i, int x) 15 | { 16 | 17 | int ok; 18 | int sum; 19 | int mini = INT_MAX; 20 | 21 | sum = 0; 22 | ok = 0; 23 | if(dp[i][x] != -1) { 24 | return dp[i][x]; 25 | } 26 | for (int j = 1; j <= n; j++) { 27 | if(check(x, j)) { 28 | sum += a[i][j]; 29 | } 30 | else { 31 | ok = 1; 32 | } 33 | 34 | 35 | } 36 | 37 | if(ok == 0) { 38 | return sum; 39 | } 40 | 41 | for (int j = 1; j <= n; j++) { 42 | if(check(x, j) == 0) { 43 | mini = min(mini, explore(j, set(x, j))); 44 | } 45 | } 46 | 47 | mini = mini + sum; 48 | 49 | return dp[i][x] = mini; 50 | 51 | } 52 | 53 | 54 | int main() 55 | { 56 | 57 | int t; 58 | 59 | scanf("%d", &t); 60 | 61 | for (int cs = 1; cs <= t; cs++) { 62 | scanf("%d", &n); 63 | memset(a, 0, sizeof a); 64 | memset(dp, -1, sizeof dp); 65 | 66 | for (int i = 1; i <= n; i++) { 67 | for (int j = 1; j <= n; j++) { 68 | scanf("%d", &a[i][j]); 69 | } 70 | } 71 | 72 | printf("Case %d: %d\n", cs, explore(0, 0)); 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /1122-Digit-Count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int m; 7 | int n; 8 | int a[12]; 9 | long long ans[12][11]; 10 | long long getCount(int i, int x) 11 | { 12 | long long count; 13 | 14 | count = 0; 15 | 16 | if(ans[i][x] != -1) { 17 | return ans[i][x]; 18 | } 19 | 20 | if(i == n) { 21 | return 1; 22 | } 23 | 24 | 25 | 26 | for (int j = 0; j < m; j++) { 27 | if(x == 0 or (abs(a[j] - x) <= 2)) { 28 | count += getCount(i+1, a[j]); 29 | } 30 | 31 | } 32 | ans[i][x] = count; 33 | return count; 34 | 35 | } 36 | 37 | 38 | 39 | 40 | int main() 41 | { 42 | 43 | int t; 44 | scanf("%d", &t); 45 | 46 | for (int cs = 1; cs <= t; cs++) { 47 | memset(ans, -1, sizeof(ans)); 48 | 49 | scanf("%d", &m); 50 | scanf("%d", &n); 51 | 52 | for (int i = 0; i < m; i++) { 53 | scanf("%d", &a[i]); 54 | } 55 | 56 | printf("Case %d: %lld\n",cs, getCount(0, 0)); 57 | 58 | } 59 | 60 | 61 | } 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /1127-Funny-Knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | long long n; 9 | long long w; 10 | long long inp[100]; 11 | 12 | vector a; 13 | vector b; 14 | 15 | long long create_a(long long i,long long sum, long long max) 16 | { 17 | if(sum > w) { 18 | return 0; 19 | } 20 | 21 | if( i == max) { 22 | a.push_back(sum); 23 | return 0; 24 | } 25 | 26 | create_a(i+1, sum + inp[i], max); 27 | create_a(i+1, sum,max); 28 | 29 | } 30 | 31 | long long create_b(long long i,long long sum, long long max) 32 | { 33 | if(sum > w) { 34 | return 0; 35 | } 36 | if( i == max) { 37 | b.push_back(sum); 38 | return 0; 39 | } 40 | 41 | create_b(i+1, sum + inp[i], max); 42 | create_b(i+1, sum,max); 43 | 44 | } 45 | 46 | 47 | long long bs(long long j, long long k, long long targ) 48 | { 49 | long long i; 50 | 51 | i = (j + k) / 2; 52 | if(j > k) { 53 | return -1; 54 | } 55 | if((i == a.size() - 1 || a[i+1] > targ) and a[i] <= targ) { 56 | return i; 57 | } 58 | 59 | else { 60 | if(a[i] > targ) { 61 | return bs(j, i-1, targ); 62 | } 63 | 64 | else { 65 | return bs(i+1, k, targ); 66 | } 67 | 68 | } 69 | 70 | } 71 | 72 | 73 | int bs(long long val) 74 | { 75 | int low = 0 , high =(int ) a.size() - 1 , mid , ans =low -1; 76 | while (low <= high ) { 77 | mid = (low + high ) >>1; 78 | if(a[mid] <= val) { 79 | ans = mid ; 80 | low = mid + 1; 81 | } 82 | else high = mid - 1; 83 | } 84 | return ans ; 85 | } 86 | 87 | 88 | int main() 89 | { 90 | 91 | long long t; 92 | long long ans; 93 | 94 | scanf("%lld", &t); 95 | for (long long cs = 1; cs <= t; cs++) { 96 | 97 | scanf("%lld", &n); 98 | scanf("%lld", &w); 99 | ans = 0; 100 | long long k; 101 | vector ta; 102 | vector tb; 103 | 104 | swap(ta, a); 105 | swap(tb, b); 106 | 107 | for (long long i = 0; i < n; i++) { 108 | scanf("%lld", &inp[i]); 109 | } 110 | 111 | create_a(0, 0, n/2); 112 | create_b(n/2, 0, n); 113 | 114 | sort(a.begin(), a.end()); 115 | sort(b.begin(), b.end()); 116 | 117 | 118 | for (long long i = 0; i < b.size(); i++) { 119 | k = bs(0, a.size() -1, w - b[i]); 120 | ans = ans + k+1; 121 | } 122 | 123 | printf("Case %lld: %lld\n", cs, ans); 124 | 125 | 126 | } 127 | 128 | } 129 | 130 | -------------------------------------------------------------------------------- /1133-Array-Simulation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | 9 | long long a[1000]; 10 | int t; 11 | int n; 12 | char choice; 13 | long long x; 14 | long long y; 15 | int count; 16 | int q; 17 | 18 | cin >> t; 19 | 20 | for (int cs = 1; cs <= t; cs++) { 21 | cin >> n; 22 | cin >> q; 23 | count = 0; 24 | 25 | for (int i = 0; i < n; i++) { 26 | cin >> a[i]; 27 | } 28 | 29 | for (int k = 0; k < q; k++) { 30 | // scanf("%c", &choice); 31 | cin >> choice; 32 | switch(choice) { 33 | case 'S': 34 | cin >> x; 35 | for (int i = 0; i < n; i++) { 36 | a[i] += x; 37 | } 38 | 39 | break; 40 | 41 | case 'M': 42 | cin >> x; 43 | for (int i = 0; i < n; i++) { 44 | a[i] *= x; 45 | } 46 | 47 | break; 48 | 49 | case 'D': 50 | cin >> x; 51 | for (int i = 0; i < n; i++) { 52 | a[i] /= x; 53 | } 54 | 55 | break; 56 | 57 | case 'R': 58 | for (int i = 0, j = n-1; i < j; i++, j--) { 59 | swap(a[i], a[j]); 60 | } 61 | break; 62 | 63 | case 'P': 64 | cin >> x; 65 | cin >> y; 66 | swap(a[x], a[y]); 67 | break; 68 | 69 | } 70 | 71 | 72 | 73 | } 74 | 75 | printf("Case %d:\n", cs); 76 | 77 | for (int i = 0; i < n; i++) { 78 | printf("%lld", a[i]); 79 | if(i != n-1) { 80 | printf(" "); 81 | } 82 | 83 | else { 84 | printf("\n"); 85 | } 86 | 87 | } 88 | 89 | 90 | 91 | 92 | 93 | } 94 | 95 | 96 | } 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /1134-Be-Efficient.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | long long fact_cal(int x) 8 | { 9 | 10 | long long nplus1 = x; 11 | nplus1++; 12 | 13 | return (long long) (x * nplus1) / 2; 14 | 15 | } 16 | 17 | int main() 18 | { 19 | 20 | int t; 21 | int n; 22 | int m; 23 | 24 | int a[500004]; 25 | int count[500005]; 26 | long long sum; 27 | 28 | scanf("%d", &t); 29 | 30 | 31 | for (int cs = 1; cs <= t; cs++) { 32 | scanf("%d", &n); 33 | scanf("%d", &m); 34 | 35 | memset(count, 0, sizeof(count)); 36 | sum = 0; 37 | 38 | for (int i = 0; i < n; i++) { 39 | scanf("%d", &a[i]); 40 | } 41 | 42 | a[0] = a[0] % m; 43 | 44 | for (int i = 1; i < n; i++) { 45 | a[i] = (a[i-1] + a[i]) % m; 46 | } 47 | 48 | 49 | for (int i = 0; i < n; i++) { 50 | count[a[i]]++; 51 | } 52 | 53 | sum += count[0]; 54 | 55 | long long temp; 56 | 57 | for (int i = 0; i <= 500000; i++) { 58 | sum = sum + fact_cal(count[i] -1); 59 | } 60 | 61 | printf("Case %d: %lld\n", cs, sum); 62 | 63 | } 64 | 65 | } 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /1138-Trailing-Zeroes-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | int q; 8 | int n; 9 | 10 | 11 | inline int zeros(int n) { 12 | int ret = 0; 13 | while(n > 0) { 14 | n /= 5; 15 | ret += n; 16 | } 17 | 18 | return ret; 19 | } 20 | 21 | int explore(int start, int end) 22 | { 23 | int mid; 24 | int count; 25 | int t; 26 | 27 | if(start > end) { 28 | return -1; 29 | } 30 | 31 | mid = (start + end) / 2; 32 | 33 | count = zeros(mid); 34 | 35 | if(count == n) { 36 | 37 | while(zeros(mid) == n) { 38 | mid--; 39 | } 40 | return ++mid; 41 | } 42 | 43 | if(count < n) { 44 | return explore(mid+1, end); 45 | } 46 | 47 | else { 48 | return explore(start, mid-1); 49 | } 50 | 51 | } 52 | 53 | 54 | 55 | 56 | 57 | 58 | int main() 59 | { 60 | 61 | int t; 62 | int ans; 63 | 64 | scanf("%d", &t); 65 | 66 | for (int cs = 1; cs <= t; cs++) { 67 | scanf("%d", &n); 68 | ans = explore(1, INT_MAX); 69 | if(ans != -1) { 70 | printf("Case %d: %d\n", cs, ans); 71 | } 72 | else { 73 | printf("Case %d: impossible\n", cs); 74 | } 75 | } 76 | 77 | 78 | } 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /1141-Number-Tranformation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | int x; 9 | int y; 10 | 11 | vector pr; 12 | 13 | 14 | 15 | 16 | int main() 17 | { 18 | 19 | int t; 20 | int primes[100]; 21 | int temp; 22 | 23 | memset(primes, 0, sizeof(primes)); 24 | 25 | for (int i = 2; i <= 1000; i++) { 26 | 27 | if(primes[i] == 0) { 28 | 29 | for (int j = 2 * i; j <= 1000; j = j + i) { 30 | primes[j] = 1; 31 | } 32 | 33 | } 34 | 35 | } 36 | 37 | for (int i = 2; i <= 1000; i++) { 38 | if(primes[i] == 0) { 39 | pr.push_back(i); 40 | } 41 | } 42 | cout << pr.size() << endl; 43 | scanf("%d", &t); 44 | 45 | 46 | for (int cs = 1; cs <= t; cs++) { 47 | scanf("%d", &x); 48 | scanf("%d", &y); 49 | 50 | int ans[2000]; 51 | 52 | for(int i = 0; i <= y; i++) { 53 | ans[i] = INT_MAX; 54 | } 55 | 56 | ans[x] = 0; 57 | 58 | int k; 59 | 60 | for (int i = x + 1; i <= y; i++) { 61 | for (int j = 0; j < pr.size(); j++) { 62 | 63 | k = pr[j]; 64 | 65 | if(((i - k) % k == 0) and (i-k != k) and i - k >= x and ans[i-k] != INT_MAX) { 66 | if(ans[i] > 1 + ans[i-k]) { 67 | ans[i] = min(ans[i], 1 + ans[i-k]); 68 | 69 | //cout << "k " << k << " " << ans[i-k] << endl; 70 | } 71 | } 72 | 73 | } 74 | 75 | //cout << i << " "< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int a[1000005]; 7 | 8 | int main() 9 | { 10 | 11 | int t; 12 | int x; 13 | int ans; 14 | int n; 15 | scanf("%d", &t); 16 | 17 | for (int cs = 1; cs <= t; cs++) { 18 | memset(a, 0, sizeof a); 19 | ans = 0; 20 | scanf("%d", &n); 21 | for (int i = 0; i < n; i++) { 22 | scanf("%d", &x); 23 | x++; 24 | if(a[x] == 0) { 25 | ans += x; 26 | a[x] = 1; 27 | if(a[x] == x) 28 | a[x] = 0; 29 | continue; 30 | } 31 | 32 | if(a[x] < x) { 33 | a[x]++; 34 | if(a[x] == x) { 35 | a[x] = 0; 36 | } 37 | } 38 | 39 | 40 | } 41 | 42 | printf("Case %d: %d\n", cs, ans); 43 | } 44 | 45 | return 0; 46 | 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /1149-Factors-and-Multiples.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int inp1[110]; 9 | int inp2[110]; 10 | int n; 11 | int m; 12 | int a[250][250]; 13 | int sor; 14 | int tar; 15 | int flow; 16 | int parent[250]; 17 | 18 | int dfs() 19 | { 20 | int x; 21 | int y; 22 | int vis[250]; 23 | 24 | memset(vis, 0 , sizeof vis); 25 | 26 | stack q; 27 | q.push(sor); 28 | 29 | while(!q.empty()) { 30 | x = q.top(); 31 | q.pop(); 32 | 33 | for (int i = 0; i <= n + m + 2; i++) { 34 | if(a[x][i] > 0 and vis[i] == 0) { 35 | parent[i] = x; 36 | q.push(i); 37 | vis[i] = 1; 38 | } 39 | } 40 | 41 | } 42 | 43 | return vis[tar] == 1; 44 | } 45 | 46 | 47 | 48 | 49 | 50 | int main() 51 | { 52 | int t; 53 | int ans; 54 | int k; 55 | 56 | scanf("%d", &t); 57 | 58 | for (int cs = 1; cs <= t; cs++) { 59 | 60 | ans = 0; 61 | 62 | scanf("%d", &n); 63 | 64 | 65 | 66 | for (int i = 0; i < n; i++) { 67 | scanf("%d", inp1 + i); 68 | } 69 | 70 | scanf("%d", &m); 71 | 72 | for (int i = 0; i < m; i++) { 73 | scanf("%d", inp2 + i); 74 | } 75 | 76 | sor = n + m + 1; 77 | tar = n + m + 2; 78 | 79 | memset(a, 0, sizeof a); 80 | 81 | for (int i = 0; i < n; i++) { 82 | for (int j = 0; j < m; j++) { 83 | if(inp2[j] % inp1[i] == 0) { 84 | a[i][j + n] = 1; 85 | } 86 | 87 | } 88 | 89 | } 90 | 91 | for (int i = 0; i < n; i++) { 92 | a[sor][i] = 1; 93 | } 94 | 95 | for(int i = 0; i < m; i++) { 96 | a[i + n][tar] = 1; 97 | } 98 | 99 | while(dfs()) { 100 | k = tar; 101 | 102 | 103 | while(k != sor) { 104 | 105 | a[parent[k]][k] -= 1; 106 | a[k][parent[k]] += 1; 107 | k = parent[k]; 108 | } 109 | 110 | ans += 1; 111 | } 112 | 113 | printf("Case %d: %d\n", cs, ans); 114 | 115 | } 116 | 117 | 118 | } 119 | 120 | 121 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /1153-Internet-Bandwidth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int s; 10 | int t; 11 | int n; 12 | 13 | vector < vector < pair > > g; 14 | int a[110][110]; 15 | int parent[110]; 16 | 17 | int dfs() 18 | { 19 | 20 | int vis[110]; 21 | int top; 22 | int x; 23 | 24 | memset(vis, 0, sizeof vis); 25 | 26 | stack q; 27 | q.push(s); 28 | vis[s] = 1; 29 | 30 | 31 | while(!q.empty()) { 32 | top = q.top(); 33 | q.pop(); 34 | for (int i = 0; i < g[top].size(); i++) { 35 | x = g[top][i].first; 36 | if(a[top][x] > 0 and vis[x] == 0) { 37 | q.push(x); 38 | vis[x] = 1; 39 | parent[x] = top; 40 | } 41 | 42 | } 43 | 44 | 45 | } 46 | return vis[t] == 1; 47 | 48 | 49 | 50 | } 51 | 52 | 53 | int main() 54 | { 55 | 56 | int x; 57 | int y; 58 | int w; 59 | int CS; 60 | int flow; 61 | int k; 62 | int c; 63 | int p; 64 | int ans; 65 | 66 | 67 | 68 | 69 | scanf("%d", &CS); 70 | 71 | for (int cs = 1; cs <= CS; cs++) { 72 | scanf("%d", &n); 73 | scanf("%d", &s); 74 | scanf("%d", &t); 75 | scanf("%d", &c); 76 | 77 | memset(a, 0, sizeof(a)); 78 | vector < vector < pair > > tg(n+2); 79 | 80 | swap(tg, g); 81 | ans = 0; 82 | 83 | for (int i = 0; i < c; i++) { 84 | scanf("%d", &x); 85 | scanf("%d", &y); 86 | scanf("%d", &w); 87 | 88 | a[x][y] += w; 89 | a[y][x] += w; 90 | g[x].push_back(make_pair(y, w)); 91 | g[y].push_back(make_pair(x, w)); 92 | } 93 | 94 | 95 | while(dfs()) { 96 | k = t; 97 | 98 | flow = INT_MAX; 99 | 100 | while(k != s) { 101 | p = parent[k]; 102 | flow = min(flow, a[k][p]); 103 | k = parent[k]; 104 | 105 | 106 | } 107 | 108 | 109 | k = t; 110 | 111 | 112 | while(k != s) { 113 | p = parent[k]; 114 | a[k][p] -= flow; 115 | a[p][k] -= flow; 116 | k = parent[k]; 117 | } 118 | 119 | ans += flow; 120 | } 121 | 122 | printf("Case %d: %d\n", cs, ans); 123 | } 124 | 125 | 126 | } 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /1159-Batman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | int t; 10 | int m; 11 | int n; 12 | int p; 13 | 14 | char a[100]; 15 | char b[100]; 16 | char c[100]; 17 | int maxi; 18 | 19 | int ans[100][100][100]; 20 | 21 | 22 | scanf("%d", &t); 23 | 24 | for (int cs = 1; cs <= t; cs++) { 25 | scanf("%s", a); 26 | scanf("%s", b); 27 | scanf("%s", c); 28 | 29 | m = strlen(a); 30 | n = strlen(b); 31 | p = strlen(c); 32 | 33 | for (int i = m; i >= 0; i--) { 34 | for (int j = n; j >= 0; j--) { 35 | for (int k = p; k >= 0; k--) { 36 | 37 | if(i == m or j == n or k == p) { 38 | ans[i][j][k] = 0; 39 | continue; 40 | } 41 | 42 | if(a[i] == b[j] and b[j] == c[k]) { 43 | ans[i][j][k] = 1 + ans[i+1][j+1][k+1]; 44 | continue; 45 | } 46 | 47 | maxi = -1; 48 | 49 | maxi = max(ans[i+1][j][k], maxi); 50 | maxi = max(ans[i][j+1][k], maxi); 51 | maxi = max(ans[i][j][k+1], maxi); 52 | maxi = max(ans[i+1][j+1][k], maxi); 53 | maxi = max(ans[i+1][j][k+1], maxi); 54 | maxi = max(ans[i][j+1][k+1], maxi); 55 | 56 | ans[i][j][k] = maxi; 57 | } 58 | 59 | } 60 | 61 | } 62 | 63 | 64 | 65 | printf("Case %d: %d\n", cs, ans[0][0][0]); 66 | 67 | } 68 | 69 | 70 | } 71 | 72 | -------------------------------------------------------------------------------- /1163-Bank-Robbery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int a[1000008]; 6 | 7 | unsigned long long calculate(unsigned long long y, int cs) 8 | { 9 | 10 | unsigned long long x; 11 | unsigned long long z; 12 | unsigned long long w; 13 | 14 | x = (10 * y) / 9; 15 | 16 | x--; 17 | 18 | printf("Case %d:", cs); 19 | 20 | if(x - (x / 10) == y) { 21 | printf(" %lld", x); 22 | } 23 | 24 | x++; 25 | 26 | 27 | if(x - (x / 10) == y) { 28 | printf(" %lld", x); 29 | } 30 | 31 | x++; 32 | 33 | 34 | if(x - (x / 10) == y) { 35 | printf(" %lld", x); 36 | } 37 | 38 | printf("\n"); 39 | } 40 | 41 | int main() 42 | { 43 | 44 | int t; 45 | unsigned long long x; 46 | unsigned long long y; 47 | 48 | scanf("%d", &t); 49 | 50 | for (int cs = 1; cs <= t; cs++) { 51 | scanf("%lld", &y); 52 | calculate(y, cs); 53 | } 54 | 55 | 56 | 57 | } 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /1164-Horrible-Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define size 100009 5 | using namespace std; 6 | 7 | long long tree[4 * size]; 8 | long long a[size]; 9 | long long lazy[4 * size]; 10 | 11 | 12 | 13 | long long update(int a, int b, int i, int j, int node, int val) 14 | { 15 | // cout << "->"< j) { 31 | // cout << " " << "0"<< endl; 32 | return 0; 33 | } 34 | 35 | 36 | if(a >= i and b <= j) { 37 | tree[node] += val * ((b - a) + 1); 38 | 39 | if(a != b) { 40 | lazy[node * 2] += val; 41 | lazy[node * 2 + 1] += val; 42 | } 43 | 44 | // cout << tree[node] << endl; 45 | 46 | return 0; 47 | } 48 | 49 | update(a, (a+b)/2, i, j, node * 2, val); 50 | update((a+b)/2 + 1,b, i, j, node * 2 + 1, val); 51 | 52 | tree[node] = tree[node * 2] + tree[node * 2 + 1]; 53 | // cout << tree[node] << endl; 54 | 55 | 56 | } 57 | 58 | long long query(int i, int j,int a, int b,int node) 59 | { 60 | long long x; 61 | long long y; 62 | 63 | 64 | if(b < a or a > j or b < i) { 65 | // cout <<"invalid -> "<< i << " " << j << " "; 66 | 67 | // cout << " 0"<= a and j <= b) { 84 | // cout <<" perfect fit -> "<< i << " " << j << " "; 85 | 86 | // cout << tree[node] << endl; 87 | return tree[node]; 88 | } 89 | 90 | x = query(i, (i+j)/2,a,b, node * 2); 91 | y = query((i+j)/2+1, j,a, b,node * 2 + 1); 92 | // cout <<"end-> "<< i << " " << j << " "; 93 | 94 | // cout << x + y << endl; 95 | 96 | return x + y; 97 | 98 | } 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | long long create_tree(int i, int j, int node) 108 | { 109 | 110 | long long x; 111 | long long y; 112 | 113 | if(i == j) { 114 | tree[node] = a[i]; 115 | return a[i]; 116 | } 117 | 118 | x = create_tree(i, (i+j)/2, node * 2); 119 | y = create_tree((i+j)/2 + 1, j, node * 2 + 1); 120 | 121 | tree[node] = x + y; 122 | 123 | return x + y; 124 | 125 | } 126 | 127 | 128 | 129 | int main() 130 | { 131 | 132 | int n; 133 | int x; 134 | int y; 135 | int ch; 136 | int val; 137 | int t; 138 | int q; 139 | 140 | 141 | scanf("%d", &t); 142 | 143 | for (int cs = 1; cs <= t; cs++) { 144 | scanf("%d", &n); 145 | scanf("%d", &q); 146 | 147 | printf("Case %d:\n", cs); 148 | 149 | 150 | memset(a, 0, sizeof(a)); 151 | memset(tree, 0, sizeof(tree)); 152 | 153 | memset(lazy, 0, sizeof(lazy)); 154 | 155 | for (int i = 0; i < q; i++) { 156 | scanf("%d", &ch); 157 | scanf("%d", &x); 158 | scanf("%d", &y); 159 | 160 | if(ch) { 161 | printf("%lld\n", query(0, n-1, x, y, 1)); 162 | } 163 | 164 | else { 165 | scanf("%d", &val); 166 | update(0, n-1,x, y, 1, val); 167 | } 168 | 169 | } 170 | 171 | } 172 | 173 | 174 | } 175 | 176 | -------------------------------------------------------------------------------- /1166-Old-Sorting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | int a[1000]; 10 | int ans; 11 | int t; 12 | 13 | scanf("%d", &t); 14 | for (int cs = 1; cs <= t; cs++) { 15 | scanf("%d", &n); 16 | ans = 0; 17 | 18 | for (int i = 1; i <= n; i++) { 19 | scanf("%d", a + i); 20 | } 21 | 22 | for (int i = 1; i <= n; i++) { 23 | if(a[i] != i) { 24 | 25 | for (int j = i + 1; j <= n; j++) { 26 | if(a[j] == i) { 27 | swap(a[j], a[i]); 28 | ans++; 29 | break; 30 | } 31 | 32 | } 33 | } 34 | 35 | } 36 | 37 | printf("Case %d: %d\n", cs, ans); 38 | 39 | } 40 | 41 | 42 | } 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /1175-Jane-and-the-Frost-Giants.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | queue < pair > q; 8 | queue < pair > fire; 9 | char a[210][210]; 10 | int vis[210][210]; 11 | int ii; 12 | int jj; 13 | int r; 14 | int c; 15 | int suc; 16 | int count; 17 | 18 | int fireUp() 19 | { 20 | int l; 21 | pair temp; 22 | l = fire.size(); 23 | 24 | for (int i = 0; i < l; i++) { 25 | temp = fire.front(); 26 | fire.pop(); 27 | 28 | ii = temp.first; 29 | jj = temp.second; 30 | 31 | if(ii + 1 < r and a[ii+1][jj] == '.') { 32 | fire.push(make_pair(ii+1, jj)); 33 | a[ii+1][jj] = 'F'; 34 | 35 | } 36 | 37 | if(jj + 1 < c and a[ii][jj + 1] == '.') { 38 | fire.push(make_pair(ii, jj+1)); 39 | a[ii][jj + 1] = 'F'; 40 | } 41 | 42 | if(ii - 1 >= 0 and a[ii - 1][jj] == '.') { 43 | fire.push(make_pair(ii-1, jj)); 44 | a[ii - 1][jj] = 'F'; 45 | } 46 | 47 | if(jj - 1 >= 0 and a[ii][jj-1] == '.') { 48 | fire.push(make_pair(ii, jj - 1)); 49 | a[ii][jj - 1] = 'F'; 50 | } 51 | 52 | 53 | 54 | 55 | 56 | } 57 | } 58 | 59 | int nextState() 60 | { 61 | int l; 62 | pair temp; 63 | 64 | while(!q.empty()) { 65 | count++; 66 | 67 | l = q.size(); 68 | for (int i = 0; i < l; i++) { 69 | 70 | 71 | temp = q.front(); 72 | q.pop(); 73 | 74 | ii = temp.first; 75 | jj = temp.second; 76 | 77 | if(a[ii][jj] == 'F') { 78 | continue; 79 | } 80 | 81 | if(ii + 1 >= r) { 82 | suc = 1; 83 | break; 84 | } 85 | 86 | else { 87 | if(a[ii+1][jj] == '.' and !vis[ii+1][jj]) { 88 | q.push(make_pair(ii+1, jj)); 89 | vis[ii+1][jj] = 1; 90 | } 91 | 92 | } 93 | 94 | if(jj + 1 >= c) { 95 | suc = 1; 96 | break; 97 | } 98 | 99 | else { 100 | if(a[ii][jj + 1] == '.' and !vis[ii][jj+1]) { 101 | q.push(make_pair(ii, jj + 1)); 102 | vis[ii][jj+1] = 1; 103 | } 104 | 105 | } 106 | 107 | if(ii - 1 < 0) { 108 | suc = 1; 109 | break; 110 | } 111 | 112 | else { 113 | if(a[ii - 1][jj] == '.' and !vis[ii-1][jj]) { 114 | q.push(make_pair(ii -1, jj)); 115 | vis[ii-1][jj] = 1; 116 | } 117 | 118 | } 119 | 120 | if(jj - 1 < 0) { 121 | suc = 1; 122 | break; 123 | } 124 | 125 | else { 126 | if(a[ii][jj - 1] == '.' and !vis[ii][jj-1]) { 127 | q.push(make_pair(ii, jj - 1)); 128 | vis[ii][jj-1] = 1; 129 | } 130 | 131 | } 132 | 133 | } 134 | 135 | 136 | if(suc == 1 or suc == 2) { 137 | break; 138 | } 139 | fireUp(); 140 | 141 | } 142 | 143 | if(suc == 1) { 144 | return count; 145 | } 146 | 147 | else { 148 | return -1; 149 | } 150 | 151 | 152 | } 153 | 154 | 155 | int readinput() 156 | { 157 | cin >> r; 158 | cin >> c; 159 | 160 | for (int i = 0; i < r; i++) { 161 | for (int j = 0; j < c; j++) { 162 | cin >> a[i][j]; 163 | 164 | if(a[i][j] == 'F') { 165 | fire.push(make_pair(i, j)); 166 | 167 | } 168 | 169 | if(a[i][j] == 'J') { 170 | ii = i; 171 | jj = j; 172 | a[i][j] = '.'; 173 | } 174 | } 175 | } 176 | 177 | q.push(make_pair(ii, jj)); 178 | } 179 | 180 | int main() 181 | { 182 | 183 | int t; 184 | 185 | cin >> t; 186 | 187 | for (int cs = 1; cs <= t; cs++) { 188 | 189 | queue < pair > tq; 190 | queue < pair > tfire; 191 | int x; 192 | 193 | swap(q, tq); 194 | swap(fire, tfire); 195 | memset(vis,0, sizeof(vis)); 196 | 197 | readinput(); 198 | 199 | pair temp; 200 | 201 | temp = fire.front(); 202 | 203 | 204 | 205 | suc = 0; 206 | count = 0; 207 | x = nextState(); 208 | 209 | if(x != -1) { 210 | printf("Case %d: %d\n", cs, x); 211 | } 212 | 213 | else { 214 | printf("Case %d: IMPOSSIBLE\n", cs); 215 | } 216 | 217 | } 218 | 219 | 220 | } 221 | 222 | -------------------------------------------------------------------------------- /1176-Getting-a-T-shirt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | 11 | vector < vector > g; 12 | 13 | int a[800][800]; 14 | int parent[800]; 15 | int n; 16 | int m; 17 | int s; 18 | int colors; 19 | int destination; 20 | 21 | int dfs() 22 | { 23 | 24 | int vis[800]; 25 | int top; 26 | int x; 27 | 28 | memset(vis, 0, sizeof vis); 29 | 30 | stack q; 31 | q.push(s); 32 | vis[s] = 1; 33 | 34 | 35 | while(!q.empty()) { 36 | top = q.top(); 37 | q.pop(); 38 | for (int i = 0; i < g[top].size(); i++) { 39 | x = g[top][i]; 40 | if(a[top][x] > 0 and vis[x] == 0) { 41 | q.push(x); 42 | vis[x] = 1; 43 | parent[x] = top; 44 | } 45 | 46 | } 47 | 48 | 49 | } 50 | if(vis[destination] == 1) { 51 | return 1; 52 | } 53 | 54 | else { 55 | return 0; 56 | } 57 | 58 | 59 | 60 | } 61 | 62 | 63 | int main() 64 | { 65 | 66 | int t; 67 | int k; 68 | 69 | string c; 70 | 71 | 72 | map mp; 73 | 74 | mp["XXL"] = 1; 75 | mp["XL"] = 2; 76 | mp["L"] = 3; 77 | mp["M"] = 4; 78 | mp["S"] = 5; 79 | mp["XS"] = 6; 80 | 81 | 82 | cin >> t; 83 | 84 | for (int cs = 1; cs <= t; cs++) { 85 | 86 | int ans; 87 | int p; 88 | int flow; 89 | 90 | 91 | ans = 0; 92 | flow = 0; 93 | 94 | 95 | cin >> n; 96 | cin >> m; 97 | 98 | s = m + 6 + 1; 99 | destination = s + 1; 100 | 101 | vector < vector > tv(800); 102 | memset(a, 0, sizeof a); 103 | swap(tv, g); 104 | 105 | for (int i = 1; i <= m; i++) { 106 | a[s][i] = 1; 107 | g[s].push_back(i); 108 | g[i].push_back(s); 109 | 110 | cin >> c; 111 | k = mp[c] + m; 112 | a[i][k] = 1; 113 | g[i].push_back(k); 114 | g[k].push_back(i); 115 | 116 | cin >> c; 117 | k = mp[c] + m; 118 | a[i][k] = 1; 119 | g[i].push_back(k); 120 | g[k].push_back(i); 121 | 122 | } 123 | 124 | for (int i = 1; i <= 6; i++) { 125 | k = i + m; 126 | a[k][destination] = n; 127 | g[k].push_back(destination); 128 | g[destination].push_back(k); 129 | } 130 | 131 | 132 | while(dfs()) { 133 | 134 | k = destination; 135 | flow = INT_MAX; 136 | 137 | 138 | while(k != s) { 139 | p = parent[k]; 140 | flow = min(flow, a[p][k]); 141 | k = parent[k]; 142 | } 143 | 144 | k = destination; 145 | 146 | while(k != s) { 147 | p = parent[k]; 148 | a[k][p] += flow; 149 | a[p][k] -= flow; 150 | k = parent[k]; 151 | } 152 | 153 | ans += flow; 154 | } 155 | 156 | if(ans == m) { 157 | printf("Case %d: YES\n", cs); 158 | } 159 | 160 | else { 161 | printf("Case %d: NO\n", cs); 162 | 163 | } 164 | 165 | 166 | } 167 | 168 | } 169 | -------------------------------------------------------------------------------- /1179-Josephus-Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int a[10000]; 5 | long long k; 6 | int explore(int n) 7 | { 8 | 9 | if(n == 1) { 10 | return 1; 11 | } 12 | 13 | return (explore(n-1) + k - 1) % n + 1; 14 | 15 | } 16 | int main() 17 | { 18 | int t; 19 | int dp; 20 | int n; 21 | scanf("%d", &t); 22 | 23 | for (int cs = 1; cs <= t;cs++) { 24 | scanf("%d", &n); 25 | scanf("%lld", &k); 26 | 27 | printf("Case %d: %d\n", cs, explore(n)); 28 | } 29 | 30 | } 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /1182-Parity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | 6 | int t; 7 | 8 | scanf("%d", &t); 9 | 10 | int n; 11 | int count; 12 | 13 | for (int cs = 1; cs <= t; cs++) { 14 | scanf("%d", &n); 15 | count = 0; 16 | while(n) { 17 | count += n % 2; 18 | n = n / 2; 19 | } 20 | 21 | if(count % 2) { 22 | printf("Case %d: odd\n", cs); 23 | } 24 | 25 | else { 26 | printf("Case %d: even\n", cs); 27 | 28 | } 29 | 30 | } 31 | 32 | 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /1184-Marriage-Media.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int n; 8 | int m; 9 | 10 | 11 | 12 | struct node 13 | { 14 | int age; 15 | int height; 16 | int divorce; 17 | 18 | }; 19 | 20 | node men[60]; 21 | node women[60]; 22 | int seen[60]; 23 | int husband[60]; 24 | 25 | int ok(int i, int j) 26 | { 27 | 28 | if((men[i].divorce == women[j].divorce) and (abs(men[i].height - women[j].height) <= 12) and (abs(men[i].age - women[j].age) <= 5)) { 29 | return true; 30 | } 31 | 32 | return false; 33 | 34 | } 35 | 36 | int bpm(int manId) 37 | { 38 | 39 | for (int i = 0; i < m; i++) { 40 | if(ok(manId, i)) { 41 | if(seen[i]) 42 | continue; 43 | 44 | seen[i] = 1; 45 | 46 | if(husband[i] < 0 or bpm(husband[i])) { 47 | husband[i] = manId; 48 | return true; 49 | } 50 | 51 | } 52 | 53 | } 54 | 55 | return false; 56 | 57 | } 58 | 59 | 60 | 61 | int main() 62 | { 63 | 64 | int t; 65 | int count; 66 | scanf("%d", &t); 67 | 68 | for (int cs = 1; cs <= t; cs++) { 69 | scanf("%d", &n); 70 | scanf("%d", &m); 71 | 72 | for (int i = 0; i < n; i++) { 73 | scanf("%d", &men[i].height); 74 | scanf("%d", &men[i].age); 75 | scanf("%d", &men[i].divorce); 76 | } 77 | 78 | for (int i = 0; i < m; i++) { 79 | scanf("%d", &women[i].height); 80 | scanf("%d", &women[i].age); 81 | scanf("%d", &women[i].divorce); 82 | } 83 | 84 | memset(husband, -1, sizeof husband); 85 | count = 0; 86 | 87 | for (int i = 0; i < n; i++) { 88 | memset(seen, 0, sizeof seen); 89 | if(bpm(i)) { 90 | count++; 91 | } 92 | 93 | } 94 | 95 | printf("Case %d: %d\n", cs, count); 96 | 97 | } 98 | 99 | } 100 | 101 | -------------------------------------------------------------------------------- /1186-Incredible-Chess.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | int n; 10 | int t; 11 | int ans; 12 | 13 | 14 | int a[106]; 15 | int b[106]; 16 | 17 | scanf("%d", &t); 18 | 19 | for (int cs = 1; cs <= t; cs++) { 20 | scanf("%d", &n); 21 | ans = 0; 22 | 23 | for (int i = 0; i < n; i++) { 24 | scanf("%d", &a[i]); 25 | } 26 | 27 | for (int i = 0; i < n; i++) { 28 | scanf("%d", &b[i]); 29 | } 30 | 31 | for (int i = 0; i < n; i++) { 32 | ans = ans ^ (b[i] - a[i] - 1); 33 | } 34 | 35 | if(ans) { 36 | printf("Case %d: white wins\n", cs); 37 | } 38 | 39 | else { 40 | printf("Case %d: black wins\n", cs); 41 | 42 | } 43 | 44 | } 45 | 46 | } 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /1188-Fast-Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct node{ 9 | int l; 10 | int r; 11 | int i; 12 | }; 13 | 14 | int ans; 15 | struct node q[50005]; 16 | int a[100007]; 17 | int mod; 18 | int counts[100007]; 19 | int fin[50007]; 20 | 21 | bool cmp(node x, node y) 22 | { 23 | if((x.l) / mod == (y.l / mod)) { 24 | return x.r < y.r; 25 | } 26 | 27 | return x.l / mod < y.l / mod; 28 | 29 | } 30 | 31 | 32 | int update(int l) 33 | { 34 | counts[a[l]]++; 35 | 36 | 37 | 38 | if(counts[a[l]] == 1) { 39 | ans++; 40 | } 41 | 42 | } 43 | 44 | int remove(int l) 45 | { 46 | 47 | counts[a[l]]--; 48 | 49 | if(counts[a[l]] == 0) { 50 | ans--; 51 | } 52 | 53 | } 54 | int main() 55 | { 56 | 57 | int t; 58 | int n; 59 | int m; 60 | int x; 61 | int y; 62 | 63 | scanf("%d", &t); 64 | for (int cs = 1; cs <= t; cs++) { 65 | scanf("%d",&n); 66 | scanf("%d",&m); 67 | mod = sqrt(n); 68 | 69 | for (int i = 0; i < n; i++) { 70 | scanf("%d", a + i); 71 | } 72 | 73 | for (int i = 0; i < m; i++) { 74 | scanf("%d", &x); 75 | scanf("%d", &y); 76 | x--; 77 | y--; 78 | q[i].l = x; 79 | q[i].r = y; 80 | q[i].i = i; 81 | } 82 | 83 | sort(q, q + m, cmp); 84 | ans = 0; 85 | 86 | int currentL; 87 | int currentR; 88 | 89 | currentL = 0; 90 | currentR = 0; 91 | 92 | memset(counts, 0, sizeof(counts)); 93 | 94 | for (int i = 0; i < m; i++) { 95 | x = q[i].l; 96 | y = q[i].r; 97 | 98 | while(currentL < x) { 99 | remove(currentL); 100 | currentL++; 101 | } 102 | 103 | 104 | while(currentL > x) { 105 | update(currentL - 1); 106 | currentL--; 107 | } 108 | 109 | while(currentR <= y) { 110 | update(currentR); 111 | currentR++; 112 | } 113 | 114 | while(currentR > y+1) { 115 | remove(currentR - 1); 116 | currentR--; 117 | } 118 | 119 | fin[q[i].i] = ans; 120 | 121 | } 122 | 123 | printf("Case %d:\n", cs); 124 | for (int i = 0; i < m; i++) { 125 | printf("%d\n", fin[i]); 126 | } 127 | 128 | 129 | 130 | 131 | } 132 | 133 | 134 | } 135 | 136 | -------------------------------------------------------------------------------- /1189-Sum-of-Factorials.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | int t; 10 | unsigned long long fact[22]; 11 | long long x; 12 | 13 | fact[0] = 1; 14 | 15 | for (int i = 1; i <= 20; i++) { 16 | fact[i] = fact[i-1] * i; 17 | } 18 | 19 | scanf("%d", &t); 20 | 21 | for (int cs = 1; cs <= t; cs++) { 22 | scanf("%lld", &x); 23 | stack q; 24 | int i = 20; 25 | while(i >= 0) { 26 | if(fact[i] <= x) { 27 | x -= fact[i]; 28 | q.push(i); 29 | } 30 | 31 | i--; 32 | } 33 | 34 | if(x == 0) { 35 | printf("Case %d: ", cs); 36 | while(q.size() != 1) { 37 | printf("%d!+", q.top()); 38 | q.pop(); 39 | 40 | } 41 | 42 | printf("%d!\n", q.top()); 43 | q.pop(); 44 | 45 | } 46 | 47 | else { 48 | printf("Case %d: impossible\n", cs); 49 | } 50 | 51 | } 52 | 53 | 54 | } 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /1198-Karate-Competetion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | bool comp(int a, int b) 9 | { 10 | 11 | if(a > b ) { 12 | return 1; 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | int main() 19 | { 20 | 21 | int t; 22 | int n; 23 | int x; 24 | int ans[100][100]; 25 | scanf("%d", &t); 26 | 27 | for (int cs = 1; cs <= t; cs++) { 28 | 29 | scanf("%d", &n); 30 | vector a; 31 | vector b; 32 | 33 | for (int i = 0; i < n; i++) { 34 | scanf("%d", &x); 35 | a.push_back(x); 36 | } 37 | 38 | for (int i = 0; i < n; i++) { 39 | scanf("%d", &x); 40 | b.push_back(x); 41 | } 42 | 43 | sort(a.begin(), a.end(), comp); 44 | sort(b.begin(), b.end(), comp); 45 | 46 | for (int i = n; i >= 0; i--) { 47 | for (int j = n; j >= 0; j--) { 48 | 49 | if(i == n or j == n) { 50 | ans[i][j] = 0; 51 | continue; 52 | } 53 | 54 | if(a[i] > b[j]) { 55 | ans[i][j] = 2 + ans[i+1][j+1]; 56 | continue; 57 | } 58 | 59 | if(a[i] == b[j]) { 60 | ans[i][j] = max(ans[i][j+1], 1 + ans[i+1][j+1]); 61 | continue; 62 | } 63 | 64 | if(a[i] < b[j]) { 65 | ans[i][j] = max(ans[i+1][j], ans[i][j+1]); 66 | } 67 | 68 | 69 | 70 | 71 | } 72 | 73 | 74 | } 75 | 76 | printf("Case %d: %d\n", cs, ans[0][0]); 77 | 78 | 79 | } 80 | 81 | 82 | } 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /1200-Thief.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n; 8 | int p[110]; 9 | int w[110]; 10 | int dp[10005][105]; 11 | int W; 12 | 13 | int explore(int sum, int i) 14 | { 15 | int x; 16 | int y; 17 | int z; 18 | 19 | x = y = z = 0; 20 | 21 | if(i == n) { 22 | return 0; 23 | } 24 | 25 | if(dp[sum][i] != -1) { 26 | return dp[sum][i]; 27 | } 28 | 29 | 30 | if(sum + w[i] <= W) 31 | x = p[i] + explore( sum + w[i], i+1); 32 | 33 | if(sum + w[i] <= W) 34 | y = p[i] + explore(sum + w[i], i); 35 | 36 | z = explore(sum, i+1); 37 | 38 | return dp[sum][i] = max(x, max(y, z)); 39 | } 40 | 41 | int main() 42 | { 43 | 44 | int t; 45 | int x; 46 | 47 | scanf("%d", &t); 48 | 49 | for (int cs = 1; cs <= t; cs++) { 50 | 51 | scanf("%d", &n); 52 | scanf("%d", &W); 53 | 54 | for (int i = 0; i < n; i++) { 55 | scanf("%d", &p[i]); 56 | scanf("%d", &x); 57 | scanf("%d", &w[i]); 58 | 59 | W = W - w[i] * x; 60 | } 61 | 62 | if(W < 0) { 63 | printf("Case %d: Impossible\n", cs); 64 | continue; 65 | } 66 | memset(dp, -1, sizeof(dp)); 67 | 68 | printf("Case %d: %d\n", cs, explore(0, 0)); 69 | 70 | 71 | } 72 | 73 | 74 | } 75 | 76 | -------------------------------------------------------------------------------- /1212-Double-Ended-Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | ios_base::sync_with_stdio(false); 10 | 11 | int t; 12 | string x; 13 | int n; 14 | int size; 15 | int temp; 16 | cin >> t; 17 | 18 | for (int cs = 1;cs <= t; cs++) { 19 | cin >> size; 20 | cin >> n; 21 | 22 | deque q; 23 | cout << "Case " << cs << ":\n"; 24 | for (int i = 0; i < n; i++) { 25 | cin >> x; 26 | 27 | if(x == "pushLeft" or x == "pushRight") { 28 | cin >> temp; 29 | if(q.size() == size) { 30 | cout << "The queue is full\n"; 31 | } 32 | 33 | else { 34 | if(x == "pushLeft") { 35 | cout << "Pushed in left: " << temp << "\n"; 36 | q.push_front(temp); 37 | } 38 | 39 | 40 | if(x == "pushRight") { 41 | cout << "Pushed in right: " << temp << "\n"; 42 | q.push_back(temp); 43 | } 44 | } 45 | 46 | } 47 | 48 | if(x == "popLeft" or x == "popRight") { 49 | if(q.size() == 0) { 50 | cout << "The queue is empty\n"; 51 | } 52 | 53 | else { 54 | if(x == "popLeft") { 55 | cout << "Popped from left: " << q.front() << "\n"; 56 | q.pop_front(); 57 | } 58 | 59 | if(x == "popRight") { 60 | cout << "Popped from right: " << q.back() << "\n"; 61 | q.pop_back(); 62 | } 63 | 64 | } 65 | 66 | } 67 | 68 | 69 | } 70 | 71 | 72 | 73 | } 74 | 75 | 76 | } -------------------------------------------------------------------------------- /1215-Finding-LCM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | #define sz 1000100 7 | #define primecnt 78504 8 | 9 | 10 | int sv[1000105]; 11 | int cnt[primecnt + 100]; 12 | 13 | vector prime; 14 | 15 | 16 | long long __lcm(long long a, long long b) 17 | { 18 | long long ans; 19 | 20 | ans = a * b; 21 | ans = ans / __gcd(a, b); 22 | 23 | return ans; 24 | 25 | } 26 | int main() 27 | { 28 | 29 | int t; 30 | long long a; 31 | long long b; 32 | long long lcm; 33 | long long c; 34 | long long temp; 35 | long long ans; 36 | long long mul; 37 | int ok; 38 | for (int i = 3; i * i <= sz; i += 2) { 39 | if(!sv[i]) { 40 | for (int j = i * i; j <= sz; j += 2 * i) { 41 | sv[j] = 1; 42 | } 43 | } 44 | } 45 | 46 | prime.push_back(2); 47 | for (int i = 3; i <= sz; i += 2) { 48 | if(sv[i] == 0) { 49 | //cout << i << endl; 50 | prime.push_back(i); 51 | } 52 | } 53 | 54 | //cout << prime.size() << endl; 55 | scanf("%d", &t); 56 | 57 | for (int cs = 1; cs <= t; cs++) { 58 | scanf("%lld", &a); 59 | scanf("%lld", &b); 60 | scanf("%lld", &lcm); 61 | 62 | 63 | temp = 1; 64 | 65 | 66 | 67 | for (int i = 0; i < prime.size(); i++) { 68 | mul = prime[i]; 69 | cnt[i] = 1; 70 | while(a % mul == 0 or b % mul == 0) { 71 | temp = temp * prime[i]; 72 | cnt[i] = cnt[i] * prime[i]; 73 | mul = mul * prime[i]; 74 | } 75 | 76 | if(cnt[i] != 1) { 77 | //cout << prime[i] << " " << cnt[i] << endl; 78 | } 79 | 80 | } 81 | 82 | c = lcm / temp; 83 | 84 | ans = 1; 85 | 86 | 87 | if(c == 0) { 88 | printf("Case %d: impossible\n", cs); 89 | continue; 90 | } 91 | for (int i = 0; i < prime.size(); i++) { 92 | mul = prime[i]; 93 | ok = 0; 94 | while(c % mul == 0) { 95 | cnt[i] = cnt[i] * prime[i]; 96 | ok = 1; 97 | c = c / mul; 98 | 99 | } 100 | 101 | if(ok) { 102 | ans = ans * cnt[i]; 103 | } 104 | } 105 | 106 | ans = ans * c; 107 | 108 | if(__lcm(a, __lcm(b, ans)) != lcm) { 109 | printf("Case %d: impossible\n", cs); 110 | continue; 111 | } 112 | 113 | 114 | printf("Case %d: %lld\n", cs, ans); 115 | 116 | 117 | } 118 | 119 | } 120 | 121 | -------------------------------------------------------------------------------- /1217-Neighbor-House-II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define N 1000 5 | using namespace std; 6 | 7 | int a[N + 5]; 8 | long long dp[N+5][2]; 9 | 10 | int n; 11 | 12 | long long explore(int i, int one) 13 | { 14 | 15 | long long x; 16 | long long y; 17 | 18 | if(i >= n) { 19 | return 0; 20 | } 21 | 22 | if(i == n-1 and one == 1) { 23 | return 0; 24 | } 25 | 26 | if(dp[i][one] != -1) 27 | return dp[i][one]; 28 | 29 | x = explore(i+1, one); 30 | 31 | if(i == 0) { 32 | one = 1; 33 | } 34 | 35 | y = explore(i+2, one) + a[i]; 36 | 37 | return dp[i][one] = max(x, y); 38 | 39 | } 40 | 41 | 42 | 43 | int main() 44 | { 45 | 46 | int t; 47 | 48 | scanf("%d", &t); 49 | 50 | for (int cs = 1; cs <= t; cs++) { 51 | scanf("%d", &n); 52 | memset(dp, -1, sizeof dp); 53 | 54 | for (int i = 0; i < n; i++) { 55 | scanf("%d", a + i); 56 | } 57 | 58 | printf("Case %d: %lld\n", cs, explore(0, 0)); 59 | } 60 | 61 | 62 | 63 | } 64 | 65 | 66 | -------------------------------------------------------------------------------- /1225-Palindromic-Numbers-II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool pali(int x) 6 | { 7 | int ans; 8 | int temp; 9 | 10 | temp = x; 11 | ans = 0; 12 | 13 | while(x) { 14 | ans = ans * 10 + x % 10; 15 | x = x / 10; 16 | } 17 | 18 | return ans == temp; 19 | } 20 | 21 | 22 | int main() 23 | { 24 | 25 | int x; 26 | int t; 27 | scanf("%d", &t); 28 | 29 | for (int cs = 1; cs <= t; cs++) { 30 | scanf("%d", &x); 31 | pali(x) ? printf("Case %d: Yes\n", cs) : printf("Case %d: No\n", cs); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /1227-Boiled-Eggs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n; 8 | int W; 9 | int maxi; 10 | 11 | int dp[33][33][33]; 12 | int w[33]; 13 | 14 | int explore(int i, int j, int sum) 15 | { 16 | int x; 17 | int y; 18 | 19 | x = 0; 20 | y = 0; 21 | 22 | if(i == n) { 23 | return 0; 24 | } 25 | 26 | if(j == maxi) { 27 | return 0; 28 | } 29 | 30 | if(dp[i][j][sum] != -1) { 31 | return dp[i][j][sum]; 32 | } 33 | 34 | if(sum + w[i] <= W) { 35 | x = 1 + explore(i+1, j+1, sum + w[i]); 36 | } 37 | 38 | y = explore(i+1, j, sum); 39 | 40 | return dp[i][j][sum] = max(x, y); 41 | 42 | } 43 | 44 | int main() 45 | { 46 | 47 | int t; 48 | scanf("%d", &t); 49 | 50 | for (int cs = 1; cs <= t; cs++) { 51 | scanf("%d", &n); 52 | scanf("%d", &maxi); 53 | scanf("%d", &W); 54 | memset(dp, -1, sizeof dp); 55 | 56 | for (int i = 0; i < n; i++) { 57 | scanf("%d", &w[i]); 58 | } 59 | 60 | printf("Case %d: %d\n", cs, explore(0, 0, 0)); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /1231-Coin-Change-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int k; 7 | int n; 8 | int a[100]; 9 | int c[100]; 10 | int dp[100][1008]; 11 | 12 | 13 | long long explore(int i, int sum) 14 | { 15 | long long ans; 16 | int mod; 17 | ans = 0; 18 | 19 | mod = 1e8 + 7; 20 | 21 | if(sum == k) { 22 | return 1; 23 | } 24 | 25 | if(i == n) { 26 | return 0; 27 | } 28 | 29 | if(dp[i][sum] != -1) { 30 | return dp[i][sum]; 31 | } 32 | 33 | for (int j = 0; j <= c[i] and (j * a[i]) + sum <= k; j++) { 34 | ans += explore(i+1, sum + j * a[i]); 35 | ans = ans % mod; 36 | } 37 | 38 | 39 | return dp[i][sum] = ans; 40 | 41 | } 42 | 43 | int main() 44 | { 45 | 46 | int t; 47 | int mod; 48 | 49 | mod = 1e8 + 7; 50 | scanf("%d", &t); 51 | 52 | for (int cs = 1; cs <= t; cs++) { 53 | scanf("%d", &n); 54 | scanf("%d", &k); 55 | 56 | 57 | for (int i = 0; i < n; i++) { 58 | scanf("%d", a + i); 59 | } 60 | 61 | for (int i = 0; i < n; i++) { 62 | scanf("%d", c + i); 63 | } 64 | 65 | memset(dp, -1, sizeof dp); 66 | 67 | printf("Case %d: %lld\n", cs, explore(0, 0)); 68 | 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /1232-Coin-Exchange-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int k; 7 | int n; 8 | int a[109]; 9 | int dp[10009]; 10 | 11 | int main() 12 | { 13 | 14 | int t; 15 | int mod; 16 | 17 | mod = 1e8 + 7; 18 | scanf("%d", &t); 19 | 20 | for (int cs = 1; cs <= t; cs++) { 21 | scanf("%d", &n); 22 | scanf("%d", &k); 23 | 24 | 25 | for (int i = 0; i < n; i++) { 26 | scanf("%d", a + i); 27 | } 28 | 29 | memset(dp, 0, sizeof dp); 30 | dp[0] = 1; 31 | 32 | for (int i = 0; i < n; i++) { 33 | for (int j = a[i]; j <= k; j++) { 34 | dp[j] += dp[j - a[i]]; 35 | dp[j] = dp[j] % mod; 36 | } 37 | 38 | } 39 | 40 | 41 | printf("Case %d: %d\n", cs, dp[k]); 42 | 43 | } 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /1234-Harmonic-Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define gama 0.57721566490 6 | double a[1000005]; 7 | 8 | int main() 9 | { 10 | 11 | int t; 12 | double ans; 13 | double ans2; 14 | int x; 15 | int n; 16 | double k; 17 | 18 | ans = 0; 19 | for (int i = 1; i <= 1000000; i++) { 20 | ans += 1.0 / i; 21 | a[i] = ans; 22 | 23 | } 24 | 25 | scanf("%d", &t); 26 | 27 | 28 | for (int cs = 1; cs <= t; cs++) { 29 | scanf("%d", &x); 30 | if(x <= 1000000) { 31 | 32 | printf("Case %d: %.9lf\n", cs, a[x]); 33 | } 34 | 35 | else { 36 | k = x + .5; 37 | ans2 = log(k) + gama; 38 | printf("Case %d: %.9lf\n", cs, ans2); 39 | } 40 | 41 | } 42 | 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /1241-Pinocchio.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | int t; 11 | int last; 12 | int a[100]; 13 | int ans; 14 | int lies; 15 | int n; 16 | int diff; 17 | 18 | scanf("%d", &t); 19 | for (int cs = 1; cs <= t; cs++) { 20 | scanf("%d", &n); 21 | for (int i = 0; i < n; i++ ) { 22 | scanf("%d", a + i); 23 | } 24 | 25 | last = 2; 26 | ans = 0; 27 | 28 | for (int i = 0; i < n; i++) { 29 | diff = a[i] - last; 30 | if(diff) { 31 | lies = ceil((float) diff / 5.0); 32 | if(lies == 0) 33 | ans++; 34 | else 35 | ans += lies; 36 | 37 | last = a[i]; 38 | } 39 | } 40 | 41 | printf("Case %d: %d\n", cs, ans); 42 | } 43 | } -------------------------------------------------------------------------------- /1249-Chocolate-Thief.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n; 10 | int t; 11 | int x; 12 | int y; 13 | int w; 14 | 15 | int sum; 16 | 17 | scanf("%d", &t); 18 | 19 | for (int cs = 1; cs <= t; cs++) { 20 | vector arr; 21 | char names[200][1000]; 22 | char temp[1000]; 23 | 24 | scanf("%d", &n); 25 | 26 | sum = 0; 27 | 28 | for(int i = 0; i < n; i++) { 29 | scanf("%s", temp); 30 | 31 | strcpy(names[i], temp); 32 | 33 | scanf("%d", &x); 34 | scanf("%d", &y); 35 | scanf("%d", &w); 36 | 37 | sum += x * y * w; 38 | arr.push_back(x * y * w); 39 | 40 | } 41 | 42 | int ans = 0; 43 | int mini = arr[0]; 44 | int pie; 45 | int ok = 0; 46 | char who[1000]; 47 | char from[1000]; 48 | pie = sum / n; 49 | 50 | for (int i = 0; i < n; i++) { 51 | if(arr[i] > pie) { 52 | strcpy(who, names[i]); 53 | ok = 1; 54 | } 55 | 56 | if(arr[i] < pie) { 57 | strcpy(from, names[i]); 58 | ok = 1; 59 | } 60 | } 61 | 62 | if(ok) 63 | printf("Case %d: %s took chocolate from %s\n", cs, who, from); 64 | else 65 | printf("Case %d: no thief\n", cs); 66 | } 67 | 68 | 69 | } 70 | 71 | -------------------------------------------------------------------------------- /1257-Farthest-Nodes-in-a-Tree-II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define vii vector < vector < pair > > 8 | using namespace std; 9 | 10 | enum consts {N = 30000}; 11 | vii a; 12 | 13 | int vis[N + 5]; 14 | int dis[N + 5]; 15 | int maxi; 16 | int n; 17 | 18 | int explore(int i, int d) 19 | { 20 | int x; 21 | int y; 22 | int w; 23 | 24 | dis[i] = d; 25 | 26 | for (int j = 0; j < a[i].size(); j++) { 27 | x = a[i][j].first; 28 | w = a[i][j].second; 29 | 30 | if(!vis[x]) { 31 | vis[x] = 1; 32 | explore(x, d + w); 33 | } 34 | 35 | } 36 | 37 | 38 | } 39 | 40 | 41 | 42 | int routine(int start) 43 | { 44 | int ans; 45 | 46 | memset(vis, 0, sizeof vis); 47 | memset(dis, 0, sizeof dis); 48 | 49 | maxi = -1; 50 | 51 | vis[start] = 1; 52 | explore(start, 0); 53 | 54 | for (int i = 0; i < n; i++) { 55 | if(dis[i] > maxi) { 56 | maxi = dis[i]; 57 | ans = i; 58 | } 59 | } 60 | 61 | return ans; 62 | 63 | } 64 | 65 | int main() 66 | { 67 | int t; 68 | int m; 69 | int x; 70 | int y; 71 | int w; 72 | 73 | int c[N+5]; 74 | 75 | scanf("%d", &t); 76 | 77 | for (int cs = 1; cs <= t; cs++) { 78 | scanf("%d", &n); 79 | 80 | vii temp(n+5); 81 | swap(temp, a); 82 | 83 | for (int i = 0; i < n - 1; i++) { 84 | scanf("%d", &x); 85 | scanf("%d", &y); 86 | scanf("%d", &w); 87 | 88 | a[x].push_back(make_pair(y, w)); 89 | a[y].push_back(make_pair(x, w)); 90 | } 91 | 92 | int k1 = routine(0); 93 | int k2 = routine(k1); 94 | 95 | for(int i = 0; i < n; i++) { 96 | c[i] = dis[i]; 97 | } 98 | 99 | int k3 = routine(k2); 100 | 101 | printf("Case %d:\n", cs); 102 | 103 | for (int i = 0; i < n; i++) { 104 | printf("%d\n", max(dis[i], c[i])); 105 | } 106 | 107 | 108 | 109 | } 110 | 111 | } 112 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /1259-Goldbachs-Conjecture.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define SIZE 10000000 5 | using namespace std; 6 | 7 | bool a[SIZE + 2]; 8 | int main() 9 | { 10 | 11 | vector prime; 12 | int t; 13 | int x; 14 | int y; 15 | int n; 16 | int count; 17 | 18 | a[0] = true; 19 | a[1] = true; 20 | 21 | for (int i = 2; i <= SIZE; i++) { 22 | if(a[i] == false) { 23 | prime.push_back(i); 24 | for (int j = 2 * i; j <= SIZE; j = j + i) { 25 | a[j] = true; 26 | } 27 | } 28 | } 29 | 30 | scanf("%d", &t); 31 | 32 | for (int cs = 1; cs <= t; cs++) { 33 | scanf("%d", &n); 34 | count = 0; 35 | 36 | for (int i = 0; true; i++) { 37 | x = prime[i]; 38 | y = n - x; 39 | if(x > y) { 40 | break; 41 | } 42 | 43 | if(a[y] == 0) { 44 | count++; 45 | } 46 | 47 | } 48 | 49 | printf("Case %d: %d\n", cs, count); 50 | 51 | } 52 | 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /1275-Internet-Service-Providers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | long long n; 6 | long long c; 7 | 8 | long long val(long long x) 9 | { 10 | return x * (c - x*n); 11 | } 12 | int main() 13 | { 14 | 15 | int t; 16 | long long k; 17 | long long kp; 18 | 19 | scanf("%d", &t); 20 | 21 | for (int cs = 1; cs <= t; cs++) { 22 | scanf("%lld %lld",&n, &c); 23 | 24 | if(n == 0) { 25 | printf("Case %d: %d\n", cs, 0); 26 | continue; 27 | } 28 | 29 | k = floor(c / (2* n)); 30 | kp = k + 1; 31 | 32 | if(val(k) >= val(kp)) { 33 | printf("Case %d: %lld\n", cs, k); 34 | 35 | } 36 | 37 | else { 38 | printf("Case %d: %lld\n", cs, kp); 39 | 40 | } 41 | 42 | } 43 | 44 | 45 | } 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /1289-LCM-from-1-to-n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define size 100000100 8 | long long a[size / 64 + 100]; 9 | unsigned prime[5761558]; 10 | unsigned int dp[5761558]; 11 | 12 | int cnt; 13 | 14 | int sieve_with_bit_masking() 15 | { 16 | 17 | 18 | for (long long i = 3; i <= sqrt(size); i += 2) { 19 | if(!(a[i/64]&(1LL<<(i%64)))) { 20 | for(long long j = i * i; j <= size; j += 2 * i) { 21 | a[j/64] |= (1LL<<(j%64)); 22 | } 23 | } 24 | } 25 | 26 | prime[cnt++] = 2; 27 | for (long long i = 3; i <= size; i += 2) { 28 | if(!(a[i / 64] & (1LL << (i % 64)))) { 29 | prime[cnt++] = i; 30 | } 31 | 32 | } 33 | 34 | return 0; 35 | 36 | 37 | } 38 | 39 | unsigned find_product(int x) 40 | { 41 | unsigned ans; 42 | unsigned temp; 43 | ans = 1; 44 | for (int i = 0; prime[i] * prime[i] <= x; i++ ) { 45 | temp = x; 46 | temp = temp / prime[i]; 47 | while(temp >= prime[i]) { 48 | temp = temp / prime[i]; 49 | ans = ans * prime[i]; 50 | } 51 | 52 | } 53 | 54 | return ans; 55 | } 56 | int main() 57 | { 58 | int t; 59 | int x; 60 | int n; 61 | unsigned ans; 62 | int upper; 63 | 64 | sieve_with_bit_masking(); 65 | 66 | dp[0] = 2; 67 | for (int i = 1; i < cnt; i++) { 68 | dp[i] = dp[i - 1] * (unsigned)prime[i]; 69 | } 70 | 71 | scanf("%d", &t); 72 | 73 | for (int cs = 1; cs <= t; cs++) { 74 | scanf("%d", &x); 75 | n = sqrt(x); 76 | ans = find_product(x); 77 | upper = upper_bound(prime, prime+cnt, x) - prime; 78 | upper--; 79 | ans *= dp[upper]; 80 | 81 | printf("Case %d: %u\n", cs, ans); 82 | 83 | } 84 | } 85 | 86 | 87 | -------------------------------------------------------------------------------- /1294-Positive-Negative-Sign.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | int t; 10 | long long m; 11 | long long n; 12 | long long ans; 13 | 14 | scanf("%d", &t); 15 | 16 | for (int cs = 1; cs <= t; cs++) { 17 | scanf("%lld %lld", &n, &m); 18 | ans = (n * m ) / 2; 19 | printf("Case %d: %lld\n", cs, ans); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /1297-Largest-Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | double l; 8 | double b; 9 | 10 | double area(double x) 11 | { 12 | return (l - 2 * x) * (b - 2 * x) * x; 13 | } 14 | 15 | int main() 16 | { 17 | 18 | int t; 19 | double x1; 20 | double x2; 21 | double lpb; 22 | double ans; 23 | 24 | scanf("%d",&t); 25 | 26 | for (int cs = 1; cs <= t; cs++) { 27 | scanf("%lf", &l); 28 | scanf("%lf", &b); 29 | 30 | lpb = l + b; 31 | x1 = (4 * lpb + sqrt(16 * lpb * lpb - 48 * l * b)) / 24.0; 32 | x2 = (4 * lpb - sqrt(16 * lpb * lpb - 48 * l * b)) / 24.0; 33 | 34 | ans = INT_MIN; 35 | 36 | if(x1 > 0) { 37 | ans = max(ans, area(x1)); 38 | } 39 | 40 | if(x2 > 0) { 41 | ans = max(ans, area(x2)); 42 | } 43 | 44 | printf("Case %d: %.9lf\n", cs, ans); 45 | 46 | 47 | } 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /1305-Area-of-a-Parallelogram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | int ax; 10 | int ay; 11 | int bx; 12 | int by; 13 | int cx; 14 | int cy; 15 | int dx; 16 | int dy; 17 | 18 | long long area; 19 | long long area1; 20 | long long area2; 21 | 22 | int t; 23 | 24 | scanf("%d", &t); 25 | 26 | for (int cs = 1; cs <= t; cs++) { 27 | 28 | scanf("%d %d %d %d %d %d",&ax, &ay, &bx, &by, &cx, &cy); 29 | 30 | dx = ax + cx - bx; 31 | dy = ay + cy - by; 32 | 33 | area1 = ax * by + bx * cy + cx * dy + dx * ay; 34 | area2 = ay * bx + by * cx + cy * dx + dy * ax; 35 | 36 | area = abs(area1 - area2); 37 | area = area / 2; 38 | 39 | printf("Case %d: %d %d %lld\n",cs,dx, dy, area); 40 | } 41 | 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /1311-Unlucky-Bird.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | double v1; 9 | double v2; 10 | double a1; 11 | double a2; 12 | double v3; 13 | int t; 14 | double tim; 15 | double ans; 16 | double dis; 17 | double tim1; 18 | double tim2; 19 | scanf("%d", &t); 20 | 21 | for (int cs = 1; cs <= t; cs++) { 22 | scanf("%lf", &v1); 23 | scanf("%lf", &v2); 24 | scanf("%lf", &v3); 25 | scanf("%lf", &a1); 26 | scanf("%lf", &a2); 27 | 28 | tim1 = v1 / a1; 29 | tim2 = v2 / a2; 30 | 31 | tim = max(tim1, tim2); 32 | 33 | 34 | ans = tim * v3; 35 | dis = v1 * tim1 + 0.5 * -a1 * tim1 * tim1; 36 | dis += v2 * tim2 + 0.5 * -a2 * tim2 * tim2; 37 | 38 | printf("Case %d: %.9lf %.9lf\n",cs,dis, ans); 39 | 40 | } 41 | 42 | return 0; 43 | 44 | } 45 | 46 | 47 | -------------------------------------------------------------------------------- /1326-Race.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define N 1003 4 | #define MOD 10056 5 | using namespace std; 6 | 7 | long long dp[N+5]; 8 | long long count[N + 5]; 9 | 10 | 11 | int main() 12 | { 13 | 14 | int n; 15 | int t; 16 | 17 | long long sum; 18 | 19 | count[1] = 1; 20 | dp[1] = 1; 21 | 22 | for (int i = 2; i <= N; i++) { 23 | sum = 1; 24 | for (int j = i; j >= 2; j--) { 25 | count[j] = (j * count[j]) % MOD + (j * count[j - 1]) % MOD; 26 | sum = (count[j] + sum) % MOD; 27 | } 28 | 29 | dp[i] = sum; 30 | dp[i] = dp[i] % MOD; 31 | } 32 | 33 | scanf("%d", &t); 34 | 35 | for (int cs = 1; cs <= t; cs++) { 36 | scanf("%d", &n); 37 | printf("Case %d: %d\n", cs, dp[n]); 38 | } 39 | 40 | } 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /1338-Hidden-Secret.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | int t; 10 | char x[110]; 11 | char y[110]; 12 | char temp[110]; 13 | int count[28]; 14 | char c; 15 | int k; 16 | int ok; 17 | char ans[10]; 18 | 19 | 20 | scanf("%d", &t); 21 | 22 | for (int cs = 1; cs <= t; cs++) { 23 | 24 | scanf("\n"); 25 | fgets(x, 104, stdin); 26 | scanf("\n"); 27 | fgets(y, 104, stdin); 28 | memset(count, 0, sizeof count); 29 | for (int i = 0; i < strlen(x); i++) { 30 | if(x[i] != ' ') { 31 | c = tolower(x[i]); 32 | k = c - 'a'; 33 | count[k]++; 34 | } 35 | } 36 | 37 | for (int i = 0; i < strlen(y); i++) { 38 | if(y[i] != ' ') { 39 | c = tolower(y[i]); 40 | k = c - 'a'; 41 | count[k]--; 42 | } 43 | } 44 | ok = 1; 45 | 46 | for (int i = 0; i < 26; i++) { 47 | if(count[i] != 0) { 48 | ok = 0; 49 | break; 50 | } 51 | } 52 | 53 | strcpy(ans, ok ? "Yes\0": "No\0"); 54 | 55 | printf("Case %d: %s\n", cs, ans); 56 | } 57 | 58 | } 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /1349-Aladdin-and-the-Optimal-Invitation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | long long count; 6 | 7 | 8 | int median(int a[50005], int m) 9 | { 10 | int mid; 11 | int mid1; 12 | int mid2; 13 | int ans1; 14 | int ans2; 15 | int sum; 16 | int ans; 17 | sum = 0; 18 | 19 | if(count % 2) { 20 | mid = count / 2 + 1; 21 | sum = 0; 22 | 23 | for (int i = 1; i <= m; i++) { 24 | if(a[i] + sum >= mid) { 25 | ans = i; 26 | break; 27 | } 28 | 29 | else { 30 | sum += a[i]; 31 | } 32 | 33 | } 34 | 35 | return ans; 36 | } 37 | 38 | else { 39 | 40 | sum = 0; 41 | 42 | mid1 = count / 2; 43 | mid2 = count / 2 + 1; 44 | 45 | for (int i = 1; i <= m; i++) { 46 | if(a[i] + sum >= mid1) { 47 | ans1 = i; 48 | 49 | if(a[i] + sum >= mid2) { 50 | ans2 = i; 51 | break; 52 | } 53 | 54 | sum += a[i]; 55 | 56 | } 57 | else { 58 | sum += a[i]; 59 | } 60 | } 61 | 62 | return (ans1 + ans2) / 2; 63 | 64 | } 65 | 66 | 67 | } 68 | 69 | 70 | 71 | 72 | int main() 73 | { 74 | int t; 75 | int x; 76 | int y; 77 | int k; 78 | int q; 79 | int m; 80 | int n; 81 | long long sumx; 82 | long long sumy; 83 | int ansx; 84 | int ansy; 85 | int a[50005]; 86 | int b[50005]; 87 | 88 | scanf("%d", &t); 89 | 90 | for (int cs = 1; cs <= t; cs++) { 91 | scanf("%d", &m); 92 | scanf("%d", &n); 93 | scanf("%d", &q); 94 | memset(a, 0, sizeof a); 95 | memset(b, 0, sizeof b); 96 | count = 0; 97 | 98 | for (int i = 0; i < q; i++) { 99 | scanf("%d", &x); 100 | scanf("%d", &y); 101 | scanf("%d", &k); 102 | a[x] += k; 103 | b[y] += k; 104 | count += k; 105 | } 106 | 107 | printf("Case %d: %d %d\n", cs, median(a,m), median(b, n)); 108 | 109 | 110 | } 111 | 112 | } 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /1369-Answering-Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | long long n; 5 | long long nn; 6 | long long a[1000005]; 7 | 8 | long long cal(long long i) 9 | { long long ans; 10 | 11 | ans = ((i - 1) * (nn - (i - 1)) + (nn - (i - 1))); 12 | 13 | // cout << " value of i is " << i << " " << ans << endl; 14 | 15 | return ans; 16 | 17 | } 18 | int main() 19 | { 20 | 21 | 22 | long long t; 23 | long long q; 24 | long long x; 25 | long long ch; 26 | long long sum; 27 | long long v; 28 | long long temp; 29 | 30 | 31 | scanf("%lld", &t); 32 | 33 | for (long long cs = 1; cs <= t; cs++) { 34 | scanf("%lld", &n); 35 | scanf("%lld", &q); 36 | sum = 0; 37 | nn = n - 1; 38 | for (long long i = 0; i < n; i++) { 39 | scanf("%lld", a + i); 40 | } 41 | 42 | for (long long i = 1; i < n; i++) { 43 | x = a[i - 1] - a[i]; 44 | sum = sum + cal(i) * x; 45 | } 46 | 47 | printf("Case %lld:\n", cs); 48 | for (long long i = 0; i < q; i++) { 49 | scanf("%lld", &ch); 50 | if(ch) { 51 | printf("%lld\n", sum); 52 | } 53 | 54 | else { 55 | scanf("%lld", &x); 56 | scanf("%lld", &v); 57 | temp = 0; 58 | 59 | if(x != 0) { 60 | temp = cal(x) * (a[x-1] - a[x]); 61 | // cout << "x is " << x << " a[x] " << a[x] << "a[x-1]" << a[x-1] << endl; 62 | } 63 | 64 | if(x != n -1) { 65 | temp += cal(x+1) * (a[x] - a[x+1]); 66 | } 67 | long long epic; 68 | epic = temp; 69 | sum -= temp; 70 | 71 | a[x] = v; 72 | 73 | temp = 0; 74 | 75 | if(x != 0) { 76 | temp = cal(x) * (a[x-1] - a[x]); 77 | } 78 | 79 | if(x != n -1) { 80 | temp += cal(x+1) * (a[x] - a[x+1]); 81 | } 82 | // cout<< "sum removed is " << epic << " | sum added " << temp << endl; 83 | 84 | sum += temp; 85 | 86 | } 87 | 88 | for (long long i = 0; i < n; i++) { 89 | // cout << a[i] << " "; 90 | } 91 | // cout << endl; 92 | 93 | } 94 | 95 | } 96 | 97 | } 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /1387-Setu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int x; 8 | string inp; 9 | int t; 10 | int sum; 11 | int n; 12 | 13 | sum = 0; 14 | cin >> t; 15 | 16 | for (int cs = 1; cs <= t; cs++) { 17 | cin >> n; 18 | sum = 0; 19 | cout << "Case " << cs <<":"<< endl; 20 | for (int i = 0; i < n; i++) { 21 | cin >> inp; 22 | if(inp == "donate") { 23 | cin >> x; 24 | sum += x; 25 | } 26 | else { 27 | cout << sum << endl;; 28 | } 29 | 30 | } 31 | } 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /1389-Scarecrow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | char a[110]; 10 | int n; 11 | int count; 12 | bool mark[110]; 13 | int t; 14 | 15 | scanf("%d", &t); 16 | 17 | for (int cs = 1; cs <= t; cs++) { 18 | scanf("%d", &n); 19 | count = 0; 20 | 21 | scanf("%s", a); 22 | 23 | memset(mark, 0, sizeof mark); 24 | 25 | for (int i = 0; i < n; i++) { 26 | if(a[i] == '.' and !mark[i]) { 27 | if(i + 1 < n) { 28 | mark[i+1] = 1; 29 | } 30 | 31 | if(i + 2 < n) { 32 | mark[i+2] = 1; 33 | } 34 | 35 | count++; 36 | } 37 | } 38 | 39 | printf("Case %d: %d\n", cs, count); 40 | } 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /1425-The-Monkey-and-the-Oiled-Bamboo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define sz 100006 4 | using namespace std; 5 | long long dis[sz]; 6 | int n; 7 | 8 | bool ok(long long k) 9 | { 10 | 11 | for (int i = 1; i <= n; i++) { 12 | if(dis[i] - dis[i-1] == k) { 13 | k--; 14 | } 15 | 16 | else { 17 | if(dis[i] - dis[i-1] > k) { 18 | return 0; 19 | } 20 | } 21 | 22 | } 23 | 24 | if(k >= 0) { 25 | return 1; 26 | } 27 | 28 | else { 29 | return 0; 30 | } 31 | 32 | } 33 | 34 | 35 | int main() 36 | { 37 | 38 | int t; 39 | long long k; 40 | int count; 41 | 42 | scanf("%d", &t); 43 | 44 | for (int cs = 1; cs <= t; cs++) { 45 | scanf("%d", &n); 46 | 47 | dis[0] = 0; 48 | 49 | for (int i = 1; i <= n; i++) { 50 | scanf("%lld", dis + i); 51 | } 52 | 53 | k = -1; 54 | 55 | for (int i = 1; i <= n; i++) { 56 | if((dis[i] - dis[i-1]) > k) { 57 | k = dis[i] - dis[i-1]; 58 | count = 1; 59 | } 60 | 61 | else { 62 | 63 | if((dis[i] - dis[i-1]) == k) { 64 | k = dis[i] - dis[i-1]; 65 | count++; 66 | } 67 | } 68 | 69 | } 70 | 71 | 72 | if(count > 1) { 73 | printf("Case %d: %lld\n", cs, k + 1); 74 | } 75 | 76 | else { 77 | if(ok(k)) { 78 | printf("Case %d: %lld\n", cs, k); 79 | } 80 | 81 | else { 82 | printf("Case %d: %lld\n", cs, k+1); 83 | } 84 | } 85 | } 86 | } 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LightOJ Solutions 2 | 3 | [LightOJ](https://lightoj.com/) Solutions with Hints in C++. 4 | 5 |

6 | 🔥🔥 7 | Check out my latest project - diff.blog 8 |

9 |
10 | 11 | ## Guidelines 12 | 13 | ## HINTS 14 | 15 | Hints to selected problems of LightOJ. Feel free to add or update the hints by sending a pull a request. If the [hints](HINTS.md) are not present for a problem, refer to the LightOJ forum. 16 | 17 | ### 1002 - Country Roads 18 | You need to use a modification of Dijkstra's algorithm for solving thr peorblem. They key catch is in Dijkstra we use 19 | 20 | `a[i][j] + d[i] < d[j]` 21 | `d[j] = a[i][j] + d[i]` 22 | 23 | While here we should use 24 | 25 | `max(a[i][j], d[i]) < d[j]` 26 | `d[j] = max(a[i][j], d[i])` 27 | 28 | ### 1003 - Drunk 29 | The task is to check whether cycle exists in the directed graph. If cycle exists he can't drink all. You can use a hash function or map to convert the drinks to numbers. Once converted use DFS or a suitable algo for checking the existance of cycles. I used `DFS` for the search of cycles. 30 | 31 | ### 1004 - Monkey Banana Problem 32 | This is a basic Dynamic Programming problem. The tricky part is to write the loops. While calculating the maximum bananas the second inner loop will run forward till i < n and for the remaining it would run backward. Check the solution for understanding the logic behind this. 33 | 34 | ### 1005 - Rooks 35 | Normal recursion problem. 36 | 37 | ### 1006 - Hex-a-bonacci 38 | This question is quite simple and straight forward as compared to the previous questions. All you need to dos is to replace the recursion with a for loop. Also make sure to take the modulo before storing the values in array as the values can be quite large 39 | 40 | ### 1007 - Mathematically Hard 41 | You should take exereme care while solving this problem. The input require very first IO method. Scanf and Printf would be the best choice if you are using C++. You can need to use a modified version of the Sieve method for cracking the problem. The approach I used was to first store the prime numbers using the sieve method and then to use a sieve like method to calculate `phi of n`. Take extra care to use `llu` while printing out the answer as the output is in the range of unsigned long long. 42 | 43 | ### 1008 - Fibsieve`s Fantabulous Birthday 44 | Try to find the pattern in which the numbers are appearing. If you look carefully you can obtain the pattern and device an formula to find the coordinates. 45 | 46 | ### 1009 - Back to Underworld 47 | I didn't use any normal graph algorithm for solving this problem. Even tough the problem uses a little concept of connected componenents. Try approaching the problem by considering the rivals as nodes in a graph. Check the code if you are stuck for a long time 48 | 49 | ### 1010 - Knights in Chessboard 50 | Adhoc problem. Try finding the pattern 51 | 52 | ### 1011 - Marriage Ceremonies 53 | This is a normal Memoization with Bit masking type problem. Normal recursion without memoization causes a TLE. 54 | 55 | ### 1012 - Guilty Prince 56 | This is one is a really simple graph problem. Keep marking the visited nodes to avoid visiting them again 57 | 58 | ### 1013- Love Calculator 59 | Awesome LCS type problem. Use the solution of first to find the solution of second in a similiar method. 60 | 61 | ### 1014 - Ifter Party 62 | This is also a normal Ad-Hoc problem. The simplified task is to find the divisors of p - l which are greater than l. 63 | 64 | ### 1016 - Brush II 65 | This is yet another adhoc problem. What you need to do is to ietrate over all the y points in ascending order in such a way that you increase the counter only when the difference of ym, ym+1, ....yn becomes greater than w. The counter would give you the answer. 66 | 67 | 68 | ### 1017 - Brush III 69 | Normal DP problem. 70 | 71 | ### 1019 - Brush IV 72 | This is a trivial application of Shortest Path algorithms. Implementing Floyd Warshal solution for this can accept the solution in a few lines of code 73 | 74 | ### 1020 - A Childhood game 75 | This is a problem under Game theory. You can write the solution for this problem by just observing the pattern of win or loss. If Alice is taking the stone first she will get a win for 0, 2, 3, 5 .... and loss for 1, 4, 7 .... You can easily use this observation for solving the problem. 76 | 77 | ### 1022 - Circle in Square 78 | The only tricky part in this question is to use double for all the calculations. Always use double instead of float in programming contests. 79 | 80 | ### 1023 - Discovering Permutations 81 | This is a pretty simple questions. You can use the C++ `next_permutation` for cracking this problem in a few lines of code 82 | 83 | ### 1025 - The Specials Menu 84 | This is a dp problem 85 | 86 | ### 1027 - A Dangerous Maze 87 | The below example have been taken from the forum 88 | 89 | Let's consider test case #3: 90 | 91 | 3 92 | 3 -6 -9 93 | 94 | 95 | There are 3 doors, and we are to find the expected amount of time it will take to get out of the maze. Let's call that expected value E. Now, E has to be equal to the average of the times that it takes to get out from each door, correct? Let's say T1 is the amount of time to get out of the maze when taking door #1, and T2 and T3 are defined similarly. Then: 96 | 97 | `E = (T1 + T2 + T3) / 3` 98 | 99 | 100 | It's easy to see that T1 is 3, but what about T2 and T3? You can think about it like this: if you take door number 2, you will spend 6 minutes going through the maze, and you end up at the starting point, and from there, how much time will it take you to get out? Well, E is the expected time it will take you to get out from the start, right? So, you can say: 101 | 102 | `T2 = 6 + E 103 | T3 = 9 + E` 104 | 105 | 106 | Putting all that together, the original equation becomes: 107 | 108 | `E = 3/3 + (6/3 + E/3) + (9/3 + E/3` 109 | `E = 6 + 2E/3` 110 | `E/3 = 6` 111 | `E = 18` 112 | 113 | Now convert the logic into simple code 114 | 115 | 116 | ### 1028 - Trailing Zeroes (I) 117 | If a number can be expressed as p1 ^ c1 * p2 ^ c2 * ..... pn ^ cn where p1 .. pn are the prime factors and c1 ... cn are the powers of the factors, then the total number of divisors of the number is given by 118 | 119 | `n = (c1 + 1) * (c2 + 1) * ... * (cn + 1)` 120 | 121 | Check the code carefully for the implementation 122 | 123 | ### 1029 - Civil and Evil Engineer 124 | This is a simple application of minimum spanning trees. Use a priority queue for reducing the time complexity. 125 | 126 | ### 1033 - Generating Palindromes 127 | Find the LCS of the given string and the reverse string. Then subtract the LCS from the actual length of the string. Find LCS using DP. 128 | 129 | ### 1034 - Hit the Light Switches 130 | Use Kosaraju’s algorithm for finding the connected componenets of the directed Graph. In Kosaraju’s algorithm we perform the second DFS in the transpose graph. Skipping the creation of transpose graph result in the generation of cconnected componenents instead of the `SCC` which is exactly we want 131 | 132 | ### 1035 - Intelligent Factorial Factorization 133 | I don't think you would need any hint for soloving this question. Don't try to generate factorial to solve this question. 134 | 135 | ### 1037 - Agent 47 136 | This can be solved using bit masking. Write a recurise solution and save the states. 137 | 138 | ### 1040 - Donation 139 | Use Prims MST algorithm accordingly for solving this problem. 140 | 141 | ### 1041 - Road Construction 142 | First you need to find the connected components of the graph. Then you need apply PRIMS MST in the connected components to find the minimum cost. 143 | 144 | ### 1042 - Secret Origins 145 | This is an adhoc problem. Don't bruteforce. See how the bits changes from input to output answer. 146 | 147 | ### 1044 - Paliandrome Partitioning 148 | This is a DP problem. you need to solve 2 seperate tasks for solving the problem. First task is to precompute the indexes of all the palidrome substrings. The second task is to solve the problem using the pre computed value with (1) lookup time. 149 | 150 | 151 | ### 1045 - Digits Of A Factorial 152 | You can find the number of digits required ny taking log of the factorial. This is equalent to 153 | `log(N!) = log(1) + log(2) + .... + log(N)` 154 | Precompute and store the values to avoid doing repetetive tasks. 155 | 156 | 157 | ### 1046 - Rider 158 | Use BFS for solving this problem. You need to find the shortest distance from each rider to all the cells in the chess board using BFS. Then use a simple loop for choosing the best cell. 159 | 160 | ### 1047 - Neighbour House 161 | Normal DP problem. Try solving it yourself. 162 | 163 | 164 | ### 1049 - One Way Roads 165 | This is a normal ad hoc problem. You can solve this by trying the two possible way of road arrangements, one being mere the compliment of the other. 166 | 167 | ### 1051 - Good Or Bad 168 | DP problem. I solved it by using memoization and considering only characters of the string from the point you are evaulating to save the state for memoization. I used only 5 characters because they only decide the outcome of the that point. 169 | 170 | ### 1053 - Higher Math 171 | If the triangle is right angle `a * a + b * b == c * c` 172 | 173 | ### 1057 - Collecting Gold 174 | Yet another dynamic programming problem. 175 | 176 | ### 1082 - Array Queries 177 | Trivial solution. Use a segment tree. 178 | 179 | ### 1093 - Ghakjini 180 | Use two priority queues. One for tracking maximum element in memeory and one for tracking minimum element. 181 | 182 | ### 1094 - Farthest Node In Tree 183 | Do to DFS. First one would return one node. The seond one from that node will get return you the other 184 | 185 | 186 | ### 1326 - Race 187 | Assume that there are N number of ways for finishing in M places. How can you find the number of ways to finish M+1 places? 188 | --------------------------------------------------------------------------------