結果
問題 | No.97 最大の値を求めるくえり |
ユーザー | Komaki__ |
提出日時 | 2014-12-07 21:19:36 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 688 ms / 5,000 ms |
コード長 | 9,677 bytes |
コンパイル時間 | 1,593 ms |
コンパイル使用メモリ | 172,016 KB |
実行使用メモリ | 8,536 KB |
最終ジャッジ日時 | 2024-06-11 17:19:38 |
合計ジャッジ時間 | 7,758 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 17 ms
6,816 KB |
testcase_01 | AC | 212 ms
8,536 KB |
testcase_02 | AC | 228 ms
6,940 KB |
testcase_03 | AC | 222 ms
6,940 KB |
testcase_04 | AC | 146 ms
6,940 KB |
testcase_05 | AC | 156 ms
6,940 KB |
testcase_06 | AC | 151 ms
6,944 KB |
testcase_07 | AC | 160 ms
6,944 KB |
testcase_08 | AC | 169 ms
6,944 KB |
testcase_09 | AC | 181 ms
6,944 KB |
testcase_10 | AC | 211 ms
6,944 KB |
testcase_11 | AC | 254 ms
6,944 KB |
testcase_12 | AC | 271 ms
6,940 KB |
testcase_13 | AC | 297 ms
6,944 KB |
testcase_14 | AC | 688 ms
6,940 KB |
testcase_15 | AC | 503 ms
6,944 KB |
testcase_16 | AC | 419 ms
6,940 KB |
testcase_17 | AC | 366 ms
6,944 KB |
testcase_18 | AC | 353 ms
8,536 KB |
ソースコード
#ifdef KOMAKI_LOCAL #include <omp.h> #else #define NDEBUG #endif #include <bits/stdc++.h> #include <sys/time.h> #include <unistd.h> using namespace std; #define i64 int64_t #define rep(i, n) for(i64 i = 0; i < ((i64)(n)); ++i) #define sz(v) ((i64)((v).size())) #define bit(n) (((i64)1)<<((i64)(n))) #define all(v) (v).begin(), (v).end() template <int POS, class TUPLE> void deploy(std::ostream &os, const TUPLE &tuple){} template <int POS, class TUPLE, class H, class ...Ts> void deploy(std::ostream &os, const TUPLE &t){ os << (POS == 0 ? "" : ", ") << get<POS>(t); deploy<POS + 1, TUPLE, Ts...>(os, t); } template <class ...Ts> std::ostream& operator<<(std::ostream &os, const std::tuple<Ts...> &t){ os << "("; deploy<0, std::tuple<Ts...>, Ts...>(os, t); os << ")"; return os; } template <class T> std::ostream& operator<<(std::ostream &os, std::vector<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "" : ", "); os << "}"; return os; } template <class T> std::ostream& operator<<(std::ostream &os, std::set<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "" : ", "); os << "}"; return os; } template <class T> std::ostream& operator<<(std::ostream &os, std::queue<T> &q){ auto qq = q; os << "{"; for(; !qq.empty(); qq.pop()){ os << qq.front() << (qq.size() != 1 ? ", " : ""); } os << "}"; return os; } template <class T> std::ostream& operator<<(std::ostream &os, std::priority_queue<T> &q){ auto qq = q; os << "{"; for(; !qq.empty(); qq.pop()){ os << qq.top() << (qq.size() != 1 ? ", " : ""); } os << "}"; return os; } template <class T, class K> std::ostream& operator<<(std::ostream &os, std::pair<T, K> &p){ os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T, class K> std::ostream& operator<<(std::ostream &os, std::map<T, K> &mp){ int remain = mp.size(); os << "{"; for(auto e: mp) os << "(" << e.first << " -> " << e.second << ")" << (--remain == 0 ? "" : ", "); os << "}"; return os; } template <class T, class K> std::ostream& operator<<(std::ostream &os, std::unordered_map<T, K> &mp){ int remain = mp.size(); os << "{"; for(auto e: mp) os << "(" << e.first << " -> " << e.second << ")" << (--remain == 0 ? "" : ", "); os << "}"; return os; } #define DEBUG0() { char buf[1000]; sprintf(buf, "line:%3d | ", __LINE__); std::cout << buf << std::endl; } #define DEBUG1(var0) { char buf[1000]; sprintf(buf, "line:%3d | ", __LINE__); std::cout << buf << (#var0) << "=" << (var0) << std::endl; } #define DEBUG2(var0, var1) { char buf[1000]; sprintf(buf, "line:%3d | ", __LINE__); std::cout << buf << (#var0) << "=" << (var0) << ", " << (#var1) << "=" << (var1) << std::endl; } #define DEBUG3(var0, var1, var2) { char buf[1000]; sprintf(buf, "line:%3d | ", __LINE__); std::cout << buf << (#var0) << "=" << (var0) << ", " << (#var1) << "=" << (var1) << ", " << (#var2) << "=" << (var2) << std::endl; } #define DEBUG4(var0, var1, var2, var3) { char buf[1000]; sprintf(buf, "line:%3d | ", __LINE__); std::cout << buf << (#var0) << "=" << (var0) << ", " << (#var1) << "=" << (var1) << ", " << (#var2) << "=" << (var2) << ", " << (#var3) << "=" << (var3) << std::endl; } #define DEBUG5(var0, var1, var2, var3, var4) { char buf[1000]; sprintf(buf, "line:%3d | ", __LINE__); std::cout << buf << (#var0) << "=" << (var0) << ", " << (#var1) << "=" << (var1) << ", " << (#var2) << "=" << (var2) << ", " << (#var3) << "=" << (var3) << ", " << (#var4) << "=" << (var4) << std::endl; } #define DEBUG6(var0, var1, var2, var3, var4, var5) { char buf[1000]; sprintf(buf, "line:%3d | ", __LINE__); std::cout << buf << (#var0) << "=" << (var0) << ", " << (#var1) << "=" << (var1) << ", " << (#var2) << "=" << (var2) << ", " << (#var3) << "=" << (var3) << ", " << (#var4) << "=" << (var4) << ", " << (#var5) << "=" << (var5) << std::endl; } unsigned xor128_x = 123456789, xor128_y = 362436069, xor128_z = 521288629, xor128_w = 88675123; unsigned xor128() { unsigned t = xor128_x ^ (xor128_x << 11); xor128_x = xor128_y; xor128_y = xor128_z; xor128_z = xor128_w; return xor128_w = xor128_w ^ (xor128_w >> 19) ^ (t ^ (t >> 8)); } /* const i64 LIMIT = 8000; const i64 MOD = 100003; void generate() { vector<i64> a; rep(i, MOD) a.push_back(xor128() % MOD); i64 bests[MOD]; rep(i, MOD) bests[i] = 0; vector<i64> updates[MOD]; i64 cnt = 0; rep(i, MOD){ //if(i % 100 == 0) DEBUG1(i); rep(q, MOD){ i64 t = (a[i] * q) % MOD; if(bests[q] < t){ if(LIMIT <= i){ ++cnt; updates[q].push_back(i); cout << q << ", " << i << ", "; if(cnt % 100 == 0) cout << endl; } bests[q] = t; } } } DEBUG1(cnt); } vector<i64> updates[MOD]; int t[] = int main() { generate(); return 0; i64 n, q; cin >> n >> q; vector<i64> a; rep(i, n) a.push_back(xor128() % MOD); i64 bests[MOD]; rep(i, MOD) bests[i] = 0; rep(i, min(LIMIT, n)){ rep(j, MOD) bests[j] = max(bests[j], (a[i] * j) % MOD); } rep(j, MOD){ for(auto t: updates[j])if(t < n) bests[j] = max(bests[j], (a[t] * j) % MOD); } rep(i, q){ i64 t; cin >> t; cout << bests[t] << endl; } } */ /******************************************************************/ /* */ /* Undefine "GF_USE_FIXED_GF" to use various mods, but slower. */ /* To use fixed one, set your mod at "static const int mod = ;" */ /* */ /******************************************************************/ #define GF_USE_FIXED_GF template <typename T> class GF { public: #ifdef GF_USE_FIXED_GF static const T mod = 100003; // Set your mod! GF() { permutation_memo = std::vector<T>(1, 1); inverse_memo = std::vector<T>(2, 1); permutation_inverse_memo = std::vector<T>(2, 1); } #else T mod; GF(T mod) : mod(mod) { permutation_memo = std::vector<T>(1, 1); inverse_memo = std::vector<T>(2, 1); permutation_inverse_memo = std::vector<T>(2, 1); } #endif T norm(T i); T add(T i0, T i1); T sub(T i0, T i1); T mul(T i0, T i1); T div(T i0, T i1); T pow(T i, int64_t p); T inv(T i); T permInv(T i); T permutation(T n, T m); // n * (n - 1) * ... * (n - m + 1), Non limited memorization, Avoid segmentation fault! T combination(T n, T m); // Memorized for n < MEMO_SIZE_LIMIT. private: static const int MEMO_SIZE_LIMIT = 2000000; std::vector<T> permutation_memo; std::vector<T> inverse_memo; std::vector<T> permutation_inverse_memo; }; template <typename T> inline T GF<T>::permutation(T n, T m) { if(m < 0 || n < m) return 0; while(permutation_memo.size() <= n){ permutation_memo.push_back(mul(permutation_memo.back(), permutation_memo.size())); } return mul(permutation_memo[n], permInv(n - m)); } template <typename T> inline T GF<T>::combination(T n, T m) { if(m < 0 || n < m) return 0; if(n - m < m) m = n - m; if(n < MEMO_SIZE_LIMIT){ T num = permutation(n, n); T den = mul(permInv(m), permInv(n - m)); return mul(num, den); } T num = 1, den = 1; for(T i = 0; i < m; ++i){ num = mul(num, n - i); den = mul(den, i + 1); } return div(num, den); } template <typename T> inline T GF<T>::norm(T i) { return (i % mod + mod) % mod; } template <typename T> inline T GF<T>::add(T i0, T i1) { T res = i0 + i1; if(mod <= res) return res - mod; return res; } template <typename T> inline T GF<T>::sub(T i0, T i1) { T res = i0 - i1; if(res < 0) return res + mod; return res; } template <typename T> inline T GF<T>::mul(T i0, T i1) { return (int64_t)i0 * i1 % mod; } template <typename T> inline T GF<T>::div(T i0, T i1) { return mul(i0, inv(i1)); } template <typename T> inline T GF<T>::permInv(T i) { while(permutation_inverse_memo.size() <= i){ permutation_inverse_memo.push_back(mul(permutation_inverse_memo.back(), inv(permutation_inverse_memo.size()))); } return permutation_inverse_memo[i]; } template <typename T> inline T GF<T>::inv(T i) { if(i < MEMO_SIZE_LIMIT){ while(inverse_memo.size() <= i){ // Deformation of formula. // b[t] * t = (M - (M / t) * t) * b[M - (M / t) * t]; // Divides by i over a finite field GF(mod), b[t] = - (M / t) * b[M % t]. int size = inverse_memo.size(); inverse_memo.push_back(mod - (int64_t)(mod / size) * inverse_memo[mod % size] % mod); } return inverse_memo[i]; } return this->pow(i, mod - 2); } template <typename T> inline T GF<T>::pow(T i, int64_t p) { T res = 1; for(; p; p >>= 1){ if(p & 1) res = mul(res, i); i = mul(i, i); } return res; } int main() { const i64 LIMIT = 1000; const i64 MOD = 100003; i64 n, q; cin >> n >> q; vector<i64> a; rep(i, n) a.push_back(xor128() % MOD); i64 bests[MOD]; rep(i, MOD) bests[i] = 0; rep(i, min(LIMIT, n))rep(j, MOD) bests[j] = max(bests[j], (a[i] * j) % MOD); GF<int> gf; if(LIMIT < n){ set<i64> s; rep(i, n) s.insert(a[i]); rep(i, MOD)if(i){ for(i64 t = MOD - 1; bests[i] < t; --t){ i64 inv = gf.div(t, i); if(s.count(inv)){ bests[i] = t; break; } } } } rep(i, q){ i64 t; cin >> t; cout << bests[t] << endl; } }