buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
#include <cmath> #include <iostream> using namespace std; int main() { int n; while (cin >> n && n != 0) { int min = n + 1; int count = 0; for (int i = min; i < 2 * n; i++) { if (i == 2 || i == 3) count++; else if (i % 2 != 0) { for (int j = 3; j <= (int)(sqrt(i)); j++) { if (i % j == 0) break; if (j == (int)(sqrt(i))) { count++; } } } } cout << count << endl; } return 0; }
#include <cmath> #include <iostream> using namespace std; int main() { int n; while (cin >> n && n != 0) { int min = n + 1; int count = 0; for (int i = min; i <= 2 * n; i++) { if (i == 2 || i == 3) count++; else if (i % 2 != 0) { for (int j = 2; j <= (int)(sqrt(i)); j++) { if (i % j == 0) break; if (j == (int)(sqrt(i))) { count++; } } } } cout << count << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 64, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 64, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
145
#include <cstdio> using namespace std; #define MAX_N 123500 int prime[MAX_N * 2]; void eratos() { for (int i = 0; i < MAX_N * 2; i++) prime[i] = 1; prime[0] = prime[1] = 0; for (int i = 2; i * i <= MAX_N * 2; i++) if (prime[i]) { for (int j = i * 2; j <= MAX_N * 2; j += i) prime[j] = 0; } return; } int main() { eratos(); for (int i = 1; i < MAX_N * 2; i++) prime[i] += prime[i - 1]; while (1) { int n; scanf("%d", &n); if (n == 0) break; printf("%d\n", prime[2 * n] - prime[n - 1]); } return 0; }
#include <cstdio> using namespace std; #define MAX_N 123500 int prime[MAX_N * 2]; void eratos() { for (int i = 0; i < MAX_N * 2; i++) prime[i] = 1; prime[0] = prime[1] = 0; for (int i = 2; i * i <= MAX_N * 2; i++) if (prime[i]) { for (int j = i * 2; j <= MAX_N * 2; j += i) prime[j] = 0; } return; } int main() { eratos(); for (int i = 1; i < MAX_N * 2; i++) prime[i] += prime[i - 1]; while (1) { int n; scanf("%d", &n); if (n == 0) break; printf("%d\n", prime[2 * n] - prime[n]); } return 0; }
[["-", 0, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["-", 0, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
203
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int func(int m, int l); int main() { int n, ans; while (1) { cin >> n; if (n == 0) break; if (n == 1) ans = 1; if (n % 2 == 0) ans = func(n + 1, n); else ans = func(n + 2, n); cout << ans << endl; } return 0; } int func(int m, int l) { int cnt = 0, a; for (int i = 0; (m + (2 * i)) <= 2 * l; i++) { a = m + (2 * i); int j = 2; while (1) { if (j * j > a) { cnt++; break; } else if (a % j == 0) break; else j++; } } return cnt; }
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int func(int m, int l); int main() { int n, ans; while (1) { cin >> n; if (n == 0) break; if (n == 1) ans = 1; else { if (n % 2 == 0) ans = func(n + 1, n); else ans = func(n + 2, n); } cout << ans << endl; } return 0; } int func(int m, int l) { int cnt = 0, a; for (int i = 0; (m + (2 * i)) <= 2 * l; i++) { a = m + (2 * i); int j = 2; while (1) { if (j * j > a) { cnt++; break; } else if (a % j == 0) break; else j++; } } return cnt; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95], ["+", 8, 9, 0, 57, 75, 76, 0, 9, 0, 45], ["+", 8, 9, 0, 57, 75, 76, 0, 9, 0, 46]]
1
199
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> using namespace std; int main() { int n; bool prime[200000]; for (int i = 0; i < 200000 + 1; i++) { prime[i] = true; } prime[0] = prime[1] = false; for (int i = 2; i < 200000 + 1; i++) { if (prime[i]) { for (int j = 0; i * (j + 2) < 200000; j++) { prime[i * (j + 2)] = false; } } } while (true) { scanf("%d", &n); if (n == 0) { break; } int cnt = 0; for (int i = n + 1; i <= n * 2; i++) { if (prime[i]) { cnt++; } } printf("%d\n", cnt); } return (0); }
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> using namespace std; int main() { int n; bool prime[300000]; for (int i = 0; i < 300000 + 1; i++) { prime[i] = true; } prime[0] = prime[1] = false; for (int i = 2; i < 300000 + 1; i++) { if (prime[i]) { for (int j = 0; i * (j + 2) < 300000; j++) { prime[i * (j + 2)] = false; } } } while (true) { scanf("%d", &n); if (n == 0) { break; } int cnt = 0; for (int i = n + 1; i <= n * 2; i++) { if (prime[i]) { cnt++; } } printf("%d\n", cnt); } return (0); }
[["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 31, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 31, 13], ["-", 0, 57, 64, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 12, 13]]
1
204
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; #define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--) #define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--) #define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--) #define vrep(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define pb push_back template <class T, class S> inline pair<T, S> mp(T x, S y) { return make_pair(x, y); } template <class T, class S> inline bool minup(T &m, S x) { return m > (T)x ? (m = (T)x, true) : false; } template <class T, class S> inline bool maxup(T &m, S x) { return m < (T)x ? (m = (T)x, true) : false; } static const int INF = 1000000000; static const ll MOD = 1000000007LL; static const double EPS = 1E-12; const int MAX_N = 123456; bool flag[2 * MAX_N + 1]; int prime[2 * MAX_N + 1]; int n; int getPrime(int MAX_N = MAX_N, bool flag[] = flag, int prime[] = prime) { int ptr = 0; prime[ptr++] = 2; for (int i = 4; i <= MAX_N; i += 2) flag[i] = true; for (int i = 3; i <= MAX_N; i += 2) { if (!flag[i]) { prime[ptr++] = i; for (int j = 2 * i; j <= MAX_N; j += i) flag[j] = true; } } return ptr; } int main() { int ptr = getPrime(2 * MAX_N); while (cin >> n, n) { cout << upper_bound(prime, prime + ptr, 2 * n) - lower_bound(prime, prime + ptr, n - 1) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; #define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--) #define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--) #define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--) #define vrep(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define pb push_back template <class T, class S> inline pair<T, S> mp(T x, S y) { return make_pair(x, y); } template <class T, class S> inline bool minup(T &m, S x) { return m > (T)x ? (m = (T)x, true) : false; } template <class T, class S> inline bool maxup(T &m, S x) { return m < (T)x ? (m = (T)x, true) : false; } static const int INF = 1000000000; static const ll MOD = 1000000007LL; static const double EPS = 1E-12; const int MAX_N = 123456; bool flag[2 * MAX_N + 1]; int prime[2 * MAX_N + 1]; int n; int getPrime(int MAX_N = MAX_N, bool flag[] = flag, int prime[] = prime) { int ptr = 0; prime[ptr++] = 2; for (int i = 4; i <= MAX_N; i += 2) flag[i] = true; for (int i = 3; i <= MAX_N; i += 2) { if (!flag[i]) { prime[ptr++] = i; for (int j = 2 * i; j <= MAX_N; j += i) flag[j] = true; } } return ptr; } int main() { int ptr = getPrime(2 * MAX_N); while (cin >> n, n) { cout << upper_bound(prime, prime + ptr, 2 * n) - upper_bound(prime, prime + ptr, n) << endl; } return 0; }
[["-", 0, 16, 31, 16, 12, 16, 12, 2, 63, 22], ["+", 0, 16, 31, 16, 12, 16, 12, 2, 63, 22], ["-", 12, 16, 12, 2, 3, 4, 0, 16, 17, 33], ["-", 12, 16, 12, 2, 3, 4, 0, 16, 12, 13]]
1
461
// AOJ 1172 // Chebyshev's Theorem #include <algorithm> #include <iostream> #include <vector> using namespace std; int prime[300001] = {0}; int MAX = 300000; void make_prime() { int i, j; for (i = 2; i < MAX; i++) { prime[i] = 1; } for (i = 2; i * i < MAX; i++) { if (prime[i]) { for (j = i * 2; j < MAX; j += i) { prime[j] = 0; } } } } int main() { make_prime(); while (true) { int input; cin >> input; if (input == 0) { break; } int n = input; int count = 0; for (int i = n; i <= n * 2; i++) { if (prime[i] == 1) { count++; } } cout << count << endl; } return 0; }
// AOJ 1172 // Chebyshev's Theorem #include <algorithm> #include <iostream> #include <vector> using namespace std; int prime[300001] = {0}; int MAX = 300000; void make_prime() { int i, j; for (i = 2; i < MAX; i++) { prime[i] = 1; } for (i = 2; i * i < MAX; i++) { if (prime[i]) { for (j = i * 2; j < MAX; j += i) { prime[j] = 0; } } } } int main() { make_prime(); while (true) { int input; cin >> input; if (input == 0) { break; } int n = input; int count = 0; for (int i = n + 1; i <= n * 2; i++) { if (prime[i] == 1) { count++; } } cout << count << endl; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
195
#include <iostream> using namespace std; int main() { int n, ans = 0, primes[94] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491}; cin >> n; while (n) { for (int i = n + 1; i <= n << 1; i++) { for (int w = 0; i > primes[w] * primes[w]; w++) { if (!(i % primes[w])) { ans--; break; } } ans++; } cout << ans << endl; ans = 0; cin >> n; } return 0; }
#include <iostream> using namespace std; int main() { int n, ans = 0, primes[95] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499}; cin >> n; while (n) { for (int i = n + 1; i <= n << 1; i++) { for (int w = 0; i >= primes[w] * primes[w]; w++) { if (!(i % primes[w])) { ans--; break; } } ans++; } cout << ans << endl; ans = 0; cin >> n; } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 21], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 47], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 20]]
1
307
#include <cstdio> #include <iostream> using namespace std; main() { int a[400000] = {0}; for (int i = 3; i <= 300000; i += 2) { if (a[i] == 0) { for (int j = i + i; j <= 40000; j += i) { a[j] = 1; } } } int n; while (cin >> n) { if (n == 0) break; int ans = 0; for (int i = n + 1; i <= 2 * n; i++) { if (i == 2) ans++; else if (i % 2 == 1 && a[i] == 0) ans++; } cout << ans << endl; } return 0; }
#include <cstdio> #include <iostream> using namespace std; main() { int a[400000] = {0}; for (int i = 3; i <= 300000; i += 2) { if (a[i] == 0) { for (int j = i + i; j <= 400000; j += i) { a[j] = 1; } } } int n; while (cin >> n) { if (n == 0) break; int ans = 0; for (int i = n + 1; i <= 2 * n; i++) { if (i == 2) ans++; else if (i % 2 == 1 && a[i] == 0) ans++; } cout << ans << endl; } return 0; }
[["-", 0, 57, 64, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 12, 13]]
1
158
#include <cmath> #include <iostream> using namespace std; int dp[300000] = {0}; // 0:未定 1:素数である 2:素数でない bool isSosu(int n) { bool res = true; if (dp[n] == 1) { res = true; } else if (dp[n] == 2) { res = false; } else { if (n % 2 == 0 && n != 2) { res = false; } else { for (int i = 2; i < sqrt(n); i++) { if (n % i == 0) { res = false; break; } } } dp[n] = res; } return res; } int solv(int f, int t) { int res = 0; for (int i = f + 1; i <= t; i++) { if (isSosu(i)) res++; } return res; } int main() { int n; cin >> n; while (n != 0) { cout << solv(n, n * 2) << endl; cin >> n; } return 0; }
#include <cmath> #include <iostream> using namespace std; int dp[300000] = {0}; // 0:未定 1:素数である 2:素数でない bool isSosu(int n) { bool res = true; if (dp[n] == 1) { res = true; } else if (dp[n] == 2) { res = false; } else { if (n % 2 == 0 && n != 2) { res = false; } else { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { res = false; break; } } } dp[n] = res; } return res; } int solv(int f, int t) { int res = 0; for (int i = f + 1; i <= t; i++) { if (isSosu(i)) res++; } return res; } int main() { int n; cin >> n; while (n != 0) { cout << solv(n, n * 2) << endl; cin >> n; } return 0; }
[["-", 75, 76, 0, 9, 0, 7, 15, 16, 17, 18], ["+", 75, 76, 0, 9, 0, 7, 15, 16, 17, 19]]
1
220
#include <iostream> using namespace std; int num[300000]; int main(void) { for (int i = 0; i < 300000; i++) { num[i] = 1; } num[0] = 0; num[1] = 0; for (int i = 2; i < 300000; i++) { for (int j = 2; i * j < 300000; j++) { num[i * j] = 0; } } int n; while (cin >> n, n) { int cnt = 0; for (int i = n; i <= 2 * n; i++) { cnt += num[i]; } cout << cnt << endl; } return 0; }
#include <iostream> using namespace std; int num[300000]; int main(void) { for (int i = 0; i < 300000; i++) { num[i] = 1; } num[0] = 0; num[1] = 0; for (int i = 2; i < 300000; i++) { for (int j = 2; i * j < 300000; j++) { num[i * j] = 0; } } int n; while (cin >> n, n) { int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) { cnt += num[i]; } cout << cnt << endl; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
151
#include <bits/stdc++.h> using namespace std; #define LOG(...) fprintf(stderr, __VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SQ(n) (n) * (n) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int N = 123456 * 2; bool sieve[N + 1]; int number[N + 1]; int main() { int cnt = 0; FOR(i, 2, N) { number[i] = cnt; if (!sieve[i]) continue; number[i] = ++cnt; for (int j = i * 2; j < N; j += i) { sieve[j] = true; } } int n; while (cin >> n, n) { cout << number[2 * n] - number[n] << endl; } }
#include <bits/stdc++.h> using namespace std; #define LOG(...) fprintf(stderr, __VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SQ(n) (n) * (n) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int N = 123456 * 2; bool sieve[N + 1]; int number[N + 1]; int main() { int cnt = 0; FOR(i, 2, N + 1) { number[i] = cnt; if (sieve[i]) continue; number[i] = ++cnt; for (int j = i * 2; j < N + 1; j += i) { sieve[j] = true; } } int n; while (cin >> n, n) { cout << number[2 * n] - number[n] << endl; } }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["-", 0, 9, 0, 57, 15, 339, 51, 91, 17, 111], ["+", 0, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 0, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
266
#include <cmath> #include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) #define N 300000 using namespace std; int main() { int array[N] = {0}; rep(i, N) { array[i] = 1; } array[0] = 0; array[1] = 0; int end; end = (int)sqrt((double)N); for (int i = 2; i < end; i++) { if (array[i] == 1) { for (int j = i * 2; j <= N; j += i) { array[j] = 0; } } } while (true) { int n; cin >> n; if (n == 0) break; int num = 0; for (int i = n; i <= 2 * n; i++) { if (array[i] == 1) { num++; } } cout << num << '\n'; } }
#include <cmath> #include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) #define N 300000 using namespace std; int main() { int array[N] = {0}; rep(i, N) { array[i] = 1; } array[0] = 0; array[1] = 0; int end; end = (int)sqrt((double)N); for (int i = 2; i < end; i++) { if (array[i] == 1) { for (int j = i * 2; j <= N; j += i) { array[j] = 0; } } } while (true) { int n; cin >> n; if (n == 0) break; int num = 0; for (int i = n + 1; i <= 2 * n; i++) { if (array[i] == 1) { num++; } } cout << num << '\n'; } }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
199
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) bool isprime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } int main() { int n; while (cin >> n && n) { int sum = 0; rep(i, n) { int x = n + i; sum += isprime(x); } cout << sum << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) bool isprime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } int main() { int n; while (cin >> n && n) { int sum = 0; rep(i, n) { int x = n + i + 1; sum += isprime(x); } cout << sum << endl; } }
[["+", 8, 9, 0, 43, 49, 50, 51, 16, 17, 72], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13]]
1
106
#include <cstring> #include <iostream> using namespace std; bool is_prime[300000]; int sieve(int a) { int p = 0, p1 = 0; is_prime[0] = is_prime[1] = false; for (int i = 2; i <= a * 2; i++) { if (i == a) p1 = p; if (is_prime[i]) { p++; for (int j = i * 2; j <= a * 2; j += i) is_prime[j] = false; } } return p - p1; } int main() { int n; memset(is_prime, true, 300000); while (cin >> n, n) { cout << sieve(n) << endl; } }
#include <cstring> #include <iostream> using namespace std; bool is_prime[300000]; int sieve(int a) { int p = 0, p1 = 0; is_prime[0] = is_prime[1] = false; for (int i = 2; i <= a * 2; i++) { if (i == a + 1) p1 = p; if (is_prime[i]) { p++; for (int j = i * 2; j <= a * 2; j += i) is_prime[j] = false; } } return p - p1; } int main() { int n; memset(is_prime, true, 300000); while (cin >> n, n) { cout << sieve(n) << endl; } }
[["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 72], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
1
151
#include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n, n) { int count = 0; for (int i = n + 1; i <= 2 * n; ++i) { int tmp_c = 0; for (int j = 1; j * j <= 2 * n; ++j) { if (i % j == 0) { ++tmp_c; } } if (tmp_c == 2) { ++count; } } cout << count << endl; } return 0; }
#include <iostream> #include <vector> //#include<math> using namespace std; int main() { int n; while (cin >> n, n) { int count = 0; for (int i = n + 1; i <= 2 * n; ++i) { int tmp_c = 0; for (int j = 1; j * j <= 2 * n; ++j) { if (i % j == 0) { ++tmp_c; } } if (tmp_c == 1) { ++count; /*cout << "sosuu is " << i << endl;*/ } } cout << count << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
110
#include <fstream> #include <iostream> #include <math.h> using namespace std; int main() { int n, i, j, pin = 0, p = 0; while (1) { cin >> n; if (n == 0) { break; } for (i = n + 1; i < 2 * n + 1; i++) { for (j = 2; j < sqrt((double)i) + 1; j++) { if (i % j == 0) { pin = 1; break; } } if (pin == 0) { p = p + 1; } else { pin = 0; } } if (n == 1) { cout << 1; } else { cout << p << endl; } p = 0; } return 0; }
#include <fstream> #include <iostream> #include <math.h> using namespace std; int main() { int n, i, j, pin = 0, p = 0; while (1) { cin >> n; if (n == 0) { break; } for (i = n + 1; i < 2 * n + 1; i++) { for (j = 2; j < sqrt((double)i) + 1; j++) { if (i % j == 0) { pin = 1; break; } } if (pin == 0) { p = p + 1; } else { pin = 0; } } if (n == 1) { cout << 1 << endl; } else { cout << p << endl; } p = 0; } return 0; }
[["+", 0, 57, 64, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 57, 64, 9, 0, 1, 0, 16, 12, 22]]
1
160
#include <bits/stdc++.h> #define INF INT_MAX #define ll long long #define MAX_PRIME 300000 using namespace std; bool isPrime[MAX_PRIME + 1]; // true:?´???°??§?????????false:?´???°??§?????? int sumOfPrime[MAX_PRIME + 1]; //?´??????? int main() { //????????? for (int i = 0; i <= MAX_PRIME; i++) { isPrime[i] = true; } isPrime[0] = isPrime[1] = false; sumOfPrime[0] = 0; //?´???°???n <= 123456???????±??????? for (ll i = 1; i <= MAX_PRIME; i++) { sumOfPrime[i] = sumOfPrime[i - 1]; if (isPrime[i] == true) { sumOfPrime[i]++; for (ll j = 2; i * j <= MAX_PRIME; j++) { isPrime[i * j] = false; } } } int x; while (true) { scanf("%d", &x); if (x == 0) break; cout << sumOfPrime[2 * x] - sumOfPrime[x + 1] << endl; } return 0; }
#include <bits/stdc++.h> #define INF INT_MAX #define ll long long #define MAX_PRIME 300000 using namespace std; bool isPrime[MAX_PRIME + 1]; // true:?´???°??§?????????false:?´???°??§?????? int sumOfPrime[MAX_PRIME + 1]; //?´??????? int main() { //????????? for (int i = 0; i <= MAX_PRIME; i++) { isPrime[i] = true; } isPrime[0] = isPrime[1] = false; sumOfPrime[0] = 0; //?´???°???n <= 123456???????±??????? for (ll i = 1; i <= MAX_PRIME; i++) { sumOfPrime[i] = sumOfPrime[i - 1]; if (isPrime[i] == true) { sumOfPrime[i]++; for (ll j = 2; i * j <= MAX_PRIME; j++) { isPrime[i * j] = false; } } } int x; while (true) { scanf("%d", &x); if (x == 0) break; cout << sumOfPrime[2 * x] - sumOfPrime[x] << endl; } return 0; }
[["-", 12, 16, 12, 69, 341, 342, 0, 16, 17, 72], ["-", 12, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
203
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n, n) { vector<int> isprime(2 * n + 1, 1); isprime[0] = 0; isprime[1] = 0; for (int i = 1; i <= 2 * n; i++) if (isprime[i]) { for (int j = i * 2; j <= 2 * n; j += i) isprime[j] = 0; } int cnt = 0; for (int i = n; i <= 2 * n; i++) cnt += isprime[i]; cout << cnt << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n, n) { vector<int> isprime(2 * n + 1, 1); isprime[0] = 0; isprime[1] = 0; for (int i = 1; i <= 2 * n; i++) if (isprime[i]) { for (int j = i * 2; j <= 2 * n; j += i) isprime[j] = 0; } int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) cnt += isprime[i]; cout << cnt << endl; } }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
139
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define vi vector<int> #define vvi vector<vector<int>> #define ll long long int #define vl vector<ll> #define vvl vector<vector<ll>> #define ld long double #define INF 1e9 #define EPS 0.0000000001 #define rep(i, n) for (int i = 0; i < n; i++) #define CC puts("-------ok--------"); #define all(in) in.begin(), in.end() #define bv vector<bool> using namespace std; typedef pair<int, int> PA; using namespace std; #define MAX 999999 int main() { // ?´???°??????sqrt(max)??§?????? vector<bool> check(MAX + 1); rep(i, MAX + 1) check[i] = true; vector<int> Primenumber(MAX + 1, 0); // Primearray int counter = 0; // Primearray counter; for (int i = 2; i < MAX + 1; i++) { if (check[i]) { for (int j = 2; i * j < MAX; j++) check[i * j] = false; //?´???°??????Primearray???????´??????????????????°?????¨???false??? Primenumber[counter] = i; counter++; } } int n; while (cin >> n, n) { int ans = 0; for (int k = n; k <= 2 * n; k++) if (check[k]) ans++; cout << ans << endl; } }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define vi vector<int> #define vvi vector<vector<int>> #define ll long long int #define vl vector<ll> #define vvl vector<vector<ll>> #define ld long double #define INF 1e9 #define EPS 0.0000000001 #define rep(i, n) for (int i = 0; i < n; i++) #define CC puts("-------ok--------"); #define all(in) in.begin(), in.end() #define bv vector<bool> using namespace std; typedef pair<int, int> PA; using namespace std; #define MAX 999999 int main() { // ?´???°??????sqrt(max)??§?????? vector<bool> check(MAX + 1); rep(i, MAX + 1) check[i] = true; vector<int> Primenumber(MAX + 1, 0); // Primearray int counter = 0; // Primearray counter; for (int i = 2; i < MAX + 1; i++) { if (check[i]) { for (int j = 2; i * j < MAX; j++) check[i * j] = false; //?´???°??????Primearray???????´??????????????????°?????¨???false??? Primenumber[counter] = i; counter++; } } int n; while (cin >> n, n) { int ans = 0; for (int k = n + 1; k <= 2 * n; k++) if (check[k]) ans++; cout << ans << endl; } }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
252
#include <bits/stdc++.h> #define REP(i, n, N) for (int i = n; i < N; i++) #define p(S) cout << S << endl using namespace std; bool prime[123457] = {false}; void isPrime() { prime[1] = true; for (int i = 2; i * i < 300000; i++) { if (!prime[i]) { for (int j = 2 * i; j < 300000; j += i) { prime[j] = true; } } } return; } int main() { isPrime(); int N; while (cin >> N, N) { int count = 0; REP(i, N + 1, 2 * N + 1) { if (!prime[i]) count++; } p(count); } return 0; }
#include <bits/stdc++.h> #define REP(i, n, N) for (int i = n; i < N; i++) #define p(S) cout << S << endl using namespace std; bool prime[300001] = {false}; void isPrime() { prime[1] = true; for (int i = 2; i * i < 300000; i++) { if (!prime[i]) { for (int j = 2 * i; j < 300000; j += i) { prime[j] = true; } } } return; } int main() { isPrime(); int N; while (cin >> N, N) { int count = 0; REP(i, N + 1, 2 * N + 1) { if (!prime[i]) count++; } p(count); } return 0; }
[["-", 0, 30, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 50, 49, 80, 81, 13]]
1
165
#include <cmath> #include <iostream> #include <vector> using std::cin; using std::cout; using std::endl; bool is_prime(int n) { if (n < 2) { return false; } if (n == 2 || n == 3) { return true; } for (int i = 3; i < n; i = i + 2) { if (n % i == 0) { return false; } } return true; } int main(void) { int N = 123456 * 2; int n; cin >> n; std::vector<bool> prime(N); for (int i = 0; i < N + 2; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int i = 2; i <= sqrt(N); i++) { if (is_prime(i)) { for (int j = 2; i * j < N + 2; j++) { prime[i * j] = false; } } } while (n != 0) { int count = 0; for (int i = n; i < (2 * n) + 1; i++) { if (prime[i]) { count++; } } cout << count << endl; cin >> n; } return 0; }
#include <cmath> #include <iostream> #include <vector> using std::cin; using std::cout; using std::endl; bool is_prime(int n) { if (n < 2) { return false; } if (n == 2 || n == 3) { return true; } for (int i = 3; i < n; i = i + 2) { if (n % i == 0) { return false; } } return true; } int main(void) { int N = 123456 * 2; int n; cin >> n; std::vector<bool> prime(N); for (int i = 0; i < N + 2; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int i = 2; i <= sqrt(N); i++) { if (is_prime(i)) { for (int j = 2; i * j < N + 2; j++) { prime[i * j] = false; } } } while (n != 0) { int count = 0; for (int i = n + 1; i < (2 * n) + 1; i++) { if (prime[i]) { count++; } } cout << count << endl; cin >> n; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
278
#include <bits/stdc++.h> using namespace std; int p[200000]; int main() { p[0] = p[1] = 1; for (int i = 2; i * i < 200000; ++i) { if (!p[i]) { for (int j = i + i; j < 200000; j += i) p[j] = 1; } } int n; while (cin >> n, n) { int cnt = 0; for (int i = n + 1; i <= 2 * n; ++i) if (!p[i]) cnt++; cout << cnt << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int p[300000]; int main() { p[0] = p[1] = 1; for (int i = 2; i * i < 300000; ++i) { if (!p[i]) { for (int j = i + i; j < 300000; j += i) p[j] = 1; } } int n; while (cin >> n, n) { int cnt = 0; for (int i = n + 1; i <= 2 * n; ++i) if (!p[i]) cnt++; cout << cnt << endl; } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 57, 64, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 12, 13]]
1
138
#include <iostream> #include <vector> using namespace std; int main(int argc, char *argv[]) { for (;;) { int n; cin >> n; if (n == 0) break; int n22 = n * 2 + 2; bool prime[n * 2 + 2]; for (int i = 0; i < n22; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int p = 2; p < n22; p++) { if (prime[p]) { for (int k = p + p; k < n22; k += p) { prime[k] = false; } } } int cnt = 0; for (int i = n; i <= 2 * n; i++) { if (prime[i]) cnt++; } cout << cnt << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; int main(int argc, char *argv[]) { for (;;) { int n; cin >> n; if (n == 0) break; int n22 = n * 2 + 2; bool prime[n * 2 + 2]; for (int i = 0; i < n22; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int p = 2; p < n22; p++) { if (prime[p]) { for (int k = p + p; k < n22; k += p) { prime[k] = false; } } } int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) { if (prime[i]) cnt++; } cout << cnt << endl; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
193
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vec; typedef pair<int, int> P; #define rep(n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define out(a) cout << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) #define INF 100000000 int arr[999999] = {0}; void Eratosthenes(int N) { for (int i = 0; i < N; i++) { arr[i] = 1; } for (int i = 2; i < sqrt(N); i++) { if (arr[i]) { for (int j = 0; i * (j + 2) < N; j++) { arr[i * (j + 2)] = 0; } } } int res = 0; for (int i = (N / 2) + 1; i < N; i++) { if (arr[i]) { res++; } } cout << res << endl; } int main() { while (true) { int n; cin >> n; if (n == 0) return 0; Eratosthenes(2 * n); } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vec; typedef pair<int, int> P; #define rep(n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define out(a) cout << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) #define INF 100000000 int arr[999999] = {0}; void Eratosthenes(int N) { for (int i = 0; i <= N; i++) { arr[i] = 1; } for (int i = 2; i <= sqrt(N); i++) { if (arr[i]) { for (int j = 0; i * (j + 2) <= N; j++) { arr[i * (j + 2)] = 0; } } } int res = 0; for (int i = (N / 2) + 1; i <= N; i++) { if (arr[i]) { res++; } } cout << res << endl; } int main() { while (true) { int n; cin >> n; if (n == 0) return 0; Eratosthenes(2 * n); } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 57, 64, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 17, 19]]
1
299
#include <iostream> using namespace std; int p[300000]; int main() { int n; p[0] = p[1] = -1; for (int i = 2; i < 300000; i++) { if (!p[i]) { p[i] = 1; for (int j = i + i; j < 300000; j += i) p[j] = -1; } } while (1) { int ans = 0; cin >> n; if (n == 0) break; for (int i = n + 1; i <= n + n; i++) { if (p[i] == 1) ans++; } cout << ans; } }
#include <iostream> using namespace std; int p[300000]; int main() { int n; p[0] = p[1] = -1; for (int i = 2; i < 300000; i++) { if (!p[i]) { p[i] = 1; for (int j = i + i; j < 300000; j += i) p[j] = -1; } } while (1) { int ans = 0; cin >> n; if (n == 0) break; for (int i = n + 1; i <= n + n; i++) { if (p[i] == 1) ans++; } cout << ans << endl; } }
[["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22]]
1
149
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &o) { out << "(" << o.first << "," << o.second << ")"; return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> V) { for (int i = 0; i < V.size(); i++) { out << V[i]; if (i != V.size() - 1) out << " "; } return out; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> Mat) { for (int i = 0; i < Mat.size(); i++) { if (i != 0) out << endl; out << Mat[i]; } return out; } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> mp) { out << "{ "; for (auto it = mp.begin(); it != mp.end(); it++) { out << it->first << ":" << it->second; if (mp.size() - 1 != distance(mp.begin(), it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1172&lang=jp> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ int n; const ll MAX_PRIME = 300000; vector<int> primes; vector<int> is_prime(MAX_PRIME + 1, true); void init_primes() { is_prime[0] = is_prime[1] = false; for (int i = 2; i <= MAX_PRIME; i++) { if (is_prime[i]) { primes.push_back(i); for (int j = i * 2; j <= MAX_PRIME; j += i) is_prime[j] = false; } } } ll solve() { return upper_bound(primes.begin(), primes.end(), 2 * n) - lower_bound(primes.begin(), primes.end(), n); } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); init_primes(); while (cin >> n, n) { cout << solve() << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &o) { out << "(" << o.first << "," << o.second << ")"; return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> V) { for (int i = 0; i < V.size(); i++) { out << V[i]; if (i != V.size() - 1) out << " "; } return out; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> Mat) { for (int i = 0; i < Mat.size(); i++) { if (i != 0) out << endl; out << Mat[i]; } return out; } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> mp) { out << "{ "; for (auto it = mp.begin(); it != mp.end(); it++) { out << it->first << ":" << it->second; if (mp.size() - 1 != distance(mp.begin(), it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1172&lang=jp> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ int n; const ll MAX_PRIME = 300000; vector<int> primes; vector<int> is_prime(MAX_PRIME + 1, true); void init_primes() { is_prime[0] = is_prime[1] = false; for (int i = 2; i <= MAX_PRIME; i++) { if (is_prime[i]) { primes.push_back(i); for (int j = i * 2; j <= MAX_PRIME; j += i) is_prime[j] = false; } } } ll solve() { return upper_bound(primes.begin(), primes.end(), 2 * n) - upper_bound(primes.begin(), primes.end(), n); } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); init_primes(); while (cin >> n, n) { cout << solve() << endl; } return 0; }
[["-", 8, 9, 0, 37, 0, 16, 12, 2, 63, 22], ["+", 8, 9, 0, 37, 0, 16, 12, 2, 63, 22]]
1
534
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long int ll; typedef pair<int, int> P; const ll MOD = 1e9 + 7; int main() { int n; int m = 250000; bool flag[300000]; memset(flag, 1, sizeof(flag)); flag[0] = 0, flag[1] = 0; for (int i = 2; i * i <= m; i++) { if (flag[i]) { for (int j = 2; i * j <= m; j++) { flag[i * j] = 0; } } } while (cin >> n) { int ans = 0; for (int i = n + 1; i <= 2 * n; i++) { if (flag[i]) { ans++; } } cout << ans << endl; } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long int ll; typedef pair<int, int> P; const ll MOD = 1e9 + 7; int main() { int n; int m = 250000; bool flag[300000]; memset(flag, 1, sizeof(flag)); flag[0] = 0, flag[1] = 0; for (int i = 2; i * i <= m; i++) { if (flag[i]) { for (int j = 2; i * j <= m; j++) { flag[i * j] = 0; } } } while (cin >> n && n) { int ans = 0; for (int i = n + 1; i <= 2 * n; i++) { if (flag[i]) { ans++; } } cout << ans << endl; } return 0; }
[["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 98], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 12, 22]]
1
207
#include <algorithm> #include <iostream> #define N 123456 using namespace std; int main() { int n, cnt; bool a[2 * N + 1]; fill(a, a + (2 * N + 1), true); a[0] = a[1] = false; for (int i = 2; i <= 2 * N; i++) if (a[i]) for (int j = i + i; j <= 2 * N; j += i) a[j] = false; while (cin >> n && n) { cnt = 0; for (int i = n; i <= 2 * n; i++) if (a[i]) cnt++; cout << cnt << endl; } return 0; }
#include <algorithm> #include <iostream> #define N 123456 using namespace std; int main() { int n, cnt; bool a[2 * N + 1]; fill(a, a + (2 * N + 1), true); a[0] = a[1] = false; for (int i = 2; i <= 2 * N; i++) if (a[i]) for (int j = i + i; j <= 2 * N; j += i) a[j] = false; while (cin >> n && n) { cnt = 0; for (int i = n + 1; i <= 2 * n; i++) if (a[i]) cnt++; cout << cnt << endl; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
159
#include <algorithm> #include <cmath> #include <complex> #include <cstdlib> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) typedef complex<double> P; typedef long long ll; const int MAX_SIZE = 10000; const int MAX_PRIME = 1000000; int prime[MAX_PRIME + 1]; bool isPrime[MAX_PRIME + 1]; int p; void erats() { // primeツδ環スツトツづーツ渉可甘コツ可サ fill(isPrime, isPrime + MAX_PRIME, true); p = 0; isPrime[0] = isPrime[1] = false; for (int i = 2; i <= MAX_PRIME; i++) { if (isPrime[i]) { prime[p++] = i; } else continue; // ツづ督づゥツつ「ツづ可つゥツつッツづゥ for (int j = 2 * i; j <= MAX_PRIME; j += i) { isPrime[j] = false; } } } int main() { erats(); int n; while (cin >> n && n != 0) { int cnt = 0; for (int i = n; i <= 2 * n; i++) { if (isPrime[i]) cnt++; } cout << cnt << endl; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdlib> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) typedef complex<double> P; typedef long long ll; const int MAX_SIZE = 10000; const int MAX_PRIME = 1000000; int prime[MAX_PRIME + 1]; bool isPrime[MAX_PRIME + 1]; int p; void erats() { // primeツδ環スツトツづーツ渉可甘コツ可サ fill(isPrime, isPrime + MAX_PRIME, true); p = 0; isPrime[0] = isPrime[1] = false; for (int i = 2; i <= MAX_PRIME; i++) { if (isPrime[i]) { prime[p++] = i; } else continue; // ツづ督づゥツつ「ツづ可つゥツつッツづゥ for (int j = 2 * i; j <= MAX_PRIME; j += i) { isPrime[j] = false; } } } int main() { erats(); int n; while (cin >> n && n != 0) { int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) { if (isPrime[i]) cnt++; } cout << cnt << endl; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
258
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, a, b) for (i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) vector<bool> isprime(123456 * 2 + 1, true); void sieve(int n) { int i, j; isprime[0] = isprime[1] = false; rep(i, n) if (isprime[i]) { for (j = 2 * i; j <= n; j += i) isprime[j] = false; } return; } int main() { int i, j; int n; int ret = 0; while (true) { ret = 0; cin >> n; if (n == 0) break; sieve(2 * n + 1); REP(i, n, 2 * n + 1) if (isprime[i]) ret++; cout << ret << endl; } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, a, b) for (i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) vector<bool> isprime(123456 * 2 + 1, true); void sieve(int n) { int i, j; isprime[0] = isprime[1] = false; rep(i, n) if (isprime[i]) { for (j = 2 * i; j <= n; j += i) isprime[j] = false; } return; } int main() { int i, j; int n; int ret = 0; while (true) { ret = 0; cin >> n; if (n == 0) break; sieve(2 * n + 1); REP(i, n + 1, 2 * n + 1) if (isprime[i]) ret++; cout << ret << endl; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
219
// AOJ 1172 #include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; #define MAX (123456 * 2) vector<int> prime; bool p[MAX]; void make_prime(void) { fill(p, p + MAX, true); p[0] = p[1] = false; for (int i = 4; i < MAX; i += 2) { p[i] = false; } prime.push_back(2); for (int i = 3; i < MAX; i += 2) { if (p[i]) { prime.push_back(i); for (int j = i + i; j < MAX; j += i) { p[j] = false; } } } return; } int main(void) { make_prime(); int n; while (cin >> n, n) { int ret = 0; for (int i = 0; prime[i] <= (2 * n); i++) { if (n <= prime[i]) { ret++; } } cout << ret << endl; } return 0; }
// AOJ 1172 #include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; #define MAX (123456 * 2) vector<int> prime; bool p[MAX]; void make_prime(void) { fill(p, p + MAX, true); p[0] = p[1] = false; for (int i = 4; i < MAX; i += 2) { p[i] = false; } prime.push_back(2); for (int i = 3; i < MAX; i += 2) { if (p[i]) { prime.push_back(i); for (int j = i + i; j < MAX; j += i) { p[j] = false; } } } return; } int main(void) { make_prime(); int n; while (cin >> n, n) { int ret = 0; for (int i = 0; prime[i] <= (2 * n); i++) { if (n < prime[i]) { ret++; } } cout << ret << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18]]
1
225
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) bool isPrime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int main() { int n; while (cin >> n, n) { int ans = 0; FOR(i, n, 2 * n) { if (isPrime(i)) ans++; } cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) bool isPrime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int main() { int n; while (cin >> n, n) { int ans = 0; FOR(i, n + 1, 2 * n + 1) { if (isPrime(i)) ans++; } cout << ans << endl; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
153
#include <iostream> using namespace std; const int MAX = 246913; const int SIZE = 123457; int main() { int n; bool prime[SIZE]; for (int i = 0; i < SIZE; i++) prime[i] = true; for (int i = 3; i < MAX; i += 2) { if (prime[((i - 1) >> 1)]) { for (int j = i + i + i; j < MAX; j += i) { if (j & 1) prime[((j - 1) >> 1)] = false; } } } while (cin >> n, n) { int cnt = 0; int m = (n << 1) + 1; for (int i = n; i < m; i++) { if ((i & 1) && (prime[((i - 1) >> 1)])) cnt++; } if (n == 2) cnt++; cout << cnt << endl; } return 0; }
#include <iostream> using namespace std; const int MAX = 246913; const int SIZE = 123457; int main() { int n; bool prime[SIZE]; for (int i = 0; i < SIZE; i++) prime[i] = true; for (int i = 3; i < MAX; i += 2) { if (prime[((i - 1) >> 1)]) { for (int j = i + i + i; j < MAX; j += i) { if (j & 1) prime[((j - 1) >> 1)] = false; } } } while (cin >> n, n) { int cnt = 0; int m = (n << 1) + 1; for (int i = n + 1; i < m; i++) { if ((i & 1) && (prime[((i - 1) >> 1)])) cnt++; } if (n == 1) cnt++; cout << cnt << endl; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
216
//============================================================================ // Name : TopCoderCompetition.cpp // Author : taguchi // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <numeric> #include <sstream> #include <string> #include <vector> using namespace std; //#define P pair<int,int> //#define max 999999 typedef unsigned int uint; typedef unsigned long ul; typedef long long ll; typedef unsigned long long ull; #define rep(i, n) for (ull i = 0; i < n; i++) #define pb(n) push_back(n) #define clear(n) memset(n, 0, sizeof(n)) typedef vector<int> vi; typedef vector<string> vs; struct edge { int cost, to; }; vector<string> split(string &in, char delimiter) { vector<string> re; uint cur = 0, next; while ((next = in.find_first_of(delimiter, cur)) != string::npos) { re.pb(string(in, cur, next - cur)); cur = next + 1; } re.pb(string(in, cur, in.size() - cur)); return re; } vector<int> split_int(string &in, char delimiter) { vector<string> str = split(in, delimiter); vector<int> re; rep(i, str.size()) { re.pb(strtol(str[i].c_str(), NULL, 10)); } return re; } class Range { public: int a, b; Range(int an, int bn) : a(an), b(bn) {} bool operator()(int n) { return a <= n && n <= b; } bool isCross(Range x) { return x(a) || x(b); } bool contains(Range x) { return this->a <= x.a && x.b <= this->b; } }; bool isprime[1000010]; uint table[246914]; int main() { for (int i = 2; i < 1000010; i++) { if (!isprime[i]) { for (int j = 2; i * j < 1000010; j++) { isprime[i * j] = true; } } } isprime[0] = true; isprime[1] = true; rep(i, 246914) { table[i] = !isprime[i]; } partial_sum(table, table + 246914, table); while (1) { int n; cin >> n; if (!n) break; cout << table[2 * n] - table[n - 1] << endl; } return 0; }
//============================================================================ // Name : TopCoderCompetition.cpp // Author : taguchi // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <numeric> #include <sstream> #include <string> #include <vector> using namespace std; //#define P pair<int,int> //#define max 999999 typedef unsigned int uint; typedef unsigned long ul; typedef long long ll; typedef unsigned long long ull; #define rep(i, n) for (ull i = 0; i < n; i++) #define pb(n) push_back(n) #define clear(n) memset(n, 0, sizeof(n)) typedef vector<int> vi; typedef vector<string> vs; struct edge { int cost, to; }; vector<string> split(string &in, char delimiter) { vector<string> re; uint cur = 0, next; while ((next = in.find_first_of(delimiter, cur)) != string::npos) { re.pb(string(in, cur, next - cur)); cur = next + 1; } re.pb(string(in, cur, in.size() - cur)); return re; } vector<int> split_int(string &in, char delimiter) { vector<string> str = split(in, delimiter); vector<int> re; rep(i, str.size()) { re.pb(strtol(str[i].c_str(), NULL, 10)); } return re; } class Range { public: int a, b; Range(int an, int bn) : a(an), b(bn) {} bool operator()(int n) { return a <= n && n <= b; } bool isCross(Range x) { return x(a) || x(b); } bool contains(Range x) { return this->a <= x.a && x.b <= this->b; } }; bool isprime[1000010]; uint table[246914]; int main() { for (int i = 2; i < 1000010; i++) { if (!isprime[i]) { for (int j = 2; i * j < 1000010; j++) { isprime[i * j] = true; } } } isprime[0] = true; isprime[1] = true; rep(i, 246914) { table[i] = !isprime[i]; } partial_sum(table, table + 246914, table); while (1) { int n; cin >> n; if (!n) break; cout << table[2 * n] - table[n] << endl; } return 0; }
[["-", 12, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["-", 12, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
520
#include <algorithm> #include <cstdio> #include <deque> #include <iostream> #include <limits> #include <sstream> #include <string> #include <vector> using namespace std; int dp[1000001]; int main() { fill((int *)dp, (int *)dp + 1000000, 1); dp[0] = 0; dp[1] = 0; for (int i = 2; i * i <= 1000000; i++) { if (!dp[i]) continue; for (int j = i + i; j <= 1000000; j += i) { dp[j] = 0; } } int n; while (~scanf("%d", &n)) { if (n == 0) break; int sum = 0; for (int i = n; i <= n * 2; i++) { sum += dp[i]; } printf("%d\n", sum); } }
#include <algorithm> #include <cstdio> #include <deque> #include <iostream> #include <limits> #include <sstream> #include <string> #include <vector> using namespace std; int dp[1000001]; int main() { fill((int *)dp, (int *)dp + 1000000, 1); dp[0] = 0; dp[1] = 0; for (int i = 2; i * i <= 1000000; i++) { if (!dp[i]) continue; for (int j = i + i; j <= 1000000; j += i) { dp[j] = 0; } } int n; while (~scanf("%d", &n)) { if (n == 0) break; int sum = 0; for (int i = n + 1; i <= n * 2; i++) { sum += dp[i]; } printf("%d\n", sum); } }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
185
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string.h> #include <string> #include <utility> #include <vector> #ifdef _DEBUG #define typeof(X) std::identity<decltype(X)>::type // C++0x (for vs2010) #else #define typeof(X) __typeof__(X) // for gcc #endif #define sz(a) int((a).size()) #define FOREACH(it, c) \ for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define FOR(i, count) for (int i = 0; i < (count); i++) #define V_CIN(v) \ do { \ for (int i = 0; i < sz(v); i++) \ cin >> (v)[i]; \ } while (0) #define all(c) (c).begin(), (c).end() using namespace std; static const double EPS = 1e-5; typedef long long ll; const int MODULO = 1000000007; typedef pair<int, int> Pii; bool t[123456 * 2 + 10]; int main() { memset(t, 1, sizeof(t)); t[1] = false; int mx = (int)sqrt(123456.0 * 2) + 1; for (int i = 2; i < mx; i++) { if (t[i]) { for (int j = i * 2; j < sizeof(t); j += i) { t[j] = false; } } } int a; while (cin >> a, a) { int count = 0; for (int i = a + 1; i < a * 2; i++) count += t[i]; cout << count << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string.h> #include <string> #include <utility> #include <vector> #ifdef _DEBUG #define typeof(X) std::identity<decltype(X)>::type // C++0x (for vs2010) #else #define typeof(X) __typeof__(X) // for gcc #endif #define sz(a) int((a).size()) #define FOREACH(it, c) \ for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define FOR(i, count) for (int i = 0; i < (count); i++) #define V_CIN(v) \ do { \ for (int i = 0; i < sz(v); i++) \ cin >> (v)[i]; \ } while (0) #define all(c) (c).begin(), (c).end() using namespace std; static const double EPS = 1e-5; typedef long long ll; const int MODULO = 1000000007; typedef pair<int, int> Pii; bool t[123456 * 2 + 10]; int main() { memset(t, 1, sizeof(t)); t[1] = false; int mx = (int)sqrt(123456.0 * 2) + 1; for (int i = 2; i < mx; i++) { if (t[i]) { for (int j = i * 2; j < sizeof(t); j += i) { t[j] = false; } } } int a; while (cin >> a, a) { int count = 0; for (int i = a + 1; i <= a * 2; i++) count += t[i]; cout << count << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
281
#include <cmath> #include <iostream> using namespace std; int main() { int n, count = 0; bool flag = false; while (true) { cin >> n; if (n == 0) break; count = 2 * n - n + 1; for (int i = 2 * n; i >= n; i--) { for (int j = 2; j <= sqrt(i); j++) { if (i % j == 0) { count--; // cout <<i<<endl; break; } } } cout << count << endl; } return 0; }
#include <cmath> #include <iostream> using namespace std; int main() { int n, count = 0; bool flag = false; while (true) { cin >> n; if (n == 0) break; count = 2 * n - n; for (int i = 2 * n; i > n; i--) { for (int j = 2; j <= sqrt(i); j++) { if (i % j == 0) { count--; // cout <<"not "<<i<<endl; break; } } } cout << count << endl; } return 0; }
[["-", 8, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["-", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 20], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 47]]
1
116
#include <iostream> using namespace std; #define MAX_N 30000 bool prime[MAX_N]; int main(int argc, char const *argv[]) { for (int i = 0; i < MAX_N; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int i = 0; i < MAX_N; i++) { if (prime[i]) { for (int j = i + i; j < MAX_N; j += i) { prime[j] = false; } } } int n; while (cin >> n, n) { int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) { cnt += prime[i]; } cout << cnt << endl; } return 0; }
#include <iostream> using namespace std; #define MAX_N 300000 bool prime[MAX_N]; int main(int argc, char const *argv[]) { for (int i = 0; i < MAX_N; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int i = 0; i < MAX_N; i++) { if (prime[i]) { for (int j = i + i; j < MAX_N; j += i) { prime[j] = false; } } } int n; while (cin >> n, n) { int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) { cnt += prime[i]; } cout << cnt << endl; } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59]]
1
172
#include <stdio.h> #define MAX_N 123456 int prime[MAX_N]; bool is_prime[MAX_N + 1]; int sieve(int n); int main(void) { int n = 1; int a; for (;;) { scanf("%d", &n); if (n == 0) break; a = sieve(2 * n) - sieve(n); printf("%d\n", a); } return 0; } int sieve(int n) { int p = 0; for (int i = 0; i <= n; i++) is_prime[i] = true; is_prime[0] = is_prime[1] = false; for (int i = 2; i <= n; i++) { if (is_prime[i]) { prime[p++] = i; for (int j = 2 * i; j <= n; j += i) is_prime[j] = false; } } return p; }
#include <stdio.h> #define MAX_N 246912 int prime[MAX_N]; bool is_prime[MAX_N + 1]; int sieve(int n); int main(void) { int n = 1; int a; for (;;) { scanf("%d", &n); if (n == 0) break; a = sieve(2 * n) - sieve(n); printf("%d\n", a); } return 0; } int sieve(int n) { int p = 0; for (int i = 0; i <= n; i++) is_prime[i] = true; is_prime[0] = is_prime[1] = false; for (int i = 2; i <= n; i++) { if (is_prime[i]) { prime[p++] = i; for (int j = 2 * i; j <= n; j += i) is_prime[j] = false; } } return p; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59]]
1
199
#include <stdio.h> #include <string.h> #define MAX (5000000) char prime[MAX]; int main(void) { memset(prime, 1, sizeof(prime)); prime[0] = prime[1] = 0; for (int i = 2; i < MAX; i++) { if (prime[i] == 1) { for (int j = i * 2; j < MAX; j += i) { prime[j] = 0; } } } while (1) { int n; int ans; scanf("%d", &n); if (n == 0) { break; } ans = 0; for (int i = n; i <= n * 2; i++) { ans += prime[i]; } printf("%d\n", ans); } return (0); }
#include <stdio.h> #include <string.h> #define MAX (5000000) char prime[MAX]; int main(void) { memset(prime, 1, sizeof(prime)); prime[0] = prime[1] = 0; for (int i = 2; i < MAX; i++) { if (prime[i] == 1) { for (int j = i * 2; j < MAX; j += i) { prime[j] = 0; } } } while (1) { int n; int ans; scanf("%d", &n); if (n == 0) { break; } ans = 0; for (int i = n + 1; i <= n * 2; i++) { ans += prime[i]; } printf("%d\n", ans); } return (0); }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
173
#include "bits/stdc++.h" using namespace std; #ifdef _DEBUG #include "dump.hpp" #else #define dump(...) #endif //#define int long long #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(c) begin(c), end(c) const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9) + 7; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } template <typename T> vector<T> compress(vector<T> v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v; } template <typename T> int index(const vector<T> &v, T i) { return lower_bound(v.begin(), v.end(), i) - v.begin(); } signed main() { cin.tie(0); ios::sync_with_stdio(false); for (int n; cin >> n && n;) { vector<string> v(n); vector<char> c; rep(i, 0, n) { cin >> v[i]; reverse(all(v[i])); rep(j, 0, v[i].size()) { if (v[i][j] >= '0' && v[i][j] <= '9') continue; c.push_back(v[i][j]); } c.insert(c.end(), v[i].begin(), v[i].end()); } vector<char> zip = compress(c); dump(zip); int mp[128]; memset(mp, -1, sizeof(mp)); rep(i, 0, 10) mp[i + '0'] = i; bool head[128]; memset(head, 0, sizeof(head)); rep(i, 0, n) { if (v[i].size() > 1) { head[v[i].back()] = false; } } char used[10]; memset(used, 0, sizeof(used)); function<int(int, int)> dfs = [&](int idx, int num) { int add = 0; rep(i, 0, 10) { int digit = add; add = 0; bool flag = false; rep(j, 0, n - 1) { if (v[j].size() <= i) continue; if (mp[v[j][i]] == -1) { flag = true; break; } digit += mp[v[j][i]]; } if (flag) break; add += digit / 10; digit %= 10; if (v[n - 1].size() > i) { if (mp[v[n - 1][i]] == -1) break; if (digit != mp[v[n - 1][i]]) return 0; } else { if (digit > 0 || add > 0) return 0; else break; } } if (zip.size() > idx + 1) { int ret = 0; rep(i, 0, 10) { if (used[i]) continue; if (i == 0 && head[zip[idx + 1]]) continue; used[i] = zip[idx + 1]; mp[zip[idx + 1]] = i; ret += dfs(idx + 1, i); mp[zip[idx + 1]] = -1; used[i] = 0; } return ret; } else { return 1; } }; int ans = 0; rep(i, 0, 10) { if (used[i]) continue; if (i == 0 && head[zip[0]]) continue; used[i] = zip[0]; mp[zip[0]] = i; ans += dfs(0, i); mp[zip[0]] = -1; used[i] = 0; } cout << ans << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; #ifdef _DEBUG #include "dump.hpp" #else #define dump(...) #endif //#define int long long #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(c) begin(c), end(c) const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9) + 7; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } template <typename T> vector<T> compress(vector<T> v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v; } template <typename T> int index(const vector<T> &v, T i) { return lower_bound(v.begin(), v.end(), i) - v.begin(); } signed main() { cin.tie(0); ios::sync_with_stdio(false); for (int n; cin >> n && n;) { vector<string> v(n); vector<char> c; rep(i, 0, n) { cin >> v[i]; reverse(all(v[i])); rep(j, 0, v[i].size()) { if (v[i][j] >= '0' && v[i][j] <= '9') continue; c.push_back(v[i][j]); } c.insert(c.end(), v[i].begin(), v[i].end()); } vector<char> zip = compress(c); dump(zip); int mp[128]; memset(mp, -1, sizeof(mp)); rep(i, 0, 10) mp[i + '0'] = i; bool head[128]; memset(head, 0, sizeof(head)); rep(i, 0, n) { if (v[i].size() > 1) { head[v[i].back()] = true; } } char used[10]; memset(used, 0, sizeof(used)); function<int(int, int)> dfs = [&](int idx, int num) { int add = 0; rep(i, 0, 10) { int digit = add; add = 0; bool flag = false; rep(j, 0, n - 1) { if (v[j].size() <= i) continue; if (mp[v[j][i]] == -1) { flag = true; break; } digit += mp[v[j][i]]; } if (flag) break; add += digit / 10; digit %= 10; if (v[n - 1].size() > i) { if (mp[v[n - 1][i]] == -1) break; if (digit != mp[v[n - 1][i]]) return 0; } else { if (digit > 0 || add > 0) return 0; else break; } } if (zip.size() > idx + 1) { int ret = 0; rep(i, 0, 10) { if (used[i]) continue; if (i == 0 && head[zip[idx + 1]]) continue; used[i] = zip[idx + 1]; mp[zip[idx + 1]] = i; ret += dfs(idx + 1, i); mp[zip[idx + 1]] = -1; used[i] = 0; } return ret; } else { return 1; } }; int ans = 0; rep(i, 0, 10) { if (used[i]) continue; if (i == 0 && head[zip[0]]) continue; used[i] = zip[0]; mp[zip[0]] = i; ans += dfs(0, i); mp[zip[0]] = -1; used[i] = 0; } cout << ans << endl; } return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 12, 147], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 146]]
1
982
#include <iomanip> #include <iostream> #include <queue> #include <vector> using namespace std; int N, M, S, G; struct state { int node, velocity; double ti; int pre_node; bool operator<(const state &right) const { return ti > right.ti; } }; struct edge { int to, dist, upp; }; double dp[31][31][31]; double inf = 1e9; int main() { while (cin >> N >> M && N > 0) { cin >> S >> G; vector<vector<edge>> v(N + 1); int x, y, d, c; for (int i = 0; i < M; i++) { cin >> x >> y >> d >> c; v[x].push_back({y, d, c}); v[y].push_back({x, d, c}); } for (int i = 1; i <= N; i++) for (int j = 1; j <= 30; j++) for (int k = 1; k <= N; k++) dp[i][j][k] = inf; dp[S][1][0] = 0; priority_queue<state> Q; Q.push({S, 0, 0, 0}); while (!Q.empty()) { state now = Q.top(); Q.pop(); if (dp[now.node][now.velocity][now.pre_node] < now.ti) continue; for (auto &x : v[now.node]) { if (now.pre_node == x.to) continue; for (int dv = -1; dv <= 1; dv++) { if (now.velocity + dv <= 0 || now.velocity + dv > x.upp) continue; state ne; ne.node = x.to; ne.velocity = now.velocity + dv; ne.ti = now.ti + 1.0 * x.dist / ne.velocity; ne.pre_node = now.node; if (dp[ne.node][ne.velocity][ne.pre_node] > ne.ti) { dp[ne.node][ne.velocity][ne.pre_node] = ne.ti; Q.push(ne); } } } } double ans = inf; for (int i = 1; i <= N; i++) ans = min(ans, dp[G][1][i]); if (ans >= inf) cout << "reachable" << endl; else { cout << fixed; cout << setprecision(10) << ans << endl; } } }
#include <iomanip> #include <iostream> #include <queue> #include <vector> using namespace std; int N, M, S, G; struct state { int node, velocity; double ti; int pre_node; bool operator<(const state &right) const { return ti > right.ti; } }; struct edge { int to, dist, upp; }; double dp[31][31][31]; double inf = 1e9; int main() { while (cin >> N >> M && N > 0) { cin >> S >> G; vector<vector<edge>> v(N + 1); int x, y, d, c; for (int i = 0; i < M; i++) { cin >> x >> y >> d >> c; v[x].push_back({y, d, c}); v[y].push_back({x, d, c}); } for (int i = 1; i <= N; i++) for (int j = 1; j <= 30; j++) for (int k = 1; k <= N; k++) dp[i][j][k] = inf; dp[S][1][0] = 0; priority_queue<state> Q; Q.push({S, 0, 0, 0}); while (!Q.empty()) { state now = Q.top(); Q.pop(); if (dp[now.node][now.velocity][now.pre_node] < now.ti) continue; for (auto &x : v[now.node]) { if (now.pre_node == x.to) continue; for (int dv = -1; dv <= 1; dv++) { if (now.velocity + dv <= 0 || now.velocity + dv > x.upp) continue; state ne; ne.node = x.to; ne.velocity = now.velocity + dv; ne.ti = now.ti + 1.0 * x.dist / ne.velocity; ne.pre_node = now.node; if (dp[ne.node][ne.velocity][ne.pre_node] > ne.ti) { dp[ne.node][ne.velocity][ne.pre_node] = ne.ti; Q.push(ne); } } } } double ans = inf; for (int i = 1; i <= N; i++) ans = min(ans, dp[G][1][i]); if (ans >= inf) cout << "unreachable" << endl; else { cout << fixed; cout << setprecision(10) << ans << endl; } } }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
566
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef ostringstream OSS; typedef istringstream ISS; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<double> VD; typedef vector<VD> VVD; typedef vector<VVD> VVVD; typedef long long LL; typedef pair<int, int> PII; typedef vector<PII> VPII; #define X first #define Y second const LL MOD = 1000000007; const double EPS = 1e-6; struct Edge { Edge(int t, int d, int s) : to(t), dist(d), speed(s){}; int to, dist, speed; }; typedef tuple<double, int, int, int> State; const double INF = DBL_MAX / 2; const int SPEED_MAX = 30; int main(void) { for (int N, M, S, G; cin >> N >> M, N;) { cin >> S >> G; S--; G--; vector<vector<Edge>> edges(N); for (int i = 0; i < M; i++) { int from, to, dist, speed; cin >> from >> to >> dist >> speed; from--; to--; edges[from].push_back(Edge(to, dist, speed)); edges[to].push_back(Edge(from, dist, speed)); } // current, before, speed VVVD times(N, VVD(N, VD(SPEED_MAX, INF))); times[S][S][0] = 0; priority_queue<State, vector<State>, greater<State>> q; q.emplace(0, S, S, 0); while (q.size()) { State p = q.top(); double now_time = get<0>(p); int current = get<1>(p); int before = get<2>(p); int speed = get<3>(p); q.pop(); if (times[current][before][speed] + EPS < now_time) { continue; } for (int di = -1; di <= 1; di++) { int next_speed = speed + di; if (next_speed < 1 || SPEED_MAX < next_speed) { continue; } for (auto &edge : edges[current]) { if (edge.to == before) { // U turn!! continue; } if (edge.speed < next_speed) { // スピード違反! continue; } auto &next_time = times[edge.to][current][next_speed]; double cost = 1. * edge.dist / next_speed; if (next_time > cost + now_time + EPS) { next_time = cost + now_time; q.emplace(next_time, edge.to, current, next_speed); } } } } double ans = INF; for (int i = 0; i < N; i++) { ans = min(ans, times[G][i][1]); } if (ans == INF) { cout << "unreachable" << endl; } else { cout << fixed << setprecision(5) << ans << endl; } } return 0; }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef ostringstream OSS; typedef istringstream ISS; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<double> VD; typedef vector<VD> VVD; typedef vector<VVD> VVVD; typedef long long LL; typedef pair<int, int> PII; typedef vector<PII> VPII; #define X first #define Y second const LL MOD = 1000000007; const double EPS = 1e-6; struct Edge { Edge(int t, int d, int s) : to(t), dist(d), speed(s){}; int to, dist, speed; }; typedef tuple<double, int, int, int> State; const double INF = DBL_MAX / 2; const int SPEED_MAX = 30; int main(void) { for (int N, M, S, G; cin >> N >> M, N;) { cin >> S >> G; S--; G--; vector<vector<Edge>> edges(N); for (int i = 0; i < M; i++) { int from, to, dist, speed; cin >> from >> to >> dist >> speed; from--; to--; edges[from].push_back(Edge(to, dist, speed)); edges[to].push_back(Edge(from, dist, speed)); } // current, before, speed VVVD times(N, VVD(N, VD(SPEED_MAX + 1, INF))); times[S][S][0] = 0; priority_queue<State, vector<State>, greater<State>> q; q.emplace(0, S, S, 0); while (q.size()) { State p = q.top(); double now_time = get<0>(p); int current = get<1>(p); int before = get<2>(p); int speed = get<3>(p); q.pop(); if (times[current][before][speed] + EPS < now_time) { continue; } for (int di = -1; di <= 1; di++) { int next_speed = speed + di; if (next_speed < 1 || SPEED_MAX < next_speed) { continue; } for (auto &edge : edges[current]) { if (edge.to == before) { // U turn!! continue; } if (edge.speed < next_speed) { // スピード違反! continue; } auto &next_time = times[edge.to][current][next_speed]; double cost = 1. * edge.dist / next_speed; if (next_time > cost + now_time + EPS) { next_time = cost + now_time; q.emplace(next_time, edge.to, current, next_speed); } } } } double ans = INF; for (int i = 0; i < N; i++) { ans = min(ans, times[G][i][1]); } if (ans == INF) { cout << "unreachable" << endl; } else { cout << fixed << setprecision(5) << ans << endl; } } return 0; }
[["+", 3, 4, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 3, 4, 0, 2, 3, 4, 0, 16, 12, 13]]
1
678
#include <cfloat> #include <iostream> #include <queue> using namespace std; int n, m; struct edge { int dist, lim; }; struct P { int from, pos, v; double time; bool operator>(const P &p) const { return time > p.time; } }; edge elist[31][31]; double G[31][31][31]; int main() { int s, g; while (cin >> n >> m, n | m) { for (int i = 0; i < 31; i++) { for (int j = 0; j < 31; j++) { elist[i][j] = {-1, -1}; } } fill_n((double *)G, 31 * 31 * 31, DBL_MAX); cin >> s >> g; for (int i = 0; i < m; i++) { int x, y, d, c; cin >> x >> y >> d >> c; elist[x][y] = {d, c}; elist[y][x] = {d, c}; } priority_queue<P, vector<P>, greater<P>> q; q.push({-1, s, 0, 0.0}); bool goal = false; while (!q.empty()) { P p = q.top(); q.pop(); // goal if (p.pos == g && p.v == 1) { printf("%f\n", p.time); goal = true; break; } for (int i = 1; i <= n; i++) { if (elist[p.pos][i].dist != -1 && p.from != i) { // v+1 if (p.v + 1 <= elist[p.pos][i].lim) { double time = p.time + (double)elist[p.pos][i].dist / (double)(p.v + 1); if (time < G[p.pos][i][p.v + 1]) { q.push({p.pos, i, p.v + 1, time}); G[p.pos][i][p.v + 1] = time; } } // v if (p.v <= elist[p.pos][i].lim) { double time = p.time + (double)elist[p.pos][i].dist / (double)p.v; if (time < G[p.pos][i][p.v]) { q.push({p.pos, i, p.v, time}); G[p.pos][i][p.v] = time; } } // v-1 if (p.v - 1 <= elist[p.pos][i].lim && p.v - 1 > 0) { double time = p.time + (double)elist[p.pos][i].dist / (double)(p.v - 1); if (time < G[p.pos][i][p.v - 1]) { q.push({p.pos, i, p.v - 1, time}); G[p.pos][i][p.v - 1] = time; } } } } } if (!goal) cout << "unreachble" << endl; } return 0; }
#include <cfloat> #include <iostream> #include <queue> using namespace std; int n, m; struct edge { int dist, lim; }; struct P { int from, pos, v; double time; bool operator>(const P &p) const { return time > p.time; } }; edge elist[31][31]; double G[31][31][31]; int main() { int s, g; while (cin >> n >> m, n | m) { for (int i = 0; i < 31; i++) { for (int j = 0; j < 31; j++) { elist[i][j] = {-1, -1}; } } fill_n((double *)G, 31 * 31 * 31, DBL_MAX); cin >> s >> g; for (int i = 0; i < m; i++) { int x, y, d, c; cin >> x >> y >> d >> c; elist[x][y] = {d, c}; elist[y][x] = {d, c}; } priority_queue<P, vector<P>, greater<P>> q; q.push({-1, s, 0, 0.0}); bool goal = false; while (!q.empty()) { P p = q.top(); q.pop(); // goal if (p.pos == g && p.v == 1) { printf("%f\n", p.time); goal = true; break; } for (int i = 1; i <= n; i++) { if (elist[p.pos][i].dist != -1 && p.from != i) { // v+1 if (p.v + 1 <= elist[p.pos][i].lim) { double time = p.time + (double)elist[p.pos][i].dist / (double)(p.v + 1); if (time < G[p.pos][i][p.v + 1]) { q.push({p.pos, i, p.v + 1, time}); G[p.pos][i][p.v + 1] = time; } } // v if (p.v <= elist[p.pos][i].lim) { double time = p.time + (double)elist[p.pos][i].dist / (double)p.v; if (time < G[p.pos][i][p.v]) { q.push({p.pos, i, p.v, time}); G[p.pos][i][p.v] = time; } } // v-1 if (p.v - 1 <= elist[p.pos][i].lim && p.v - 1 > 0) { double time = p.time + (double)elist[p.pos][i].dist / (double)(p.v - 1); if (time < G[p.pos][i][p.v - 1]) { q.push({p.pos, i, p.v - 1, time}); G[p.pos][i][p.v - 1] = time; } } } } } if (!goal) cout << "unreachable" << endl; } return 0; }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
743
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) \ ; using namespace std; const int MAX_V = 31; const int MAX_S = 31; const double INF = 9e9; int n, m, s, g; struct edge { int to, dist, speed; edge(int t_to, int t_dist, int t_speed) { to = t_to; dist = t_dist; speed = t_speed; } }; struct state { double cost; int c_v, p_v, speed; state(int t_c_v, int t_p_v, double t_cost, int t_speed) { c_v = t_c_v; p_v = t_p_v; cost = t_cost; speed = t_speed; } bool operator>(const state &s) const { return cost > s.cost; } }; int ds[] = {-1, 0, 1}; vector<vector<edge>> G(MAX_V); double d[MAX_V][MAX_V][MAX_S]; void dijkstra() { priority_queue<state, vector<state>, greater<state>> que; fill_n((double *)d, sizeof(d) / sizeof(double), INF); rep(i, G[s].size()) { edge t_e = G[s][i]; que.push(state(t_e.to, s, t_e.dist, 1)); d[t_e.to][s][1] = (double)t_e.dist; } while (!que.empty()) { state p = que.top(); que.pop(); int c_v = p.c_v; int p_v = p.p_v; if (p.c_v == g && p.speed == 1) { cout << p.cost << endl; return; } if (d[c_v][p_v][p.speed] < p.cost) continue; rep(i, G[c_v].size()) { edge e = G[c_v][i]; if (e.to == p_v) continue; rep(j, 3) { if (p.speed + ds[j] > 0 && p.speed + ds[j] <= e.speed) { double cost = p.cost + ((double)e.dist / (double)(p.speed + ds[j])); if (d[e.to][c_v][p.speed + ds[j]] > cost + 1e-8) { d[e.to][c_v][p.speed + ds[j]] = cost; que.push(state(e.to, c_v, cost, p.speed + ds[j])); } } } } } cout << "unreached" << endl; } int main() { cout << fixed << setprecision(6); while (1) { cin >> n >> m; if (n == 0 && m == 0) break; cin >> s >> g; s--; g--; G.clear(); G.resize(MAX_V); rep(i, m) { int a, b, dist, c; cin >> a >> b >> dist >> c; a--; b--; edge tmp1(b, dist, c); edge tmp2(a, dist, c); G[a].push_back(tmp1); G[b].push_back(tmp2); } dijkstra(); } }
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) \ ; using namespace std; const int MAX_V = 31; const int MAX_S = 31; const double INF = 9e9; int n, m, s, g; struct edge { int to, dist, speed; edge(int t_to, int t_dist, int t_speed) { to = t_to; dist = t_dist; speed = t_speed; } }; struct state { double cost; int c_v, p_v, speed; state(int t_c_v, int t_p_v, double t_cost, int t_speed) { c_v = t_c_v; p_v = t_p_v; cost = t_cost; speed = t_speed; } bool operator>(const state &s) const { return cost > s.cost; } }; int ds[] = {-1, 0, 1}; vector<vector<edge>> G(MAX_V); double d[MAX_V][MAX_V][MAX_S]; void dijkstra() { priority_queue<state, vector<state>, greater<state>> que; fill_n((double *)d, sizeof(d) / sizeof(double), INF); rep(i, G[s].size()) { edge t_e = G[s][i]; que.push(state(t_e.to, s, t_e.dist, 1)); d[t_e.to][s][1] = (double)t_e.dist; } while (!que.empty()) { state p = que.top(); que.pop(); int c_v = p.c_v; int p_v = p.p_v; if (p.c_v == g && p.speed == 1) { cout << p.cost << endl; return; } if (d[c_v][p_v][p.speed] < p.cost) continue; rep(i, G[c_v].size()) { edge e = G[c_v][i]; if (e.to == p_v) continue; rep(j, 3) { if (p.speed + ds[j] > 0 && p.speed + ds[j] <= e.speed) { double cost = p.cost + ((double)e.dist / (double)(p.speed + ds[j])); if (d[e.to][c_v][p.speed + ds[j]] > cost + 1e-8) { d[e.to][c_v][p.speed + ds[j]] = cost; que.push(state(e.to, c_v, cost, p.speed + ds[j])); } } } } } cout << "unreachable" << endl; } int main() { cout << fixed << setprecision(6); while (1) { cin >> n >> m; if (n == 0 && m == 0) break; cin >> s >> g; s--; g--; G.clear(); G.resize(MAX_V); rep(i, m) { int a, b, dist, c; cin >> a >> b >> dist >> c; a--; b--; edge tmp1(b, dist, c); edge tmp2(a, dist, c); G[a].push_back(tmp1); G[b].push_back(tmp2); } dijkstra(); } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
762
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define all(c) (c).begin(), (c).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define pb(e) push_back(e) #define mp(a, b) make_pair(a, b) #define fr first #define sc second const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; typedef pair<int, int> P; typedef long long ll; template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } struct Edge { int to, d, c; Edge(int to = 0, int d = 0, int c = 0) : to(to), d(d), c(c) {} }; struct State { int v, u; int spd; double t; State(int v = 0, int u = 0, int spd = 0, double t = 0) : v(v), u(u), spd(spd), t(t) {} bool operator<(const State &rhs) const { return t > rhs.t; } }; int s, g; int n, m; vector<Edge> G[40]; double dijk() { // v,u,spd; double d[40][40][40]; rep(i, 40) rep(j, 40) rep(k, 40) d[i][j][k] = INF; // rep(i,40) d[s][i][1]=1; d[s][s][0] = 0; priority_queue<State> que; que.push(State(s, s, 0)); while (que.size()) { State s = que.top(); que.pop(); rep(i, G[s.v].size()) { Edge e = G[s.v][i]; if (e.to == s.v) continue; for (int k = -1; k <= 1; k++) { if (!(0 <= s.spd + k && s.spd + k <= e.c)) continue; if (s.spd + k <= 0) continue; if (chmin(d[e.to][s.v][s.spd + k], d[s.v][s.u][s.spd] + 1.0 * e.d / (s.spd + k))) { que.push(State(e.to, s.v, s.spd + k, d[e.to][s.v][s.spd + k])); } } } } double ret = INF; rep(i, 40) chmin(ret, d[g][i][1]); return ret; } void solve() { rep(i, 40) G[i].clear(); cin >> s >> g; s--, g--; rep(i, m) { int x, y, d, c; cin >> x >> y >> d >> c; x--, y--; G[x].push_back(Edge(y, d, c)); G[y].push_back(Edge(x, d, c)); } double ans = dijk(); if (ans == INF) cout << "unreachable" << endl; else printf("%.10f\n", ans); } int main() { while (cin >> n >> m) { if (n == 0) break; solve(); } return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define all(c) (c).begin(), (c).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define pb(e) push_back(e) #define mp(a, b) make_pair(a, b) #define fr first #define sc second const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; typedef pair<int, int> P; typedef long long ll; template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } struct Edge { int to, d, c; Edge(int to = 0, int d = 0, int c = 0) : to(to), d(d), c(c) {} }; struct State { int v, u; int spd; double t; State(int v = 0, int u = 0, int spd = 0, double t = 0) : v(v), u(u), spd(spd), t(t) {} bool operator<(const State &rhs) const { return t > rhs.t; } }; int s, g; int n, m; vector<Edge> G[40]; double dijk() { // v,u,spd; double d[40][40][40]; rep(i, 40) rep(j, 40) rep(k, 40) d[i][j][k] = INF; // rep(i,40) d[s][i][1]=1; d[s][s][0] = 0; priority_queue<State> que; que.push(State(s, s, 0)); while (que.size()) { State s = que.top(); que.pop(); rep(i, G[s.v].size()) { Edge e = G[s.v][i]; if (e.to == s.u) continue; for (int k = -1; k <= 1; k++) { if (!(0 <= s.spd + k && s.spd + k <= e.c)) continue; if (s.spd + k <= 0) continue; if (chmin(d[e.to][s.v][s.spd + k], d[s.v][s.u][s.spd] + 1.0 * e.d / (s.spd + k))) { que.push(State(e.to, s.v, s.spd + k, d[e.to][s.v][s.spd + k])); } } } } double ret = INF; rep(i, 40) chmin(ret, d[g][i][1]); return ret; } void solve() { rep(i, 40) G[i].clear(); cin >> s >> g; s--, g--; rep(i, m) { int x, y, d, c; cin >> x >> y >> d >> c; x--, y--; G[x].push_back(Edge(y, d, c)); G[y].push_back(Edge(x, d, c)); } double ans = dijk(); if (ans == INF) cout << "unreachable" << endl; else printf("%.10f\n", ans); } int main() { while (cin >> n >> m) { if (n == 0) break; solve(); } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 118, 119, 120], ["+", 0, 57, 15, 339, 51, 16, 12, 118, 119, 120]]
1
806
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; class state { public: double cost; // 所要時間 int pp; // 前の位置 int np; // 今の位置 int v; // 速度 state(double _cost, int _pp, int _np, int _v) { cost = _cost; pp = _pp; np = _np; v = _v; } bool operator>(const state &s) const { return cost > s.cost; } }; int main() { int n, m; while (true) { cin >> n >> m; if (n == 0 && m == 0) { break; } int s, g; cin >> s >> g; s--; g--; if (m == 0) { std::cout << "unreachable" << std::endl; continue; } vector<int> graph[n]; double cost[n][n]; double dist[n][n]; for (int i = 0; i < m; i++) { int x, y; double d, c; cin >> x >> y >> d >> c; x--; y--; graph[x].push_back(y); graph[y].push_back(x); cost[x][y] = c; cost[y][x] = c; dist[x][y] = d; dist[y][x] = d; } double memo[n][n][32]; // prev now v for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < 32; k++) { memo[i][j][k] = -1; } } } // for (int i = 0; i < n; i++) { // std::cout << i << ":"; // for (int j = 0; j < graph[i].size(); j++) { // std::cout << graph[i][j] << " "; // } // std::cout << "" << std::endl; // } priority_queue<state, vector<state>, greater<state>> pque; for (int i = 0; i < graph[s].size(); i++) { int np = graph[s][i]; double nt = dist[s][np] / 1.; memo[s][np][1] = nt; pque.push(state(nt, -1, np, 1)); // 前の状態がないときは-1 } while (!pque.empty()) { state p = pque.top(); pque.pop(); for (int i = 0; i < graph[p.np].size(); i++) { int np = graph[p.np][i]; if (p.pp == np) { continue; } for (int j = -1; j <= 1; j++) { int v = p.v + j; if (v <= 0 || v > cost[p.np][np]) { continue; } double nt = p.cost + dist[p.np][np] / double(v); if (memo[p.np][np][v] == -1 || memo[p.np][np][v] > nt) { memo[p.np][np][v] = nt; pque.push(state(nt, p.np, np, v)); } } } } double ans = -1; for (int i = 0; i < n; i++) { if (memo[i][g][1] == -1) { continue; } if (ans == -1 || memo[i][g][1] < ans) { ans = memo[i][g][1]; } } if (ans == -1) { std::cout << "unreachable" << std::endl; } else { printf("%.5f\n", ans); } } }
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; class state { public: double cost; // 所要時間 int pp; // 前の位置 int np; // 今の位置 int v; // 速度 state(double _cost, int _pp, int _np, int _v) { cost = _cost; pp = _pp; np = _np; v = _v; } bool operator>(const state &s) const { return cost > s.cost; } }; int main() { int n, m; while (true) { cin >> n >> m; if (n == 0 && m == 0) { break; } int s, g; cin >> s >> g; s--; g--; if (m == 0) { std::cout << "unreachable" << std::endl; continue; } vector<int> graph[n]; double cost[n][n]; double dist[n][n]; for (int i = 0; i < m; i++) { int x, y; double d, c; cin >> x >> y >> d >> c; x--; y--; graph[x].push_back(y); graph[y].push_back(x); cost[x][y] = c; cost[y][x] = c; dist[x][y] = d; dist[y][x] = d; } double memo[n][n][32]; // prev now v for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < 32; k++) { memo[i][j][k] = -1; } } } priority_queue<state, vector<state>, greater<state>> pque; for (int i = 0; i < graph[s].size(); i++) { int np = graph[s][i]; double nt = dist[s][np]; memo[s][np][1] = nt; pque.push(state(nt, s, np, 1)); } while (!pque.empty()) { state p = pque.top(); pque.pop(); for (int i = 0; i < graph[p.np].size(); i++) { int np = graph[p.np][i]; if (p.pp == np) { continue; } for (int j = -1; j <= 1; j++) { int v = p.v + j; if (v <= 0 || v > cost[p.np][np]) { continue; } double nt = p.cost + dist[p.np][np] / double(v); if (memo[p.np][np][v] == -1 || memo[p.np][np][v] > nt) { memo[p.np][np][v] = nt; pque.push(state(nt, p.np, np, v)); } } } } double ans = -1; for (int i = 0; i < n; i++) { if (memo[i][g][1] == -1) { continue; } if (ans == -1 || memo[i][g][1] < ans) { ans = memo[i][g][1]; } } if (ans == -1) { std::cout << "unreachable" << std::endl; } else { printf("%.5f\n", ans); } } }
[["-", 8, 9, 0, 43, 49, 50, 51, 16, 17, 85], ["-", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13], ["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 13], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 22]]
1
788
#include <cstdio> #include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; #define EPS (double)1e-9 #define INF (double)1e9 #define MAX_SPEED 30 vector<vector<vector<double>>> dijk(int s_point, int s_speed, vector<vector<vector<pair<double, pair<int, int>>>>> &adj) { int v = adj.size(); int t = adj[0].size(); vector<vector<vector<double>>> result( v, vector<vector<double>>(v, vector<double>(t, INF))); priority_queue<pair<double, pair<int, pair<int, int>>>> wait; wait.push(make_pair(0, make_pair(0, make_pair(s_point, s_speed)))); for (int i = 0; i < v; i++) { result[s_point][i][s_speed] = 0; } while (!wait.empty()) { double nowcost = -wait.top().first; int prevpoint = wait.top().second.first; int nowpoint = wait.top().second.second.first; int nowspeed = wait.top().second.second.second; wait.pop(); if (result[nowpoint][prevpoint][nowspeed] + EPS < nowcost) { continue; } for (size_t i = 0; i < adj[nowpoint][nowspeed].size(); i++) { double nextcost = adj[nowpoint][nowspeed][i].first + nowcost; int nextpoint = adj[nowpoint][nowspeed][i].second.first; int nextspeed = adj[nowpoint][nowspeed][i].second.second; if (nextpoint != prevpoint && result[nextpoint][nowpoint][nextspeed] - EPS > nextcost) { wait.push(make_pair( -nextcost, make_pair(nowpoint, make_pair(nextpoint, nextspeed)))); result[nextpoint][nowpoint][nextspeed] = nextcost; } } } return result; } int main(void) { while (true) { int n, m; cin >> n >> m; if (n == 0 && m == 0) { break; } int s, g; cin >> s >> g; int x, y, d, c; vector<vector<vector<pair<double, pair<int, int>>>>> adjlist( n + 1, vector<vector<pair<double, pair<int, int>>>>(MAX_SPEED + 1)); for (int i = 0; i < m; i++) { cin >> x >> y >> d >> c; for (int j = 0; j <= MAX_SPEED; j++) { if (1 <= j - 1 && j - 1 <= c) { adjlist[x][j].push_back( make_pair((double)d / (j - 1), make_pair(y, j - 1))); adjlist[y][j].push_back( make_pair((double)d / (j - 1), make_pair(x, j - 1))); } if (1 <= j && j <= c) { adjlist[x][j].push_back(make_pair((double)d / j, make_pair(y, j))); adjlist[y][j].push_back(make_pair((double)d / j, make_pair(x, j))); } if (j + 1 <= c) { adjlist[x][j].push_back( make_pair((double)d / (j + 1), make_pair(y, j + 1))); adjlist[y][j].push_back( make_pair((double)d / (j + 1), make_pair(x, j + 1))); } } } double ans = INF; vector<vector<vector<double>>> result = dijk(s, 0, adjlist); for (int i = 0; i < n; i++) { ans = min(ans, result[g][i][1]); } if (ans + EPS >= INF) { printf("unreachable\n"); } else { printf("%.10f\n", ans); } } return 0; }
#include <cstdio> #include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; #define EPS (double)1e-9 #define INF (double)1e9 #define MAX_SPEED 33 vector<vector<vector<double>>> dijk(int s_point, int s_speed, vector<vector<vector<pair<double, pair<int, int>>>>> &adj) { int v = adj.size(); int t = adj[0].size(); vector<vector<vector<double>>> result( v, vector<vector<double>>(v, vector<double>(t, INF))); priority_queue<pair<double, pair<int, pair<int, int>>>> wait; wait.push(make_pair(0, make_pair(0, make_pair(s_point, s_speed)))); for (int i = 0; i < v; i++) { result[s_point][i][s_speed] = 0; } while (!wait.empty()) { double nowcost = -wait.top().first; int prevpoint = wait.top().second.first; int nowpoint = wait.top().second.second.first; int nowspeed = wait.top().second.second.second; wait.pop(); if (result[nowpoint][prevpoint][nowspeed] + EPS < nowcost) { continue; } for (size_t i = 0; i < adj[nowpoint][nowspeed].size(); i++) { double nextcost = adj[nowpoint][nowspeed][i].first + nowcost; int nextpoint = adj[nowpoint][nowspeed][i].second.first; int nextspeed = adj[nowpoint][nowspeed][i].second.second; if (nextpoint != prevpoint && result[nextpoint][nowpoint][nextspeed] - EPS > nextcost) { wait.push(make_pair( -nextcost, make_pair(nowpoint, make_pair(nextpoint, nextspeed)))); result[nextpoint][nowpoint][nextspeed] = nextcost; } } } return result; } int main(void) { while (true) { int n, m; cin >> n >> m; if (n == 0 && m == 0) { break; } int s, g; cin >> s >> g; int x, y, d, c; vector<vector<vector<pair<double, pair<int, int>>>>> adjlist( n + 1, vector<vector<pair<double, pair<int, int>>>>(MAX_SPEED + 1)); for (int i = 0; i < m; i++) { cin >> x >> y >> d >> c; for (int j = 0; j <= MAX_SPEED; j++) { if (1 <= j - 1 && j - 1 <= c) { adjlist[x][j].push_back( make_pair((double)d / (j - 1), make_pair(y, j - 1))); adjlist[y][j].push_back( make_pair((double)d / (j - 1), make_pair(x, j - 1))); } if (1 <= j && j <= c) { adjlist[x][j].push_back(make_pair((double)d / j, make_pair(y, j))); adjlist[y][j].push_back(make_pair((double)d / j, make_pair(x, j))); } if (j + 1 <= c) { adjlist[x][j].push_back( make_pair((double)d / (j + 1), make_pair(y, j + 1))); adjlist[y][j].push_back( make_pair((double)d / (j + 1), make_pair(x, j + 1))); } } } double ans = INF; vector<vector<vector<double>>> result = dijk(s, 0, adjlist); for (int i = 0; i <= n; i++) { ans = min(ans, result[g][i][1]); } if (ans + EPS >= INF) { printf("unreachable\n"); } else { printf("%.10f\n", ans); } } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
904
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> Pi; typedef pair<double, int> Pd; typedef pair<Pd, Pi> PP; struct edge { int to, lim; double dist; edge() {} edge(int to, int lim, double dist) : to(to), lim(lim), dist(dist) {} }; vector<vector<edge>> G; double mint[33][33][33]; int n, m, s, g; const double inf = 1 << 25; void init() { for (int i = 0; i < 33; i++) { for (int j = 0; j < 33; j++) { for (int k = 0; k < 33; k++) mint[i][j][k] = inf; } } } void dijkstra() { init(); priority_queue<PP, vector<PP>, greater<PP>> que; mint[s][0][0] = 0.0; que.push(PP(Pd(0.0, 0), Pi(s, 0))); while (!que.empty()) { PP p = que.top(); que.pop(); int now = p.second.first, prev = p.second.second, v = p.first.second; double t = p.first.first; if (now == g && v == 1) continue; if (mint[now][prev][v] < t) continue; for (int i = 0; i < G[now].size(); i++) { edge e = G[now][i]; if (e.to == prev) continue; for (int d = -1; d <= 1; d++) { if (v + d <= 0 || e.lim < v + d) continue; if (t + e.dist / (v + d) < mint[e.to][now][v + d]) { mint[e.to][now][v + d] = t + e.dist / (v + d); que.push(PP(Pd(mint[e.to][now][v + d], v + d), Pi(e.to, now))); } } } } } int main() { while (cin >> n >> m, n) { cin >> s >> g; G.clear(); G.resize(n + 1); while (m--) { int x, y, d, c; cin >> x >> y >> d >> c; G[x].push_back(edge(y, c, d)); G[y].push_back(edge(x, c, d)); } dijkstra(); double ans = inf; for (int i = 0; i < n; i++) ans = min(ans, mint[g][i][1]); if (ans == inf) puts("unreachable"); else printf("%.4f\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> Pi; typedef pair<double, int> Pd; typedef pair<Pd, Pi> PP; struct edge { int to, lim; double dist; edge() {} edge(int to, int lim, double dist) : to(to), lim(lim), dist(dist) {} }; vector<vector<edge>> G; double mint[33][33][33]; int n, m, s, g; const double inf = 1 << 25; void init() { for (int i = 0; i < 33; i++) { for (int j = 0; j < 33; j++) { for (int k = 0; k < 33; k++) mint[i][j][k] = inf; } } } void dijkstra() { init(); priority_queue<PP, vector<PP>, greater<PP>> que; mint[s][0][0] = 0.0; que.push(PP(Pd(0.0, 0), Pi(s, 0))); while (!que.empty()) { PP p = que.top(); que.pop(); int now = p.second.first, prev = p.second.second, v = p.first.second; double t = p.first.first; if (now == g && v == 1) continue; if (mint[now][prev][v] < t) continue; for (int i = 0; i < G[now].size(); i++) { edge e = G[now][i]; if (e.to == prev) continue; for (int d = -1; d <= 1; d++) { if (v + d <= 0 || e.lim < v + d) continue; if (t + e.dist / (v + d) < mint[e.to][now][v + d]) { mint[e.to][now][v + d] = t + e.dist / (v + d); que.push(PP(Pd(mint[e.to][now][v + d], v + d), Pi(e.to, now))); } } } } } int main() { while (cin >> n >> m, n) { cin >> s >> g; G.clear(); G.resize(n + 1); while (m--) { int x, y, d, c; cin >> x >> y >> d >> c; G[x].push_back(edge(y, c, d)); G[y].push_back(edge(x, c, d)); } dijkstra(); double ans = inf; for (int i = 1; i <= n; i++) ans = min(ans, mint[g][i][1]); if (ans == inf) puts("unreachable"); else printf("%.12f\n", ans); } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
674
#include <bits/stdc++.h> #define INF 0x3f3f3f3f #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; struct P { int to, c, d; }; vector<P> E[30]; double d[30][31][30]; struct st { int p, h, pr; double c; }; bool operator<(st a, st b) { return a.c > b.c; } int main() { int n, m; while (scanf("%d%d", &n, &m), n) { int s, g; scanf("%d%d", &s, &g); s--; g--; rep(i, n) E[i].clear(); rep(i, m) { int x, y, d, c; scanf("%d%d%d%d", &x, &y, &d, &c); x--; y--; E[x].push_back({y, c, d}); E[y].push_back({x, c, d}); } priority_queue<st> que; fill(d[0][0], d[30][0], INF); for (P v : E[s]) { d[v.to][1][s] = v.d; que.push({v.to, 1, 0, d[v.to][1][s]}); } while (!que.empty()) { st p = que.top(); que.pop(); if (d[p.p][p.h][p.pr] != p.c) continue; for (P v : E[p.p]) { if (v.to == p.pr) continue; for (int i = -1; i <= 1; i++) { if (p.h + i > 0 && p.h + i <= v.c) { if (d[v.to][p.h + i][p.p] > p.c + v.d / double(p.h + i)) { d[v.to][p.h + i][p.p] = p.c + v.d / double(p.h + i); que.push({v.to, p.h + i, p.p, d[v.to][p.h + i][p.p]}); } } } } } double Min = INF; rep(i, n) Min = min(Min, d[g][1][i]); if (Min == INF) puts("unreachable"); else printf("%.7lf\n", Min); } }
#include <bits/stdc++.h> #define INF 0x3f3f3f3f #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; struct P { int to, c, d; }; vector<P> E[30]; double d[30][31][30]; struct st { int p, h, pr; double c; }; bool operator<(st a, st b) { return a.c > b.c; } int main() { int n, m; while (scanf("%d%d", &n, &m), n) { int s, g; scanf("%d%d", &s, &g); s--; g--; rep(i, n) E[i].clear(); rep(i, m) { int x, y, d, c; scanf("%d%d%d%d", &x, &y, &d, &c); x--; y--; E[x].push_back({y, c, d}); E[y].push_back({x, c, d}); } priority_queue<st> que; fill(d[0][0], d[30][0], INF); for (P v : E[s]) { d[v.to][1][s] = v.d; que.push({v.to, 1, s, d[v.to][1][s]}); } while (!que.empty()) { st p = que.top(); que.pop(); if (d[p.p][p.h][p.pr] != p.c) continue; for (P v : E[p.p]) { if (v.to == p.pr) continue; for (int i = -1; i <= 1; i++) { if (p.h + i > 0 && p.h + i <= v.c) { if (d[v.to][p.h + i][p.p] > p.c + v.d / double(p.h + i)) { d[v.to][p.h + i][p.p] = p.c + v.d / double(p.h + i); que.push({v.to, p.h + i, p.p, d[v.to][p.h + i][p.p]}); } } } } } double Min = INF; rep(i, n) Min = min(Min, d[g][1][i]); if (Min == INF) puts("unreachable"); else printf("%.7lf\n", Min); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 83, 0, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 83, 0, 22]]
1
598
#include <bits/stdc++.h> #define S second #define F first #define EPS (1e-7) #define INF (1e9) #define N 31 using namespace std; typedef pair<int, int> P1; typedef pair<int, P1> P2; typedef pair<int, P1> P3; typedef pair<P3, int> P4; vector<P2> G[N]; int n, m, s, g; double dijkstra() { double d[N][N][N]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < N; k++) d[i][j][k] = INF; priority_queue<P4, vector<P4>, greater<P4>> q; for (int i = 0; i < G[s].size(); i++) { int nx = G[s][i].first, cost = G[s][i].second.first; d[s][nx][1] = cost; q.push(P4(P3(cost, P1(s, nx)), 1)); } while (!q.empty()) { P4 t = q.top(); q.pop(); int px = t.F.S.F, x = t.F.S.S, v = t.S; double cost = t.F.F; if (d[px][x][v] + EPS < cost) continue; if (x == g && v == 1) return cost; for (int i = 0; i < G[x].size(); i++) { int nx = G[x][i].F, cost2 = G[x][i].S.F; int lim = G[x][i].S.S; if (nx == px) continue; for (int j = -1; j <= 1; j++) { int nv = v + j; if (nv < 1 || 30 < nv || nv > lim) continue; double ncost = cost + 1.0 * cost2 / nv; if (d[x][nx][nv] > ncost + EPS) { d[x][nx][nv] = ncost; q.push(P4(P3(ncost, P1(x, nx)), nv)); } } } } return -1; } int main() { while (1) { cin >> n >> m; if (!n && !m) break; cin >> s >> g; s--, g--; int a, b, c, d; for (int i = 0; i < m; i++) { cin >> a >> b >> c >> d; a--, b--; G[a].push_back(P2(b, P1(c, d))); G[b].push_back(P2(a, P1(c, d))); } double r = dijkstra(); if (r == -1) cout << "unreachable" << endl; else printf("%.5f\n", r); for (int i = 0; i < n; i++) G[i].clear(); } return 0; }
#include <bits/stdc++.h> #define S second #define F first #define EPS (1e-7) #define INF (1e9) #define N 31 using namespace std; typedef pair<int, int> P1; typedef pair<int, P1> P2; typedef pair<double, P1> P3; typedef pair<P3, int> P4; vector<P2> G[N]; int n, m, s, g; double dijkstra() { double d[N][N][N]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < N; k++) d[i][j][k] = INF; priority_queue<P4, vector<P4>, greater<P4>> q; for (int i = 0; i < G[s].size(); i++) { int nx = G[s][i].first, cost = G[s][i].second.first; d[s][nx][1] = cost; q.push(P4(P3(cost, P1(s, nx)), 1)); } while (!q.empty()) { P4 t = q.top(); q.pop(); int px = t.F.S.F, x = t.F.S.S, v = t.S; double cost = t.F.F; if (d[px][x][v] + EPS < cost) continue; if (x == g && v == 1) return cost; for (int i = 0; i < G[x].size(); i++) { int nx = G[x][i].F, cost2 = G[x][i].S.F; int lim = G[x][i].S.S; if (nx == px) continue; for (int j = -1; j <= 1; j++) { int nv = v + j; if (nv < 1 || 30 < nv || nv > lim) continue; double ncost = cost + 1.0 * cost2 / nv; if (d[x][nx][nv] > ncost + EPS) { d[x][nx][nv] = ncost; q.push(P4(P3(ncost, P1(x, nx)), nv)); } } } } return -1; } int main() { while (1) { cin >> n >> m; if (!n && !m) break; cin >> s >> g; s--, g--; int a, b, c, d; for (int i = 0; i < m; i++) { cin >> a >> b >> c >> d; a--, b--; G[a].push_back(P2(b, P1(c, d))); G[b].push_back(P2(a, P1(c, d))); } double r = dijkstra(); if (r == -1) cout << "unreachable" << endl; else printf("%.5f\n", r); for (int i = 0; i < n; i++) G[i].clear(); } return 0; }
[["-", 0, 134, 39, 344, 3, 347, 0, 77, 39, 40], ["+", 0, 134, 39, 344, 3, 347, 0, 77, 39, 40]]
1
702
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define rep(i, n) repl(i, 0, n) #define each(itr, v) for (auto itr : v) #define pb(s) push_back(s) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x " = " << x << endl #define print(x) cout << x << endl #define maxch(x, y) x = max(x, y) #define minch(x, y) x = min(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt(x) bitset<32>(x).count() typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<P, int> PPI; typedef pair<int, P> PIP; typedef pair<ll, ll> PL; typedef pair<P, ll> PPL; typedef set<int> S; #define INF INT_MAX / 3 #define MAX_N 1000000001 int dv[] = {-1, 0, 1}; struct state { int city; int from; double cost; int v; }; bool operator<(const state &a, const state &b) { return a.cost > b.cost; } int main() { cin.sync_with_stdio(false); int n, m; while (cin >> n >> m, n) { int s, g; cin >> s >> g; s--; g--; int d[n][n] = {}, limit[n][n] = {}; rep(i, m) { int x, y; cin >> x >> y; x--, y--; cin >> d[x][y] >> limit[x][y]; d[y][x] = d[x][y]; limit[y][x] = limit[y][x]; } priority_queue<state> q; bool visited[n][n][30] = {}; q.push(state{s, s, 0, 0}); double ans = INF; while (!q.empty()) { state st = q.top(); q.pop(); if (st.city == g && st.v == 1) { ans = st.cost; break; } if (visited[st.city][st.from][st.v]) continue; else visited[st.city][st.from][st.v] = true; rep(i, n) { if (d[st.city][i] != 0 && i != st.from) { rep(j, 3) { if (st.v + dv[j] > 0 && st.v + dv[j] <= limit[st.city][i] && !visited[i][st.city][st.v + dv[j]]) { q.push(state{i, st.city, st.cost + (double)d[st.city][i] / (st.v + dv[j]), st.v + dv[j]}); } } } } } if (ans != INF) { printf("%.5f\n", ans); } else { print("unreachable"); } } return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define rep(i, n) repl(i, 0, n) #define each(itr, v) for (auto itr : v) #define pb(s) push_back(s) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x " = " << x << endl #define print(x) cout << x << endl #define maxch(x, y) x = max(x, y) #define minch(x, y) x = min(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt(x) bitset<32>(x).count() typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<P, int> PPI; typedef pair<int, P> PIP; typedef pair<ll, ll> PL; typedef pair<P, ll> PPL; typedef set<int> S; #define INF INT_MAX / 3 #define MAX_N 1000000001 int dv[] = {-1, 0, 1}; struct state { int city; int from; double cost; int v; }; bool operator<(const state &a, const state &b) { return a.cost > b.cost; } int main() { cin.sync_with_stdio(false); int n, m; while (cin >> n >> m, n) { int s, g; cin >> s >> g; s--; g--; int d[n][n] = {}, limit[n][n] = {}; rep(i, m) { int x, y; cin >> x >> y; x--, y--; cin >> d[x][y] >> limit[x][y]; d[y][x] = d[x][y]; limit[y][x] = limit[x][y]; } priority_queue<state> q; bool visited[n][n][31] = {}; q.push(state{s, s, 0, 0}); double ans = INF; while (!q.empty()) { state st = q.top(); q.pop(); if (st.city == g && st.v == 1) { ans = st.cost; break; } if (visited[st.city][st.from][st.v]) continue; else visited[st.city][st.from][st.v] = true; rep(i, n) { if (d[st.city][i] != 0 && i != st.from) { rep(j, 3) { if (st.v + dv[j] > 0 && st.v + dv[j] <= limit[st.city][i] && !visited[i][st.city][st.v + dv[j]]) { q.push(state{i, st.city, st.cost + (double)d[st.city][i] / (st.v + dv[j]), st.v + dv[j]}); } } } } } if (ans != INF) { printf("%.5f\n", ans); } else { printf("unreachable\n"); } } return 0; }
[["-", 0, 11, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 12, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 12, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 12, 69, 341, 342, 0, 22], ["-", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["-", 75, 76, 0, 9, 0, 1, 0, 2, 63, 22], ["+", 75, 76, 0, 9, 0, 1, 0, 2, 63, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
683
#include <iostream> #include <queue> #include <vector> using namespace std; class Node { public: int pre; int now; int vel; double time; Node(int p, int n, int v, double t) { pre = p; now = n; vel = v; time = t; } bool operator<(const Node &n) const { return time > n.time; } }; int main() { while (true) { int n, m; cin >> n >> m; if (n == 0) { break; } int s, g; cin >> s >> g; vector<vector<int>> d(n + 1), c(n + 1); for (int i = 0; i <= n; i++) { d[i].resize(n + 1); c[i].resize(n + 1); fill(d[i].begin(), d[i].end(), -1); fill(c[i].begin(), c[i].end(), 0); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; cin >> d[x][y] >> c[x][y]; d[y][x] = d[x][y]; c[y][x] = c[x][y]; } bool f[31][31][31]; for (int i = 0; i < 31; i++) { for (int j = 0; j < 31; j++) { for (int k = 0; k < 31; k++) { f[i][j][k] = false; } } } bool goal = false; Node snd(0, 1, 0, 0.0); priority_queue<Node> q; q.push(snd); while (!q.empty()) { Node nd = q.top(); q.pop(); if (nd.now == g && nd.vel == 1) { goal = true; cout << nd.time << endl; break; } if (f[nd.pre][nd.now][nd.vel]) { continue; } f[nd.pre][nd.now][nd.vel] = true; for (int i = 1; i <= n; i++) { if (i != nd.now && d[nd.now][i] != -1) { for (int j = -1; j <= 1; j++) { int vel = nd.vel + j; if (vel > 0 && vel <= c[nd.now][i]) { Node next(nd.now, i, vel, nd.time + (double)d[nd.now][i] / vel); q.push(next); } } } } } if (!goal) { cout << "unreachable" << endl; } } return 0; }
#include <iostream> #include <queue> #include <vector> using namespace std; class Node { public: int pre; int now; int vel; double time; Node(int p, int n, int v, double t) { pre = p; now = n; vel = v; time = t; } bool operator<(const Node &n) const { return time > n.time; } }; int main() { while (true) { int n, m; cin >> n >> m; if (n == 0) { break; } int s, g; cin >> s >> g; vector<vector<int>> d(n + 1), c(n + 1); for (int i = 0; i <= n; i++) { d[i].resize(n + 1); c[i].resize(n + 1); fill(d[i].begin(), d[i].end(), -1); fill(c[i].begin(), c[i].end(), 0); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; cin >> d[x][y] >> c[x][y]; d[y][x] = d[x][y]; c[y][x] = c[x][y]; } bool f[31][31][31]; for (int i = 0; i < 31; i++) { for (int j = 0; j < 31; j++) { for (int k = 0; k < 31; k++) { f[i][j][k] = false; } } } bool goal = false; Node snd(0, s, 0, 0.0); priority_queue<Node> q; q.push(snd); while (!q.empty()) { Node nd = q.top(); q.pop(); if (nd.now == g && nd.vel == 1) { goal = true; cout << nd.time << endl; break; } if (f[nd.pre][nd.now][nd.vel]) { continue; } f[nd.pre][nd.now][nd.vel] = true; for (int i = 1; i <= n; i++) { if (i != nd.pre && d[nd.now][i] != -1) { for (int j = -1; j <= 1; j++) { int vel = nd.vel + j; if (vel > 0 && vel <= c[nd.now][i]) { Node next(nd.now, i, vel, nd.time + (double)d[nd.now][i] / vel); q.push(next); } } } } } if (!goal) { cout << "unreachable" << endl; } } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 4, 0, 22], ["-", 15, 339, 51, 16, 31, 16, 12, 118, 119, 120], ["+", 15, 339, 51, 16, 31, 16, 12, 118, 119, 120]]
1
648
#include <algorithm> #include <iomanip> #include <iostream> #include <queue> #include <vector> using namespace std; const int S_MAX = 30; const double inf = 1e9; struct info { int curr; int prev; int v; double t; info(int c, int p, int v, double t) : curr(c), prev(p), v(v), t(t) {} }; namespace std { bool operator<(const info &a, const info &b) { return b.t < a.t; } } // namespace std int main() { while (1) { int n, m; cin >> n >> m; if (n == 0) break; int s, g; cin >> s >> g; vector<vector<vector<info>>> adj(n + 1, vector<vector<info>>(S_MAX + 1)); for (int i = 0; i < m; i++) { int x, y, d, c; cin >> x >> y >> d >> c; for (int v = 1; v <= S_MAX; v++) { for (int r = -1; r <= 1; r++) { if (v + r > 0 && v + r <= c) { adj[x][v].push_back(info(y, x, v + r, (double)d / (v + r))); adj[y][v].push_back(info(x, y, v + r, (double)d / (v + r))); } } } } priority_queue<info> pq; pq.push(info(s, 0, 1, 0)); vector<vector<vector<double>>> mincost( n + 1, vector<vector<double>>(n + 1, vector<double>(S_MAX, inf))); mincost[s][0][1] = 0; while (!pq.empty()) { int curr = pq.top().curr; int prev = pq.top().prev; int v = pq.top().v; double cost = pq.top().t; pq.pop(); if (cost > mincost[curr][prev][v]) continue; if (curr == g && v == 1) break; for (int i = 0; i < (int)adj[curr][v].size(); i++) { info next = adj[curr][v][i]; if (next.curr == prev) continue; if (prev == 0 && next.v != 1) continue; if (cost + next.t < mincost[next.curr][next.prev][next.v]) { pq.push(info(next.curr, next.prev, next.v, cost + next.t)); mincost[next.curr][next.prev][next.v] = cost + next.t; } } } double ans = inf; for (int i = 1; i <= n; i++) { ans = min(ans, mincost[g][i][1]); } if (ans == inf) { cout << "unreachable" << endl; } else { cout << fixed; cout << setprecision(10); cout << ans << endl; } } return 0; }
#include <algorithm> #include <iomanip> #include <iostream> #include <queue> #include <vector> using namespace std; const int S_MAX = 30; const double inf = 1e9; struct info { int curr; int prev; int v; double t; info(int c, int p, int v, double t) : curr(c), prev(p), v(v), t(t) {} }; namespace std { bool operator<(const info &a, const info &b) { return b.t < a.t; } } // namespace std int main() { while (1) { int n, m; cin >> n >> m; if (n == 0) break; int s, g; cin >> s >> g; vector<vector<vector<info>>> adj(n + 1, vector<vector<info>>(S_MAX + 1)); for (int i = 0; i < m; i++) { int x, y, d, c; cin >> x >> y >> d >> c; for (int v = 1; v <= S_MAX; v++) { for (int r = -1; r <= 1; r++) { if (v + r > 0 && v + r <= c) { adj[x][v].push_back(info(y, x, v + r, (double)d / (v + r))); adj[y][v].push_back(info(x, y, v + r, (double)d / (v + r))); } } } } priority_queue<info> pq; pq.push(info(s, 0, 1, 0)); vector<vector<vector<double>>> mincost( n + 1, vector<vector<double>>(n + 1, vector<double>(S_MAX + 1, inf))); mincost[s][0][1] = 0; while (!pq.empty()) { int curr = pq.top().curr; int prev = pq.top().prev; int v = pq.top().v; double cost = pq.top().t; pq.pop(); if (cost > mincost[curr][prev][v]) continue; if (curr == g && v == 1) break; for (int i = 0; i < (int)adj[curr][v].size(); i++) { info next = adj[curr][v][i]; if (next.curr == prev) continue; if (prev == 0 && next.v != 1) continue; if (cost + next.t < mincost[next.curr][next.prev][next.v]) { pq.push(info(next.curr, next.prev, next.v, cost + next.t)); mincost[next.curr][next.prev][next.v] = cost + next.t; } } } double ans = inf; for (int i = 1; i <= n; i++) { ans = min(ans, mincost[g][i][1]); } if (ans == inf) { cout << "unreachable" << endl; } else { cout << fixed; cout << setprecision(12); cout << ans << endl; } } return 0; }
[["+", 3, 4, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 3, 4, 0, 2, 3, 4, 0, 16, 12, 13], ["-", 0, 1, 0, 16, 12, 2, 3, 4, 0, 13], ["+", 0, 1, 0, 16, 12, 2, 3, 4, 0, 13]]
1
716
#include <iostream> #include <queue> #include <tuple> #include <vector> using namespace std; const double INF = 1e10; struct Edge { int cost, to, c; Edge(int to, int cost, int c) : to(to), cost(cost), c(c) {} }; double dijkstra(int s, int g, vector<vector<Edge>> &G) { int n = G.size(); vector<vector<vector<double>>> D( n, (vector<vector<double>>(n, vector<double>(30, INF)))); priority_queue<tuple<double, int, int, int>> wait; wait.emplace(0, 0, 0, -1); // time, v, cur, from while (!wait.empty()) { double t = -get<0>(wait.top()); int v = get<1>(wait.top()) + 1, cur = get<2>(wait.top()), from = get<3>(wait.top()); wait.pop(); if (from != -1 && t >= D[cur][from][v - 1]) continue; if (from != -1) D[cur][from][v - 1] = t; for (int i = 0; i < G[cur].size(); ++i) { int to = G[cur][i].to, d = G[cur][i].cost, c = G[cur][i].c; if (to == from) continue; if (from != -1 && v + 1 <= c && D[to][cur][v] > t + (double)d / (v + 1)) wait.emplace(-t - (double)d / (v + 1), v, to, cur); if (v <= c && D[to][cur][v - 1] > t + (double)d / v) wait.emplace(-t - (double)d / v, v - 1, to, cur); if (v > 1 && v - 1 <= c && D[to][cur][v - 2] > t + (double)d / (v - 1)) wait.emplace(-t - (double)d / (v - 1), v - 2, to, cur); } } double ret = INF; for (int i = 0; i < n; ++i) ret = min(ret, D[g][i][0]); return ret; } int main() { int n, m; while (cin >> n >> m, n) { int s, g; cin >> s >> g; --s; --g; int x, y, d, c; vector<vector<Edge>> G(n); for (int i = 0; i < m; ++i) { cin >> x >> y >> d >> c; --x; --y; G[x].emplace_back(y, d, c); G[y].emplace_back(x, d, c); } double ans = dijkstra(s, g, G); if (ans >= INF) cout << "unreachable" << endl; else printf("%.12f\n", ans); } return 0; }
#include <iostream> #include <queue> #include <tuple> #include <vector> using namespace std; const double INF = 1e10; struct Edge { int cost, to, c; Edge(int to, int cost, int c) : to(to), cost(cost), c(c) {} }; double dijkstra(int s, int g, vector<vector<Edge>> &G) { int n = G.size(); vector<vector<vector<double>>> D( n, (vector<vector<double>>(n, vector<double>(30, INF)))); priority_queue<tuple<double, int, int, int>> wait; wait.emplace(0, 0, s, -1); // time, v, cur, from while (!wait.empty()) { double t = -get<0>(wait.top()); int v = get<1>(wait.top()) + 1, cur = get<2>(wait.top()), from = get<3>(wait.top()); wait.pop(); if (from != -1 && t >= D[cur][from][v - 1]) continue; if (from != -1) D[cur][from][v - 1] = t; for (int i = 0; i < G[cur].size(); ++i) { int to = G[cur][i].to, d = G[cur][i].cost, c = G[cur][i].c; if (to == from) continue; if (from != -1 && v + 1 <= c && D[to][cur][v] > t + (double)d / (v + 1)) wait.emplace(-t - (double)d / (v + 1), v, to, cur); if (v <= c && D[to][cur][v - 1] > t + (double)d / v) wait.emplace(-t - (double)d / v, v - 1, to, cur); if (v > 1 && v - 1 <= c && D[to][cur][v - 2] > t + (double)d / (v - 1)) wait.emplace(-t - (double)d / (v - 1), v - 2, to, cur); } } double ret = INF; for (int i = 0; i < n; ++i) ret = min(ret, D[g][i][0]); return ret; } int main() { int n, m; while (cin >> n >> m, n) { int s, g; cin >> s >> g; --s; --g; int x, y, d, c; vector<vector<Edge>> G(n); for (int i = 0; i < m; ++i) { cin >> x >> y >> d >> c; --x; --y; G[x].emplace_back(y, d, c); G[y].emplace_back(x, d, c); } double ans = dijkstra(s, g, G); if (ans >= INF) cout << "unreachable" << endl; else printf("%.12f\n", ans); } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
723
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define INF 1000000007 #define fi first #define se second using namespace std; typedef pair<int, int> pi; typedef pair<double, int> pd; typedef pair<pd, pi> PP; struct edge { int to, dist, limit; }; int n, m, s, g; double dp[31][31][31]; vector<edge> G[31]; void dijkstra() { rep(i, n) { rep(j, n) { rep(k, 31) { dp[i][j][k] = INF; } } } priority_queue<PP, vector<PP>, greater<PP>> que; que.push(PP(pd(0.0, s), pi(s, 0))); dp[s][s][0] = 0.0; while (!que.empty()) { PP pp = que.top(); pd p = pp.fi; pi q = pp.se; que.pop(); double cost = p.fi; int v = p.se, prv = q.fi, sp = q.se; if (dp[v][prv][sp] < cost) continue; if (v == g && sp == 1) { printf("%.12lf\n", cost); return; } for (int i = sp - 1; i <= sp + 1; i++) { if (i <= 0) continue; for (auto &e : G[v]) { if (e.to != prv && i <= e.limit && dp[e.to][v][i] > cost + (double)e.dist / i) { dp[e.to][v][i] = cost + (double)e.dist / i; que.push(PP(pd(dp[e.to][v][i], e.to), pi(v, i))); } } } } printf("unreachble\n"); } int main() { while (1) { cin >> n >> m; if (n == 0) { break; } rep(i, n) { G[i].clear(); } cin >> s >> g; s--, g--; rep(i, m) { int a, b, c, d; cin >> a >> b >> c >> d; G[a - 1].push_back((edge){b - 1, c, d}); G[b - 1].push_back((edge){a - 1, c, d}); } dijkstra(); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define INF 1000000007 #define fi first #define se second using namespace std; typedef pair<int, int> pi; typedef pair<double, int> pd; typedef pair<pd, pi> PP; struct edge { int to, dist, limit; }; int n, m, s, g; double dp[31][31][31]; vector<edge> G[31]; void dijkstra() { rep(i, n) { rep(j, n) { rep(k, 31) { dp[i][j][k] = INF; } } } priority_queue<PP, vector<PP>, greater<PP>> que; que.push(PP(pd(0.0, s), pi(s, 0))); dp[s][s][0] = 0.0; while (!que.empty()) { PP pp = que.top(); pd p = pp.fi; pi q = pp.se; que.pop(); double cost = p.fi; int v = p.se, prv = q.fi, sp = q.se; if (dp[v][prv][sp] < cost) continue; if (v == g && sp == 1) { printf("%.12lf\n", cost); return; } for (int i = sp - 1; i <= sp + 1; i++) { if (i <= 0) continue; for (auto &e : G[v]) { if (e.to != prv && i <= e.limit && dp[e.to][v][i] > cost + (double)e.dist / i) { dp[e.to][v][i] = cost + (double)e.dist / i; que.push(PP(pd(dp[e.to][v][i], e.to), pi(v, i))); } } } } printf("unreachable\n"); } int main() { while (1) { cin >> n >> m; if (n == 0) { break; } rep(i, n) { G[i].clear(); } cin >> s >> g; s--, g--; rep(i, m) { int a, b, c, d; cin >> a >> b >> c >> d; G[a - 1].push_back((edge){b - 1, c, d}); G[b - 1].push_back((edge){a - 1, c, d}); } dijkstra(); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
575
#include <algorithm> #include <cmath> #include <cstdio> #include <queue> #include <vector> using namespace std; typedef pair<pair<double, int>, pair<int, int>> P; class road { public: int to, d, c; road(int x, int y, int z) { to = x; d = y; c = z; }; }; vector<road> G[31]; int main() { int n, m, s, g; int x, y, d, c; for (;;) { scanf("%d %d", &n, &m); if (!n && !m) break; scanf("%d %d", &s, &g); double t[31][31][31]; bool v[31][31][31]; for (int i = 0; i < n; i++) G[i].clear(); for (int i = 0; i < m; i++) { scanf("%d %d %d %d", &x, &y, &d, &c); G[x].push_back(road(y, d, c)); G[y].push_back(road(x, d, c)); } for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <= 30; k++) { t[i][j][k] = 1e10; v[i][j][k] = false; } t[s][0][0] = 0.0; priority_queue<P, vector<P>, greater<P>> q; q.push(P(pair<double, int>(0.0, s), pair<int, int>(0, 0))); while (q.size()) { P p = q.top(); q.pop(); double time = p.first.first; int now, prv, speed; now = p.first.second; prv = p.second.first; speed = p.second.second; if (v[now][prv][speed]) continue; v[now][prv][speed] = true; for (int i = 0; i < (int)G[now].size(); i++) { road r = G[now][i]; if (r.to != prv) { for (int dif = -1; dif <= 1; dif++) { if (speed + dif > 0 && speed + dif <= r.c) { if (!v[r.to][now][speed + dif]) { if (t[r.to][now][speed + dif] > time + (double)r.d / (speed + dif)) { t[r.to][now][speed + dif] = time + (double)r.d / (speed + dif); q.push(P(pair<double, int>(t[r.to][now][speed + dif], r.to), pair<int, int>(now, speed + dif))); } } } } } } } double ans = 1e10; for (int i = 0; i <= n; i++) ans = min(ans, t[g][i][1]); if (ans == 1e10) printf("unreachable\n"); else printf("%.4lf\n", ans); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <queue> #include <vector> using namespace std; typedef pair<pair<double, int>, pair<int, int>> P; class road { public: int to, d, c; road(int x, int y, int z) { to = x; d = y; c = z; }; }; vector<road> G[31]; int main() { int n, m, s, g; int x, y, d, c; for (;;) { scanf("%d %d", &n, &m); if (!n && !m) break; scanf("%d %d", &s, &g); double t[31][31][31]; bool v[31][31][31]; for (int i = 0; i <= n; i++) G[i].clear(); for (int i = 0; i < m; i++) { scanf("%d %d %d %d", &x, &y, &d, &c); G[x].push_back(road(y, d, c)); G[y].push_back(road(x, d, c)); } for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <= 30; k++) { t[i][j][k] = 1e10; v[i][j][k] = false; } t[s][0][0] = 0.0; priority_queue<P, vector<P>, greater<P>> q; q.push(P(pair<double, int>(0.0, s), pair<int, int>(0, 0))); while (q.size()) { P p = q.top(); q.pop(); double time = p.first.first; int now, prv, speed; now = p.first.second; prv = p.second.first; speed = p.second.second; if (v[now][prv][speed]) continue; v[now][prv][speed] = true; for (int i = 0; i < (int)G[now].size(); i++) { road r = G[now][i]; if (r.to != prv) { for (int dif = -1; dif <= 1; dif++) { if (speed + dif > 0 && speed + dif <= r.c) { if (!v[r.to][now][speed + dif]) { if (t[r.to][now][speed + dif] > time + (double)r.d / (speed + dif)) { t[r.to][now][speed + dif] = time + (double)r.d / (speed + dif); q.push(P(pair<double, int>(t[r.to][now][speed + dif], r.to), pair<int, int>(now, speed + dif))); } } } } } } } double ans = 1e10; for (int i = 0; i <= n; i++) ans = min(ans, t[g][i][1]); if (ans == 1e10) printf("unreachable\n"); else printf("%.4lf\n", ans); } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
765
#include <cstdio> #include <iostream> using namespace std; const int MAX = 123456; int Sosuu[MAX] = {0}; void make_Sosuu() { for (int i = 2; i < MAX; i++) Sosuu[i] = 1; for (int i = 2; i < MAX; i++) { if (Sosuu[i]) { for (int j = i * 2; j < MAX; j += i) { Sosuu[j] = 0; } } } } int main() { make_Sosuu(); int n; while (scanf("%d", &n), n) { int count = 0; for (int i = n + 1; i <= n * 2; i++) { if (Sosuu[i]) count++; } printf("%d\n", count); } }
#include <cstdio> #include <iostream> using namespace std; const int MAX = 123456 * 2; int Sosuu[MAX] = {0}; void make_Sosuu() { for (int i = 2; i < MAX; i++) Sosuu[i] = 1; for (int i = 2; i < MAX; i++) { if (Sosuu[i]) { for (int j = i * 2; j < MAX; j += i) { Sosuu[j] = 0; } } } } int main() { make_Sosuu(); int n; while (scanf("%d", &n), n) { int count = 0; for (int i = n + 1; i <= n * 2; i++) { if (Sosuu[i]) count++; } printf("%d\n", count); } }
[["+", 0, 30, 0, 43, 49, 50, 51, 16, 17, 48], ["+", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13]]
1
176
#include <iostream> using namespace std; int main() { int n; bool table[123457 * 2]; for (int i = 0; i < 123457 * 2; i++) table[i] = true; table[0] = table[1] = false; for (int i = 2; i < 123457; i++) { if (table[i]) for (int j = 2 * i; j < 123457 * 2; j += i) table[j] = false; } while (1) { cin >> n; if (!n) break; int count = 0; for (int i = n; i <= 2 * n; i++) if (table[i]) count++; cout << count << endl; } }
#include <iostream> using namespace std; int main() { int n; bool table[123457 * 2]; for (int i = 0; i < 123457 * 2; i++) table[i] = true; table[0] = table[1] = false; for (int i = 2; i < 123457; i++) { if (table[i]) for (int j = 2 * i; j < 123457 * 2; j += i) table[j] = false; } while (1) { cin >> n; if (!n) break; int count = 0; for (int i = n + 1; i <= 2 * n; i++) if (table[i]) count++; cout << count << endl; } }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
161
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define INF (INT_MAX / 3 - 10) long n; /* input data */ void init() { n = 0; } bool input() { cin >> n; if (n == 0) { return false; } return true; } /*素数*/ vector<int> prime; // prime[n]; vector<int> prime_list; void prime_set(int n) { n += 100; prime.resize(n); for (int i = 0; i < n; i++) { prime[i] = 1; } prime[0] = prime[1] = 0; for (int i = 0; i * i < n; i++) { if (prime[i]) { for (int j = i * 2; j < n; j += i) { prime[j] = 0; } } } for (int i = 0; i < n; i++) { if (prime[i]) prime_list.push_back(i); } } /* dp とか */ void init_solve() {} void q_input() {} int solve() { int count = 0; for (int i = n; i <= 2 * n && i < prime.size(); i++) { if (prime[i]) count++; } return count; } int main() { prime_set(123456 * 2); while (init(), input()) { cout << solve() << endl; } }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define INF (INT_MAX / 3 - 10) long n; /* input data */ void init() { n = 0; } bool input() { cin >> n; if (n == 0) { return false; } return true; } /*素数*/ vector<int> prime; // prime[n]; vector<int> prime_list; void prime_set(int n) { n += 100; prime.resize(n); for (int i = 0; i < n; i++) { prime[i] = 1; } prime[0] = prime[1] = 0; for (int i = 0; i * i < n; i++) { if (prime[i]) { for (int j = i * 2; j < n; j += i) { prime[j] = 0; } } } for (int i = 0; i < n; i++) { if (prime[i]) prime_list.push_back(i); } } /* dp とか */ void init_solve() {} void q_input() {} int solve() { int count = 0; for (int i = n + 1; i <= 2 * n && i < prime.size(); i++) { if (prime[i]) count++; } return count; } int main() { prime_set(123456 * 2); while (init(), input()) { cout << solve() << endl; } }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
320
from math import sqrt size = 123456 primes = [x for x in range(2, 2*size+1) if x == 2 or x % 2 == 1] for i in range(2, int(sqrt(2*size))): primes = [x for x in primes if x % i != 0 or x == i] while True: n = int(input()) if n == 0: break nums = [x for x in primes if x >= n and x <= 2*n] print(len(nums))
from math import sqrt size = 123456 primes = [x for x in range(2, 2*size+1) if x == 2 or x % 2 == 1] for i in range(2, int(sqrt(2*size))): primes = [x for x in primes if x % i != 0 or x == i] while True: n = int(input()) if n == 0: break nums = [x for x in primes if x > n and x <= 2*n] print(len(nums))
[["-", 12, 658, 0, 678, 0, 679, 31, 666, 667, 20], ["+", 12, 658, 0, 678, 0, 679, 31, 666, 667, 47]]
5
115
primes = [0, 0] + [1]*246911 for i in range(2, 497): if primes[i]: for j in range(i*i, 246913, i): primes[j] = 0 while True: n = int(input()) if n == 0: break print(sum(1 for i in range(n, 2*n+1) if primes[i]))
primes = [0, 0] + [1]*246911 for i in range(2, 497): if primes[i]: for j in range(i*i, 246913, i): primes[j] = 0 while True: n = int(input()) if n == 0: break print(sum(1 for i in range(n+1, 2*n+1) if primes[i]))
[["+", 0, 659, 12, 652, 3, 4, 0, 657, 17, 72], ["+", 0, 659, 12, 652, 3, 4, 0, 657, 12, 612]]
5
91
input_list = [] while True: N = int(input()) if N == 0: break input_list.append(N) n = 123456*2 isPrime = [True for i in range(n)] for i in range(int(n)): if i % 2 != 0: for j in range(2, int(i ** 0.5) + 1): if i % j == 0: isPrime[i] = False break else: isPrime[i] = False isPrime[2] = True while input_list: tmp = input_list.pop(0) tmp_isPrime = isPrime[tmp+1: tmp*2] print(tmp_isPrime.count(True))
input_list = [] while True: N = int(input()) if N == 0: break input_list.append(N) n = 123456*2 isPrime = [True for i in range(n)] for i in range(int(n)): if i % 2 != 0: for j in range(2, int(i ** 0.5) + 1): if i % j == 0: isPrime[i] = False break else: isPrime[i] = False isPrime[2] = True while input_list: tmp = input_list.pop(0) tmp_isPrime = isPrime[tmp+1: tmp*2+1] print(tmp_isPrime.count(True))
[["+", 0, 662, 12, 206, 206, 663, 0, 657, 17, 72], ["+", 0, 662, 12, 206, 206, 663, 0, 657, 12, 612]]
5
139
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { String str = br.readLine(); if (str == null) { break; } char[] input = str.toCharArray(); if (balanced(input)) { System.out.println("yes"); } else { System.out.println("no"); } } } static boolean balanced(char[] input) { Stack<Character> stack = new Stack<Character>(); for (int i = 0; i < input.length; i++) { char c = input[i]; if (c == '(' || c == '[') { stack.push(c); } else if (c == ')') { if (stack.isEmpty() || stack.peek() != '(') { return false; } stack.pop(); } else if (c == ']') { if (stack.isEmpty() || stack.peek() != '[') { return false; } stack.pop(); } } if (stack.isEmpty()) { return true; } else { return false; } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { String str = br.readLine(); if (str.equals(".")) { break; } char[] input = str.toCharArray(); // System.out.println(str); if (balanced(input)) { System.out.println("yes"); } else { System.out.println("no"); } } } static boolean balanced(char[] input) { Stack<Character> stack = new Stack<Character>(); for (int i = 0; i < input.length; i++) { char c = input[i]; if (c == '(' || c == '[') { stack.push(c); } else if (c == ')') { if (stack.isEmpty() || stack.peek() != '(') { return false; } stack.pop(); } else if (c == ']') { if (stack.isEmpty() || stack.peek() != '[') { return false; } stack.pop(); } } if (stack.isEmpty()) { return true; } else { return false; } } }
[["-", 8, 196, 0, 57, 15, 15, 0, 16, 17, 60], ["-", 8, 196, 0, 57, 15, 15, 0, 16, 12, 215], ["+", 8, 196, 0, 57, 15, 15, 0, 492, 0, 131], ["+", 8, 196, 0, 57, 15, 15, 0, 492, 141, 22], ["+", 0, 57, 15, 15, 0, 492, 3, 4, 0, 24], ["+", 15, 15, 0, 492, 3, 4, 0, 5, 0, 62], ["+", 15, 15, 0, 492, 3, 4, 0, 5, 0, 491], ["+", 0, 57, 15, 15, 0, 492, 3, 4, 0, 25]]
3
295
#include <cstring> #include <iostream> #include <queue> #define INF 1e9 using namespace std; class DP { public: double cost; unsigned char cur, sp, prev; DP() {} DP(double c, int cu, int s, int pr) { cost = c; cur = cu; sp = s; prev = pr; } bool operator<(const DP &opp) const { return cost > opp.cost; } }; int n, m, s, go; char visit[31][31][31]; int g[31][31]; int lim[31][31]; main() { int i, j, d, c, x, y; while (cin >> n >> m, n) { priority_queue<DP> dp; memset(g, 0, sizeof(g)); memset(visit, 0, sizeof(visit)); cin >> s >> go; for (i = 1; i <= m; i++) { cin >> x >> y >> d >> c; g[x][y] = g[y][x] = d; lim[x][y] = lim[y][x] = c; } DP T, S; dp.push(DP(0, 1, 0, 0)); while (!dp.empty()) { S = dp.top(); dp.pop(); if (visit[S.cur][S.sp][S.prev]) continue; if (S.cur == go && S.sp == 1) break; for (i = 1; i <= n; i++) { if (!g[S.cur][i]) continue; for (j = S.sp - 1; j <= S.sp + 1; j++) { if (lim[S.cur][i] < j || j < 1 || i == S.prev || visit[i][j][S.cur]) continue; T.cost = S.cost + g[S.cur][i] / (double)j; T.cur = i; T.sp = j; T.prev = S.cur; dp.push(T); } } visit[S.cur][S.sp][S.prev] = 1; } if (S.cur == go && S.sp == 1) printf("%.5lf\n", S.cost); else cout << "unreachable" << endl; } }
#include <cstring> #include <iostream> #include <queue> #define INF 1e9 using namespace std; class DP { public: double cost; unsigned char cur, sp, prev; DP() {} DP(double c, int cu, int s, int pr) { cost = c; cur = cu; sp = s; prev = pr; } bool operator<(const DP &opp) const { return cost > opp.cost; } }; int n, m, s, go; char visit[31][31][31]; int g[31][31]; int lim[31][31]; main() { int i, j, d, c, x, y; while (cin >> n >> m, n) { priority_queue<DP> dp; memset(g, 0, sizeof(g)); memset(visit, 0, sizeof(visit)); cin >> s >> go; for (i = 1; i <= m; i++) { cin >> x >> y >> d >> c; g[x][y] = g[y][x] = d; lim[x][y] = lim[y][x] = c; } DP T, S; dp.push(DP(0, s, 0, 0)); while (!dp.empty()) { S = dp.top(); dp.pop(); if (visit[S.cur][S.sp][S.prev]) continue; if (S.cur == go && S.sp == 1) break; for (i = 1; i <= n; i++) { if (!g[S.cur][i]) continue; for (j = S.sp - 1; j <= S.sp + 1; j++) { if (lim[S.cur][i] < j || j < 1 || i == S.prev || visit[i][j][S.cur]) continue; T.cost = S.cost + g[S.cur][i] / (double)j; T.cur = i; T.sp = j; T.prev = S.cur; dp.push(T); } } visit[S.cur][S.sp][S.prev] = 1; } if (S.cur == go && S.sp == 1) printf("%.4lf\n", S.cost); else cout << "unreachable" << endl; } }
[["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 13], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 22], ["-", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
532
#include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; #define rep(i, a) for (int i = 0; i < a; i++) int n, m; int s, g; struct edge { int to, d, c; edge() {} edge(int to, int d, int c) : to(to), d(d), c(c) {} }; vector<vector<edge>> E; struct node { double cost; int prev, v, speed; node() {} node(double cost, int prev, int v, int speed) : cost(cost), prev(prev), v(v), speed(speed) {} bool operator<(const node &t) const { return cost > t.cost; } }; double mem[32][32][32]; void solve() { rep(i, 32) rep(j, 32) rep(k, 32) mem[i][j][k] = 1e15; priority_queue<node> q; q.push(node(0.0, n, 0, 0)); while (!q.empty()) { double cost = q.top().cost; int prev = q.top().prev; int v = q.top().v; int speed = q.top().speed; q.pop(); // cerr<<cost<<' '<<prev<<' '<<v<<' '<<speed<<endl; if (mem[prev][v][speed] <= cost) continue; if (v == g && speed == 1) { printf("%.8f\n", cost); return; } mem[prev][v][speed] = cost; rep(i, E[v].size()) { if (E[v][i].to == prev) continue; for (int ns = speed - 1; ns <= speed + 1; ++ns) { if (ns > 0 && ns <= E[v][i].c) { q.push(node(cost + (double)E[v][i].d / ns, v, E[v][i].to, ns)); } } } } cout << "unreachable" << endl; } int main() { while (cin >> n >> m && n) { cin >> s >> g; --s, --g; E.clear(); E.resize(n); rep(i, m) { int x, y, d, c; cin >> x >> y >> d >> c; --x, --y; E[x].push_back(edge(y, d, c)); E[y].push_back(edge(x, d, c)); } solve(); } }
#include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; #define rep(i, a) for (int i = 0; i < a; i++) int n, m; int s, g; struct edge { int to, d, c; edge() {} edge(int to, int d, int c) : to(to), d(d), c(c) {} }; vector<vector<edge>> E; struct node { double cost; int prev, v, speed; node() {} node(double cost, int prev, int v, int speed) : cost(cost), prev(prev), v(v), speed(speed) {} bool operator<(const node &t) const { return cost > t.cost; } }; double mem[32][32][32]; void solve() { rep(i, 32) rep(j, 32) rep(k, 32) mem[i][j][k] = 1e15; priority_queue<node> q; q.push(node(0.0, n, s, 0)); while (!q.empty()) { double cost = q.top().cost; int prev = q.top().prev; int v = q.top().v; int speed = q.top().speed; q.pop(); // cerr<<cost<<' '<<prev<<' '<<v<<' '<<speed<<endl; if (mem[prev][v][speed] <= cost) continue; if (v == g && speed == 1) { printf("%.8f\n", cost); return; } mem[prev][v][speed] = cost; rep(i, E[v].size()) { if (E[v][i].to == prev) continue; for (int ns = speed - 1; ns <= speed + 1; ++ns) { if (ns > 0 && ns <= E[v][i].c) { q.push(node(cost + (double)E[v][i].d / ns, v, E[v][i].to, ns)); } } } } cout << "unreachable" << endl; } int main() { while (cin >> n >> m && n) { cin >> s >> g; --s, --g; E.clear(); E.resize(n); rep(i, m) { int x, y, d, c; cin >> x >> y >> d >> c; --x, --y; E[x].push_back(edge(y, d, c)); E[y].push_back(edge(x, d, c)); } solve(); } return 0; }
[["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 13], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 22], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 38], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 13], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 35]]
1
579
#include <bits/stdc++.h> using namespace std; #define MAX_V 1010 const int INF = 1e9; struct edge { int to, cap, rev; }; vector<edge> G[MAX_V]; bool used[MAX_V]; void add_edge(int from, int to, int cap) { G[from].push_back((edge){to, cap, G[to].size()}); G[to].push_back((edge){from, 0, G[from].size() - 1}); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { memset(used, 0, sizeof(used)); int f = dfs(s, t, INF); if (f == 0) return flow; flow += f; } } void init() { for (int i = 0; i < MAX_V; i++) G[i].clear(); memset(used, 0, sizeof(used)); } bool ok(int a, int b) { return __gcd(a, b) != 1; } int main() { int N, M; while (cin >> N >> M, N | M) { init(); int source = M + N; int tink = source + 1; vector<int> A(N), B(M); for (int i = 0; i < N; i++) cin >> A[i]; for (int i = 0; i < M; i++) cin >> B[i]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (ok(A[i], B[j])) add_edge(i, M + j, 1); } } for (int i = 0; i < M; i++) add_edge(source, i, 1); for (int i = 0; i < N; i++) add_edge(M + i, tink, 1); cout << max_flow(source, tink) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX_V 1010 const int INF = 1e9; struct edge { int to, cap, rev; }; vector<edge> G[MAX_V]; bool used[MAX_V]; void add_edge(int from, int to, int cap) { G[from].push_back((edge){to, cap, G[to].size()}); G[to].push_back((edge){from, 0, G[from].size() - 1}); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { memset(used, 0, sizeof(used)); int f = dfs(s, t, INF); if (f == 0) return flow; flow += f; } } void init() { for (int i = 0; i < MAX_V; i++) G[i].clear(); memset(used, 0, sizeof(used)); } bool ok(int a, int b) { return __gcd(a, b) != 1; } int main() { int N, M; while (cin >> N >> M, N | M) { init(); int source = M + N; int tink = source + 1; vector<int> A(N), B(M); for (int i = 0; i < N; i++) cin >> A[i]; for (int i = 0; i < M; i++) cin >> B[i]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (ok(A[i], B[j])) add_edge(i, N + j, 1); } } for (int i = 0; i < N; i++) add_edge(source, i, 1); for (int i = 0; i < M; i++) add_edge(N + i, tink, 1); cout << max_flow(source, tink) << endl; } return 0; }
[["-", 64, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 64, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 8, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 8, 1, 0, 2, 3, 4, 0, 16, 31, 22]]
1
594
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < ((int)(n)); i++) #define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++) #define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--) #define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--) typedef long long int lli; typedef pair<int, int> mp; #define fir first #define sec second #define IINF INT_MAX #define LINF LLONG_MAX #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define pque(type) priority_queue<type, vector<type>, greater<type>> #define memst(a, b) memset(a, b, sizeof(a)) struct edge { int to, co, p; }; vector<edge> vs[1005]; void cleae() { rep(i, 1005) vs[i].clear(); } void adde(int a, int b, int c) { // printf("fr %d to %d co %d\n",a,b,c); int sa = vs[a].size(), sb = vs[b].size(); edge e; e.co = c; e.to = b; e.p = sb; vs[a].push_back(e); e.to = a; e.p = sa; vs[b].push_back(e); } int st, gl; int gone[1005]; int dfs(int no, int f) { if (gone[no]) return 0; gone[no] = 1; if (no == gl) return f; rep(i, vs[no].size()) { edge &e = vs[no][i]; if (gone[e.to]) continue; if (e.co > 0) { int gf = dfs(e.to, min(e.co, f)); if (gf > 0) { e.co -= gf; vs[e.to][e.p].co += gf; return gf; } } } return 0; } int flow() { int res = 0; for (;;) { memset(gone, 0, sizeof(gone)); int f = dfs(st, IINF); if (f == 0) return res; res += f; } } int na, nb; int da[505]; int db[505]; int gcd(int p, int q) { if (p < q) swap(p, q); if (p % q == 0) return q; return gcd(q, p % q); } int main(void) { for (;;) { scanf("%d%d", &na, &nb); if (na == 0) break; rep(i, na) scanf("%d", &da[i]); rep(i, nb) scanf("%d", &db[i]); cleae(); st = 0; gl = na + nb + 1; rep(i, na) adde(st, i + 1, 1); rep(i, nb) adde(i + na + 1, gl, 1); rep(i, na) rep(j, nb) { if (gcd(da[i], db[j]) > 1) adde(i + 1, j + na + 1, 1); } printf("%d\n", flow()); } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < ((int)(n)); i++) #define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++) #define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--) #define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--) typedef long long int lli; typedef pair<int, int> mp; #define fir first #define sec second #define IINF INT_MAX #define LINF LLONG_MAX #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define pque(type) priority_queue<type, vector<type>, greater<type>> #define memst(a, b) memset(a, b, sizeof(a)) struct edge { int to, co, p; }; vector<edge> vs[1005]; void cleae() { rep(i, 1005) vs[i].clear(); } void adde(int a, int b, int c) { // printf("fr %d to %d co %d\n",a,b,c); int sa = vs[a].size(), sb = vs[b].size(); edge e; e.co = c; e.to = b; e.p = sb; vs[a].push_back(e); e.co = 0; e.to = a; e.p = sa; vs[b].push_back(e); } int st, gl; int gone[1005]; int dfs(int no, int f) { if (gone[no]) return 0; gone[no] = 1; if (no == gl) return f; rep(i, vs[no].size()) { edge &e = vs[no][i]; if (gone[e.to]) continue; if (e.co > 0) { int gf = dfs(e.to, min(e.co, f)); if (gf > 0) { e.co -= gf; vs[e.to][e.p].co += gf; return gf; } } } return 0; } int flow() { int res = 0; for (;;) { memset(gone, 0, sizeof(gone)); int f = dfs(st, IINF); if (f == 0) return res; res += f; } } int na, nb; int da[505]; int db[505]; int gcd(int p, int q) { if (p < q) swap(p, q); if (p % q == 0) return q; return gcd(q, p % q); } int main(void) { for (;;) { scanf("%d%d", &na, &nb); if (na == 0) break; rep(i, na) scanf("%d", &da[i]); rep(i, nb) scanf("%d", &db[i]); cleae(); st = 0; gl = na + nb + 1; rep(i, na) adde(st, i + 1, 1); rep(i, nb) adde(i + na + 1, gl, 1); rep(i, na) rep(j, nb) { if (gcd(da[i], db[j]) > 1) adde(i + 1, j + na + 1, 1); } printf("%d\n", flow()); } return 0; }
[["+", 8, 9, 0, 1, 0, 11, 31, 118, 119, 120], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 1, 0, 11, 31, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 11, 31, 118, 17, 131]]
1
701
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int b[500], r[500]; vector<int> E[500]; bool used[1000]; int match[1000]; bool dfs(int u) { used[u] = true; for (int v : E[u]) { int w = match[v]; if (w == -1 || (!used[w] && dfs(w))) { match[u] = v; match[v] = u; return true; } } return false; } int main() { int m, n; while (scanf("%d%d", &m, &n), m) { rep(i, 1000) E[i].clear(); rep(i, m) scanf("%d", &b[i]); rep(i, n) scanf("%d", &r[i]); rep(i, m) rep(j, n) { if (gcd(b[i], r[j]) != 1) E[i].push_back(m + j); } memset(match, -1, sizeof(match)); int ans = 0; rep(i, m) { if (match[i] == -1) { memset(used, 0, sizeof(used)); if (dfs(i)) ans++; } } printf("%d\n", ans); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int b[500], r[500]; vector<int> E[500]; bool used[1000]; int match[1000]; bool dfs(int u) { used[u] = true; for (int v : E[u]) { int w = match[v]; if (w == -1 || (!used[w] && dfs(w))) { match[u] = v; match[v] = u; return true; } } return false; } int main() { int m, n; while (scanf("%d%d", &m, &n), m) { rep(i, m) E[i].clear(); rep(i, m) scanf("%d", &b[i]); rep(i, n) scanf("%d", &r[i]); rep(i, m) rep(j, n) { if (gcd(b[i], r[j]) != 1) E[i].push_back(m + j); } memset(match, -1, sizeof(match)); int ans = 0; rep(i, m) { if (match[i] == -1) { memset(used, 0, sizeof(used)); if (dfs(i)) ans++; } } printf("%d\n", ans); } }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 42, 0, 2, 3, 4, 0, 22], ["-", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
349
#include <bits/stdc++.h> using namespace std; struct Bipartite_Matching { vector<vector<int>> graph; vector<int> dist, match; vector<bool> used, vv; Bipartite_Matching(int n, int m) { graph.resize(n); match.assign(m, -1); used.assign(n, false); } void add_edge(int u, int v) { graph[u].push_back(v); } void bfs() { dist.assign(graph.size(), -1); queue<int> que; for (int i = 0; i < graph.size(); i++) { if (!used[i]) { que.emplace(i); dist[i] = 0; } } while (!que.empty()) { int a = que.front(); que.pop(); for (auto &b : graph[a]) { int c = match[b]; if (c >= 0 && dist[c] == -1) { dist[c] = dist[a] + 1; que.emplace(c); } } } } bool dfs(int a) { vv[a] = true; for (auto &b : graph[a]) { int c = match[b]; if (c < 0 || (!vv[c] && dist[c] == dist[a] + 1 && dfs(c))) { match[b] = a; used[a] = true; return (true); } } return (false); } int bipartite_matching() { int ret = 0; while (true) { bfs(); vv.assign(graph.size(), false); int flow = 0; for (int i = 0; i < graph.size(); i++) { if (!used[i] && dfs(i)) ++flow; } if (flow == 0) return (ret); ret += flow; } } }; int dp[1000][1000]; int gcd(int x, int y) { if (x < 1000) { if (~dp[x][y]) return (dp[x][y]); int r = x % y; if (r == 0) return dp[x][y] = y; return dp[x][y] = gcd(y, r); } else { int r = x % y; if (r == 0) return y; return gcd(y, r); } } int main() { memset(dp, -1, sizeof(dp)); int M, N, B[500], R[500]; while (scanf("%d %d", &M, &N), M) { Bipartite_Matching flow(M, N); for (int i = 0; i < M; i++) scanf("%d", &B[i]); for (int i = 0; i < N; i++) scanf("%d", &R[i]); for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { if (gcd(min(B[i], R[j]), max(B[i], R[j])) > 1) flow.add_edge(i, j); } } printf("%d\n", flow.bipartite_matching()); } }
#include <bits/stdc++.h> using namespace std; struct Bipartite_Matching { vector<vector<int>> graph; vector<int> dist, match; vector<bool> used, vv; Bipartite_Matching(int n, int m) { graph.resize(n); match.assign(m, -1); used.assign(n, false); } void add_edge(int u, int v) { graph[u].push_back(v); } void bfs() { dist.assign(graph.size(), -1); queue<int> que; for (int i = 0; i < graph.size(); i++) { if (!used[i]) { que.emplace(i); dist[i] = 0; } } while (!que.empty()) { int a = que.front(); que.pop(); for (auto &b : graph[a]) { int c = match[b]; if (c >= 0 && dist[c] == -1) { dist[c] = dist[a] + 1; que.emplace(c); } } } } bool dfs(int a) { vv[a] = true; for (auto &b : graph[a]) { int c = match[b]; if (c < 0 || (!vv[c] && dist[c] == dist[a] + 1 && dfs(c))) { match[b] = a; used[a] = true; return (true); } } return (false); } int bipartite_matching() { int ret = 0; while (true) { bfs(); vv.assign(graph.size(), false); int flow = 0; for (int i = 0; i < graph.size(); i++) { if (!used[i] && dfs(i)) ++flow; } if (flow == 0) return (ret); ret += flow; } } }; int dp[1000][1000]; int gcd(int x, int y) { if (x < 1000) { if (~dp[x][y]) return (dp[x][y]); int r = x % y; if (r == 0) return dp[x][y] = y; return dp[x][y] = gcd(y, r); } else { int r = x % y; if (r == 0) return y; return gcd(y, r); } } int main() { memset(dp, -1, sizeof(dp)); int M, N, B[500], R[500]; while (scanf("%d %d", &M, &N), M) { Bipartite_Matching flow(M, N); for (int i = 0; i < M; i++) scanf("%d", &B[i]); for (int i = 0; i < N; i++) scanf("%d", &R[i]); for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { if (gcd(max(B[i], R[j]), min(B[i], R[j])) > 1) flow.add_edge(i, j); } } printf("%d\n", flow.bipartite_matching()); } }
[["-", 51, 16, 31, 2, 3, 4, 0, 2, 63, 22], ["+", 51, 16, 31, 2, 3, 4, 0, 2, 63, 22]]
1
744
#pragma GCC optimize("O3") #pragma GCC target("avx") #include "bits/stdc++.h" using namespace std; using ll = long long int; #define debug(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ cout << (v) << endl; \ } #define debugv(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ for (auto e : (v)) { \ cout << e << " "; \ } \ cout << endl; \ } #define debuga(m, w) \ { \ printf("L%d %s > ", __LINE__, #m); \ for (int x = 0; x < (w); x++) { \ cout << (m)[x] << " "; \ } \ cout << endl; \ } #define debugaa(m, h, w) \ { \ printf("L%d %s >\n", __LINE__, #m); \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ cout << (m)[y][x] << " "; \ } \ cout << endl; \ } \ } #define ALL(v) (v).begin(), (v).end() #define repeat(cnt, l) \ for (remove_const<decltype(l)>::type cnt = 0; (cnt) < (l); ++(cnt)) #define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt)) #define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt)) #define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt)) const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L; template <typename T1, typename T2> inline void assert_equal(T1 expected, T2 actual) { if (!(expected == actual)) { cerr << "assertion fault: expected=" << expected << " actual=" << actual << endl; abort(); } } template <typename T1, typename T2> inline void assert_less(T1 actual, T2 threshold) { if (!(actual < threshold)) { cerr << "assertion fault: " << actual << " < (const)" << threshold << endl; abort(); } } template <typename T1, typename T2> inline void assert_eqless(T1 actual, T2 threshold) { if (!(actual <= threshold)) { cerr << "assertion fault: " << actual << " <= (const)" << threshold << endl; abort(); } } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; } template <typename Vec> inline ostream &_ostream_vecprint(ostream &o, const Vec &p) { o << '['; for (auto &e : p) o << e << ','; o << ']'; return o; } template <typename T> inline ostream &operator<<(ostream &o, const vector<T> &v) { return _ostream_vecprint(o, v); } template <typename T, size_t S> inline ostream &operator<<(ostream &o, const array<T, S> &v) { return _ostream_vecprint(o, v); } template <typename T> inline T &maxset(T &to, const T &val) { return to = max(to, val); } template <typename T> inline T &minset(T &to, const T &val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); template <typename T> inline T rand(T l, T h) { return uniform_int_distribution<T>(l, h)(randdev); } template <> inline double rand<double>(double l, double h) { return uniform_real_distribution<double>(l, h)(randdev); } template <> inline float rand<float>(float l, float h) { return uniform_real_distribution<float>(l, h)(randdev); } #if defined(_WIN32) || defined(_WIN64) #define getchar_unlocked _getchar_nolock #define putchar_unlocked _putchar_nolock #elif defined(__GNUC__) #else #define getchar_unlocked getchar #define putchar_unlocked putchar #endif namespace { #define isvisiblechar(c) (0x21 <= (c) && (c) <= 0x7E) class MaiScanner { public: template <typename T> void input_integer(T &var) { var = 0; T sign = 1; int cc = getchar_unlocked(); for (; cc < '0' || '9' < cc; cc = getchar_unlocked()) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked()) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; } inline int c() { return getchar_unlocked(); } inline MaiScanner &operator>>(int &var) { input_integer<int>(var); return *this; } inline MaiScanner &operator>>(long long &var) { input_integer<long long>(var); return *this; } inline MaiScanner &operator>>(string &var) { int cc = getchar_unlocked(); for (; !isvisiblechar(cc); cc = getchar_unlocked()) ; for (; isvisiblechar(cc); cc = getchar_unlocked()) var.push_back(cc); return *this; } template <typename IT> void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { public: template <typename T> void output_integer(T var) { if (var == 0) { putchar_unlocked('0'); return; } if (var < 0) putchar_unlocked('-'), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putchar_unlocked(stack[--stack_p]); } inline MaiPrinter &operator<<(char c) { putchar_unlocked(c); return *this; } inline MaiPrinter &operator<<(int var) { output_integer<int>(var); return *this; } inline MaiPrinter &operator<<(long long var) { output_integer<long long>(var); return *this; } inline MaiPrinter &operator<<(char *str_p) { while (*str_p) putchar_unlocked(*(str_p++)); return *this; } inline MaiPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putchar_unlocked(*p++); return *this; } template <typename IT> void join(IT begin, IT end, char sep = '\n') { for (auto it = begin; it != end; ++it) *this << *it << sep; } }; } // namespace MaiScanner scanner; MaiPrinter printer; class DGraphF { public: typedef int cap_t; size_t n_; struct Arc { int from, to; // 残量 cap_t left; // 容量 cap_t cap; Arc(int from = 0, int to = 0, cap_t w = 1) : from(from), to(to), left(w), cap(w) {} inline bool operator<(const Arc &a) const { return (left != a.left) ? left < a.left : (left < a.left) | (cap < a.cap) | (from < a.from) | (to < a.to); } inline bool operator==(const Arc &a) const { return (from == a.from) && (to == a.to) && (left == a.left) && (cap == a.cap); } }; vector<vector<int>> vertex_to; vector<vector<int>> vertex_from; vector<Arc> edges; DGraphF(int n = 1) : n_(n), vertex_to(n), vertex_from(n) {} void connect(int from, int to, cap_t left) { vertex_to[(size_t)from].push_back((int)edges.size()); // toto vertex_from[(size_t)to].push_back((int)edges.size()); // fromfrom edges.emplace_back(from, to, left); } }; void dinic(DGraphF &graph, vector<DGraphF::cap_t> &result, int i_source, int i_sink) { assert(i_source != i_sink); result.resize(graph.n_); vector<int> dist(graph.n_); queue<int> q; vector<int> flag(graph.n_); static function<DGraphF::cap_t(int, int, DGraphF::cap_t)> _dfs = [&](int u, int i_sink, DGraphF::cap_t mini) { // DAG // TODO: 経路再利用 if (i_sink == u) return mini; if (flag[u]) return (DGraphF::cap_t)-1; flag[u] = true; DGraphF::cap_t sumw = 0; bool term = true; for (int e : graph.vertex_to[u]) { auto &edge = graph.edges[e]; if (edge.left > 0 && dist[u] > dist[edge.to]) { DGraphF::cap_t w = (mini < 0) ? edge.left : min(edge.left, mini); w = _dfs(edge.to, i_sink, w); if (w == -1) continue; edge.left -= w; result[edge.to] += w; sumw += w; mini -= w; term = false; flag[u] = false; // TODO: 末尾では? if (mini == 0) return sumw; } } for (int e : graph.vertex_from[u]) { auto &edge = graph.edges[e]; if (edge.cap > edge.left && dist[u] > dist[edge.from]) { DGraphF::cap_t w = (mini < 0) ? (edge.cap - edge.left) : min(edge.cap - edge.left, mini); w = _dfs(edge.from, i_sink, w); if (w == -1) continue; edge.left += w; result[edge.to] -= w; sumw += w; mini -= w; term = false; flag[u] = false; if (mini == 0) return sumw; } } return term ? (DGraphF::cap_t)(-1) : sumw; }; for (int distbegin = 0;; distbegin += (int)graph.n_) { q.emplace(i_sink); // bfsはsinkからsourceへの距離を計算. dist[i_sink] = distbegin + 1; while (!q.empty()) { int v = q.front(); q.pop(); for (int ie : graph.vertex_from[v]) { const auto edge = graph.edges[ie]; if (0 < edge.left && dist[edge.from] <= distbegin) { dist[edge.from] = dist[v] + 1; q.emplace(edge.from); } } for (int ie : graph.vertex_to[v]) { const auto edge = graph.edges[ie]; if (edge.left < edge.cap && dist[edge.to] <= distbegin) { dist[edge.to] = dist[v] + 1; q.emplace(edge.to); } } } fill(flag.begin(), flag.end(), false); if (dist[i_source] <= distbegin) break; else result[i_source] += _dfs(i_source, i_sink, -1); } } template <typename T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template <typename T> inline T lcm(T a, T b) { return a * b / gcd(a, b); } int main() { int blue[555], red[555]; int m, n; while (scanner >> m >> n, m != 0 || n != 0) { DGraphF g(m + n + 2); const int vs = m + n, vt = m + n + 1; scanner.in(blue, blue + m); scanner.in(red, red + n); repeat(i, m) repeat(j, n) if (gcd(blue[i], red[j]) > 1) g.connect(i, m + j, 1); repeat(i, m) g.connect(vs, i, 1); repeat(j, n) g.connect(j, vt, 1); vector<int> f; dinic(g, f, vs, vt); printer << f[vt] << '\n'; } return 0; }
#pragma GCC optimize("O3") #pragma GCC target("avx") #include "bits/stdc++.h" using namespace std; using ll = long long int; #define debug(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ cout << (v) << endl; \ } #define debugv(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ for (auto e : (v)) { \ cout << e << " "; \ } \ cout << endl; \ } #define debuga(m, w) \ { \ printf("L%d %s > ", __LINE__, #m); \ for (int x = 0; x < (w); x++) { \ cout << (m)[x] << " "; \ } \ cout << endl; \ } #define debugaa(m, h, w) \ { \ printf("L%d %s >\n", __LINE__, #m); \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ cout << (m)[y][x] << " "; \ } \ cout << endl; \ } \ } #define ALL(v) (v).begin(), (v).end() #define repeat(cnt, l) \ for (remove_const<decltype(l)>::type cnt = 0; (cnt) < (l); ++(cnt)) #define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt)) #define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt)) #define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt)) const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L; template <typename T1, typename T2> inline void assert_equal(T1 expected, T2 actual) { if (!(expected == actual)) { cerr << "assertion fault: expected=" << expected << " actual=" << actual << endl; abort(); } } template <typename T1, typename T2> inline void assert_less(T1 actual, T2 threshold) { if (!(actual < threshold)) { cerr << "assertion fault: " << actual << " < (const)" << threshold << endl; abort(); } } template <typename T1, typename T2> inline void assert_eqless(T1 actual, T2 threshold) { if (!(actual <= threshold)) { cerr << "assertion fault: " << actual << " <= (const)" << threshold << endl; abort(); } } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; } template <typename Vec> inline ostream &_ostream_vecprint(ostream &o, const Vec &p) { o << '['; for (auto &e : p) o << e << ','; o << ']'; return o; } template <typename T> inline ostream &operator<<(ostream &o, const vector<T> &v) { return _ostream_vecprint(o, v); } template <typename T, size_t S> inline ostream &operator<<(ostream &o, const array<T, S> &v) { return _ostream_vecprint(o, v); } template <typename T> inline T &maxset(T &to, const T &val) { return to = max(to, val); } template <typename T> inline T &minset(T &to, const T &val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); template <typename T> inline T rand(T l, T h) { return uniform_int_distribution<T>(l, h)(randdev); } template <> inline double rand<double>(double l, double h) { return uniform_real_distribution<double>(l, h)(randdev); } template <> inline float rand<float>(float l, float h) { return uniform_real_distribution<float>(l, h)(randdev); } #if defined(_WIN32) || defined(_WIN64) #define getchar_unlocked _getchar_nolock #define putchar_unlocked _putchar_nolock #elif defined(__GNUC__) #else #define getchar_unlocked getchar #define putchar_unlocked putchar #endif namespace { #define isvisiblechar(c) (0x21 <= (c) && (c) <= 0x7E) class MaiScanner { public: template <typename T> void input_integer(T &var) { var = 0; T sign = 1; int cc = getchar_unlocked(); for (; cc < '0' || '9' < cc; cc = getchar_unlocked()) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked()) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; } inline int c() { return getchar_unlocked(); } inline MaiScanner &operator>>(int &var) { input_integer<int>(var); return *this; } inline MaiScanner &operator>>(long long &var) { input_integer<long long>(var); return *this; } inline MaiScanner &operator>>(string &var) { int cc = getchar_unlocked(); for (; !isvisiblechar(cc); cc = getchar_unlocked()) ; for (; isvisiblechar(cc); cc = getchar_unlocked()) var.push_back(cc); return *this; } template <typename IT> void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { public: template <typename T> void output_integer(T var) { if (var == 0) { putchar_unlocked('0'); return; } if (var < 0) putchar_unlocked('-'), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putchar_unlocked(stack[--stack_p]); } inline MaiPrinter &operator<<(char c) { putchar_unlocked(c); return *this; } inline MaiPrinter &operator<<(int var) { output_integer<int>(var); return *this; } inline MaiPrinter &operator<<(long long var) { output_integer<long long>(var); return *this; } inline MaiPrinter &operator<<(char *str_p) { while (*str_p) putchar_unlocked(*(str_p++)); return *this; } inline MaiPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putchar_unlocked(*p++); return *this; } template <typename IT> void join(IT begin, IT end, char sep = '\n') { for (auto it = begin; it != end; ++it) *this << *it << sep; } }; } // namespace MaiScanner scanner; MaiPrinter printer; class DGraphF { public: typedef int cap_t; size_t n_; struct Arc { int from, to; // 残量 cap_t left; // 容量 cap_t cap; Arc(int from = 0, int to = 0, cap_t w = 1) : from(from), to(to), left(w), cap(w) {} inline bool operator<(const Arc &a) const { return (left != a.left) ? left < a.left : (left < a.left) | (cap < a.cap) | (from < a.from) | (to < a.to); } inline bool operator==(const Arc &a) const { return (from == a.from) && (to == a.to) && (left == a.left) && (cap == a.cap); } }; vector<vector<int>> vertex_to; vector<vector<int>> vertex_from; vector<Arc> edges; DGraphF(int n = 1) : n_(n), vertex_to(n), vertex_from(n) {} void connect(int from, int to, cap_t left) { vertex_to[(size_t)from].push_back((int)edges.size()); // toto vertex_from[(size_t)to].push_back((int)edges.size()); // fromfrom edges.emplace_back(from, to, left); } }; void dinic(DGraphF &graph, vector<DGraphF::cap_t> &result, int i_source, int i_sink) { assert(i_source != i_sink); result.resize(graph.n_); vector<int> dist(graph.n_); queue<int> q; vector<int> flag(graph.n_); static function<DGraphF::cap_t(int, int, DGraphF::cap_t)> _dfs = [&](int u, int i_sink, DGraphF::cap_t mini) { // DAG // TODO: 経路再利用 if (i_sink == u) return mini; if (flag[u]) return (DGraphF::cap_t)-1; flag[u] = true; DGraphF::cap_t sumw = 0; bool term = true; for (int e : graph.vertex_to[u]) { auto &edge = graph.edges[e]; if (edge.left > 0 && dist[u] > dist[edge.to]) { DGraphF::cap_t w = (mini < 0) ? edge.left : min(edge.left, mini); w = _dfs(edge.to, i_sink, w); if (w == -1) continue; edge.left -= w; result[edge.to] += w; sumw += w; mini -= w; term = false; flag[u] = false; // TODO: 末尾では? if (mini == 0) return sumw; } } for (int e : graph.vertex_from[u]) { auto &edge = graph.edges[e]; if (edge.cap > edge.left && dist[u] > dist[edge.from]) { DGraphF::cap_t w = (mini < 0) ? (edge.cap - edge.left) : min(edge.cap - edge.left, mini); w = _dfs(edge.from, i_sink, w); if (w == -1) continue; edge.left += w; result[edge.to] -= w; sumw += w; mini -= w; term = false; flag[u] = false; if (mini == 0) return sumw; } } return term ? (DGraphF::cap_t)(-1) : sumw; }; for (int distbegin = 0;; distbegin += (int)graph.n_) { q.emplace(i_sink); // bfsはsinkからsourceへの距離を計算. dist[i_sink] = distbegin + 1; while (!q.empty()) { int v = q.front(); q.pop(); for (int ie : graph.vertex_from[v]) { const auto edge = graph.edges[ie]; if (0 < edge.left && dist[edge.from] <= distbegin) { dist[edge.from] = dist[v] + 1; q.emplace(edge.from); } } for (int ie : graph.vertex_to[v]) { const auto edge = graph.edges[ie]; if (edge.left < edge.cap && dist[edge.to] <= distbegin) { dist[edge.to] = dist[v] + 1; q.emplace(edge.to); } } } fill(flag.begin(), flag.end(), false); if (dist[i_source] <= distbegin) break; else result[i_source] += _dfs(i_source, i_sink, -1); } } template <typename T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template <typename T> inline T lcm(T a, T b) { return a * b / gcd(a, b); } int main() { int blue[555], red[555]; int m, n; while (scanner >> m >> n, m != 0 || n != 0) { DGraphF g(m + n + 2); const int vs = m + n, vt = m + n + 1; scanner.in(blue, blue + m); scanner.in(red, red + n); repeat(i, m) repeat(j, n) if (gcd(blue[i], red[j]) > 1) g.connect(i, m + j, 1); repeat(i, m) g.connect(vs, i, 1); repeat(j, n) g.connect(m + j, vt, 1); vector<int> f; dinic(g, f, vs, vt); printer << f[vt] << '\n'; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72]]
1
2,496
#include <iostream> #include <vector> using namespace std; #define vec vector #define pb push_back #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); i++) // typedef long long Int; typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst) : src(src), dst(dst) {} Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vec<Edge> Edges; typedef vec<Edges> Graph; #define MAXMN 500 int B[MAXMN]; int R[MAXMN]; int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; } bool augment(const Graph &g, int u, vector<int> &matchTo, vector<bool> &visited) { if (u < 0) return true; FOR(e, g[u]) if (!visited[e->dst]) { visited[e->dst] = true; if (augment(g, matchTo[e->dst], matchTo, visited)) { matchTo[e->src] = e->dst; matchTo[e->dst] = e->src; return true; } } return false; } int bipartiteMatching(const Graph &g, int L) { const int n = g.size(); vector<int> matchTo(n, -1); int match = 0; REP(u, L) { vector<bool> visited(n); if (augment(g, u, matchTo, visited)) ++match; } return match; } int main(int argc, char **argv) { int M, N; Graph g; g.reserve(MAXMN + MAXMN); while (cin >> M >> N, M) { g.clear(); g.resize(M + N); REP(i, M) cin >> B[i]; REP(i, N) cin >> R[i]; REP(i, M) { Edges es, es2; REP(j, N) { if (gcd(B[i], R[j]) != 1) { es.pb(Edge(i, M + j)); es2.pb(Edge(M + j, i)); } } g[i] = es; g[M + i] = es2; } cout << bipartiteMatching(g, M) << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; #define vec vector #define pb push_back #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); i++) // typedef long long Int; typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst) : src(src), dst(dst) {} Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vec<Edge> Edges; typedef vec<Edges> Graph; #define MAXMN 500 int B[MAXMN]; int R[MAXMN]; int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; } bool augment(const Graph &g, int u, vector<int> &matchTo, vector<bool> &visited) { if (u < 0) return true; FOR(e, g[u]) if (!visited[e->dst]) { visited[e->dst] = true; if (augment(g, matchTo[e->dst], matchTo, visited)) { matchTo[e->src] = e->dst; matchTo[e->dst] = e->src; return true; } } return false; } int bipartiteMatching(const Graph &g, int L) { const int n = g.size(); vector<int> matchTo(n, -1); int match = 0; REP(u, L) { vector<bool> visited(n); if (augment(g, u, matchTo, visited)) ++match; } return match; } int main(int argc, char **argv) { int M, N; Graph g; g.reserve(MAXMN + MAXMN); while (cin >> M >> N, M) { g.clear(); g.resize((M + N) * 2); REP(i, M) cin >> B[i]; REP(i, N) cin >> R[i]; REP(i, M) { Edges es, es2; REP(j, N) { if (gcd(B[i], R[j]) != 1) { es.pb(Edge(i, M + j)); es2.pb(Edge(M + j, i)); } } g[i] = es; g[M + i] = es2; } cout << bipartiteMatching(g, M) << endl; } return 0; }
[["+", 0, 2, 3, 4, 0, 16, 31, 23, 0, 24], ["+", 0, 2, 3, 4, 0, 16, 31, 23, 0, 25], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 48], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
583
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int MAXV = 2005; const int MAXE = MAXV * MAXV; const int INF = 1 << 29; struct BiparateMatching { int N, M, E, match[MAXV + 1], dist[MAXV + 1]; int last[MAXE], prev[MAXE], to[MAXE]; BiparateMatching(int n, int m) : N(n), M(m), E(0) { memset(last, -1, sizeof(last)); } void add_edge(int x, int y) { // x \in [0, N), y \in [0, M) to[E] = y + N; prev[E] = last[x]; last[x] = E; E++; } bool bfs() { queue<int> q; for (int i = 0; i < N; ++i) { if (match[i] == -1) { q.push(i); dist[i] = 0; } else dist[i] = INF; } bool ret = false; while (!q.empty()) { int v = q.front(); q.pop(); for (int e = last[v]; e != -1; e = prev[e]) { if (match[to[e]] == -1) ret = true; else if (dist[match[to[e]]] == INF) { dist[match[to[e]]] = dist[v] + 1; q.push(match[to[e]]); } } } return ret; } bool dfs(int v) { for (int e = last[v]; e != -1; e = prev[e]) { bool update = false; if (match[to[e]] == -1) { match[to[e]] = v; match[v] = to[e]; update = true; } else if (dist[match[to[e]]] == dist[v] + 1) { if (dfs(match[to[e]])) { match[to[e]] = v; match[v] = to[e]; update = true; } } if (update) return true; } dist[v] = INF; return false; } int matching() { int ret = 0; memset(match, -1, sizeof(match)); while (bfs()) { for (int i = 0; i < N; ++i) if (match[i] == -1 && dfs(i)) ret++; } return ret; } }; int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int b[501], r[501]; int main() { int m, n; while (scanf("%d %d", &m, &n), m | n) { BiparateMatching bm(m, n); for (int i = 0; i < m; ++i) scanf("%d", &b[i]); for (int i = 0; i < n; ++i) scanf("%d", &r[i]); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) if (gcd(b[i], r[j]) > 1) bm.add_edge(i, j); } printf("%d\n", bm.matching()); } }
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int MAXV = 2005; const int MAXE = 505 * 505; const int INF = 1 << 29; struct BiparateMatching { int N, M, E, match[MAXV + 1], dist[MAXV + 1]; int last[MAXE], prev[MAXE], to[MAXE]; BiparateMatching(int n, int m) : N(n), M(m), E(0) { memset(last, -1, sizeof(last)); } void add_edge(int x, int y) { // x \in [0, N), y \in [0, M) to[E] = y + N; prev[E] = last[x]; last[x] = E; E++; } bool bfs() { queue<int> q; for (int i = 0; i < N; ++i) { if (match[i] == -1) { q.push(i); dist[i] = 0; } else dist[i] = INF; } bool ret = false; while (!q.empty()) { int v = q.front(); q.pop(); for (int e = last[v]; e != -1; e = prev[e]) { if (match[to[e]] == -1) ret = true; else if (dist[match[to[e]]] == INF) { dist[match[to[e]]] = dist[v] + 1; q.push(match[to[e]]); } } } return ret; } bool dfs(int v) { for (int e = last[v]; e != -1; e = prev[e]) { bool update = false; if (match[to[e]] == -1) { match[to[e]] = v; match[v] = to[e]; update = true; } else if (dist[match[to[e]]] == dist[v] + 1) { if (dfs(match[to[e]])) { match[to[e]] = v; match[v] = to[e]; update = true; } } if (update) return true; } dist[v] = INF; return false; } int matching() { int ret = 0; memset(match, -1, sizeof(match)); while (bfs()) { for (int i = 0; i < N; ++i) if (match[i] == -1 && dfs(i)) ret++; } return ret; } }; int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int b[501], r[501]; int main() { int m, n; while (scanf("%d %d", &m, &n), m | n) { BiparateMatching bm(m, n); for (int i = 0; i < m; ++i) scanf("%d", &b[i]); for (int i = 0; i < n; ++i) scanf("%d", &r[i]); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) if (gcd(b[i], r[j]) > 1) bm.add_edge(i, j); } printf("%d\n", bm.matching()); } }
[["-", 0, 30, 0, 43, 49, 50, 51, 16, 31, 22], ["+", 0, 30, 0, 43, 49, 50, 51, 16, 31, 13], ["-", 0, 30, 0, 43, 49, 50, 51, 16, 12, 22], ["+", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13]]
1
758
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> using namespace std; #define dump(n) cout << "# " << #n << '=' << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define iter(c) __typeof__((c).begin()) #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define all(c) (c).begin(), (c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int, int> pii; const int INFTY = 1 << 29; const double EPS = 1e-9; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) { os << '['; rep(i, a.size()) os << (i ? " " : "") << a[i]; return os << ']'; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } struct Edge { int src, dst; Edge() {} Edge(int s, int d) : src(s), dst(d) {} }; typedef vector<vector<Edge>> Graph; bool BFS(const Graph &g, const vi &side, const vi &match, vi &level) { int n = g.size(); level.assign(n, n); queue<pii> q; rep(i, n) if (side[i] == 0 && match[i] == -1) q.push(mp(i, 0)); bool res = false; while (!q.empty()) { pii cur = q.front(); q.pop(); int u = cur.first, l = cur.second; if (level[u] <= l) continue; level[u] = l; rep(i, g[u].size()) { int v = g[u][i].dst; if (match[v] == -1) res = true; else q.push(mp(match[v], l + 2)); } } return res; } bool DFS(const Graph &g, const vi &side, int u, vi &match, vi &level) { rep(i, g[u].size()) { int v = g[u][i].dst; if (match[v] == -1 || level[match[v]] > level[u] && DFS(g, side, match[v], match, level)) { match[u] = v; match[v] = u; return true; } } level[u] = -1; return false; } int HopcroftKarp(const Graph &g, const vi &side, vi &match) { int n = g.size(); match.assign(n, -1); int res = 0; for (vi level; BFS(g, side, match, level);) rep(i, n) if (side[i] == 0 && match[i] == -1) res += DFS(g, side, i, match, level); return res; } int main() { for (int m, n; scanf("%d%d", &m, &n), m | n;) { vi bs(m), rs(n); rep(i, m) scanf("%d", &bs[i]); rep(i, n) scanf("%d", &rs[i]); Graph g(m + n); rep(i, m) rep(j, n) if (gcd(bs[i], rs[j]) > 1) { g[i].push_back(Edge(i, m + j)); g[m + j].push_back(Edge(m + j, i)); } vi side(m + n), match; fill(n + all(side), 1); printf("%d\n", HopcroftKarp(g, side, match)); } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> using namespace std; #define dump(n) cout << "# " << #n << '=' << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define iter(c) __typeof__((c).begin()) #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define all(c) (c).begin(), (c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int, int> pii; const int INFTY = 1 << 29; const double EPS = 1e-9; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) { os << '['; rep(i, a.size()) os << (i ? " " : "") << a[i]; return os << ']'; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } struct Edge { int src, dst; Edge() {} Edge(int s, int d) : src(s), dst(d) {} }; typedef vector<vector<Edge>> Graph; bool BFS(const Graph &g, const vi &side, const vi &match, vi &level) { int n = g.size(); level.assign(n, n); queue<pii> q; rep(i, n) if (side[i] == 0 && match[i] == -1) q.push(mp(i, 0)); bool res = false; while (!q.empty()) { pii cur = q.front(); q.pop(); int u = cur.first, l = cur.second; if (level[u] <= l) continue; level[u] = l; rep(i, g[u].size()) { int v = g[u][i].dst; if (match[v] == -1) res = true; else q.push(mp(match[v], l + 2)); } } return res; } bool DFS(const Graph &g, const vi &side, int u, vi &match, vi &level) { rep(i, g[u].size()) { int v = g[u][i].dst; if (match[v] == -1 || level[match[v]] > level[u] && DFS(g, side, match[v], match, level)) { match[u] = v; match[v] = u; return true; } } level[u] = -1; return false; } int HopcroftKarp(const Graph &g, const vi &side, vi &match) { int n = g.size(); match.assign(n, -1); int res = 0; for (vi level; BFS(g, side, match, level);) rep(i, n) if (side[i] == 0 && match[i] == -1) res += DFS(g, side, i, match, level); return res; } int main() { for (int m, n; scanf("%d%d", &m, &n), m | n;) { vi bs(m), rs(n); rep(i, m) scanf("%d", &bs[i]); rep(i, n) scanf("%d", &rs[i]); Graph g(m + n); rep(i, m) rep(j, n) if (gcd(bs[i], rs[j]) > 1) { g[i].push_back(Edge(i, m + j)); g[m + j].push_back(Edge(m + j, i)); } vi side(m + n), match; fill(m + all(side), 1); printf("%d\n", HopcroftKarp(g, side, match)); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22]]
1
1,000
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; #define MAX_V 1111 struct edge { int to, cap, rev; }; const int inf = 1 << 21; vector<edge> G[MAX_V]; int level[MAX_V]; int iter[MAX_V]; void add_edge(int f, int t, int cap) { G[f].push_back((edge){t, cap, (int)G[t].size()}); G[t].push_back((edge){f, 0, (int)G[f].size() - 1}); } void bfs(int s) { memset(level, -1, sizeof(level)); queue<int> q; level[s] = 0; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (int i = 0; i < int(G[v].size()); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; q.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < int(G[v].size()); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { bfs(s); if (level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); int f; while ((f = dfs(s, t, inf)) > 0) { flow += f; } } } int main(void) { int m, n; int a[555], b[555]; for (;;) { scanf("%d%d", &m, &n); if (m + n == 0) break; for (int i = 0; i < m + n + 2; i++) G[i].clear(); for (int i = 0; i < m; i++) scanf("%d", a + i); for (int i = 0; i < n; i++) scanf("%d", b + i); for (int i = 0; i < m; i++) { add_edge(0, i + 1, 1); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (__gcd(a[i], b[j]) != 1) { add_edge(i + 1, n + 1 + j, 1); } } } for (int i = 0; i < n; i++) { add_edge(n + 1 + i, n + m + 1, 1); } printf("%d\n", max_flow(0, n + m + 1)); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; #define MAX_V 1111 struct edge { int to, cap, rev; }; const int inf = 1 << 21; vector<edge> G[MAX_V]; int level[MAX_V]; int iter[MAX_V]; void add_edge(int f, int t, int cap) { G[f].push_back((edge){t, cap, (int)G[t].size()}); G[t].push_back((edge){f, 0, (int)G[f].size() - 1}); } void bfs(int s) { memset(level, -1, sizeof(level)); queue<int> q; level[s] = 0; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (int i = 0; i < int(G[v].size()); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; q.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < int(G[v].size()); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { bfs(s); if (level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); int f; while ((f = dfs(s, t, inf)) > 0) { flow += f; } } } int main(void) { int m, n; int a[555], b[555]; for (;;) { scanf("%d%d", &m, &n); if (m + n == 0) break; for (int i = 0; i < m + n + 2; i++) G[i].clear(); for (int i = 0; i < m; i++) scanf("%d", a + i); for (int i = 0; i < n; i++) scanf("%d", b + i); for (int i = 0; i < m; i++) { add_edge(0, i + 1, 1); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (__gcd(a[i], b[j]) != 1) { add_edge(i + 1, m + 1 + j, 1); } } } for (int i = 0; i < n; i++) { add_edge(m + 1 + i, n + m + 1, 1); } printf("%d\n", max_flow(0, n + m + 1)); } return 0; }
[["-", 0, 2, 3, 4, 0, 16, 31, 16, 31, 22], ["+", 0, 2, 3, 4, 0, 16, 31, 16, 31, 22]]
1
778
while True: s = input() if s[0] == '.': break r = [] f = False for c in s: if c == '(' or c == '[': r.append(c) elif c == ')': if not r or r.pop() != '(': print("no") break elif c == ']': if not r or r.pop() != '[': print("no") break else: print("yes")
while True: s = input() if s[0] == '.': break r = [] f = False for c in s: if c == '(' or c == '[': r.append(c) elif c == ')': if not r or r.pop() != '(': print("no") break elif c == ']': if not r or r.pop() != '[': print("no") break else: print("yes" if not r else "no")
[["+", 0, 1, 0, 652, 3, 4, 0, 41, 0, 121], ["+", 0, 652, 3, 4, 0, 41, 0, 681, 0, 372], ["+", 0, 652, 3, 4, 0, 41, 0, 681, 28, 22], ["+", 0, 1, 0, 652, 3, 4, 0, 41, 0, 95], ["+", 0, 652, 3, 4, 0, 41, 0, 557, 0, 654], ["+", 0, 652, 3, 4, 0, 41, 0, 557, 0, 6], ["+", 0, 652, 3, 4, 0, 41, 0, 557, 0, 655]]
5
114
import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Main p = new Main(); } public Main() { Scanner sc = new Scanner(System.in); while (true) { h = sc.nextInt(); w = sc.nextInt(); c = sc.nextInt(); if (h == 0 && w == 0 && c == 0) break; field = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) field[i][j] = sc.nextInt(); } solve(); } } int h; int w; int c; int[][] field; public void solve() { visited = new boolean[7][h * w]; int res = 0; for (int i = 1; i <= 6; i++) { res = Math.max(res, rec(1, field[0][0], i)); } System.out.println(res); } int[] dx = {-1, 0, 1, 0}; int[] dy = {0, -1, 0, 1}; boolean[][] visited; Queue<Integer> queue = new LinkedList<Integer>(); private int rec(int cur, int cc, int nc) { if (cur == 6) return count(); int mask = (1 << 3) - 1; queue.add(0); Arrays.fill(visited[cur], false); while (!queue.isEmpty()) { int p = queue.poll(); int x = p & (0x07); int y = p >> 3; if (visited[cur][y * w + x]) continue; visited[cur][y * w + x] = true; field[y][x] = (field[y][x] << 3) + nc; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) continue; if ((field[ny][nx] & mask) == cc) queue.add(ny << 3 + nx); } } int ret = 0; for (int i = 1; i <= 6; i++) ret = Math.max(ret, rec(cur + 1, field[0][0] & mask, i)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (visited[cur][i * w + j]) { field[i][j] >>= 3; } } } return ret; } private int count() { int res = 0; int mask = (1 << 3) - 1; queue.add(0); Arrays.fill(visited[6], false); while (!queue.isEmpty()) { int p = queue.poll(); int x = p & 0x07; int y = p >> 3; if (visited[6][y * w + x]) continue; visited[6][y * w + x] = true; res++; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) continue; if ((field[ny][nx] & mask) == c) queue.add(ny << 3 + nx); } } // System.out.println(res); return res; } }
import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Main p = new Main(); } public Main() { Scanner sc = new Scanner(System.in); while (true) { h = sc.nextInt(); w = sc.nextInt(); c = sc.nextInt(); if (h == 0 && w == 0 && c == 0) break; field = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) field[i][j] = sc.nextInt(); } solve(); } } int h; int w; int c; int[][] field; public void solve() { visited = new boolean[7][h * w]; int res = 0; for (int i = 1; i <= 6; i++) { res = Math.max(res, rec(1, field[0][0], i)); } System.out.println(res); } int[] dx = {-1, 0, 1, 0}; int[] dy = {0, -1, 0, 1}; boolean[][] visited; Queue<Integer> queue = new LinkedList<Integer>(); private int rec(int cur, int cc, int nc) { if (cur == 6) return count(); int mask = (1 << 3) - 1; queue.add(0); Arrays.fill(visited[cur], false); while (!queue.isEmpty()) { int p = queue.poll(); int x = p & (0x07); int y = p >> 3; if (visited[cur][y * w + x]) continue; visited[cur][y * w + x] = true; field[y][x] = (field[y][x] << 3) + nc; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) continue; if ((field[ny][nx] & mask) == cc) queue.add((ny << 3) + nx); } } int ret = 0; for (int i = 1; i <= 6; i++) ret = Math.max(ret, rec(cur + 1, field[0][0] & mask, i)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (visited[cur][i * w + j]) { field[i][j] >>= 3; } } } return ret; } private int count() { int res = 0; int mask = (1 << 3) - 1; queue.add(0); Arrays.fill(visited[6], false); while (!queue.isEmpty()) { int p = queue.poll(); int x = p & 0x07; int y = p >> 3; if (visited[6][y * w + x]) continue; visited[6][y * w + x] = true; res++; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) continue; if ((field[ny][nx] & mask) == c) queue.add((ny << 3) + nx); } } // System.out.println(res); return res; } }
[["+", 0, 492, 3, 4, 0, 16, 31, 23, 0, 24], ["+", 0, 492, 3, 4, 0, 16, 31, 23, 0, 25], ["+", 0, 57, 64, 1, 0, 492, 3, 4, 0, 24]]
3
849
import static java.lang.Integer.parseInt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; import java.util.StringTokenizer; /** * Identically Colored Panels Connection */ public class Main { enum COLOR { NONE, YELLOW, PINK, RED, PURPLE, GREEN, BLUE, } static int[][] DIR = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, }; static COLOR tc; static int max = 0; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; String[] words; while ((line = br.readLine()) != null && !line.isEmpty()) { StringTokenizer st; st = new StringTokenizer(line); int h, w; h = parseInt(st.nextToken()); w = parseInt(st.nextToken()); tc = COLOR.values()[parseInt(st.nextToken())]; if ((h | w) == 0) break; COLOR[][] pane = new COLOR[h + 2][w + 2]; for (COLOR[] pp : pane) { Arrays.fill(pp, COLOR.NONE); } for (int i = 0; i < h; i++) { st = new StringTokenizer(br.readLine()); for (int j = 0; j < w; j++) { pane[i + 1][j + 1] = COLOR.values()[st.nextToken().charAt(0) - '0']; } } // max = 0; solve(pane, 5); System.out.println(max); } } static void solve(COLOR[][] pane, int r) { if (r == 0) { if (pane[1][1] == tc) { int count = 0; Deque<int[]> queue = new ArrayDeque<>(); queue.offer(new int[] {1, 1}); while (!queue.isEmpty()) { int[] q = queue.poll(); for (int[] dir : DIR) { int ny, nx; ny = q[0] + dir[0]; nx = q[1] + dir[1]; if (pane[ny][nx] == tc) { pane[ny][nx] = COLOR.NONE; queue.offer(new int[] {ny, nx}); count++; } } } if (count == 0) count = 1; max = Math.max(max, count); } return; } COLOR pc = pane[1][1]; for (COLOR nc : COLOR.values()) { if (nc != COLOR.NONE && nc != pc) { if (r == 1 && nc != tc) break; COLOR[][] _pane = copy(pane); _pane[1][1] = nc; // Deque<int[]> queue = new ArrayDeque<>(); queue.offer(new int[] {1, 1}); while (!queue.isEmpty()) { int[] q = queue.poll(); for (int[] dir : DIR) { int ny, nx; ny = q[0] + dir[0]; nx = q[1] + dir[1]; if (_pane[ny][nx] == pc) { _pane[ny][nx] = nc; queue.offer(new int[] {ny, nx}); } } } solve(_pane, r - 1); } } } static COLOR[][] copy(COLOR[][] pane) { COLOR[][] _pane = new COLOR[pane.length][]; for (int i = 0; i < pane.length; i++) { _pane[i] = pane[i].clone(); } return _pane; } }
import static java.lang.Integer.parseInt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; import java.util.StringTokenizer; /** * Identically Colored Panels Connection */ public class Main { enum COLOR { NONE, YELLOW, PINK, RED, PURPLE, GREEN, BLUE, } static int[][] DIR = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, }; static COLOR tc; static int max = 0; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; String[] words; while ((line = br.readLine()) != null && !line.isEmpty()) { StringTokenizer st; st = new StringTokenizer(line); int h, w; h = parseInt(st.nextToken()); w = parseInt(st.nextToken()); tc = COLOR.values()[parseInt(st.nextToken())]; if ((h | w) == 0) break; COLOR[][] pane = new COLOR[h + 2][w + 2]; for (COLOR[] pp : pane) { Arrays.fill(pp, COLOR.NONE); } for (int i = 0; i < h; i++) { st = new StringTokenizer(br.readLine()); for (int j = 0; j < w; j++) { pane[i + 1][j + 1] = COLOR.values()[st.nextToken().charAt(0) - '0']; } } // max = 0; solve(pane, 5); System.out.println(max); } } static void solve(COLOR[][] pane, int r) { if (r == 0) { if (pane[1][1] == tc) { int count = 0; Deque<int[]> queue = new ArrayDeque<>(); queue.offer(new int[] {1, 1}); while (!queue.isEmpty()) { int[] q = queue.poll(); for (int[] dir : DIR) { int ny, nx; ny = q[0] + dir[0]; nx = q[1] + dir[1]; if (pane[ny][nx] == tc) { pane[ny][nx] = COLOR.NONE; queue.offer(new int[] {ny, nx}); count++; } } } if (count == 0) count = 1; max = Math.max(max, count); } return; } COLOR pc = pane[1][1]; for (COLOR nc : COLOR.values()) { if (nc != COLOR.NONE && nc != pc) { if (r == 1 && nc != tc) continue; COLOR[][] _pane = copy(pane); _pane[1][1] = nc; // Deque<int[]> queue = new ArrayDeque<>(); queue.offer(new int[] {1, 1}); while (!queue.isEmpty()) { int[] q = queue.poll(); for (int[] dir : DIR) { int ny, nx; ny = q[0] + dir[0]; nx = q[1] + dir[1]; if (_pane[ny][nx] == pc) { _pane[ny][nx] = nc; queue.offer(new int[] {ny, nx}); } } } solve(_pane, r - 1); } } } static COLOR[][] copy(COLOR[][] pane) { COLOR[][] _pane = new COLOR[pane.length][]; for (int i = 0; i < pane.length; i++) { _pane[i] = pane[i].clone(); } return _pane; } }
[["-", 0, 57, 64, 196, 0, 57, 64, 93, 0, 94], ["+", 0, 57, 64, 196, 0, 57, 64, 116, 0, 117]]
3
881
#include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define L(a, d) ((a) << ((d)*3)) #define R(a, d) (((a) >> ((d)*3)) & 7) int h, w, c, f[16][16], ofs[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; void P(int d) { int m = ~L(7, d + 1); rep(i, h) rep(j, w) { f[i][j] &= m; f[i][j] |= L(R(f[i][j], d), d + 1); } } int F(int x, int y, int d, int C) { int n = 1; int a = R(f[y][x], d); int m = ~L(7, d + 1); f[y][x] &= m; f[y][x] |= L(C, d + 1); for (auto l : ofs) { int s = x + l[0]; int t = y + l[1]; if (0 <= s && s < w && 0 <= t && t < h && R(f[t][s], d) == a && R(f[t][s], d + 1) != C) n += F(s, t, d, C); } return n; } int D(int d) { if (d & 4) { P(d); F(0, 0, d, c); P(d + 1); return F(0, 0, d + 1, 7); } int x; for (int r = 1; r <= 6; ++r) { P(d); F(0, 0, d, r); int v = D(d + 1); x = x > v ? x : v; } return x; } int main() { while (cin >> h >> w >> c, h) { rep(i, h) rep(j, w) cin >> f[i][j]; cout << D(0) << endl; } return 0; }
#include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define L(a, d) ((a) << ((d)*3)) #define R(a, d) (((a) >> ((d)*3)) & 7) int h, w, c, f[16][16], ofs[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; void P(int d) { int m = ~L(7, d + 1); rep(i, h) rep(j, w) { f[i][j] &= m; f[i][j] |= L(R(f[i][j], d), d + 1); } } int F(int x, int y, int d, int C) { int n = 1; int a = R(f[y][x], d); int m = ~L(7, d + 1); f[y][x] &= m; f[y][x] |= L(C, d + 1); for (auto l : ofs) { int s = x + l[0]; int t = y + l[1]; if (0 <= s && s < w && 0 <= t && t < h && R(f[t][s], d) == a && R(f[t][s], d + 1) != C) n += F(s, t, d, C); } return n; } int D(int d) { if (d & 4) { P(d); F(0, 0, d, c); P(d + 1); return F(0, 0, d + 1, 7); } int x = 0; for (int r = 1; r <= 6; ++r) { P(d); F(0, 0, d, r); int v = D(d + 1); x = x > v ? x : v; } return x; } int main() { while (cin >> h >> w >> c, h) { rep(i, h) rep(j, w) cin >> f[i][j]; cout << D(0) << endl; } return 0; }
[["+", 0, 14, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
491
#include <cstdio> #include <iostream> #include <queue> #define xx first #define yy second #define mp make_pair #define pb push using namespace std; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int anss = 0; void play(int h, int w, int c, int mas[10][10], int cnt) { // cout << "x" << endl; // cout << endl; int con[10][10] = {0}; int ans = 0; con[0][0] = 1; queue<pair<int, int>> qu; qu.pb(mp(0, 0)); while (qu.size() > 0) { int xx = qu.front().xx; int yy = qu.front().yy; qu.pop(); for (int i = 0; i < 4; i++) { int xxx = xx + dx[i]; int yyy = yy + dy[i]; if (xxx >= 0 && xxx < h && yyy >= 0 && yyy < w) { if (mas[xxx][yyy] == mas[xx][yy] && con[xxx][yyy] == 0) { con[xxx][yyy] = 1; qu.pb(mp(xxx, yyy)); } } } // cout << qu.size() << endl; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (con[i][j] == 1) ans++; } } /* for(int i = 0 ; i < h ; i++){ for(int j= 0 ; j < w ;j++){ cout << con[i][j] << " "; } cout << " | "; for(int j= 0 ; j < w ;j++){ cout << mas[i][j] << " "; } cout << endl; } */ if (cnt == 5) { if (mas[0][0] == c) { anss = max(anss, ans); } return; } int poss[10] = {0}; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (con[i][j] == 1) { for (int k = 0; k < 4; k++) { int xx = i + dx[k]; int yy = j + dy[k]; if (xx >= 0 && xx < h && yy >= 0 && yy < w) { if (mas[xx][yy] != mas[i][j]) poss[mas[xx][yy]] = 1; } } } } } int m[10][10]; for (int col = 1; col <= 6; col++) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { m[i][j] = mas[i][j]; } } if (poss[col] == 1) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (con[i][j] == 1) m[i][j] = col; } } int cntt = cnt; cntt++; play(h, w, c, m, cntt); } } } main() { int h, w, c; while (cin >> h >> w >> c, h != 0) { int mas[10][10]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> mas[i][j]; } } anss = 0; play(h, w, c, mas, 0); cout << anss << endl; } }
#include <cstdio> #include <iostream> #include <queue> #define xx first #define yy second #define mp make_pair #define pb push using namespace std; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int anss = 0; void play(int h, int w, int c, int mas[10][10], int cnt) { // cout << "x" << endl; // cout << endl; int con[10][10] = {0}; int ans = 0; con[0][0] = 1; queue<pair<int, int>> qu; qu.pb(mp(0, 0)); while (qu.size() > 0) { int xx = qu.front().xx; int yy = qu.front().yy; qu.pop(); for (int i = 0; i < 4; i++) { int xxx = xx + dx[i]; int yyy = yy + dy[i]; if (xxx >= 0 && xxx < h && yyy >= 0 && yyy < w) { if (mas[xxx][yyy] == mas[xx][yy] && con[xxx][yyy] == 0) { con[xxx][yyy] = 1; qu.pb(mp(xxx, yyy)); } } } // cout << qu.size() << endl; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (con[i][j] == 1) ans++; } } /* for(int i = 0 ; i < h ; i++){ for(int j= 0 ; j < w ;j++){ cout << con[i][j] << " "; } cout << " | "; for(int j= 0 ; j < w ;j++){ cout << mas[i][j] << " "; } cout << endl; } */ if (cnt == 5) { if (mas[0][0] == c) { anss = max(anss, ans); } return; } int poss[10] = {0}; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (con[i][j] == 1) { for (int k = 0; k < 4; k++) { int xx = i + dx[k]; int yy = j + dy[k]; if (xx >= 0 && xx < h && yy >= 0 && yy < w) { if (mas[xx][yy] != mas[i][j]) poss[mas[xx][yy]] = 1; } } } } } int m[10][10]; for (int col = 1; col <= 6; col++) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { m[i][j] = mas[i][j]; } } if (1) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (con[i][j] == 1) m[i][j] = col; } } int cntt = cnt; cntt++; play(h, w, c, m, cntt); } } } main() { int h, w, c; while (cin >> h >> w >> c, h != 0) { int mas[10][10]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> mas[i][j]; } } anss = 0; play(h, w, c, mas, 0); cout << anss << endl; } }
[["-", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 70], ["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 73], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
1
770
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) const int MAX = 1000000; int dp[MAX + 1]; int dp2[MAX + 1]; void solve() { fill(dp, dp + MAX + 1, MAX); fill(dp2, dp2 + MAX + 1, MAX); dp[0] = dp[2] = 0; vector<int> v; for (int i = 0;; i++) { int t = i * (i + 1) * (i + 2) / 6; if (t <= MAX) v.push_back(t); else break; } for (int i = 0; i <= MAX; i++) { for (int j = 0; j < v.size(); j++) { if (i + v[j] <= MAX) { if (v[j] & 1) dp2[i + v[j]] = min(dp2[i + v[j]], dp2[i] + 1); dp[i + v[j]] = min(dp[i + v[j]], dp[i] + 1); } } } } int main() { solve(); while (1) { int n; cin >> n; if (n == 0) break; cout << dp[n] << " " << dp2[n] << endl; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) const int MAX = 1000000; int dp[MAX + 1]; int dp2[MAX + 1]; void solve() { fill(dp, dp + MAX + 1, MAX); fill(dp2, dp2 + MAX + 1, MAX); dp[0] = dp2[0] = 0; vector<int> v; for (int i = 0;; i++) { int t = i * (i + 1) * (i + 2) / 6; if (t <= MAX) v.push_back(t); else break; } for (int i = 0; i <= MAX; i++) { for (int j = 0; j < v.size(); j++) { if (i + v[j] <= MAX) { if (v[j] & 1) dp2[i + v[j]] = min(dp2[i + v[j]], dp2[i] + 1); dp[i + v[j]] = min(dp[i + v[j]], dp[i] + 1); } } } } int main() { solve(); while (1) { int n; cin >> n; if (n == 0) break; cout << dp[n] << " " << dp2[n] << endl; } }
[["-", 0, 1, 0, 11, 12, 11, 31, 69, 28, 22], ["+", 0, 1, 0, 11, 12, 11, 31, 69, 28, 22], ["-", 0, 11, 12, 11, 31, 69, 341, 342, 0, 13], ["+", 0, 11, 12, 11, 31, 69, 341, 342, 0, 13]]
1
305
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; #define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--) #define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--) #define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--) #define vrep(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define pb push_back template <class T, class S> inline pair<T, S> mp(T x, S y) { return make_pair(x, y); } template <class T, class S> inline bool minup(T &m, S x) { return m > (T)x ? (m = (T)x, true) : false; } template <class T, class S> inline bool maxup(T &m, S x) { return m < (T)x ? (m = (T)x, true) : false; } static const int INF = 1000000000; static const ll MOD = 1000000007LL; static const double EPS = 1E-12; const int MAX_N = 1000011; int dp1[MAX_N + 2]; int dp2[MAX_N + 2]; vi w1; vi w2; int N; inline int get(int i) { return i * (i + 1) / 2 * (i + 2) / 3; } int main() { rrep(i, 1, MAX_N) { if (get(i) >= MAX_N) break; w1.pb(get(i)); if (get(i) & 1) w2.pb(get(i)); } int n1 = w1.size(); int n2 = w2.size(); rep(i, MAX_N) dp1[i] = dp2[i] = INF; rep(i, n1) dp1[w1[i]] = 1; rep(i, n2) dp2[w2[i]] = 1; rrep(i, 1, 111) rep(j, n1) if (i + w1[j] < MAX_N) minup(dp1[i + w1[j]], dp1[i] + 1); rrep(i, 1, 111) rep(j, n2) if (i + w2[j] < MAX_N) minup(dp2[i + w2[j]], dp2[i] + 1); while (cin >> N, N) cout << dp1[N] << ' ' << dp2[N] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; #define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--) #define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--) #define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--) #define vrep(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define pb push_back template <class T, class S> inline pair<T, S> mp(T x, S y) { return make_pair(x, y); } template <class T, class S> inline bool minup(T &m, S x) { return m > (T)x ? (m = (T)x, true) : false; } template <class T, class S> inline bool maxup(T &m, S x) { return m < (T)x ? (m = (T)x, true) : false; } static const int INF = 1000000000; static const ll MOD = 1000000007LL; static const double EPS = 1E-12; const int MAX_N = 1000011; int dp1[MAX_N + 2]; int dp2[MAX_N + 2]; vi w1; vi w2; int N; inline int get(int i) { return i * (i + 1) / 2 * (i + 2) / 3; } int main() { rrep(i, 1, MAX_N) { if (get(i) >= MAX_N) break; w1.pb(get(i)); if (get(i) & 1) w2.pb(get(i)); } int n1 = w1.size(); int n2 = w2.size(); rep(i, MAX_N) dp1[i] = dp2[i] = INF; rep(i, n1) dp1[w1[i]] = 1; rep(i, n2) dp2[w2[i]] = 1; rrep(i, 1, MAX_N) rep(j, n1) if (i + w1[j] < MAX_N) minup(dp1[i + w1[j]], dp1[i] + 1); rrep(i, 1, MAX_N) rep(j, n2) if (i + w2[j] < MAX_N) minup(dp2[i + w2[j]], dp2[i] + 1); while (cin >> N, N) cout << dp1[N] << ' ' << dp2[N] << endl; return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
559
#include <algorithm> #include <iostream> #include <vector> using namespace std; vector<int> c; vector<int> a, b; int main() { c.push_back(0); for (int i = 1; c[i - 1] <= 1000010; i++) c.push_back(i * (i + 1) * (i + 2) / 6); for (int i = 0; i <= 1000000; i++) { a.push_back(i); b.push_back(i); } for (int i = 0; i < c.size(); i++) { for (int j = 1; j <= 1000000; j++) { if (j - c[i] >= 0) { if (a[j - c[i]] >= 0) a[j] = min(a[j], a[j - c[i]] + 1); if (c[i] % 2 == 1 && b[j - c[i]] > 0) { b[j] = min(b[j], b[j - c[i]] + 1); } } } } while (1) { int m; cin >> m; if (m == 0) break; cout << a[m] << " " << b[m] << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; vector<int> c; vector<int> a, b; int main() { c.push_back(0); for (int i = 1; c[i - 1] <= 1000010; i++) c.push_back(i * (i + 1) * (i + 2) / 6); for (int i = 0; i <= 1000001; i++) { a.push_back(i); b.push_back(i); } for (int i = 0; i < c.size(); i++) { for (int j = 1; j <= 1000000; j++) { if (j - c[i] >= 0) { if (a[j - c[i]] >= 0) a[j] = min(a[j], a[j - c[i]] + 1); if (c[i] % 2 == 1 && b[j - c[i]] >= 0) { b[j] = min(b[j], b[j - c[i]] + 1); } } } } while (1) { int m; cin >> m; if (m == 0) break; cout << a[m] << " " << b[m] << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 20]]
1
287
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <utility> #define pi 3.14159 #define Inf (int)pow(2., 12.) using namespace std; typedef std::pair<int, int> mypair; queue<mypair> qu; #define M 1000000 int odd[M], all[M]; void update(int *ar, int x) { for (int i = x; i <= M; ++i) { ar[i] = min(ar[i], ar[i - x] + 1); } } int main() { odd[0] = all[0] = 0; for (int i = 1; i <= M; ++i) { odd[i] = all[i] = 10000000; } for (int i = 1;; ++i) { int x = i * (i + 1) * (i + 2) / 6; if (x > M) break; update(all, x); if (x % 2 == 1) update(odd, x); } int n; while (1) { cin >> n; if (n == 0) break; cout << all[n] << " " << odd[n] << endl; } }
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <utility> #define pi 3.14159 #define Inf (int)pow(2., 12.) using namespace std; typedef std::pair<int, int> mypair; queue<mypair> qu; #define M 1000000 int odd[M + 1], all[M + 1]; void update(int *ar, int x) { for (int i = x; i <= M; ++i) { ar[i] = min(ar[i], ar[i - x] + 1); } } int main() { odd[0] = all[0] = 0; for (int i = 1; i <= M; ++i) { odd[i] = all[i] = 10000000; } for (int i = 1;; ++i) { int x = i * (i + 1) * (i + 2) / 6; if (x > M) break; update(all, x); if (x % 2 == 1) update(odd, x); } int n; while (1) { cin >> n; if (n == 0) break; cout << all[n] << " " << odd[n] << endl; } }
[["+", 0, 30, 0, 43, 49, 80, 81, 16, 17, 72], ["+", 0, 30, 0, 43, 49, 80, 81, 16, 12, 13]]
1
254
#include <algorithm> #include <iostream> using namespace std; int i, j, n, three[1000000], four[1000000], four_2[1000000], c[1000000], c_2[1000000]; int main() { three[0] = 1; four[0] = 1; for (i = 1; i < 1000000; i++) three[i] = i + 1 + three[i - 1]; for (i = 1; i < 1000000; i++) four[i] = three[i] + four[i - 1]; int k = 0; for (i = 0; i < 1000000; i++) { if (four[i] % 2 == 1) { four_2[k] = four[i]; k++; } } for (i = 0; i <= n; i++) { c[i] = 2000000; c_2[i] = 2000000; } c[0] = 0; for (i = 0; i < 1000; i++) { for (j = four[i]; j <= n; j++) { c[j] = min(c[j], c[j - four[i]] + 1); } } c_2[0] = 0; for (i = 0; i < 1000; i++) { for (j = four_2[i]; j <= n; j++) { c_2[j] = min(c_2[j], c_2[j - four_2[i]] + 1); } } while (1) { int n; cin >> n; if (n == 0) break; cout << c[n] << " " << c_2[n] << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int i, j, n, three[1000000], four[1000000], four_2[1000000], c[1000000], c_2[1000000]; int main() { three[0] = 1; four[0] = 1; for (i = 1; i < 1000000; i++) three[i] = i + 1 + three[i - 1]; for (i = 1; i < 1000000; i++) four[i] = three[i] + four[i - 1]; int k = 0; for (i = 0; i < 1000000; i++) { if (four[i] % 2 == 1) { four_2[k] = four[i]; k++; } } for (i = 0; i < 1000000; i++) { c[i] = 2000000; c_2[i] = 2000000; } c[0] = 0; for (i = 0; i < 1000; i++) { for (j = four[i]; j <= 1000000; j++) { c[j] = min(c[j], c[j - four[i]] + 1); } } c_2[0] = 0; for (i = 0; i < 1000; i++) { for (j = four_2[i]; j <= 1000000; j++) { c_2[j] = min(c_2[j], c_2[j - four_2[i]] + 1); } } while (1) { int n; cin >> n; if (n == 0) break; cout << c[n] << " " << c_2[n] << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
1
366
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1167&lang=jp #include <fstream> #include <iostream> #define Max_Len 1000000 using namespace std; int triCalc(int num) { static short int count[Max_Len]; static short int triangle[Max_Len]; static bool f = true; if (f) { //????????? for (int i = 0; i < Max_Len; i++) count[i] = triangle[i] = Max_Len; f = false; } int triNum; int i = 1; while (i * (i + 1) * (i + 2) / 6 <= num) i++; i--; triNum = i * (i + 1) * (i + 2) / 6; if (triNum == num) return 1; while (num / 5 < triNum) { if (triangle[num - triNum] == Max_Len) { if (count[num] > triCalc(num - triNum) + 1) count[num] = triCalc(num - triNum) + 1; } else if (count[num] > triangle[num - triNum] + 1) count[num] = triangle[num - triNum] + 1; i--; triNum = i * (i + 1) * (i + 2) / 6; } return triangle[num] = count[num]; } int oddCalc(int num) { static short int count[Max_Len]; static short int triangle[Max_Len]; static bool f = true; if (f) { //????????? for (int i = 0; i < Max_Len; i++) count[i] = triangle[i] = Max_Len; f = false; } int triNum; int i = 1; while (i * (i + 1) * (i + 2) / 6 <= num) i++; i--; while ((i * (i + 1) * (i + 2) / 6) % 2 == 0) i--; triNum = i * (i + 1) * (i + 2) / 6; if (triNum == num) return 1; while (i > 0) { if (triangle[num - triNum] == Max_Len) { if (count[num] > oddCalc(num - triNum) + 1) count[num] = oddCalc(num - triNum) + 1; } else if (count[num] > triangle[num - triNum] + 1) count[num] = triangle[num - triNum] + 1; i--; while ((i * (i + 1) * (i + 2) / 6) % 2 == 0) i--; triNum = i * (i + 1) * (i + 2) / 6; } return triangle[num] = count[num]; } int main() { /* ifstream fin; ofstream fout; fin.open("input.txt"); fout.open("output.txt"); */ int n; while (true) { cin >> n; if (n == 0) break; cout << triCalc(n) << " " << oddCalc(n) << endl; } // cout << oddCalc(40); return 0; }
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1167&lang=jp #include <fstream> #include <iostream> #define Max_Len 1000000 using namespace std; int triCalc(int num) { static int count[Max_Len]; static int triangle[Max_Len]; static bool f = true; if (f) { //初期化 for (int i = 0; i < Max_Len; i++) count[i] = triangle[i] = Max_Len; f = false; } int triNum; int i = 1; while (i * (i + 1) * (i + 2) / 6 <= num) i++; i--; triNum = i * (i + 1) * (i + 2) / 6; if (triNum == num) return 1; while (num / 5 < triNum) { if (triangle[num - triNum] == Max_Len) { if (count[num] > triCalc(num - triNum) + 1) count[num] = triCalc(num - triNum) + 1; } else if (count[num] > triangle[num - triNum] + 1) count[num] = triangle[num - triNum] + 1; i--; triNum = i * (i + 1) * (i + 2) / 6; } return triangle[num] = count[num]; } int oddCalc(int num) { static int count[Max_Len]; static int triangle[Max_Len]; static bool f = true; if (f) { //初期化 for (int i = 0; i < Max_Len; i++) count[i] = triangle[i] = Max_Len; f = false; } int triNum; int i = 1; while (i * (i + 1) * (i + 2) / 6 <= num) i++; i--; while ((i * (i + 1) * (i + 2) / 6) % 2 == 0) i--; triNum = i * (i + 1) * (i + 2) / 6; if (triNum == num) return 1; while (i > 0) { if (triangle[num - triNum] == Max_Len) { if (count[num] > oddCalc(num - triNum) + 1) count[num] = oddCalc(num - triNum) + 1; } else if (count[num] > triangle[num - triNum] + 1) count[num] = triangle[num - triNum] + 1; i--; while ((i * (i + 1) * (i + 2) / 6) % 2 == 0) i--; triNum = i * (i + 1) * (i + 2) / 6; } return triangle[num] = count[num]; } int main() { int n; while (true) { cin >> n; if (n == 0) break; cout << triCalc(n) << " " << oddCalc(n) << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 39, 86, 0, 133]]
1
602
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int x[8000000][2], a[999], b[999], s, n; int main() { a[0] = 1; b[0] = 1; for (int i = 1; i < 300; i++) { a[i] = a[i - 1] + (i + 1) * (i + 2) / 2; if (a[i] % 2 == 1) { s++; b[s] = a[i]; } } n = 1200000; memset(x, 127, sizeof(x)); x[0][0] = 0; x[0][1] = 0; for (int i = 0; i < 200; i++) { for (int j = 0; j < n; j++) { x[j + a[i]][0] = min(x[j + a[i]][0], x[j][0] + 1); } } for (int i = 0; i < 50; i++) { for (int j = 0; j < n; j++) { x[j + b[i]][1] = min(x[j + b[i]][1], x[j][1] + 1); } } while (true) { cin >> n; if (!n) { break; } cout << x[n][0] << x[n][1] << endl; } }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int x[8000000][2], a[999], b[999], s, n; int main() { a[0] = 1; b[0] = 1; for (int i = 1; i < 300; i++) { a[i] = a[i - 1] + (i + 1) * (i + 2) / 2; if (a[i] % 2 == 1) { s++; b[s] = a[i]; } } n = 1200000; memset(x, 127, sizeof(x)); x[0][0] = 0; x[0][1] = 0; for (int i = 0; i < 200; i++) { for (int j = 0; j < n; j++) { x[j + a[i]][0] = min(x[j + a[i]][0], x[j][0] + 1); } } for (int i = 0; i < 50; i++) { for (int j = 0; j < n; j++) { x[j + b[i]][1] = min(x[j + b[i]][1], x[j][1] + 1); } } while (true) { cin >> n; if (!n) { break; } cout << x[n][0] << ' ' << x[n][1] << endl; } }
[["+", 0, 16, 31, 16, 31, 16, 12, 103, 0, 104], ["+", 0, 16, 31, 16, 31, 16, 12, 103, 0, 125], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 17, 151]]
1
338
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORR(i, a, b) for (int i = a; i >= b; --i) typedef long long ll; #define N 1000000 int x[1000001] = {}, y[1000001] = {}; int main(void) { vector<int> a, b; int i = 1; while (i * (i + 1) * (i + 2) / 6 <= N) { int f = i * (i + 1) * (i + 2) / 6; a.push_back(f); if (f % 2 == 1) b.push_back(f); i++; } int n = a.size(), m = b.size(); FOR(i, 1, N) { x[i] = N; y[i] = N; } FOR(i, 0, n) { REP(j, n) { if (i + a[j] > N) break; x[i + a[j]] = min(x[i + a[j]], x[i] + 1); } REP(j, m) { if (i + b[j] > N) break; y[i + b[j]] = min(y[i + b[j]], y[i] + 1); } } while (cin >> n && n) { cout << x[n] << " " << y[n] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORR(i, a, b) for (int i = a; i >= b; --i) typedef long long ll; #define N 1000000 int x[1000001] = {}, y[1000001] = {}; int main(void) { vector<int> a, b; int i = 1; while (i * (i + 1) * (i + 2) / 6 <= N) { int f = i * (i + 1) * (i + 2) / 6; a.push_back(f); if (f % 2 == 1) b.push_back(f); i++; } int n = a.size(), m = b.size(); FOR(i, 1, N) { x[i] = N; y[i] = N; } FOR(i, 0, N - 1) { REP(j, n) { if (i + a[j] > N) break; x[i + a[j]] = min(x[i + a[j]], x[i] + 1); } REP(j, m) { if (i + b[j] > N) break; y[i + b[j]] = min(y[i + b[j]], y[i] + 1); } } while (cin >> n && n) { cout << x[n] << " " << y[n] << endl; } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
355
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; const int MAX_N = 1e6; vector<int> dp1, dp2; vector<int> tetra; int main(void) { int n = 1; do { tetra.push_back(n * (n + 1) * (n + 2) / 6); ++n; } while (tetra.back() <= MAX_N); dp1.push_back(0); dp2.push_back(0); for (int i = 1; i <= n; ++i) { int a1 = INT_MAX, a2 = INT_MAX; for (int t : tetra) { if (t > i) break; a1 = min(a1, dp1[i - t] + 1); if (t % 2) { a2 = min(a2, dp2[i - t] + 1); } } dp1.push_back(a1); dp2.push_back(a2); } int x; while (cin >> x, x) { cout << dp1[x] << " " << dp2[x] << endl; } return 0; }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; const int MAX_N = 1e6; vector<int> dp1, dp2; vector<int> tetra; int main(void) { int n = 1; do { tetra.push_back(n * (n + 1) * (n + 2) / 6); ++n; } while (tetra.back() <= MAX_N); dp1.push_back(0); dp2.push_back(0); for (int i = 1; i <= MAX_N; ++i) { int a1 = INT_MAX, a2 = INT_MAX; for (int t : tetra) { if (t > i) break; a1 = min(a1, dp1[i - t] + 1); if (t % 2) { a2 = min(a2, dp2[i - t] + 1); } } dp1.push_back(a1); dp2.push_back(a2); } int x; while (cin >> x, x) { cout << dp1[x] << " " << dp2[x] << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22]]
1
260
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define vi vector<int> #define vvi vector<vector<int>> #define ll long long int #define vl vector<ll> #define vvl vector<vector<ll>> #define vb vector<bool> #define vc vector<char> #define vs vector<string> #define ld long double #define INF 1e9 #define EPS 0.0000000001 #define rep(i, n) for (int i = 0; i < n; i++) #define loop(i, s, n) for (int i = s; i < n; i++) #define all(in) in.begin(), in.end() template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define MAX 1e6 + 10 using namespace std; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; using namespace std; #define int ll vector<int> precal; vector<int> dp(MAX, MAX); vector<bool> flag(MAX + 10, false); signed main() { int n; precal.push_back(0); dp[0] = 0; for (int i = 1;; i++) { int cnt = ((i) * (i + 1) * (i + 2)) / 6; if (cnt > MAX) break; precal.push_back(cnt); flag[cnt] = true; } for (int i = 1; i <= MAX - 9; i++) { if (flag[i]) { dp[i] = 1; continue; } int index = (int)(upper_bound(precal.begin(), precal.end(), i) - precal.begin()); for (int j = 0; j <= index; j++) dp[i] = min(dp[i], dp[precal[j]] + dp[i - precal[j]]); } vector<int> oddcal; vector<int> odp(MAX, MAX); vector<bool> oddflag(MAX + 10, false); oddcal.push_back(0); odp[0] = 0; for (int i = 1;; i++) { int cnt = (i * (i + 1) * (i + 2)) / 6; if (cnt > MAX) break; if (!(cnt % 2)) continue; oddcal.push_back(cnt); oddflag[cnt] = true; } for (int i = 1; i <= MAX - 9; i++) { if (oddflag[i]) { odp[i] = 1; continue; } int index = (int)(upper_bound(oddcal.begin(), oddcal.end(), i) - oddcal.begin()); for (int j = 0; j <= index; j++) odp[i] = min(odp[i], odp[oddcal[j]] + odp[i - oddcal[j]]); } while (cin >> n, n) { cout << dp[n] << " " << odp[n] << endl; } }
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define vi vector<int> #define vvi vector<vector<int>> #define ll long long int #define vl vector<ll> #define vvl vector<vector<ll>> #define vb vector<bool> #define vc vector<char> #define vs vector<string> #define ld long double #define INF 1e9 #define EPS 0.0000000001 #define rep(i, n) for (int i = 0; i < n; i++) #define loop(i, s, n) for (int i = s; i < n; i++) #define all(in) in.begin(), in.end() template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define MAX 1e6 + 10 using namespace std; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; using namespace std; #define int ll vector<int> precal; vector<int> dp(MAX + 10, MAX); vector<bool> flag(MAX + 10, false); signed main() { int n; precal.push_back(0); dp[0] = 0; for (int i = 1;; i++) { int cnt = ((i) * (i + 1) * (i + 2)) / 6; if (cnt > MAX) break; precal.push_back(cnt); flag[cnt] = true; } for (int i = 1; i <= MAX - 9; i++) { if (flag[i]) { dp[i] = 1; continue; } int index = (int)(upper_bound(precal.begin(), precal.end(), i) - precal.begin()); for (int j = 0; j < index; j++) dp[i] = min(dp[i], dp[precal[j]] + dp[i - precal[j]]); } vector<int> oddcal; vector<int> odp(MAX + 10, MAX); vector<bool> oddflag(MAX + 10, false); oddcal.push_back(0); odp[0] = 0; for (int i = 1;; i++) { int cnt = (i * (i + 1) * (i + 2)) / 6; if (cnt > MAX) break; if (!(cnt % 2)) continue; oddcal.push_back(cnt); oddflag[cnt] = true; } for (int i = 1; i <= MAX - 9; i++) { if (oddflag[i]) { odp[i] = 1; continue; } int index = (int)(upper_bound(oddcal.begin(), oddcal.end(), i) - oddcal.begin()); for (int j = 0; j < index; j++) odp[i] = min(odp[i], odp[oddcal[j]] + odp[i - oddcal[j]]); } while (cin >> n, n) { cout << dp[n] << " " << odp[n] << endl; } }
[["+", 0, 43, 49, 50, 51, 4, 0, 16, 17, 72], ["+", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18]]
1
678
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using LL = long long; using ULL = unsigned long long; constexpr long long MOD = 10007; constexpr int INF = 2000000000; constexpr int HINF = INF / 2; constexpr double DINF = 100000000000000000.0; constexpr long long LINF = 9223372036854775807; constexpr long long HLINF = 4500000000000000000; const double PI = acos(-1); int dp[1001000], dp2[1001000]; vector<int> pollock, pollock2; int main() { for (int i = 1;; i++) { LL num = i * (i + 1) * (i + 2) / 6; if (num > 1000000) break; if (num % 2 == 0) pollock2.emplace_back(num); pollock.emplace_back(num); } // cout << 1 << endl; for (int i = 0; i < 1000000; i++) { dp[i] = dp2[i] = i; } // cout << 2 << endl; for (int i = 1; i < pollock.size(); i++) { for (int j = pollock[i]; j <= 1000000; j++) { dp[j] = min(dp[j], dp[j - pollock[i]] + 1); } } // cout << 3 << endl; for (int i = 1; i < pollock2.size(); i++) { for (int j = pollock2[i]; j <= 1000000; j++) { dp2[j] = min(dp2[j], dp2[j - pollock2[i]] + 1); } } // cout << 4 << endl; int n; while (cin >> n && n) { cout << dp[n] << " " << dp2[n] << endl; } }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using LL = long long; using ULL = unsigned long long; constexpr long long MOD = 10007; constexpr int INF = 2000000000; constexpr int HINF = INF / 2; constexpr double DINF = 100000000000000000.0; constexpr long long LINF = 9223372036854775807; constexpr long long HLINF = 4500000000000000000; const double PI = acos(-1); int dp[1001000], dp2[1001000]; vector<int> pollock, pollock2; int main() { for (int i = 1;; i++) { LL num = i * (i + 1) * (i + 2) / 6; if (num > 1000000) break; if (num % 2 == 1) pollock2.emplace_back(num); pollock.emplace_back(num); } // cout << 1 << endl; for (int i = 0; i < 1000000; i++) { dp[i] = dp2[i] = i; } // cout << 2 << endl; for (int i = 1; i < pollock.size(); i++) { for (int j = pollock[i]; j <= 1000000; j++) { dp[j] = min(dp[j], dp[j - pollock[i]] + 1); } } // cout << 3 << endl; for (int i = 1; i < pollock2.size(); i++) { for (int j = pollock2[i]; j <= 1000000; j++) { dp2[j] = min(dp2[j], dp2[j - pollock2[i]] + 1); } } // cout << 4 << endl; int n; while (cin >> n && n) { cout << dp[n] << " " << dp2[n] << endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
377
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> #define INF 1e9 #define llINF 9223372036854775807 #define pb push_back #define mp make_pair #define F first #define S second #define ll long long using namespace std; int main() { int dp[1000010] = {}; int dp2[1000010] = {}; for (int i = 1; i <= 1000000; i++) { dp[i] = INF; dp2[i] = INF; } vector<ll> gu; vector<ll> ki; ll cnt = 1; while (1) { if ((cnt * (cnt + 1) * (cnt + 2) / 6) > 1000000) break; if (((cnt * (cnt + 1) * (cnt + 2)) / 6) % 2) { ki.pb(cnt * (cnt + 1) * (cnt + 2) / 6); dp[ki[ki.size() - 1]] = 1; dp2[gu[gu.size() - 1]] = 1; } else { gu.pb(cnt * (cnt + 1) * (cnt + 2) / 6); dp[gu[gu.size() - 1]] = 1; } cnt++; } dp[1] = 1; dp2[1] = 1; for (int i = 1; i < 1000000; i++) { for (int j = 0; j < gu.size(); j++) { if (i + gu[j] > 1000000) break; dp[i + gu[j]] = min(dp[i + gu[j]], (dp[i] + 1)); } for (int j = 0; j < ki.size(); j++) { if (i + ki[j] > 1000000) break; dp[i + ki[j]] = min(dp[i + ki[j]], (dp[i] + 1)); dp2[i + ki[j]] = min(dp2[i + ki[j]], (dp2[i] + 1)); } } int n; while (cin >> n, n) { cout << dp[n] << " " << dp2[n] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> #define INF 1e9 #define llINF 9223372036854775807 #define pb push_back #define mp make_pair #define F first #define S second #define ll long long using namespace std; int main() { int dp[1000010] = {}; int dp2[1000010] = {}; for (int i = 1; i <= 1000000; i++) { dp[i] = INF; dp2[i] = INF; } vector<ll> gu; vector<ll> ki; ll cnt = 1; while (1) { if ((cnt * (cnt + 1) * (cnt + 2) / 6) > 1000000) break; if (((cnt * (cnt + 1) * (cnt + 2)) / 6) % 2) { ki.pb(cnt * (cnt + 1) * (cnt + 2) / 6); dp[ki[ki.size() - 1]] = 1; dp2[ki[ki.size() - 1]] = 1; } else { gu.pb(cnt * (cnt + 1) * (cnt + 2) / 6); dp[gu[gu.size() - 1]] = 1; } cnt++; } dp[1] = 1; dp2[1] = 1; for (int i = 1; i < 1000000; i++) { for (int j = 0; j < gu.size(); j++) { if (i + gu[j] > 1000000) break; dp[i + gu[j]] = min(dp[i + gu[j]], (dp[i] + 1)); } for (int j = 0; j < ki.size(); j++) { if (i + ki[j] > 1000000) break; dp[i + ki[j]] = min(dp[i + ki[j]], (dp[i] + 1)); dp2[i + ki[j]] = min(dp2[i + ki[j]], (dp2[i] + 1)); } } int n; while (cin >> n, n) { cout << dp[n] << " " << dp2[n] << endl; } return 0; }
[["-", 0, 11, 31, 69, 341, 342, 0, 69, 28, 22], ["+", 0, 11, 31, 69, 341, 342, 0, 69, 28, 22], ["-", 341, 342, 0, 16, 31, 2, 63, 118, 28, 22], ["+", 341, 342, 0, 16, 31, 2, 63, 118, 28, 22]]
1
496
#include <iomanip> #include <iostream> #include <vector> using namespace std; const int inf = 1000000009; int main() { vector<int> memo(1000000, inf); memo[0] = 0; vector<int> odd_memo(1000000, inf); odd_memo[0] = 0; for (int i = 0; i < 1000000; i++) { memo[i] = i; odd_memo[i] = i; } for (int l = 2; l < 85; l++) { const int n = (l * (l + 1) * (l + 2)) / 6; for (int i = 0; i < 1000000; i++) { if (i - n >= 0) { memo[i] = min(memo[i - n] + 1, memo[i]); if (l % 2 == 1) { odd_memo[i] = min(odd_memo[i - n] + 1, odd_memo[i]); } } } } int n; while (cin >> n, n != 0) { cout << memo[n] << " " << odd_memo[n] << endl; } }
#include <iomanip> #include <iostream> #include <vector> using namespace std; const int inf = 1000000009; int main() { vector<int> memo(1000000, inf); memo[0] = 0; vector<int> odd_memo(1000000, inf); odd_memo[0] = 0; for (int i = 0; i < 1000000; i++) { memo[i] = i; odd_memo[i] = i; } for (int l = 2; l < 200; l++) { const int n = (l * (l + 1) * (l + 2)) / 6; for (int i = 0; i < 1000000; i++) { if (i - n >= 0) { memo[i] = min(memo[i - n] + 1, memo[i]); if (n % 2 == 1) { odd_memo[i] = min(odd_memo[i - n] + 1, odd_memo[i]); } } } } int n; while (cin >> n, n != 0) { cout << memo[n] << " " << odd_memo[n] << endl; } }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22]]
1
239
#include <bits/stdc++.h> using namespace std; #define lp(i, n) for (int i = 0; i < n; i++) #define lpin(a, i, n) \ for (int i = 0; i < n; i++) { \ cin >> a[i]; \ } void calc(int a[], int f) { for (int i = f * (f + 1) * (f + 2) / 6; i <= 1000000; i++) { if (a[i] == 0) a[i] = a[i - f * (f + 1) * (f + 2) / 6] + 1; else if (a[i] > a[i - f * (f + 1) * (f + 2) / 6] + 1) a[i] = a[i - f * (f + 1) * (f + 2) / 6] + 1; } } int x[1000001], b[1000001]; int main() { for (int i = 1; i * (i + 1) * (i + 2) / 6 < 1000000; i++) { calc(x, i); } for (int i = 1; i * (i + 1) * (i + 2) / 6 < 1000000; i++) { if (i * (i + 1) * (i + 2) / 6 % 2 == 1) calc(b, i); } while (1) { int z; cin >> z; if (z == 0) break; cout << x[z] << " " << b[z] - 3 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define lp(i, n) for (int i = 0; i < n; i++) #define lpin(a, i, n) \ for (int i = 0; i < n; i++) { \ cin >> a[i]; \ } void calc(int a[], int f) { for (int i = f * (f + 1) * (f + 2) / 6; i <= 1000000; i++) { if (a[i] == 0) a[i] = a[i - f * (f + 1) * (f + 2) / 6] + 1; else if (a[i] > a[i - f * (f + 1) * (f + 2) / 6] + 1) a[i] = a[i - f * (f + 1) * (f + 2) / 6] + 1; } } int x[1000001], b[1000001]; int main() { for (int i = 1; i * (i + 1) * (i + 2) / 6 < 1000000; i++) { calc(x, i); } for (int i = 1; i * (i + 1) * (i + 2) / 6 < 1000000; i++) { if (i * (i + 1) * (i + 2) / 6 % 2 == 1) { calc(b, i); } } while (1) { int z; cin >> z; if (z == 0) break; cout << x[z] << " " << b[z] << endl; } return 0; }
[["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 33], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
321
#include "algorithm" #include "bitset" #include "climits" #include "cmath" #include "cstdio" #include "functional" #include "iomanip" #include "iostream" #include "list" #include "map" #include "queue" #include "random" #include "set" #include "stack" #include "string" #include "unordered_map" #include "unordered_set" using namespace std; const long long int MOD = 1000000007; long long int N, M, K, H, W, L, R; int main() { ios::sync_with_stdio(false); cin.tie(0); vector<int> e(1000000, MOD); e[0] = 0; for (int i = 0; i < 1000000; i++) { for (int j = 1; j < 1000; j++) { int box = i + j * (j + 1) * (j + 2) / 6; if (box >= 1000000) break; e[box] = min(e[box], e[i] + 1); } } vector<int> o(1000000, MOD); o[0] = 0; for (int i = 0; i < 1000000; i++) { for (int j = 1; j < 1000; j += 2) { int box = i + j * (j + 1) * (j + 2) / 6; if (box >= 1000000) break; o[box] = min(o[box], o[i] + 1); } } cin >> N; while (N) { cout << e[N] << " " << o[N] << endl; cin >> N; } return 0; }
#include "algorithm" #include "bitset" #include "climits" #include "cmath" #include "cstdio" #include "functional" #include "iomanip" #include "iostream" #include "list" #include "map" #include "queue" #include "random" #include "set" #include "stack" #include "string" #include "unordered_map" #include "unordered_set" using namespace std; const long long int MOD = 1000000007; long long int N, M, K, H, W, L, R; int main() { ios::sync_with_stdio(false); cin.tie(0); vector<int> e(1000000, MOD); e[0] = 0; for (int i = 0; i < 1000000; i++) { for (int j = 1; j < 1000; j++) { int box = i + j * (j + 1) * (j + 2) / 6; if (box >= 1000000) break; e[box] = min(e[box], e[i] + 1); } } vector<int> o(1000000, MOD); o[0] = 0; for (int i = 0; i < 1000000; i++) { for (int j = 1; j < 1000; j += 4) { int box = i + j * (j + 1) * (j + 2) / 6; if (box >= 1000000) break; o[box] = min(o[box], o[i] + 1); } } cin >> N; while (N) { cout << e[N] << " " << o[N] << endl; cin >> N; } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 26, 11, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 26, 11, 12, 13]]
1
351
#include <algorithm> #include <cmath> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, j, k) for (int i = (int)j; i < (int)k; i++) #define itrep(i, x) for (auto i = (x).begin(); i != (x).end(); i++) #define Sort(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define vec vector #define INF (int)1e9 #define INFL 1e18 #define MOD 1000000007 #define pb push_back #define MP make_pair #define PI 3.1415926535 typedef long long int ll; typedef std::pair<int, int> P; int D = 1; int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; using namespace std; int main() { vector<int> v, oddv; for (ll i = 1;; i++) { if (i * (i + 1) * (i + 2) / 6 <= 1000000) { v.pb(i * (i + 1) * (i + 2) / 6); } else break; } int ans1[1000010], ans2[1000010]; rep(i, 0, 1000010) ans1[i] = ans2[i] = INF; ans1[0] = 0; ans2[0] = 0; rep(i, 0, 1000000) { ans1[i] = i; ans2[i] = i; for (int j = 0; i >= v[j]; j++) { ans1[i] = min(ans1[i - v[j]] + 1, ans1[i]); if (v[j] % 2 == 1) ans2[i] = min(ans2[i], ans2[i - v[j]] + 1); } } int n; while (cin >> n && n) { cout << ans1[n] << " " << ans2[n] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, j, k) for (int i = (int)j; i < (int)k; i++) #define itrep(i, x) for (auto i = (x).begin(); i != (x).end(); i++) #define Sort(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define vec vector #define INF (int)1e9 #define INFL 1e18 #define MOD 1000000007 #define pb push_back #define MP make_pair #define PI 3.1415926535 typedef long long int ll; typedef std::pair<int, int> P; int D = 1; int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; using namespace std; int main() { vector<int> v, oddv; for (ll i = 1;; i++) { if (i * (i + 1) * (i + 2) / 6 <= 2000000) { v.pb(i * (i + 1) * (i + 2) / 6); } else break; } int ans1[1000010], ans2[1000010]; rep(i, 0, 1000010) ans1[i] = ans2[i] = INF; ans1[0] = 0; ans2[0] = 0; rep(i, 0, 1000000) { ans1[i] = i; ans2[i] = i; for (int j = 0; i >= v[j]; j++) { ans1[i] = min(ans1[i - v[j]] + 1, ans1[i]); if (v[j] % 2 == 1) ans2[i] = min(ans2[i], ans2[i - v[j]] + 1); } } int n; while (cin >> n && n) { cout << ans1[n] << " " << ans2[n] << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
393
#include <iostream> #define MAX 1000000 #define INF 1000000000 using namespace std; int dp1[MAX + 1], dp2[MAX + 1]; int main() { // INFで初期化 for (int i = 0; i <= MAX; i++) { dp1[i] = INF; dp2[i] = INF; } for (int n = 1; n * (n + 1) * (n * 2) / 6 <= MAX; n++) { int a = n * (n + 1) * (n + 2) / 6; //ポロック数の生成 for (int i = a; i <= MAX; i++) //正四面体数の個数dp(a以下は必要ない) dp1[i] = min(dp1[i], dp1[i - a] + 1); //更新しないor左側から更新する if (a % 2 == 0) continue; //偶数の時は次のroopへ for (int i = a; i <= MAX; i++) //奇数の正四面体数の個数dp(a以下は必要ない) dp2[i] = min(dp2[i], dp2[i - a] + 1); //更新しないor左側から更新する } //入出力 while (true) { int n; cin >> n; if (n == 0) break; cout << dp1[n] << " " << dp2[n] << endl; } return 0; }
#include <iostream> #define MAX 1000000 #define INF 1000000000 using namespace std; int dp1[MAX + 1], dp2[MAX + 1]; int main() { // INFで初期化 for (int i = 1; i <= MAX; i++) { dp1[i] = INF; dp2[i] = INF; } for (int n = 1; n * (n + 1) * (n + 2) / 6 <= MAX; n++) { int a = n * (n + 1) * (n + 2) / 6; //ポロック数の生成 for (int i = a; i <= MAX; i++) { //正四面体数の個数dp(a以下は必要ない) dp1[i] = min(dp1[i], dp1[i - a] + 1); //更新しないor左側から更新する } if (a % 2 == 0) continue; //偶数の時は次のroopへ for (int i = a; i <= MAX; i++) { //奇数の正四面体数の個数dp(a以下は必要ない) dp2[i] = min(dp2[i], dp2[i - a] + 1); //更新しないor左側から更新する } } //入出力 while (true) { int n; cin >> n; if (n == 0) break; cout << dp1[n] << " " << dp2[n] << endl; } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 31, 16, 31, 16, 12, 23, 0, 16, 17, 48], ["+", 31, 16, 31, 16, 12, 23, 0, 16, 17, 72], ["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 45], ["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46]]
1
244
#include <iostream> using namespace std; #define MAX 1000000 #define INF 1000000000 int dp1[MAX + 1], dp2[MAX + 2]; int main() { for (int i = 1; i <= MAX; i++) { dp1[i] = INF; dp2[i] = INF; } for (int n = 1; n * (n + 1) * (n + 2) / 6 <= MAX; n++) { int a = n * (n + 1) * (n + 2) / 6; for (int i = a; i <= MAX; i++) { dp1[i] = min(dp1[i], dp2[i - a] + 1); } if (a % 2 == 0) continue; for (int i = a; i <= MAX; i++) { dp2[i] = min(dp2[i], dp2[i - a] + 1); } } while (true) { int n; cin >> n; if (n == 0) break; cout << dp1[n] << " " << dp2[n] << endl; } return 0; }
#include <iostream> using namespace std; #define MAX 1000000 #define INF 1000000000 int dp1[MAX + 1], dp2[MAX + 2]; int main() { for (int i = 1; i <= MAX; i++) { dp1[i] = INF; dp2[i] = INF; } for (int n = 1; n * (n + 1) * (n + 2) / 6 <= MAX; n++) { int a = n * (n + 1) * (n + 2) / 6; for (int i = a; i <= MAX; i++) { dp1[i] = min(dp1[i], dp1[i - a] + 1); } if (a % 2 == 0) continue; for (int i = a; i <= MAX; i++) { dp2[i] = min(dp2[i], dp2[i - a] + 1); } } while (true) { int n; cin >> n; if (n == 0) break; cout << dp1[n] << " " << dp2[n] << endl; } return 0; }
[["-", 12, 2, 3, 4, 0, 16, 31, 69, 28, 22], ["+", 12, 2, 3, 4, 0, 16, 31, 69, 28, 22]]
1
240
#include <algorithm> #include <iostream> using namespace std; int dp[2][1000001]; int f(int n) { return n * (n + 1) * (n + 2) / 6; } int main() { int n; for (int k = 0; k < 2; k++) fill(dp[k], dp[k] + 1000001, 1000000); dp[0][0] = dp[1][0] = 0; for (int i = 1; i <= n; i++) { for (int k = 1;; k++) { int num = f(k); if (i - num >= 0) { dp[0][i] = min(dp[0][i], dp[0][i - num] + 1); if ((num % 2) == 1) { dp[1][i] = min(dp[1][i], dp[1][i - num] + 1); } } else { break; } } } while (cin >> n && n) { cout << dp[0][n] << " " << dp[1][n] << endl; } }
#include <algorithm> #include <iostream> using namespace std; int dp[2][1000001]; int f(int n) { return n * (n + 1) * (n + 2) / 6; } int main() { int n; for (int k = 0; k < 2; k++) fill(dp[k], dp[k] + 1000001, 1000000); dp[0][0] = dp[1][0] = 0; for (int i = 1; i <= 1000000; i++) { for (int k = 1;; k++) { int num = f(k); if (i - num >= 0) { dp[0][i] = min(dp[0][i], dp[0][i - num] + 1); if ((num % 2) == 1) { dp[1][i] = min(dp[1][i], dp[1][i - num] + 1); } } else { break; } } } while (cin >> n && n) { cout << dp[0][n] << " " << dp[1][n] << endl; } }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13]]
1
260
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; int n; int sum[222]; // int dp[1111111][222]; int dp[2][1111111]; /* int solve(int s,int i,int f){ //if(!s) return 0; if(i >= m) return s; if(dp[s][i])return dp[s][i]; int ret = 1 << 28; if(!((sum[i] + 1) % f)){ for(int j = 0;s - sum[i] * j >= 0 ; j++){ ret = min(ret,solve(s - sum[i] * j,i + 1,f) + j); } } ret = min(ret,solve(s,i + 1,f)); return dp[s][i] = ret; } */ int solve(int s, int f) { if (!s) return 0; if (dp[f - 1][s]) return dp[f - 1][s]; int ret = 1 << 28; for (int i = 0; s >= sum[i]; i++) { if (!((sum[i] + 1) % f)) { ret = min(ret, solve(s - sum[i], f) + 1); } } return dp[f - 1][s] = ret; } int main(void) { sum[0] = 1; for (int i = 1; sum[i - 1] <= 1000000; i++) { sum[i] = sum[i - 1] * (i + 3) / i; } for (int i = 1; i < 1000000; i++) { solve(i, 1); solve(i, 2); } puts("ok"); while (1) { scanf("%d", &n); if (!n) break; printf("%d %d\n", dp[0][n], dp[1][n]); } }
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; int n; int sum[222]; // int dp[1111111][222]; int dp[2][1111111]; /* int solve(int s,int i,int f){ //if(!s) return 0; if(i >= m) return s; if(dp[s][i])return dp[s][i]; int ret = 1 << 28; if(!((sum[i] + 1) % f)){ for(int j = 0;s - sum[i] * j >= 0 ; j++){ ret = min(ret,solve(s - sum[i] * j,i + 1,f) + j); } } ret = min(ret,solve(s,i + 1,f)); return dp[s][i] = ret; } */ int solve(int s, int f) { if (!s) return 0; if (dp[f - 1][s]) return dp[f - 1][s]; int ret = 1 << 28; for (int i = 0; s >= sum[i]; i++) { if (!((sum[i] + 1) % f)) { ret = min(ret, solve(s - sum[i], f) + 1); } } return dp[f - 1][s] = ret; } int main(void) { sum[0] = 1; for (int i = 1; sum[i - 1] <= 1000000; i++) { sum[i] = sum[i - 1] * (i + 3) / i; } for (int i = 1; i < 1000000; i++) { solve(i, 1); solve(i, 2); } // puts("ok"); while (1) { scanf("%d", &n); if (!n) break; printf("%d %d\n", dp[0][n], dp[1][n]); } }
[["-", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 62], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
1
289