├── 2019-Xuzhou ├── C.py ├── D.py ├── E.cpp ├── M.cpp ├── K.cpp ├── B.cpp ├── G.cpp ├── J.cpp ├── I.cpp ├── L.cpp ├── H.cpp └── A.cpp ├── README.md ├── 2019-Nanchang ├── G.cpp ├── E.cpp ├── H.cpp ├── B.cpp ├── C.cpp └── D.cpp ├── 2019-CCPC ├── 1001.cpp ├── 1006.cpp ├── 1007.cpp ├── 1008.cpp ├── 1005.cpp ├── 1003.cpp ├── 1004.cpp └── 1002.cpp ├── 2019-Shenyang ├── C.cpp ├── G.cpp ├── K.cpp ├── E.cpp ├── F.cpp ├── J.cpp ├── D.cpp ├── B.cpp └── H.cpp ├── 2019-Nanjing ├── H.cpp ├── B.cpp ├── D.cpp ├── F.cpp └── A.cpp └── .gitignore /2019-Xuzhou/C.py: -------------------------------------------------------------------------------- 1 | w = int(input().strip()) 2 | print("YES" if w >= 4 and w % 2 == 0 else "NO") 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Failed System Test 2 | 3 | 上海大学现役队伍“你以为你CF过了四题” 2019赛季 ICPC/CCPC 网络赛代码仓库 4 | 5 | [队伍维基](http://acmoj.shu.edu.cn/wiki/index.php/Failed_System_Test_(2019)) [校外镜像](http://modcsl.cn/wiki/index.php/Failed_System_Test_(2019)) -------------------------------------------------------------------------------- /2019-Xuzhou/D.py: -------------------------------------------------------------------------------- 1 | t = input() 2 | for i in range(int(input())): 3 | s = input() 4 | if(len(t) == len(s)): 5 | print("jntm!" if s == t else "friend!") 6 | elif (len(t) > len(s)): 7 | print("my child!" if s in t else "oh, child!") 8 | else: 9 | print("my teacher!" if t in s else "senior!") 10 | -------------------------------------------------------------------------------- /2019-Nanchang/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | int main() 11 | { 12 | int T; 13 | scanf("%d", &T); 14 | while (T--) 15 | { 16 | int n; 17 | scanf("%d", &n); 18 | puts(n == 1 ? "18000" : "0"); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /2019-CCPC/1001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef long long i64; 4 | typedef unsigned long long u64; 5 | 6 | const int MAXN = 2000; 7 | 8 | int t; 9 | i64 a, b; 10 | 11 | int main() { 12 | 13 | std::scanf("%d", &t); 14 | for(int test = 0; test < t; ++test) { 15 | 16 | std::scanf("%lld%lld", &a, &b); 17 | i64 c = a & b; 18 | if(!c) { 19 | 20 | c = 1; 21 | while(!(a & c) && !(b & c)) c <<= 1; 22 | 23 | } 24 | std::printf("%lld\n", c); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /2019-Shenyang/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | int main() 11 | { 12 | int n, m; 13 | while (~scanf("%d%d", &n, &m)) 14 | { 15 | vector dp(10001, 0x3f3f3f3f); 16 | dp[0] = 0; 17 | for (int i = 0, p, c; i < n; i++) 18 | { 19 | scanf("%d%d", &p, &c); 20 | for (int j = c; j <= 10000; j++) 21 | dp[j] = min(dp[j], dp[j - c] + p); 22 | } 23 | int ans = 0x3f3f3f3f, ww = 0; 24 | for (int i = m; i < 10001; i++) 25 | { 26 | if (ans >= dp[i]) ans = dp[i], ww = i; 27 | } 28 | printf("%d %d\n", ans, ww); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /2019-Shenyang/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | using i64 = long long; 11 | 12 | const int MAXN = 1001000; 13 | 14 | int t; 15 | char ns[MAXN]; 16 | int n; 17 | double a; 18 | 19 | int main() { 20 | 21 | std::scanf("%d", &t); 22 | for(int test = 0; test < t; ++test) { 23 | 24 | std::scanf("%s%lf", &ns, &a); 25 | int len = std::strlen(ns); 26 | if(len > 5) n = 100000; 27 | else std::sscanf(ns, "%d", &n); 28 | double result = 2; 29 | for(int i = 1; i < n * 2; ++i) 30 | result = 1 / (1 + 1 / result) + 1; 31 | std::printf("%.10f\n", result * a); 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /2019-CCPC/1006.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef long long i64; 4 | typedef unsigned long long u64; 5 | 6 | const int MAXN = 101000; 7 | 8 | using li = std::list; 9 | 10 | int n, m; 11 | int arr[MAXN]; 12 | li list; 13 | li::iterator pos[MAXN]; 14 | 15 | int main() { 16 | 17 | std::scanf("%d%d", &n, &m); 18 | for(int i = 0; i < n; ++i) 19 | std::scanf("%d", &arr[i]), --arr[i]; 20 | 21 | for(int i = 0; i < n; ++i) 22 | list.push_back(arr[i]), pos[arr[i]] = --list.end(); 23 | 24 | for(int i = 0; i < m; ++i) { 25 | 26 | int x; 27 | std::scanf("%d", &x), --x; 28 | auto it = pos[x]; 29 | list.erase(it); 30 | list.push_front(x); 31 | pos[x] = list.begin(); 32 | 33 | } 34 | 35 | for(int x : list) 36 | std::printf("%d ", x + 1); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /2019-Shenyang/K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | typedef long long ll; 11 | const int mod = 1e9 + 7; 12 | 13 | int main() 14 | { 15 | int T; 16 | scanf("%d", &T); 17 | while (T--) 18 | { 19 | int k; 20 | ll n; 21 | scanf("%d%lld", &k, &n); 22 | vector a(k * 2); 23 | for (int i = 0; i < k * 2; i++) scanf("%d", &a[i]); 24 | if (n <= k * 2) 25 | { 26 | int sum = 0; 27 | for (int i = 0; i < n; i++) (sum += a[i]) %= mod; 28 | printf("%d\n", sum); 29 | } 30 | else 31 | { 32 | int sum = 0; 33 | for (auto& t : a) (sum += t) %= mod; 34 | debug(sum); 35 | (sum += 1LL * a.back() * ((n - 2 * k) % mod) % mod) %= mod; 36 | printf("%d\n", sum); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /2019-CCPC/1007.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef long long i64; 4 | typedef unsigned long long u64; 5 | 6 | const int MAXN = 2000; 7 | 8 | int t, k; 9 | bool mat[MAXN][MAXN]; 10 | 11 | int main() { 12 | 13 | mat[0][0] = 0; 14 | mat[0][1] = 0; 15 | mat[1][0] = 0; 16 | mat[1][1] = 1; 17 | 18 | for(int i = 1; i < 10; ++i) { 19 | 20 | int s = 1 << i; 21 | int t = 2 << i; 22 | for(int x = 0; x < t; ++x) 23 | for(int y = 0; y < t; ++y) { 24 | 25 | mat[x][y] = mat[x % s][y % s] ^ ((x / s) && (y / s)); 26 | 27 | } 28 | 29 | } 30 | 31 | std::scanf("%d", &t); 32 | for(int test = 0; test < t; ++test) { 33 | 34 | std::scanf("%d", &k); 35 | int s = 1 << k; 36 | for(int x = 0; x < s; ++x) { 37 | 38 | for(int y = s - 1; y >= 0; --y) 39 | std::putchar("CP"[mat[x][y]]); 40 | std::putchar('\n'); 41 | 42 | } 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /2019-Xuzhou/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | const int MAXN = 501000; 11 | 12 | int n, m; 13 | int arr[MAXN]; 14 | int result[MAXN]; 15 | int cnt; 16 | int idx[MAXN]; 17 | 18 | int main() { 19 | 20 | std::scanf("%d%d", &n, &m); 21 | for(int i = 0; i < n; ++i) 22 | std::scanf("%d", &arr[i]); 23 | 24 | for(int i = n - 1; i >= 0; --i) { 25 | 26 | if(!cnt) idx[cnt++] = i, result[i] = -1; 27 | else { 28 | 29 | auto it = std::lower_bound(idx, idx + cnt, arr[i] + m, [](int a, int b) { 30 | return arr[a] < b; 31 | }); 32 | if(it == idx + cnt) result[i] = -1; 33 | else result[i] = *it - i - 1; 34 | if(arr[i] > arr[idx[cnt - 1]]) idx[cnt++] = i; 35 | 36 | } 37 | 38 | } 39 | for(int i = 0; i < n; ++i) 40 | std::printf("%d%c", result[i], " \n"[i == n - 1]); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /2019-Nanjing/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | typedef long long ll; 11 | 12 | int main() 13 | { 14 | int T; 15 | scanf("%d", &T); 16 | while (T--) 17 | { 18 | int n, m; 19 | scanf("%d%d", &n, &m); 20 | vector> g(n, vector(n, 1e18)); 21 | for (int i = 0; i < n; i++) g[i][i] = 0; 22 | for (int i = 0, u, v, w; i < m; i++) 23 | { 24 | scanf("%d%d%d", &u, &v, &w); 25 | g[u][v] = w; 26 | } 27 | for (int i = 0, u, v; i < 6; i++) 28 | { 29 | for (int k = 0; k < n; k++) 30 | for (int i = 0; i < n; i++) 31 | for (int j = 0; j < n; j++) 32 | g[i][j] = min(g[i][k] + g[k][j], g[i][j]); 33 | scanf("%d%d", &u, &v); 34 | printf("%lld\n", -g[v][u]); 35 | g[u][v] = -g[v][u]; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /2019-CCPC/1008.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef long long i64; 4 | typedef unsigned long long u64; 5 | 6 | const int MAXN = 101000; 7 | 8 | int t; 9 | int n, m; 10 | i64 k; 11 | i64 arr[MAXN]; 12 | 13 | int main() { 14 | 15 | std::scanf("%d", &t); 16 | for(int test = 0; test < t; ++test) { 17 | 18 | std::scanf("%d%lld", &n, &k); 19 | for(int i = 0; i < n; ++i) 20 | std::scanf("%lld", &arr[i]); 21 | 22 | i64 sum = 0, s0 = 0; 23 | for(int i = 0; i < n; ++i) 24 | sum += arr[i], s0 += arr[i] / k; 25 | 26 | if(s0 >= n - 1) std::printf("%lld\n", sum + k); 27 | else { 28 | 29 | m = n - 1; 30 | for(int i = 0; i < n; ++i) 31 | m -= arr[i] / k, arr[i] %= k; 32 | 33 | std::sort(arr, arr + n, std::greater()); 34 | i64 result = k * n; 35 | for(int i = m; i < n; ++i) 36 | result += arr[i]; 37 | 38 | std::printf("%lld\n", result); 39 | 40 | } 41 | 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /2019-Xuzhou/M.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | const int MAXN = 1001000; 11 | 12 | int n, m; 13 | char s[MAXN], t[MAXN]; 14 | int nx[MAXN][26]; 15 | 16 | int main() { 17 | 18 | scanf("%d%d%s%s", &n, &m, s, t); 19 | 20 | for(int i = 0; i < 26; ++i) 21 | nx[n][i] = n; 22 | for(int i = n - 1; i >= 0; --i) { 23 | 24 | for(int j = 0; j < 26; ++j) 25 | nx[i][j] = nx[i + 1][j]; 26 | nx[i][s[i] - 'a'] = i; 27 | 28 | } 29 | 30 | int result = -1; 31 | int cur = 0; 32 | for(int i = 0; i < m; ++i) { 33 | 34 | for(char j = t[i] + 1; j <= 'z'; ++j) { 35 | 36 | int p = nx[cur][j - 'a']; 37 | if(p < n) result = std::max(result, i + (n - p)); 38 | 39 | } 40 | cur = nx[cur][t[i] - 'a'] + 1; 41 | if(cur >= n) break; 42 | 43 | } 44 | if(cur < n) result = std::max(result, m + (n - cur)); 45 | 46 | std::printf("%d\n", result); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /2019-Nanjing/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using i64 = long long; 4 | 5 | i64 mm(i64 x, i64 m) { 6 | 7 | return x < m ? x : x % m + m; 8 | 9 | } 10 | 11 | i64 pow64(i64 a, i64 b, i64 m) { 12 | 13 | i64 result = 1; 14 | while(b) { 15 | 16 | if(b & 1) result = mm(result * a, m); 17 | a = mm(a * a, m); 18 | b >>= 1; 19 | 20 | } 21 | return result; 22 | 23 | } 24 | 25 | const int N = 1001000; 26 | int phi[N] = {0, 1}; 27 | void caleuler() 28 | { 29 | for (int i = 2; i < N; i++) 30 | if (!phi[i]) 31 | for (int j = i; j < N; j += i) 32 | { 33 | if (!phi[j]) phi[j] = j; 34 | phi[j] = phi[j] / i * (i - 1); 35 | } 36 | } 37 | 38 | int t; 39 | int a, b, m; 40 | 41 | int calc(int a, int b, int m) { 42 | 43 | if(m == 1) return 1; 44 | if(!b) return 1; 45 | int p = phi[m]; 46 | int x = calc(a, b - 1, p); 47 | return pow64(a, x, m); 48 | 49 | } 50 | 51 | int main() { 52 | 53 | caleuler(); 54 | 55 | std::scanf("%d", &t); 56 | for(int i = 0; i < t; ++i) { 57 | 58 | std::scanf("%d%d%d", &a, &b, &m); 59 | std::printf("%d\n", calc(a, b, m) % m); 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /2019-Xuzhou/K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #ifndef ONLINE_JUDGE 3 | #include 4 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 5 | #else 6 | #define debug(...) 7 | #endif 8 | 9 | using pp = std::pair; 10 | using i64 = long long; 11 | 12 | const int MAXN = 2000; 13 | 14 | int n; 15 | pp arr[MAXN]; 16 | std::set points; 17 | std::map centers; 18 | 19 | int main() { 20 | 21 | std::scanf("%d", &n); 22 | for(int i = 0; i < n; ++i) { 23 | 24 | std::scanf("%d%d", &arr[i].first, &arr[i].second); 25 | arr[i].first *= 2; 26 | arr[i].second *= 2; 27 | points.insert(arr[i]); 28 | 29 | } 30 | if(n <= 2) std::puts("0"); 31 | else { 32 | 33 | for(int i = 0; i < n - 1; ++i) 34 | for(int j = i + 1; j < n; ++j) { 35 | 36 | pp center = {(arr[i].first + arr[j].first) / 2, (arr[i].second + arr[j].second) / 2}; 37 | ++centers[center]; 38 | 39 | } 40 | 41 | int result = n; 42 | for(auto&& p : centers) { 43 | 44 | int value = n - p.second * 2 - points.count(p.first); 45 | result = std::min(result, value); 46 | 47 | } 48 | std::printf("%d\n", result); 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /2019-Shenyang/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | const int MAXN = 1001000; 11 | const int MAXP = 1100; 12 | 13 | int n, p; 14 | 15 | int s2[MAXP][MAXP]; 16 | bool visited[MAXN]; 17 | int arr[MAXN]; 18 | 19 | void init() { 20 | 21 | for(int i = 0; i <= n; ++i) 22 | visited[i] = false; 23 | 24 | s2[0][0] = 1; 25 | for(int i = 1; i <= p; ++i) { 26 | 27 | s2[i][0] = 0; 28 | for(int j = 1; j <= i; ++j) 29 | s2[i][j] = (s2[i - 1][j - 1] + s2[i - 1][j] * j) % p; 30 | 31 | } 32 | for(int i = 0; i <= p; ++i) { 33 | 34 | visited[i] = true; 35 | int sum = 0; 36 | for(int j = 0; j <= i; ++j) 37 | sum += s2[i][j]; 38 | arr[i] = sum % p; 39 | 40 | } 41 | 42 | } 43 | 44 | int calc(int x) { 45 | 46 | if(visited[x]) return arr[x]; 47 | visited[x] = true; 48 | return arr[x] = (calc(x - p) + calc(x - p + 1)) % p; 49 | 50 | } 51 | 52 | int main() { 53 | int T; 54 | scanf("%d", &T); 55 | while(T--) { 56 | 57 | std::scanf("%d%d", &n, &p); 58 | init(); 59 | std::printf("%d\n", calc(n)); 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /2019-Shenyang/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int maxn = 5e5+5; 5 | int a[maxn]; 6 | using ll = long long ; 7 | 8 | int n,k; 9 | int solve(vector &a, int less){ 10 | if(a.empty()) return 0; 11 | sort(a.begin(),a.end(),greater()); 12 | ll tot = k; 13 | int num = 1; 14 | a.push_back(0); 15 | for(int i=0;i b,c; 32 | for(int i=1;i<=n;i++){ 33 | if(a[i] > avr) b.push_back(a[i] - avr); 34 | else if(a[i] < avr) c.push_back(avr - a[i]); 35 | } 36 | int t1 = solve(c,0); 37 | if(t1 == 0){ 38 | if(sum % n == 0) puts("0"); 39 | else puts("1"); 40 | } else{ 41 | int t2 = solve(b, 1); 42 | // printf("%d %d\n",t1,t2); 43 | printf("%d\n",t1 + t2); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /2019-Nanchang/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | const int MAXQ = 200; 11 | 12 | int t, n, m, q; 13 | int qq[MAXQ]; 14 | int result[MAXQ]; 15 | 16 | int main() 17 | { 18 | scanf("%d", &t); 19 | while (t--) 20 | { 21 | scanf("%d%d%d", &n, &m, &q); 22 | std::unordered_map mm; 23 | for (int i = 0; i < q; ++i) 24 | std::scanf("%d", &qq[i]), --qq[i], mm[qq[i]] = i; 25 | std::deque dq; 26 | for(int i = 0; i < n; ++i) { 27 | 28 | auto it = mm.find(i); 29 | if(it == mm.end()) dq.push_back(-1); 30 | else dq.push_back(it->second); 31 | 32 | } 33 | int count = q; 34 | for(int i = 0; i < n; ++i) { 35 | 36 | int x = dq.front(); 37 | dq.pop_front(); 38 | if(x >= 0) { 39 | 40 | result[x] = i; 41 | --count; 42 | if(!count) break; 43 | 44 | } 45 | for(int j = 0; j < m; ++j) { 46 | 47 | int y = dq.front(); 48 | dq.pop_front(); 49 | dq.push_back(y); 50 | 51 | } 52 | 53 | } 54 | for(int i = 0; i < q; ++i) 55 | std::printf("%d\n", result[i] + 1); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /2019-Shenyang/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | const int mod = 1e9 + 7; 11 | 12 | typedef long long ll; 13 | const int maxn = 1 << 20; 14 | int a[maxn]; 15 | int f[maxn], invf[maxn]; 16 | 17 | ll pow(ll x, ll n, ll mod) 18 | { 19 | assert(n >= 0); 20 | ll ret = mod != 1; 21 | for (x %= mod; n; n >>= 1, x = x * x % mod) 22 | if (n & 1) ret = ret * x % mod; 23 | return ret; 24 | } 25 | ll inv(ll a, ll p) { return pow(a, p - 2, p); } 26 | void init() 27 | { 28 | f[0] = 1; 29 | for (int i = 1; i < maxn; i++) f[i] = ((ll)f[i - 1] * i) % mod; 30 | invf[maxn - 1] = inv(f[maxn - 1], mod); 31 | for (int i = maxn - 2; ~i; i--) invf[i] = (ll)invf[i + 1] * (i + 1) % mod; 32 | } 33 | inline int p(int n, int m) { return n < m || m < 0 ? 0 : (ll)f[n] * invf[n - m] % mod; } 34 | 35 | int main() 36 | { 37 | init(); 38 | int T; 39 | scanf("%d", &T); 40 | while (T--) 41 | { 42 | int n, x; 43 | scanf("%d%d", &n, &x); 44 | if (x >= n) 45 | { 46 | puts("1"); 47 | continue; 48 | } 49 | a[0] = 1; 50 | for (int i = 1; i < x; i++) a[i] = f[i]; 51 | for (int i = x - 1; i < n; i++) 52 | { 53 | a[i + 1] = (a[i] - p(i - 1, x - 1) * (ll)a[i - x] % mod + mod) % mod * i % mod + a[i]; 54 | a[i + 1] %= mod; 55 | } 56 | printf("%lld\n", (ll)a[n] * invf[n] % mod); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /2019-Xuzhou/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector> query; 5 | vector a; 6 | 7 | const int N = 1 << 21; 8 | int fa[N]; 9 | 10 | int find(int x) 11 | { 12 | return fa[x] == x ? x : fa[x] = find(fa[x]); 13 | } 14 | 15 | void unite(int x, int y) 16 | { // x->y; 17 | x = find(x), y = find(y); 18 | if (x == y) return; 19 | fa[x] = y; 20 | } 21 | 22 | int getid(int x) 23 | { 24 | return lower_bound(a.begin(), a.end(), x) - a.begin(); 25 | } 26 | 27 | int main() 28 | { 29 | int n, q; 30 | scanf("%d%d", &n, &q); 31 | query.reserve(q); 32 | for (int i = 0, z, x; i < q; i++) 33 | { 34 | scanf("%d%d", &z, &x); 35 | query.emplace_back(z, x); 36 | if (z == 1) 37 | { 38 | a.push_back(x); 39 | a.push_back(x + 1); 40 | } 41 | } 42 | a.push_back(n + 1); 43 | sort(a.begin(), a.end()); 44 | a.erase(unique(a.begin(), a.end()), a.end()); 45 | for (int i = 0; i < a.size(); i++) fa[i] = i; 46 | for (auto& qq : query) 47 | { 48 | if (qq.first == 2) 49 | { 50 | int id = getid(qq.second); 51 | if(a[id] != qq.second) { 52 | printf("%d\n", qq.second); 53 | continue; 54 | } 55 | int ans = find(id); 56 | if (a[ans] == n + 1) { 57 | puts("-1"); 58 | } else { 59 | printf("%d\n", a[ans]); 60 | } 61 | } 62 | else 63 | { 64 | unite(getid(qq.second), getid(qq.second + 1)); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /2019-Nanjing/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | using i64 = long long; 11 | 12 | const int MAXN = 101000; 13 | 14 | int t; 15 | int n, m; 16 | std::vector g[MAXN]; 17 | 18 | double dp[MAXN][2]; 19 | bool vis[MAXN]; 20 | 21 | double dfs1(int u) 22 | { 23 | if (vis[u]) return dp[u][0]; 24 | vis[u] = true; 25 | double result = 0; 26 | int x = g[u].size(); 27 | if (x == 0) return dp[u][0] = 0; 28 | for (auto& v : g[u]) result += dfs1(v); 29 | return dp[u][0] = (result / (x + 1) + 1) * (x + 1) / x; 30 | } 31 | 32 | double dfs2(int u) 33 | { 34 | if (vis[u]) return dp[u][1]; 35 | vis[u] = true; 36 | double result = 0; 37 | int x = g[u].size(); 38 | if (x == 0) return dp[u][1] = 0; 39 | double result2 = 0; 40 | for (auto& v : g[u]) result += dfs2(v), result2 += dp[v][0]; 41 | result2 += dp[u][0]; 42 | result2 /= (x + 1); 43 | result = result / (x + 1) + result2 + 1; 44 | return dp[u][1] = result * (x + 1) / x; 45 | } 46 | 47 | int main() 48 | { 49 | scanf("%d", &t); 50 | for (int test = 0; test < t; ++test) 51 | { 52 | scanf("%d%d", &n, &m); 53 | for (int i = 0; i < n; ++i) g[i].clear(); 54 | for (int i = 0, u, v; i < m; ++i) 55 | { 56 | scanf("%d%d", &u, &v); 57 | --u, --v; 58 | g[u].push_back(v); 59 | } 60 | std::fill(vis, vis + n, false); 61 | dfs1(0); 62 | std::fill(vis, vis + n, false); 63 | dfs2(0); 64 | for (int i = 0; i < n; i++) debug(dp[i][0], dp[i][1]); 65 | printf("%.2f\n", dp[0][1]); 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /2019-Nanchang/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | using i64 = long long; 11 | 12 | const int mod = 119 << 23 | 1; 13 | 14 | using Arr = int[129]; 15 | 16 | inline int Pow(const Arr* a, long long n) 17 | { 18 | n %= (mod - 1); 19 | int t = 1; 20 | for(int i = 0; n; ++i, n >>= 7) { 21 | 22 | int x = n & 127; 23 | if(x) t = i64(t) * a[i][x] % mod; 24 | 25 | } 26 | return t; 27 | } 28 | 29 | const int sq = 473844410; 30 | const int inv2 = mod + 1 >> 1; 31 | const int rt1 = 1LL * (sq + 3) * inv2 % mod; 32 | const int rt2 = 1LL * (mod - sq + 3) * inv2 % mod; 33 | 34 | int tt1[11][129], tt2[11][129]; 35 | 36 | const int A = 169935932; 37 | const int B = 329186245; 38 | // 0 1 3 39 | int main() 40 | { 41 | debug(1LL * sq * sq % mod); 42 | debug(rt1, rt2); 43 | tt1[0][0] = 1, tt2[0][0] = 1; 44 | for(int i = 1; i <= 128; ++i) 45 | tt1[0][i] = i64(tt1[0][i - 1]) * rt1 % mod, tt2[0][i] = i64(tt2[0][i - 1]) * rt2 % mod; 46 | for(int i = 1; i < 10; ++i) { 47 | 48 | tt1[i][0] = 1, tt2[i][0] = 1; 49 | for(int j = 1; j <= 128; ++j) { 50 | 51 | tt1[i][j] = i64(tt1[i][j - 1]) * tt1[i - 1][128] % mod; 52 | tt2[i][j] = i64(tt2[i][j - 1]) * tt2[i - 1][128] % mod; 53 | 54 | } 55 | 56 | } 57 | int q; 58 | long long n; 59 | scanf("%d%lld", &q, &n); 60 | int ans = 0; 61 | int tmp; 62 | for (int i = 0; i < q; i++) 63 | { 64 | tmp = (1LL * A * Pow(tt1, n + 1) + 1LL * B * Pow(tt2, n + 1)) % mod; 65 | // debug(tmp); 66 | ans ^= tmp; 67 | n ^= (1LL * tmp * tmp); 68 | } 69 | printf("%d\n", ans); 70 | } 71 | -------------------------------------------------------------------------------- /2019-Xuzhou/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | const int maxn = 3e5 + 5; 11 | struct Palindromic_Tree 12 | { 13 | int ch[maxn][26], f[maxn], len[maxn], s[maxn]; 14 | int cnt[maxn]; // 结点表示的本质不同的回文串的个数(调用count()后) 15 | int num[maxn]; // 结点表示的最长回文串的最右端点为回文串结尾的回文串个数 16 | int mask[maxn]; 17 | int last, sz, n; 18 | int newnode(int x) 19 | { 20 | memset(ch[sz], 0, sizeof(ch[sz])); 21 | cnt[sz] = num[sz] = 0, len[sz] = x; 22 | mask[sz] = 0; 23 | return sz++; 24 | } 25 | void init() 26 | { 27 | sz = 0; 28 | newnode(0), newnode(-1); 29 | last = n = 0, s[0] = -1, f[0] = 1; 30 | } 31 | int get_fail(int u) 32 | { 33 | while (s[n - len[u] - 1] != s[n]) u = f[u]; 34 | return u; 35 | } 36 | void add(int c) 37 | { // c-='a' 38 | s[++n] = c; 39 | int u = get_fail(last); 40 | if (!ch[u][c]) 41 | { 42 | int np = newnode(len[u] + 2); 43 | f[np] = ch[get_fail(f[u])][c]; 44 | num[np] = num[f[np]] + 1; 45 | mask[np] = (mask[u] | (1 << c)); 46 | ch[u][c] = np; 47 | } 48 | last = ch[u][c]; 49 | cnt[last]++; 50 | } 51 | void count() 52 | { 53 | for (int i = sz - 1; i > 1; i--) cnt[f[i]] += cnt[i]; 54 | } 55 | long long solve() 56 | { 57 | long long ans = 0; 58 | for (int i = 2; i < sz; i++) 59 | { 60 | debug(mask[i], cnt[i]); 61 | ans += __builtin_popcount(mask[i]) * cnt[i]; 62 | } 63 | return ans; 64 | } 65 | 66 | } pam; 67 | 68 | char s[maxn]; 69 | 70 | int main() 71 | { 72 | scanf("%s", s); 73 | pam.init(); 74 | for (int i = 0; s[i]; i++) pam.add(s[i] - 'a'); 75 | pam.count(); 76 | printf("%lld\n", pam.solve()); 77 | } 78 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ 2 | # ========================= 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | 22 | # Compiled Static libraries 23 | *.lai 24 | *.la 25 | *.a 26 | *.lib 27 | 28 | # Executables 29 | *.exe 30 | *.out 31 | *.app 32 | 33 | # Input files 34 | *.in 35 | 36 | # Java 37 | # ========================= 38 | 39 | # Compiled class file 40 | *.class 41 | 42 | # Log file 43 | *.log 44 | 45 | # BlueJ files 46 | *.ctxt 47 | 48 | # Mobile Tools for Java (J2ME) 49 | .mtj.tmp/ 50 | 51 | # Package Files # 52 | *.jar 53 | *.war 54 | *.ear 55 | *.zip 56 | *.tar.gz 57 | *.rar 58 | 59 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 60 | hs_err_pid* 61 | 62 | # ========================= 63 | # Operating System Files 64 | # ========================= 65 | 66 | # OSX 67 | # ========================= 68 | 69 | .DS_Store 70 | .AppleDouble 71 | .LSOverride 72 | 73 | # Thumbnails 74 | ._* 75 | 76 | # Files that might appear in the root of a volume 77 | .DocumentRevisions-V100 78 | .fseventsd 79 | .Spotlight-V100 80 | .TemporaryItems 81 | .Trashes 82 | .VolumeIcon.icns 83 | 84 | # Directories potentially created on remote AFP share 85 | .AppleDB 86 | .AppleDesktop 87 | Network Trash Folder 88 | Temporary Items 89 | .apdisk 90 | 91 | # Windows 92 | # ========================= 93 | 94 | # Windows image file caches 95 | Thumbs.db 96 | ehthumbs.db 97 | 98 | # Folder config file 99 | Desktop.ini 100 | 101 | # Recycle Bin used on file shares 102 | $RECYCLE.BIN/ 103 | 104 | # Windows Installer files 105 | *.cab 106 | *.msi 107 | *.msm 108 | *.msp 109 | 110 | # Windows shortcuts 111 | *.lnk 112 | 113 | # ========================= 114 | # VSCode Setting Files 115 | # ========================= 116 | .vscode/* 117 | 118 | # ========================= 119 | # Sample Inout/Output 120 | # ========================= 121 | *.in 122 | *.out 123 | *.txt -------------------------------------------------------------------------------- /2019-Shenyang/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | const int N = 10005; 11 | const int mod = 1e9 + 7; 12 | vector> g[N]; 13 | 14 | int ans[3]; 15 | int dp[N][3]; 16 | void dfs(int u, int fa) 17 | { 18 | dp[u][0] = 1; 19 | dp[u][1] = 0; 20 | dp[u][2] = 0; 21 | for (auto& e : g[u]) 22 | { 23 | int v = e.first; 24 | int w = e.second; 25 | if (v == fa) continue; 26 | dfs(v, u); 27 | for (int i = 0; i < 3; i++) 28 | (dp[u][(i + w) % 3] += dp[v][i]) %= mod; 29 | } 30 | } 31 | 32 | void DFS(int u, int fa, vector last, int val) 33 | { 34 | // printf("u = %d: ", u); 35 | // for(int i=0;i<3;i++) 36 | // printf("%d ",last[i]); 37 | // puts(""); 38 | 39 | for (auto& e : g[u]) 40 | { 41 | int v = e.first; 42 | int w = e.second; 43 | if (v == fa) continue; 44 | vector tmp(3), a(3); 45 | for (int i = 0; i < 3; i++) 46 | tmp[(i + w) % 3] = dp[v][i]; 47 | for (int i = 0; i < 3; i++) 48 | a[(i + w) % 3] = ((last[i] + dp[u][i]) % mod - tmp[i] + mod) % mod; 49 | DFS(v, u, a, w); 50 | } 51 | for (int i = 0; i < 3; i++) 52 | for (int j = 0; j < 3; j++) 53 | ans[(i + j) % 3] = (ans[(i + j) % 3] + 1LL * last[i] * dp[u][j] % mod * val % mod) % mod; 54 | } 55 | int main() 56 | { 57 | int n; 58 | while (~scanf("%d", &n)) 59 | { 60 | for (int i = 0; i < n; i++) g[i].clear(); 61 | for (int i = 1, a, b, c; i < n; i++) 62 | { 63 | scanf("%d%d%d", &a, &b, &c); 64 | g[a].emplace_back(b, c); 65 | g[b].emplace_back(a, c); 66 | } 67 | dfs(0, -1); 68 | for (int i = 0; i < 3; i++) ans[i] = 0; 69 | vector tmp(3); 70 | DFS(0, -1, tmp, 0); 71 | for (int i = 0; i < 3; i++) 72 | printf("%d%c", ans[i] * 2 % mod, i == 2 ? '\n' : ' '); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /2019-Xuzhou/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | using i64 = long long; 11 | 12 | const int MOD = 1000000007; 13 | 14 | i64 pow64(i64 a, i64 b) { 15 | 16 | a %= MOD; 17 | i64 result = 1; 18 | while(b) { 19 | 20 | if(b & 1) result = result * a % MOD; 21 | a = a * a % MOD; 22 | b >>= 1; 23 | 24 | } 25 | return result; 26 | 27 | } 28 | i64 inv64(i64 x) { return pow64(x, MOD - 2); } 29 | 30 | const int MAXN = 1001000; 31 | 32 | int n; 33 | std::vector edges[MAXN]; 34 | bool visited[MAXN]; 35 | int cnt[MAXN], depth[MAXN]; 36 | int md; 37 | int result[MAXN]; 38 | 39 | void dfs1(int u) { 40 | 41 | visited[u] = true; 42 | cnt[u] = 0; 43 | for(int v : edges[u]) { 44 | 45 | if(visited[v]) continue; 46 | ++cnt[u]; 47 | depth[v] = depth[u] + 1; 48 | dfs1(v); 49 | 50 | } 51 | 52 | } 53 | 54 | void dfs2(int u) { 55 | 56 | visited[u] = true; 57 | if(!cnt[u]) { 58 | 59 | result[u] = depth[u] == md; 60 | return; 61 | 62 | } 63 | int sum = 0; 64 | for(int v : edges[u]) { 65 | 66 | if(visited[v]) continue; 67 | dfs2(v); 68 | sum = (sum + result[v]) % MOD; 69 | 70 | } 71 | sum = sum * inv64(cnt[u]) % MOD; 72 | sum = (1 - sum + MOD) % MOD; 73 | sum = pow64(sum, cnt[u]); 74 | sum = (1 - sum + MOD) % MOD; 75 | result[u] = sum; 76 | 77 | } 78 | 79 | int main() { 80 | 81 | std::scanf("%d", &n); 82 | for(int i = 0; i < n - 1; ++i) { 83 | 84 | int u, v; 85 | std::scanf("%d%d", &u, &v); 86 | --u, --v; 87 | edges[u].push_back(v); 88 | edges[v].push_back(u); 89 | 90 | } 91 | 92 | for(int i = 0; i < n; ++i) 93 | visited[i] = false; 94 | depth[0] = 0; 95 | dfs1(0); 96 | md = *std::max_element(depth, depth + n); 97 | for(int i = 0; i < n; ++i) 98 | visited[i] = false; 99 | dfs2(0); 100 | 101 | std::printf("%d\n", result[0]); 102 | 103 | } 104 | -------------------------------------------------------------------------------- /2019-CCPC/1005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef long long i64; 4 | typedef unsigned long long u64; 5 | 6 | const int MOD = 1000000007; 7 | const int MAXN = 5000000; 8 | 9 | 10 | bool visited[MAXN]; 11 | int pc; 12 | int primes[MAXN / 4]; 13 | int phi[MAXN]; 14 | 15 | i64 pow64(i64 a, i64 b) { 16 | 17 | if(!b) return 1; 18 | a %= MOD; 19 | i64 result = 1; 20 | while(b) { 21 | 22 | if(b & 1) result = result * a % MOD; 23 | a = a * a % MOD; 24 | b >>= 1; 25 | 26 | } 27 | return result; 28 | 29 | } 30 | i64 inv64(i64 x) { return pow64(x, MOD - 2); } 31 | 32 | void generate() { 33 | 34 | pc = 0; 35 | phi[0] = 0; 36 | phi[1] = 1; 37 | for(int i = 2; i < MAXN; ++i) { 38 | 39 | if(!visited[i]) primes[pc++] = i, phi[i] = i - 1; 40 | for(int j = 0; j < MAXN && i * primes[j] < MAXN; ++j) { 41 | 42 | int k = i * primes[j]; 43 | visited[k] = true; 44 | if(!(i % primes[j])) { 45 | 46 | phi[k] = phi[i] * primes[j]; 47 | break; 48 | 49 | } else phi[k] = phi[i] * (primes[j] - 1); 50 | 51 | } 52 | 53 | } 54 | 55 | for(int i = 1; i < MAXN; ++i) 56 | phi[i] = (phi[i - 1] + i64(phi[i]) * i) % MOD; 57 | 58 | } 59 | 60 | std::unordered_map spi; 61 | int sumPhi(int x) { 62 | 63 | if(x < MAXN) return phi[x]; 64 | auto it = spi.find(x); 65 | if(it != spi.end()) return it->second; 66 | int result = (__int128(x) * (x + 1) * (x * 2 + 1) / 6) % MOD; 67 | for(int l = 2, r; l <= x; l = r + 1) { 68 | 69 | r = x / (x / l); 70 | result = ((result - (i64(l + r) * (r - l + 1) / 2) % MOD * sumPhi(x / l) % MOD) % MOD + MOD) % MOD; 71 | 72 | } 73 | return spi[x] = result; 74 | 75 | } 76 | 77 | const int INV2 = inv64(2); 78 | 79 | int t; 80 | int n, a, b; 81 | 82 | int main() { 83 | 84 | generate(); 85 | 86 | std::scanf("%d", &t); 87 | for(int test = 0; test < t; ++test) { 88 | 89 | std::scanf("%d%d%d", &n, &a, &b); 90 | std::printf("%d\n", int(i64(sumPhi(n) + MOD - 1) % MOD * INV2 % MOD)); 91 | 92 | } 93 | 94 | } -------------------------------------------------------------------------------- /2019-Nanjing/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | #define lson (rt << 1) 10 | #define rson (rt << 1 | 1) 11 | #define Lson l, m , lson 12 | #define Rson m+1,r, rson 13 | const int maxn = 1e5+5; 14 | using ll = long long ; 15 | int a[maxn],pos[maxn]; 16 | 17 | struct Node{ 18 | ll dp; 19 | int val; 20 | } seg[maxn <<2]; 21 | 22 | void build(int l,int r,int rt){ 23 | seg[rt].dp = -1e18; 24 | if(l == r) { 25 | seg[rt].val = a[l]; 26 | return ; 27 | } 28 | int m = l + r >> 1; 29 | build(Lson); 30 | build(Rson); 31 | } 32 | 33 | void update(int l,int r,int rt,int p, ll x){ 34 | if(l == r) { 35 | seg[rt].dp = x; 36 | return ; 37 | } 38 | int m = l + r >> 1; 39 | if(p <= m)update(Lson,p,x); 40 | else update(Rson, p,x); 41 | auto &o = seg[rt], &ls = seg[lson], &rs = seg[rson]; 42 | if(rs.dp >= 0 && ls.dp >= 0){ 43 | if(rs.val > ls.val) o = rs; 44 | else o = ls; 45 | } else if(rs.dp >= 0){ 46 | o = rs; 47 | } else o = ls; 48 | } 49 | Node query(int l,int r,int rt,int L,int R){ 50 | if(L <= l && R >= r) return seg[rt]; 51 | int m = l + r >> 1; 52 | Node ret; 53 | ret.dp = -1e18; 54 | ret.val = -1; 55 | if(L <= m) 56 | ret = query(Lson,L,R); 57 | if(m+1 <= R) { 58 | auto it = query(Rson,L,R); 59 | if(it.dp >= 0 && ret.dp >= 0){ 60 | if(it.val > ret.val) ret = it; 61 | } else if(it.dp >= 0) ret = it; 62 | } 63 | return ret; 64 | } 65 | int main(){ 66 | int T; 67 | scanf("%d",&T); 68 | while(T--){ 69 | int n,k; 70 | scanf("%d%d",&n,&k); 71 | for(int i=1;i<=n;i++) scanf("%d",a+i), pos[a[i]] = i; 72 | build(1,n,1); 73 | for(int i=1;i<=n;i++){ 74 | int l = max(1, pos[i] - k); 75 | int r = min(n, pos[i] + k); 76 | auto res = query(1,n,1,l,r); 77 | if(res.dp < 0) res.dp = 0; 78 | update(1,n,1,pos[i],res.dp+1); 79 | } 80 | for(int i=1;i<=n;i++) printf("%lld%c",query(1,n,1,pos[i],pos[i]).dp, i == n ? '\n': ' '); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /2019-Shenyang/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | using i64 = long long; 11 | 12 | const int MAXN = 101000; 13 | 14 | int t, n, m, k; 15 | std::vector edges[MAXN]; 16 | bool monster[MAXN]; 17 | 18 | int dsu[MAXN], sz[MAXN]; 19 | void init() { 20 | 21 | for(int i = 0; i < n; ++i) 22 | dsu[i] = i, sz[i] = 1; 23 | 24 | } 25 | int find(int u) { return dsu[u] == u ? u : dsu[u] = find(dsu[u]); } 26 | void join(int u, int v) { u = find(u), v = find(v); if(u == v) return; dsu[v] = u, sz[u] += sz[v]; } 27 | 28 | int main() { 29 | 30 | std::scanf("%d", &t); 31 | for(int test = 0; test < t; ++test) { 32 | 33 | std::scanf("%d%d%d", &n, &m, &k); 34 | for(int i = 0; i < n; ++i) 35 | edges[i].clear(), monster[i] = false; 36 | for(int i = 0; i < m; ++i) { 37 | 38 | int u, v; 39 | std::scanf("%d%d", &u, &v); 40 | --u, --v; 41 | edges[u].push_back(v); 42 | edges[v].push_back(u); 43 | 44 | } 45 | for(int i = 0; i < k; ++i) { 46 | 47 | int u; 48 | std::scanf("%d", &u); 49 | --u; 50 | monster[u] = true; 51 | 52 | } 53 | init(); 54 | for(int u = 0; u < n; ++u) { 55 | 56 | if(monster[u]) continue; 57 | for(int v : edges[u]) { 58 | 59 | if(monster[v]) continue; 60 | join(u, v); 61 | 62 | } 63 | 64 | } 65 | int d0 = find(0), s0 = sz[d0]; 66 | double result = 0; 67 | for(int u = 0; u < n; ++u) { 68 | 69 | if(!monster[u]) continue; 70 | bool ok = false; 71 | for(int v : edges[u]) 72 | if(find(v) == d0) { ok = true; break; } 73 | if(!ok) continue; 74 | i64 sum = 0; 75 | for(int v : edges[u]) { 76 | 77 | if(monster[v]) continue; 78 | int d = find(v); 79 | if(d != d0) sum += sz[d]; 80 | 81 | } 82 | result = std::max(result, double(sum) / edges[u].size()); 83 | 84 | } 85 | std::printf("%.6f\n", result + sz[d0]); 86 | 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /2019-Nanchang/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | const int N = 1 << 10; 11 | int f[N]; 12 | 13 | struct Edge 14 | { 15 | int from, to, cost; 16 | Edge(int u, int v, int w) : from(u), to(v), cost(w) {} 17 | }; 18 | 19 | vector edges; 20 | vector G[N]; 21 | void addedge(int u, int v, int w) 22 | { 23 | edges.emplace_back(u, v, w); 24 | G[u].push_back(edges.size() - 1); 25 | } 26 | 27 | long long d[N][N]; 28 | bool vis[N]; 29 | 30 | void init(int n) 31 | { 32 | for (int i = 1; i <= n; i++) G[i].clear(); 33 | edges.clear(); 34 | } 35 | 36 | struct HeapNode 37 | { 38 | long long d; 39 | int u; 40 | HeapNode(long long d, int u) : d(d), u(u) {} 41 | bool operator<(const HeapNode& rhs) const 42 | { 43 | return d > rhs.d; 44 | } 45 | }; 46 | 47 | void dijkstra(int s, int n, long long d[]) 48 | { 49 | for (int i = 1; i <= n; i++) d[i] = 1e18, vis[i] = 0; 50 | priority_queue q; 51 | d[s] = 0; 52 | q.emplace(d[s], s); 53 | while (!q.empty()) 54 | { 55 | auto x = q.top(); 56 | int u = x.u; 57 | q.pop(); 58 | if (vis[u]) continue; 59 | vis[u] = 1; 60 | for (auto& id : G[u]) 61 | { 62 | auto& e = edges[id]; 63 | if (d[e.to] > d[e.from] + e.cost) 64 | { 65 | d[e.to] = d[e.from] + e.cost; 66 | q.emplace(d[e.to], e.to); 67 | } 68 | } 69 | } 70 | } 71 | 72 | int main() 73 | { 74 | int T; 75 | scanf("%d", &T); 76 | while (T--) 77 | { 78 | int v, e, s, k, c; 79 | scanf("%d%d%d%d%d", &v, &e, &s, &k, &c); 80 | for (int i = 0; i < k; i++) scanf("%d", &f[i]); 81 | init(v); 82 | for (int i = 0, u, v, w; i < e; i++) 83 | { 84 | scanf("%d%d%d", &u, &v, &w); 85 | addedge(u, v, w); 86 | addedge(v, u, w); 87 | } 88 | for (int i = 1; i <= v; i++) dijkstra(i, v, d[i]); 89 | long long hero = *max_element(d[s] + 1, d[s] + v + 1); 90 | long long fire = 0; 91 | for (int i = 1; i <= v; i++) 92 | { 93 | long long cur = 1e18; 94 | for (int j = 0; j < k; j++) 95 | cur = min(cur, d[i][f[j]]); 96 | fire = max(fire, cur); 97 | } 98 | debug(fire, hero); 99 | if (hero <= fire * c) 100 | printf("%lld\n", hero); 101 | else 102 | printf("%lld\n", fire); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /2019-Xuzhou/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | #define lson (rt << 1) 10 | #define rson (rt << 1 | 1) 11 | #define Lson l, m, lson 12 | #define Rson m + 1, r, rson 13 | const int maxn = 1e5 + 5; 14 | int a[maxn], ans[maxn]; 15 | 16 | struct Q 17 | { 18 | int l, r, id; 19 | bool operator<(const Q& rhs) const 20 | { 21 | return r > rhs.r; 22 | } 23 | } q[maxn]; 24 | 25 | int sum[maxn << 2]; 26 | 27 | void update(int l, int r, int rt, int L, int R) 28 | { 29 | if (L <= l && R >= r) 30 | { 31 | sum[rt] += 1; 32 | return; 33 | } 34 | int m = l + r >> 1; 35 | if (L <= m) update(Lson, L, R); 36 | if (m + 1 <= R) update(Rson, L, R); 37 | } 38 | int query(int l, int r, int rt, int p) 39 | { 40 | if (l == r) 41 | return sum[rt]; 42 | int m = l + r >> 1; 43 | int ret = sum[rt]; 44 | if (p <= m) 45 | ret += query(Lson, p); 46 | else 47 | ret += query(Rson, p); 48 | return ret; 49 | } 50 | 51 | bool alive[maxn]; 52 | int pos[maxn]; 53 | vector G[maxn]; 54 | int main() 55 | { 56 | int n, m; 57 | scanf("%d%d", &n, &m); 58 | for (int i = 1; i <= n; i++) 59 | { 60 | scanf("%d", a + i); 61 | alive[i] = true; 62 | pos[a[i]] = i; 63 | assert(a[i] <= n); 64 | } 65 | for(int i=1;i<=n;i++){ 66 | for(int j=2 * a[i]; j <= n; j += a[i]){ 67 | G[j].push_back(i); 68 | G[a[i]].push_back(pos[j]); 69 | } 70 | } 71 | // for(int i=1;i<=n;i++){ 72 | // cerr << a[i] <<": "; 73 | // for(auto &v:G[a[i]]){ 74 | // cerr << v << ' '; 75 | // } 76 | // cerr << endl; 77 | // } 78 | 79 | for (int i = 1; i <= m; i++) 80 | { 81 | scanf("%d%d", &q[i].l, &q[i].r); 82 | q[i].id = i; 83 | } 84 | sort(q + 1, q + 1 + m, [](const Q& x, const Q& y) { 85 | return x.l < y.l; 86 | }); 87 | int p = 1; 88 | priority_queue pq; 89 | for (int i = 1; i <= n; i++) 90 | { 91 | while (p <= m && q[p].l == i) 92 | { 93 | int id = q[p].id; 94 | ans[id] = -query(1, n, 1, q[p].r); 95 | pq.push(q[p++]); 96 | } 97 | for(auto &v:G[a[i]]){ 98 | if(v > i) 99 | update(1,n,1,v, n); 100 | } 101 | while (!pq.empty() && pq.top().r == i) 102 | { 103 | auto it = pq.top(); 104 | pq.pop(); 105 | ans[it.id] += query(1, n, 1, it.r); 106 | } 107 | alive[a[i]] = false; 108 | } 109 | for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); 110 | } 111 | -------------------------------------------------------------------------------- /2019-Xuzhou/L.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | enum { UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, TOP = 4, BOTTOM = 5 }; 11 | const int DIR[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; 12 | const int TRANS[6][4] = { 13 | {BOTTOM, TOP, UP, UP}, 14 | {TOP, BOTTOM, DOWN, DOWN}, 15 | {LEFT, LEFT, BOTTOM, TOP}, 16 | {RIGHT, RIGHT, TOP, BOTTOM}, 17 | {UP, DOWN, LEFT, RIGHT}, 18 | {DOWN, UP, RIGHT, LEFT}, 19 | }; 20 | 21 | const int MAXX = 4010; 22 | const int OFFSET = 5; 23 | const int LIMIT = 20; 24 | 25 | const int INF = 0x3f3f3f3f; 26 | 27 | namespace SB { 28 | int dp[MAXX][LIMIT][6]; 29 | void bfs() { 30 | 31 | std::memset(dp, -1, sizeof(dp)); 32 | std::queue> q; 33 | q.emplace(OFFSET, OFFSET, TOP); 34 | dp[OFFSET][OFFSET][TOP] = 0; 35 | do { 36 | 37 | auto t = q.front(); 38 | q.pop(); 39 | int x = std::get<0>(t), y = std::get<1>(t), s = std::get<2>(t), d = dp[x][y][s] + 1; 40 | for(int i = 0; i < 4; ++i) { 41 | 42 | int dx = x + DIR[i][0], dy = y + DIR[i][1], ds = TRANS[s][i]; 43 | if(dx < 0 || dx >= MAXX || dy < 0 || dy >= LIMIT) continue; 44 | int& xx = dp[dx][dy][ds]; 45 | if(xx >= 0) continue; 46 | xx = d; 47 | q.emplace(dx, dy, ds); 48 | 49 | } 50 | 51 | } while(!q.empty()); 52 | 53 | } 54 | 55 | int getdis(int x1, int y1, int x2, int y2) { 56 | 57 | int dx = x2 - x1, dy = y2 - y1; 58 | if(dx < 0) dx = -dx; 59 | if(dy < 0) dy = -dy; 60 | if(dx < dy) std::swap(dx, dy); 61 | if(dy + OFFSET >= LIMIT) return dx + dy; 62 | return dp[dx + OFFSET][dy + OFFSET][TOP]; 63 | 64 | } 65 | } 66 | 67 | int dp[1 << 16][16]; 68 | 69 | int main() { 70 | 71 | SB::bfs(); 72 | 73 | int T; 74 | scanf("%d", &T); 75 | while (T--) 76 | { 77 | int n; 78 | scanf("%d", &n); 79 | vector> p; 80 | for (int i = 0, x, y; i < n; i++) 81 | { 82 | scanf("%d%d", &x, &y); 83 | p.emplace_back(x, y); 84 | } 85 | for (int i = 0; i < (1 << n); i++) 86 | for (int j = 0; j < n; j++) 87 | dp[i][j] = INF; 88 | for (int i = 0; i < n; i++) 89 | { 90 | dp[1 << i][i] = SB::getdis(0, 0, p[i].first, p[i].second); 91 | } 92 | for (int i = 1; i < (1 << n); i++) 93 | for (int j = 0; j < n; j++) 94 | if (i >> j & 1) 95 | for (int k = 0; k < n; k++) 96 | if (!(i >> k & 1)) 97 | dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], 98 | dp[i][j] + SB::getdis(p[j].first, p[j].second, p[k].first, p[k].second)); 99 | printf("%d\n", *min_element(dp[(1 << n) - 1], dp[(1 << n) - 1] + n)); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /2019-Nanjing/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | const int N = 1 << 20; 11 | long long bit[N]; 12 | void update(int x, int v) 13 | { 14 | for (; x < N; x += x & -x) bit[x] += v; 15 | } 16 | long long query(int x) 17 | { 18 | long long ret = 0; 19 | for (; x; x -= x & -x) ret += bit[x]; 20 | return ret; 21 | } 22 | struct Point 23 | { 24 | int x, y; 25 | int value; 26 | Point(int x, int y, int v) : x(x), y(y), value(v) {} 27 | bool operator<(const Point& rhs) const { return y == rhs.y ? x < rhs.x : y < rhs.y; } 28 | }; 29 | struct Query : public Point 30 | { 31 | int id, sgn; 32 | Query(int x, int y, int id, int sgn, int v = -1) : Point(x, y, v), id(id), sgn(sgn) {} 33 | }; 34 | 35 | using i64 = long long; 36 | 37 | i64 gao(int x, int y, int n) 38 | { 39 | --x, --y; 40 | int p = std::min({x, y, n - x - 1, n - y - 1}), q = n - p - 1; 41 | i64 base = i64(n) * n - i64(n - 2 * p) * i64(n - 2 * p) + 1; 42 | if (x == q) return base + (q - y); 43 | base += n - 2 * p - 1; 44 | if (y == p) return base + (q - x); 45 | base += n - 2 * p - 1; 46 | if (x == p) return base + (y - p); 47 | base += n - 2 * p - 1; 48 | return base + (x - p); 49 | } 50 | 51 | int nb(long long x) 52 | { 53 | 54 | int result = 0; 55 | while (x) 56 | { 57 | 58 | result += x % 10; 59 | x /= 10; 60 | } 61 | return result; 62 | } 63 | 64 | int main() 65 | { 66 | int T; 67 | scanf("%d", &T); 68 | while (T--) 69 | { 70 | memset(bit, 0, sizeof(bit)); 71 | int n, m, q; 72 | scanf("%d%d%d", &n, &m, &q); 73 | std::vector points; 74 | std::vector queries; 75 | vector ans(q); 76 | for (int i = 0, x, y; i < m; i++) 77 | { 78 | scanf("%d%d", &x, &y); 79 | long long v = nb(gao(x, y, n)); 80 | debug(v); 81 | points.emplace_back(x, y, v); 82 | } 83 | points.emplace_back(1, 1000001, 0); 84 | std::sort(points.begin(), points.end()); 85 | for (int i = 0, x1, y1, x2, y2; i < q; i++) 86 | { 87 | scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 88 | queries.emplace_back(x1 - 1, y1 - 1, i, 1); 89 | queries.emplace_back(x1 - 1, y2, i, -1); 90 | queries.emplace_back(x2, y1 - 1, i, -1); 91 | queries.emplace_back(x2, y2, i, 1); 92 | } 93 | std::sort(queries.begin(), queries.end()); 94 | for (int pi = 0, pj, qi = 0, qj; pi < m + 1 || qi < q * 4; pi = pj, qi = qj) 95 | { 96 | for (qj = qi; qj < q * 4 && queries[qj].y < points[pi].y; qj++) 97 | { 98 | int id = queries[qj].id, sgn = queries[qj].sgn; 99 | long long res = query(queries[qj].x); 100 | ans[id] += sgn * res; 101 | } 102 | for (pj = pi; pj < m + 1 && points[pi].y == points[pj].y; pj++) update(points[pj].x, points[pj].value); 103 | } 104 | for (int i = 0; i < q; i++) printf("%lld\n", ans[i]); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /2019-Xuzhou/H.cpp: -------------------------------------------------------------------------------- 1 | // unsolved 2 | #include 3 | using namespace std; 4 | #ifndef ONLINE_JUDGE 5 | #include 6 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 7 | #else 8 | #define debug(...) 9 | #endif 10 | 11 | using ll = long long ; 12 | const int maxn = 1e5+5; 13 | const int mod = 998244353; 14 | 15 | bool isprime[maxn]; 16 | vector prime; 17 | ll ans, n; 18 | ll sn; 19 | ll g[maxn]; 20 | ll sum[100], tot[100]; 21 | 22 | void init(){ 23 | for(int i=2;i<=sn;i++) isprime[i] = true; 24 | for(int i=2;i<=sn;i++){ 25 | if(isprime[i]){ 26 | prime.push_back(i); 27 | for(int j=2*i;j<=sn;j+=i) isprime[j] = false; 28 | } 29 | } 30 | g[1] = 0; 31 | for(int i=2,x;i<=sn;i++){ 32 | x = i; 33 | for(auto &v:prime){ 34 | if(v > x) break; 35 | while(x % v == 0){ 36 | g[i]++; 37 | x /= v; 38 | } 39 | } 40 | tot[g[i]]++; 41 | (sum[g[i]] += i) %= mod; 42 | ans = (ans + g[i] * (n - i + 1) % mod) % mod; 43 | } 44 | } 45 | 46 | ll tot_prime, sum_prime; 47 | 48 | const ll inv2 = mod + 1 >> 1; 49 | struct SB{ 50 | const static int N=1e6+5; 51 | ll f1[N],f2[N],n; 52 | ll cal_tot(ll n){ 53 | int lim = sqrt(n); 54 | for(int i=1;i<=lim;++i)f1[i]=i-1,f2[i]=n/i-1; 55 | for(int p=2;p<=lim;++p){ 56 | if(f1[p]==f1[p-1])continue; 57 | for(int i=1;i<=lim/p;++i)f2[i]-=f2[i*p]-f1[p-1]; 58 | for(int i=lim/p+1;(ll)i<=n/(ll)p/(ll)p&&i<=lim;++i)f2[i]-=f1[n/i/p]-f1[p-1]; 59 | for(int i=lim;i>=(ll)p*p;--i)f1[i]-=f1[i/p]-f1[p-1]; 60 | } 61 | return f2[1]; 62 | } 63 | 64 | inline ll S(const ll&x){return (x+1) % mod * (x % mod) % mod * inv2 % mod;} 65 | ll cal_sum(ll n){ 66 | if(!n)return 0; 67 | ll lim=sqrt(n); 68 | for(ll i=1;i<=lim;++i)f1[i]=S(i),f2[i]=S(n/i); 69 | for(ll p=2;p<=lim;++p){ 70 | if(f1[p] == f1[p-1]) continue; 71 | ll t=f1[p-1]; 72 | for(ll i=1;i<=lim/p;++i) 73 | f2[i] = (f2[i] - (f2[i*p] - t + mod) % mod * p % mod + mod) % mod; 74 | for(ll i=lim/p+1;i<=lim&&i<=n/p/p;++i) 75 | f2[i] = (f2[i] - (f1[n/i/p]-t + mod) % mod * p % mod + mod) % mod; 76 | for(ll i=lim;i>=p*p;--i) 77 | f1[i] = (f1[i] - (f1[i/p]-t + mod) % mod * p % mod + mod) % mod; 78 | } 79 | return f2[1]; 80 | } 81 | } sb; 82 | void first(){ 83 | tot_prime = (sb.cal_tot(n) - sb.cal_tot(sn) + mod) % mod; 84 | sum_prime = (sb.cal_sum(n) - sb.cal_sum(sn) + mod) % mod; 85 | for(ll i=1;i<50;i++){ 86 | ll tmp = tot[i] * (n + 1) % mod * tot_prime % mod * (i + 1) % mod - sum[i] * (i + 1) % mod * sum_prime % mod + mod; 87 | cerr << tmp << endl; 88 | ans = (ans + tmp % mod) % mod; 89 | } 90 | } 91 | 92 | 93 | ll suf_g[maxn], suf_j[maxn], suf_gj[maxn]; 94 | 95 | void second(){ 96 | for(ll i = sn;i>0;i--) suf_g[i] = (suf_g[i+1] + g[i]) % mod, suf_j[i] = (suf_j[i+1] + i) % mod, suf_gj[i] = (suf_gj[i+1] + g[i] * i % mod) % mod; 97 | for(ll i=1; i <= sn;i++){ 98 | ll j = n / i; 99 | while(j * i < n) j++; 100 | if(j > sn) continue; 101 | ll len = sn - j + 1; 102 | ll tmp = len * g[i] % mod * (n + 1) % mod + suf_g[j] * (n+1) % mod; 103 | tmp %= mod; 104 | ll tmp1 = g[i] * i % mod * suf_j[j] % mod + i * suf_gj[j] % mod; 105 | tmp1 = (mod - tmp1 % mod) % mod; 106 | 107 | tmp = (tmp - tmp1 + mod) % mod; 108 | ans = (ans + tmp) % mod; 109 | } 110 | } 111 | int main() { 112 | scanf("%lld",&n); 113 | ans = 0; 114 | sn = sqrt(n); 115 | while(sn * sn > n) sn--; 116 | 117 | init(); 118 | first(); 119 | second(); 120 | printf("%lld\n",ans); 121 | } 122 | -------------------------------------------------------------------------------- /2019-Shenyang/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | const int MAXN = 101000; 5 | 6 | struct Hand { 7 | 8 | char name[20]; 9 | char cards[13]; 10 | std::vector count[4]; 11 | char sum; 12 | 13 | int getRank() const { 14 | 15 | if(count[0].size() == 5) { 16 | 17 | if(cards[9] == 1 && cards[10] == 1 && cards[11] == 1 && cards[12] == 1 && cards[0] == 1) return 0; 18 | if(count[0][0] + 4 == count[0][4]) return 1; 19 | 20 | } 21 | if(count[3].size() == 1) return 2; 22 | if(count[2].size() == 1) { 23 | 24 | if(count[1].size() == 1) return 3; 25 | else return 4; 26 | 27 | } 28 | if(count[1].size() == 2) return 5; 29 | if(count[1].size() == 1) return 6; 30 | return 7; 31 | 32 | } 33 | 34 | friend bool operator <(const Hand& a, const Hand& b) { 35 | 36 | int ra = a.getRank(), rb = b.getRank(); 37 | if(ra != rb) return ra < rb; 38 | switch(ra) { 39 | case 0: break; 40 | case 1: { 41 | 42 | if(a.count[0][0] != b.count[0][0]) return a.count[0][0] > b.count[0][0]; 43 | break; 44 | 45 | } 46 | case 2: { 47 | 48 | if(a.count[3][0] != b.count[3][0]) return a.count[3][0] > b.count[3][0]; 49 | if(a.count[0][0] != b.count[0][0]) return a.count[0][0] > b.count[0][0]; 50 | break; 51 | 52 | } 53 | case 3: { 54 | 55 | if(a.count[2][0] != b.count[2][0]) return a.count[2][0] > b.count[2][0]; 56 | if(a.count[1][0] != b.count[1][0]) return a.count[1][0] > b.count[1][0]; 57 | break; 58 | 59 | } 60 | case 4: { 61 | 62 | if(a.count[2][0] != b.count[2][0]) return a.count[2][0] > b.count[2][0]; 63 | int sa = a.count[0][0] + a.count[0][1], sb = b.count[0][0] + b.count[0][1]; 64 | if(sa != sb) return sa > sb; 65 | break; 66 | 67 | } 68 | case 5: { 69 | 70 | if(a.count[1][1] != b.count[1][1]) return a.count[1][1] > b.count[1][1]; 71 | if(a.count[1][0] != b.count[1][0]) return a.count[1][0] > b.count[1][0]; 72 | if(a.count[0][0] != b.count[0][0]) return a.count[0][0] > b.count[0][0]; 73 | break; 74 | 75 | } 76 | case 6: { 77 | 78 | if(a.count[1][0] != b.count[1][0]) return a.count[1][0] > b.count[1][0]; 79 | int sa = a.count[0][0] + a.count[0][1] + a.count[0][2], sb = b.count[0][0] + b.count[0][1] + b.count[0][2]; 80 | if(sa != sb) return sa > sb; 81 | break; 82 | 83 | } 84 | case 7: { 85 | 86 | if(a.sum != b.sum) return a.sum > b.sum; 87 | break; 88 | 89 | } 90 | } 91 | return std::strcmp(a.name, b.name) < 0; 92 | 93 | } 94 | 95 | }; 96 | 97 | 98 | int read() { 99 | 100 | char c; 101 | while(std::isspace(c = std::getchar())); 102 | if(c == 'A') return 0; 103 | if(c == 'J') return 10; 104 | if(c == 'Q') return 11; 105 | if(c == 'K') return 12; 106 | if(c == '1') { std::getchar(); return 9; } 107 | return c - '1'; 108 | 109 | } 110 | 111 | int n; 112 | Hand hands[MAXN]; 113 | int indexs[MAXN]; 114 | 115 | int main() { 116 | 117 | std::scanf("%d", &n); 118 | for(int i = 0; i < n; ++i) { 119 | 120 | std::scanf("%s", hands[i].name); 121 | for(int j = 0; j < 5; ++j) 122 | ++hands[i].cards[read()]; 123 | for(int j = 0; j < 13; ++j) { 124 | 125 | hands[i].sum += j * hands[i].cards[j]; 126 | if(hands[i].cards[j] == 5) *(int*)0 = 0; 127 | if(hands[i].cards[j]) 128 | hands[i].count[hands[i].cards[j] - 1].push_back(j); 129 | } 130 | indexs[i] = i; 131 | 132 | } 133 | 134 | std::sort(indexs, indexs + n, [](int a, int b) { return hands[a] < hands[b]; }); 135 | for(int i = 0; i < n; ++i) 136 | std::puts(hands[indexs[i]].name); 137 | 138 | } 139 | -------------------------------------------------------------------------------- /2019-Nanchang/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | typedef long long ll; 11 | typedef long double ld; 12 | const int inf = 0x3f3f3f3f; 13 | const int maxn = 200000 + 100; 14 | const int maxb = 1000; 15 | int match[4] = {2, 0, 1, 9}; 16 | struct node 17 | { 18 | int rm[5][5]; 19 | }; 20 | node sig[maxn * 4]; 21 | char s[maxn]; 22 | 23 | int n; 24 | void pushup(int rt) 25 | { 26 | const int lchild = rt << 1; 27 | const int rchild = rt << 1 | 1; 28 | for (int i = 0; i < 5; i++) 29 | { 30 | for (int j = i; j < 5; j++) 31 | { 32 | for (int k = i; k <= j; k++) 33 | { 34 | sig[rt].rm[i][j] = min(sig[rt].rm[i][j], sig[lchild].rm[i][k] + sig[rchild].rm[k][j]); 35 | } 36 | } 37 | } 38 | } 39 | void build(int l = 0, int r = n, int rt = 1) 40 | { 41 | if (l >= r) 42 | { 43 | return; 44 | } 45 | memset(sig[rt].rm, inf, sizeof(sig[rt].rm)); 46 | if (l + 1 == r) 47 | { 48 | for (int i = 0; i < 5; i++) 49 | { 50 | sig[rt].rm[i][i] = 0; 51 | } 52 | if (s[l] == '2') 53 | { 54 | sig[rt].rm[0][0] = 1; 55 | sig[rt].rm[0][1] = 0; 56 | } 57 | else if (s[l] == '0') 58 | { 59 | sig[rt].rm[1][1] = 1; 60 | sig[rt].rm[1][2] = 0; 61 | } 62 | else if (s[l] == '1') 63 | { 64 | sig[rt].rm[2][2] = 1; 65 | sig[rt].rm[2][3] = 0; 66 | } 67 | else if (s[l] == '9') 68 | { 69 | sig[rt].rm[3][3] = 1; 70 | sig[rt].rm[3][4] = 0; 71 | } 72 | else if (s[l] == '8') 73 | { 74 | sig[rt].rm[3][3] = sig[rt].rm[4][4] = 1; 75 | } 76 | return; 77 | } 78 | const int mid = l + (r - l) / 2; 79 | build(l, mid, rt << 1); 80 | build(mid, r, rt << 1 | 1); 81 | pushup(rt); 82 | /* cout << "debug : "< mid) 118 | { 119 | rhs = query(L, R, mid, r, rt << 1 | 1); 120 | } 121 | for (int i = 0; i < 5; i++) 122 | { 123 | for (int j = i; j < 5; j++) 124 | { 125 | for (int k = i; k <= j; k++) 126 | { 127 | ans.rm[i][j] = min(ans.rm[i][j], lhs.rm[i][k] + rhs.rm[k][j]); 128 | } 129 | } 130 | } 131 | return ans; 132 | } 133 | int main() 134 | { 135 | int q; 136 | while (~scanf("%d%d", &n, &q)) 137 | { 138 | scanf("%s", s); 139 | reverse(s, s + n); 140 | build(); 141 | for (int i = 0; i < q; i++) 142 | { 143 | int l, r; 144 | scanf("%d%d", &l, &r); 145 | r = n - r + 1, l = n - l + 1; 146 | swap(l, r); 147 | debug(l, r); 148 | node ans = query(l - 1, r); 149 | if (ans.rm[0][4] == inf) 150 | { 151 | ans.rm[0][4] = -1; 152 | } 153 | printf("%d\n", ans.rm[0][4]); 154 | } 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /2019-CCPC/1003.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #ifndef ONLINE_JUDGE 3 | #define debug(fmt, ...) fprintf(stderr, "[%s] " fmt "\n", __func__, ##__VA_ARGS__) 4 | #else 5 | #define debug(...) 6 | #endif 7 | 8 | const int maxn = 1 << 17; 9 | namespace Suffix_Array 10 | { 11 | char s[maxn]; 12 | int sa[maxn], t[maxn], t2[maxn], c[maxn], rank[maxn], height[maxn]; 13 | void build_sa(int m, int n) 14 | { 15 | n++; 16 | int *x = t, *y = t2; 17 | for (int i = 0; i < m; i++) c[i] = 0; 18 | for (int i = 0; i < n; i++) c[x[i] = s[i]]++; 19 | for (int i = 1; i < m; i++) c[i] += c[i - 1]; 20 | for (int i = n - 1; ~i; i--) sa[--c[x[i]]] = i; 21 | for (int k = 1; k <= n; k <<= 1) 22 | { 23 | int p = 0; 24 | for (int i = n - k; i < n; i++) y[p++] = i; 25 | for (int i = 0; i < n; i++) 26 | if (sa[i] >= k) y[p++] = sa[i] - k; 27 | for (int i = 0; i < m; i++) c[i] = 0; 28 | for (int i = 0; i < n; i++) c[x[y[i]]]++; 29 | for (int i = 1; i < m; i++) c[i] += c[i - 1]; 30 | for (int i = n - 1; ~i; i--) sa[--c[x[y[i]]]] = y[i]; 31 | std::swap(x, y); 32 | p = 1; 33 | x[sa[0]] = 0; 34 | for (int i = 1; i < n; i++) 35 | x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k] ? p - 1 : p++; 36 | if (p >= n) break; 37 | m = p; 38 | } 39 | n--; 40 | int k = 0; 41 | for (int i = 0; i <= n; i++) rank[sa[i]] = i; 42 | for (int i = 0; i < n; i++) 43 | { 44 | if (k) k--; 45 | int j = sa[rank[i] - 1]; 46 | while (s[i + k] == s[j + k]) k++; 47 | height[rank[i]] = k; 48 | } 49 | } 50 | 51 | int dp[maxn][30]; 52 | void initrmq(int n) 53 | { 54 | for (int i = 1; i <= n; i++) 55 | dp[i][0] = height[i]; 56 | for (int j = 1; (1 << j) <= n; j++) 57 | for (int i = 1; i + (1 << j) - 1 <= n; i++) 58 | dp[i][j] = std::min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]); 59 | } 60 | int rmq(int l, int r) 61 | { 62 | int k = 31 - __builtin_clz(r - l + 1); 63 | return std::min(dp[l][k], dp[r - (1 << k) + 1][k]); 64 | } 65 | int lcp(int a, int b) 66 | { 67 | a = rank[a], b = rank[b]; 68 | if (a > b) std::swap(a, b); 69 | return rmq(a + 1, b); 70 | } 71 | } // namespace Suffix_Array 72 | using Suffix_Array::build_sa; 73 | using Suffix_Array::initrmq; 74 | using Suffix_Array::lcp; 75 | using Suffix_Array::rank; 76 | using Suffix_Array::rmq; 77 | using Suffix_Array::sa; 78 | 79 | int rt[maxn]; 80 | int cnt; 81 | int lson[maxn << 5], rson[maxn << 5], sum[maxn << 5]; 82 | #define Lson l, m, lson[x], lson[y] 83 | #define Rson m + 1, r, rson[x], rson[y] 84 | void update(int p, int l, int r, int& x, int y) 85 | { 86 | lson[++cnt] = lson[y], rson[cnt] = rson[y], sum[cnt] = sum[y] + 1, x = cnt; 87 | if (l == r) return; 88 | int m = (l + r) >> 1; 89 | if (p <= m) 90 | update(p, Lson); 91 | else 92 | update(p, Rson); 93 | } 94 | int query(int l, int r, int x, int y, int k) 95 | { 96 | if (l == r) return l; 97 | int m = (l + r) >> 1; 98 | int s = sum[lson[y]] - sum[lson[x]]; 99 | if (s >= k) 100 | return query(Lson, k); 101 | else 102 | return query(Rson, k - s); 103 | } 104 | 105 | int main() 106 | { 107 | int T; 108 | scanf("%d", &T); 109 | while (T--) 110 | { 111 | int n, m; 112 | scanf("%d%d", &n, &m); 113 | scanf("%s", Suffix_Array::s); 114 | build_sa(128, n); 115 | initrmq(n); 116 | cnt = 0; 117 | // for (int i = 1; i <= n; i++) debug("%d", sa[i]); 118 | // for (int i = 0; i < n; i++) printf("%d ", rank[i]); 119 | // puts(""); 120 | for (int i = 1; i <= n; i++) update(sa[i], 0, n - 1, rt[i], rt[i - 1]); 121 | for (int i = 0, L, R, k; i < m; i++) 122 | { 123 | scanf("%d%d%d", &L, &R, &k); 124 | --L, --R; 125 | int l = 1, r = rank[L] - 1; 126 | int lb = rank[L], ub = rank[L]; 127 | // debug("[%d %d]", lb, ub); 128 | while (l <= r) 129 | { 130 | int m = l + r >> 1; 131 | if (rmq(m + 1, rank[L]) >= R - L + 1) 132 | lb = m, r = m - 1; 133 | else 134 | l = m + 1; 135 | } 136 | l = rank[L] + 1, r = n; 137 | while (l <= r) 138 | { 139 | int m = l + r >> 1; 140 | if (rmq(rank[L] + 1, m) >= R - L + 1) 141 | ub = m, l = m + 1; 142 | else 143 | r = m - 1; 144 | } 145 | // [lb, ub] 146 | // debug("[%d %d]", lb, ub); 147 | if (ub - lb + 1 < k) 148 | puts("-1"); 149 | else 150 | printf("%d\n", query(0, n - 1, rt[lb - 1], rt[ub], k) + 1); 151 | } 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /2019-Nanchang/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | namespace fft 11 | { 12 | 13 | using type = int; 14 | using db = double; 15 | 16 | struct cp 17 | { 18 | db x, y; 19 | cp() { x = y = 0; } 20 | cp(db x, db y) : x(x), y(y) {} 21 | }; 22 | 23 | inline cp operator+(cp a, cp b) { return cp(a.x + b.x, a.y + b.y); } 24 | inline cp operator-(cp a, cp b) { return cp(a.x - b.x, a.y - b.y); } 25 | inline cp operator*(cp a, cp b) { return cp(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } 26 | inline cp conj(cp a) { return cp(a.x, -a.y); } 27 | 28 | type base = 1; 29 | vector roots = {{0, 0}, 30 | {1, 0}}; 31 | vector rev = {0, 1}; 32 | 33 | const db PI = acosl(-1.0); 34 | 35 | void ensure_base(type nbase) 36 | { 37 | if (nbase <= base) return; 38 | rev.resize(static_cast(1 << nbase)); 39 | for (type i = 0; i < (1 << nbase); i++) 40 | rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1)); 41 | roots.resize(static_cast(1 << nbase)); 42 | while (base < nbase) 43 | { 44 | db angle = 2 * PI / (1 << (base + 1)); 45 | for (type i = 1 << (base - 1); i < (1 << base); i++) 46 | { 47 | roots[i << 1] = roots[i]; 48 | db angle_i = angle * (2 * i + 1 - (1 << base)); 49 | roots[(i << 1) + 1] = cp(cos(angle_i), sin(angle_i)); 50 | } 51 | base++; 52 | } 53 | } 54 | 55 | void fft(vector& a, type n = -1) 56 | { 57 | if (n == -1) n = a.size(); 58 | assert((n & (n - 1)) == 0); 59 | type zeros = __builtin_ctz(n); 60 | ensure_base(zeros); 61 | type shift = base - zeros; 62 | for (type i = 0; i < n; i++) 63 | if (i < (rev[i] >> shift)) 64 | swap(a[i], a[rev[i] >> shift]); 65 | for (type k = 1; k < n; k <<= 1) 66 | for (type i = 0; i < n; i += 2 * k) 67 | for (type j = 0; j < k; j++) 68 | { 69 | cp z = a[i + j + k] * roots[j + k]; 70 | a[i + j + k] = a[i + j] - z; 71 | a[i + j] = a[i + j] + z; 72 | } 73 | } 74 | 75 | vector fa, fb; 76 | 77 | vector multiply(const vector& a, const vector& b) 78 | { 79 | type need = a.size() + b.size() - 1; 80 | type nbase = 0; 81 | while ((1 << nbase) < need) nbase++; 82 | ensure_base(nbase); 83 | type sz = 1 << nbase; 84 | if (sz > (type)fa.size()) 85 | fa.resize(static_cast(sz)); 86 | for (type i = 0; i < sz; i++) 87 | { 88 | type x = (i < (type)a.size() ? a[i] : 0); 89 | type y = (i < (type)b.size() ? b[i] : 0); 90 | fa[i] = cp(x, y); 91 | } 92 | fft(fa, sz); 93 | cp r(0, -0.25 / sz); 94 | for (type i = 0; i <= (sz >> 1); i++) 95 | { 96 | type j = (sz - i) & (sz - 1); 97 | cp z = (fa[j] * fa[j] - conj(fa[i] * fa[i])) * r; 98 | if (i != j) 99 | fa[j] = (fa[i] * fa[i] - conj(fa[j] * fa[j])) * r; 100 | fa[i] = z; 101 | } 102 | fft(fa, sz); 103 | vector res(static_cast(need)); 104 | for (type i = 0; i < need; i++) res[i] = fa[i].x + 0.5; 105 | return res; 106 | } 107 | 108 | vector multiply_mod(const vector& a, const vector& b, type m) 109 | { 110 | type need = a.size() + b.size() - 1; 111 | type nbase = 0; 112 | while ((1 << nbase) < need) nbase++; 113 | ensure_base(nbase); 114 | type sz = 1 << nbase; 115 | if (sz > (type)fa.size()) fa.resize(static_cast(sz)); 116 | for (type i = 0; i < (type)a.size(); i++) 117 | { 118 | type x = (a[i] % m + m) % m; 119 | fa[i] = cp(x & ((1 << 15) - 1), x >> 15); 120 | } 121 | fill(fa.begin() + a.size(), fa.begin() + sz, cp{0, 0}); 122 | fft(fa, sz); 123 | if (sz > (type)fb.size()) fb.resize(static_cast(sz)); 124 | for (type i = 0; i < (type)b.size(); i++) 125 | { 126 | type x = (b[i] % m + m) % m; 127 | fb[i] = cp(x & ((1 << 15) - 1), x >> 15); 128 | } 129 | fill(fb.begin() + b.size(), fb.begin() + sz, cp{0, 0}); 130 | fft(fb, sz); 131 | db ratio = 0.25 / sz; 132 | cp r2(0, -1); 133 | cp r3(ratio, 0); 134 | cp r4(0, -ratio); 135 | cp r5(0, 1); 136 | for (type i = 0; i <= (sz >> 1); i++) 137 | { 138 | type j = (sz - i) & (sz - 1); 139 | cp a1 = (fa[i] + conj(fa[j])); 140 | cp a2 = (fa[i] - conj(fa[j])) * r2; 141 | cp b1 = (fb[i] + conj(fb[j])) * r3; 142 | cp b2 = (fb[i] - conj(fb[j])) * r4; 143 | if (i != j) 144 | { 145 | cp c1 = (fa[j] + conj(fa[i])); 146 | cp c2 = (fa[j] - conj(fa[i])) * r2; 147 | cp d1 = (fb[j] + conj(fb[i])) * r3; 148 | cp d2 = (fb[j] - conj(fb[i])) * r4; 149 | fa[i] = c1 * d1 + c2 * d2 * r5; 150 | fb[i] = c1 * d2 + c2 * d1; 151 | } 152 | fa[j] = a1 * b1 + a2 * b2 * r5; 153 | fb[j] = a1 * b2 + a2 * b1; 154 | } 155 | fft(fa, sz); 156 | fft(fb, sz); 157 | vector res(static_cast(need)); 158 | for (type i = 0; i < need; i++) 159 | { 160 | long long aa = fa[i].x + 0.5; 161 | long long bb = fb[i].x + 0.5; 162 | long long cc = fa[i].y + 0.5; 163 | res[i] = (aa + ((bb % m) << 15) + ((cc % m) << 30)) % m; 164 | } 165 | return res; 166 | } 167 | }; // namespace fft 168 | 169 | const int mod = 100003; 170 | using ll = long long; 171 | ll pow(ll a, ll n, ll mod) 172 | { 173 | ll result = 1; 174 | for (; n; n >>= 1, (a *= a) %= mod) 175 | if (n & 1) (result *= a) %= mod; 176 | return result; 177 | } 178 | 179 | ll inv(ll a, ll mod) { return pow(a, mod - 2, mod); } 180 | 181 | const int maxn = 100001; 182 | ll f[maxn]; 183 | ll finv[maxn]; 184 | void CalFact() 185 | { 186 | f[0] = 1; 187 | for (int i = 1; i < maxn; i++) f[i] = (f[i - 1] * i) % mod; 188 | finv[maxn - 1] = inv(f[maxn - 1], mod); 189 | for (int i = maxn - 2; ~i; i--) finv[i] = finv[i + 1] * (i + 1) % mod; 190 | } 191 | 192 | vector solve(int l, int r, const vector>& poly) 193 | { 194 | if (l + 1 == r) return poly[l]; 195 | const int m = l + r >> 1; 196 | return fft::multiply_mod(solve(l, m, poly), solve(m, r, poly), mod); 197 | } 198 | ll C(int n, int m) { return n < m || m < 0 ? 0 : f[n] * finv[m] % mod * finv[n - m] % mod; } 199 | 200 | int main() 201 | { 202 | CalFact(); 203 | int n, a, q; 204 | scanf("%d%d%d", &n, &a, &q); 205 | vector> poly(n); 206 | for (int i = 0, x; i < n; i++) 207 | { 208 | scanf("%d", &x); 209 | poly[i] = {1, static_cast(pow(a, x, mod))}; 210 | } 211 | debug(poly); 212 | auto ans = solve(0, n, poly); 213 | int denominator = inv(a - 1, mod); 214 | debug(ans); 215 | for (int i = 0, k; i < q; i++) 216 | { 217 | scanf("%d", &k); 218 | debug(n, k, C(n, k)); 219 | printf("%lld\n", static_cast(ans[k] - C(n, k)) % mod * denominator % mod); 220 | } 221 | } -------------------------------------------------------------------------------- /2019-CCPC/1004.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | 8 | #define ENABLE_FREAD 9 | 10 | namespace io_impl 11 | { 12 | inline bool maybe_digit(char c) 13 | { 14 | return c >= '0' && c <= '9'; 15 | } 16 | 17 | inline bool maybe_decimal(char c) 18 | { 19 | return (c >= '0' && c <= '9') || (c == '.'); 20 | } 21 | 22 | struct io_s 23 | { 24 | 25 | bool negative; 26 | bool ok = true; 27 | char ch = next_char(); 28 | 29 | int precious = 6; 30 | long double epslion = 1e-6; 31 | 32 | #ifdef ENABLE_FREAD 33 | inline char next_char() 34 | { 35 | static char buf[100000], *p1 = buf, *p2 = buf; 36 | return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; 37 | } 38 | #else 39 | inline char next_char() const 40 | { 41 | return getchar(); 42 | } 43 | #endif 44 | 45 | /// read int 46 | 47 | template 48 | inline bool rn(T& _v) 49 | { 50 | negative = false; 51 | _v = 0; 52 | while (!maybe_digit(ch) && ch != EOF) 53 | { 54 | negative = ch == '-'; 55 | ch = next_char(); 56 | }; 57 | if (ch == EOF) return ok = false; 58 | do 59 | { 60 | _v = (_v << 1) + (_v << 3) + ch - '0'; 61 | } while (maybe_digit(ch = next_char())); 62 | if (negative) _v = -_v; 63 | return true; 64 | } 65 | 66 | template 67 | inline void rn_unsafe(T& _v) 68 | { 69 | negative = false; 70 | _v = 0; 71 | while (!maybe_digit(ch)) 72 | { 73 | negative = ch == '-'; 74 | ch = next_char(); 75 | }; 76 | do 77 | { 78 | _v = (_v << 1) + (_v << 3) + ch - '0'; 79 | } while (maybe_digit(ch = next_char())); 80 | if (negative) _v = -_v; 81 | } 82 | 83 | template 84 | inline T rn() 85 | { 86 | T v = T(); 87 | rn_unsafe(v); 88 | return v; 89 | } 90 | 91 | inline int ri() { return rn(); } 92 | inline ll rll() { return rn(); } 93 | 94 | /// read unsigned 95 | 96 | template 97 | inline bool run(T& _v) 98 | { 99 | _v = 0; 100 | while (!maybe_digit(ch) && ch != EOF) ch = next_char(); 101 | if (ch == EOF) return ok = false; 102 | do 103 | { 104 | _v = (_v << 1) + (_v << 3) + ch - '0'; 105 | } while (maybe_digit(ch = next_char())); 106 | return true; 107 | } 108 | 109 | template 110 | inline void run_unsafe(T& _v) 111 | { 112 | _v = 0; 113 | while (!maybe_digit(ch)) ch = next_char(); 114 | do 115 | { 116 | _v = (_v << 1) + (_v << 3) + ch - '0'; 117 | } while (maybe_digit(ch = next_char())); 118 | } 119 | 120 | template 121 | inline T run() 122 | { 123 | T v = T(); 124 | run_unsafe(v); 125 | return v; 126 | } 127 | 128 | /// read double 129 | 130 | template 131 | inline bool rd(T& _v) 132 | { 133 | negative = false; 134 | _v = 0; 135 | while (!maybe_digit(ch) && ch != EOF) 136 | { 137 | negative = ch == '-'; 138 | ch = next_char(); 139 | }; 140 | if (ch == EOF) return ok = false; 141 | do 142 | { 143 | _v = (_v * 10) + (ch - '0'); 144 | } while (maybe_digit(ch = next_char())); 145 | static int stk[70], tp; 146 | if (ch == '.') 147 | { 148 | tp = 0; 149 | T _v2 = 0; 150 | while (maybe_digit(ch = next_char())) 151 | { 152 | stk[tp++] = ch - '0'; 153 | } 154 | while (tp--) 155 | { 156 | _v2 = _v2 / 10 + stk[tp]; 157 | } 158 | _v += _v2 / 10; 159 | } 160 | if (ch == 'e' || ch == 'E') 161 | { 162 | ch = next_char(); 163 | static bool _neg = false; 164 | if (ch == '+') 165 | ch = next_char(); 166 | else if (ch == '-') 167 | _neg = true, ch = next_char(); 168 | if (maybe_digit(ch)) 169 | { 170 | _v *= pow(10, run() * (_neg ? -1 : 1)); 171 | } 172 | } 173 | if (negative) _v = -_v; 174 | return true; 175 | } 176 | 177 | template 178 | inline T rd() 179 | { 180 | T v = T(); 181 | rd(v); 182 | return v; 183 | } 184 | 185 | /// read string 186 | 187 | inline int gets(char* s) 188 | { 189 | char* c = s; 190 | while (ch == '\n' || ch == '\r' || ch == ' ') ch = next_char(); 191 | if (ch == EOF) return (ok = false), *c = 0; 192 | do 193 | { 194 | *(c++) = ch; 195 | ch = next_char(); 196 | } while (ch != '\n' && ch != '\r' && ch != ' ' && ch != EOF); 197 | *(c++) = '\0'; 198 | return static_cast(c - s - 1); 199 | } 200 | 201 | inline int getline(char* s) 202 | { 203 | char* c = s; 204 | while (ch == '\n' || ch == '\r') ch = next_char(); 205 | if (ch == EOF) return (ok = false), *c = 0; 206 | do 207 | { 208 | *(c++) = ch; 209 | ch = next_char(); 210 | } while (ch != '\n' && ch != '\r' && ch != EOF); 211 | *(c++) = '\0'; 212 | return static_cast(c - s - 1); 213 | } 214 | 215 | inline char get_alpha() 216 | { 217 | while (!isalpha(ch)) ch = next_char(); 218 | const char ret = ch; 219 | ch = next_char(); 220 | return ret; 221 | } 222 | 223 | inline char get_nonblank() 224 | { 225 | while (isblank(ch)) ch = next_char(); 226 | const char ret = ch; 227 | ch = next_char(); 228 | return ret; 229 | } 230 | 231 | inline char get_char() 232 | { 233 | const char ret = ch; 234 | ch = next_char(); 235 | return ret; 236 | } 237 | 238 | template 239 | inline void o(T p) 240 | { 241 | static int stk[70], tp; 242 | if (p == 0) 243 | { 244 | putchar('0'); 245 | return; 246 | } 247 | if (p < 0) 248 | { 249 | p = -p; 250 | putchar('-'); 251 | } 252 | while (p) stk[++tp] = p % 10, p /= 10; 253 | while (tp) putchar(stk[tp--] + '0'); 254 | } 255 | 256 | template 257 | inline void od(T v) 258 | { 259 | static int stk[70], tp; 260 | tp = 0; 261 | if (fabs(v) < epslion / 10) 262 | { 263 | putchar('0'); 264 | if (precious > 0) 265 | { 266 | putchar('.'); 267 | for (int i = 0; i < precious; ++i) putchar('0'); 268 | } 269 | return; 270 | } 271 | else 272 | { 273 | if (v < 0) 274 | { 275 | v = -v; 276 | putchar('-'); 277 | } 278 | v += epslion / 2; 279 | T p = floor(v) + epslion / 10; 280 | while (fabs(p) > 1 - epslion) 281 | { 282 | stk[++tp] = fmod(p, 10), p /= 10; 283 | } 284 | while (tp) putchar(stk[tp--] + '0'); 285 | } 286 | if (precious == 0) return; 287 | putchar('.'); 288 | v -= floor(v); 289 | for (int i = 0; i < precious; ++i) 290 | { 291 | v *= 10; 292 | putchar((int)floor(v) + '0'); 293 | v = fmod(v, 1); 294 | } 295 | } 296 | 297 | /// Enhancement 298 | 299 | typedef void io_operator(io_s& v); 300 | typedef char* charptr; 301 | 302 | template 303 | inline io_s& operator>>(T& x) 304 | { 305 | if (numeric_limits::is_signed) 306 | rn(x); 307 | else 308 | run(x); 309 | return *this; 310 | } 311 | 312 | template 313 | inline io_s& operator<<(const T& x); 314 | 315 | inline io_s& operator<<(io_operator* v) 316 | { 317 | v(*this); 318 | return *this; 319 | } 320 | 321 | operator bool() const { return ok; } 322 | 323 | void set_precious(int x) 324 | { 325 | precious = x; 326 | epslion = pow(10, -x); 327 | } 328 | }; 329 | 330 | template <> 331 | inline io_s& io_s::operator>>(char*& x) 332 | { 333 | gets(x); 334 | return *this; 335 | } 336 | 337 | template <> 338 | inline io_s& io_s::operator>>(float& x) 339 | { 340 | rd(x); 341 | return *this; 342 | } 343 | 344 | template <> 345 | inline io_s& io_s::operator>>(double& x) 346 | { 347 | rd(x); 348 | return *this; 349 | } 350 | 351 | template <> 352 | inline io_s& io_s::operator>>(long double& x) 353 | { 354 | rd(x); 355 | return *this; 356 | } 357 | 358 | template <> 359 | inline void io_s::o(const char* p) 360 | { 361 | printf(p); 362 | } 363 | 364 | template <> 365 | inline void io_s::o(const char p) 366 | { 367 | putchar(p); 368 | } 369 | 370 | template <> 371 | inline void io_s::o(float p) 372 | { 373 | od(p); 374 | } 375 | 376 | template <> 377 | inline void io_s::o(double p) 378 | { 379 | od(p); 380 | } 381 | 382 | template <> 383 | inline void io_s::o(long double p) 384 | { 385 | od(p); 386 | } 387 | 388 | template 389 | inline io_s& io_s::operator<<(const T& x) 390 | { 391 | o(x); 392 | return *this; 393 | } 394 | 395 | inline void new_line(io_s& v) 396 | { 397 | v.o('\n'); 398 | } 399 | 400 | io_s::io_operator* nl = new_line; 401 | } // namespace io_impl 402 | 403 | using namespace io_impl; 404 | 405 | io_s io; 406 | 407 | namespace solution 408 | { 409 | using ll = long long; 410 | const int maxn = 5e4 + 5; 411 | using pii = pair; 412 | struct Node 413 | { 414 | ll w; 415 | int u; 416 | bool operator<(const Node& rhs) const 417 | { 418 | if (w != rhs.w) return w < rhs.w; 419 | return u < rhs.u; 420 | } 421 | }; 422 | multiset SET; 423 | vector G[maxn]; 424 | 425 | int n, m, q, K, k[maxn]; 426 | ll ans[maxn]; 427 | 428 | void solve() 429 | { 430 | for (int i = 1; i <= K; i++) 431 | { 432 | auto it = *SET.begin(); 433 | SET.erase(SET.begin()); 434 | ans[i] = it.w; 435 | for (auto& V : G[it.u]) 436 | { 437 | if (SET.size() <= K - i) 438 | { 439 | SET.insert({it.w + V.first, V.second}); 440 | } 441 | else 442 | { 443 | if (it.w + V.first < (SET.rbegin()->w)) 444 | { 445 | SET.erase(--SET.end()); 446 | SET.insert({it.w + V.first, V.second}); 447 | } 448 | else 449 | break; 450 | } 451 | } 452 | } 453 | for (int i = 0; i < q; i++) 454 | { 455 | io << ans[k[i]] << nl; 456 | // printf("%lld\n", ans[k[i]]); 457 | } 458 | } 459 | int main() 460 | { 461 | int T; 462 | io >> T; 463 | while (T--) 464 | { 465 | io >> n >> m >> q; 466 | for (int i = 1; i <= n; i++) G[i].clear(); 467 | SET.clear(); 468 | for (int i = 0, u, v, w; i < m; i++) 469 | { 470 | io >> u >> v >> w; 471 | G[u].push_back({w, v}); 472 | SET.insert({w, v}); 473 | } 474 | for (int i = 1; i <= n; i++) sort(G[i].begin(), G[i].end()); 475 | K = 0; 476 | for (int i = 0; i < q; i++) io >> k[i], K = max(K, k[i]); 477 | 478 | solve(); 479 | } 480 | return 0; 481 | } 482 | } // namespace solution 483 | 484 | int main() 485 | { 486 | return solution::main(); 487 | } -------------------------------------------------------------------------------- /2019-CCPC/1002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | 8 | #define ENABLE_FREAD 9 | 10 | namespace io_impl 11 | { 12 | inline bool maybe_digit(char c) 13 | { 14 | return c >= '0' && c <= '9'; 15 | } 16 | 17 | inline bool maybe_decimal(char c) 18 | { 19 | return (c >= '0' && c <= '9') || (c == '.'); 20 | } 21 | 22 | struct io_s 23 | { 24 | 25 | bool negative; 26 | bool ok = true; 27 | char ch = next_char(); 28 | 29 | int precious = 6; 30 | long double epslion = 1e-6; 31 | 32 | #ifdef ENABLE_FREAD 33 | inline char next_char() 34 | { 35 | static char buf[100000], *p1 = buf, *p2 = buf; 36 | return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; 37 | } 38 | #else 39 | inline char next_char() const 40 | { 41 | return getchar(); 42 | } 43 | #endif 44 | 45 | /// read int 46 | 47 | template 48 | inline bool rn(T& _v) 49 | { 50 | negative = false; 51 | _v = 0; 52 | while (!maybe_digit(ch) && ch != EOF) 53 | { 54 | negative = ch == '-'; 55 | ch = next_char(); 56 | }; 57 | if (ch == EOF) return ok = false; 58 | do 59 | { 60 | _v = (_v << 1) + (_v << 3) + ch - '0'; 61 | } while (maybe_digit(ch = next_char())); 62 | if (negative) _v = -_v; 63 | return true; 64 | } 65 | 66 | template 67 | inline void rn_unsafe(T& _v) 68 | { 69 | negative = false; 70 | _v = 0; 71 | while (!maybe_digit(ch)) 72 | { 73 | negative = ch == '-'; 74 | ch = next_char(); 75 | }; 76 | do 77 | { 78 | _v = (_v << 1) + (_v << 3) + ch - '0'; 79 | } while (maybe_digit(ch = next_char())); 80 | if (negative) _v = -_v; 81 | } 82 | 83 | template 84 | inline T rn() 85 | { 86 | T v = T(); 87 | rn_unsafe(v); 88 | return v; 89 | } 90 | 91 | inline int ri() { return rn(); } 92 | inline ll rll() { return rn(); } 93 | 94 | /// read unsigned 95 | 96 | template 97 | inline bool run(T& _v) 98 | { 99 | _v = 0; 100 | while (!maybe_digit(ch) && ch != EOF) ch = next_char(); 101 | if (ch == EOF) return ok = false; 102 | do 103 | { 104 | _v = (_v << 1) + (_v << 3) + ch - '0'; 105 | } while (maybe_digit(ch = next_char())); 106 | return true; 107 | } 108 | 109 | template 110 | inline void run_unsafe(T& _v) 111 | { 112 | _v = 0; 113 | while (!maybe_digit(ch)) ch = next_char(); 114 | do 115 | { 116 | _v = (_v << 1) + (_v << 3) + ch - '0'; 117 | } while (maybe_digit(ch = next_char())); 118 | } 119 | 120 | template 121 | inline T run() 122 | { 123 | T v = T(); 124 | run_unsafe(v); 125 | return v; 126 | } 127 | 128 | /// read double 129 | 130 | template 131 | inline bool rd(T& _v) 132 | { 133 | negative = false; 134 | _v = 0; 135 | while (!maybe_digit(ch) && ch != EOF) 136 | { 137 | negative = ch == '-'; 138 | ch = next_char(); 139 | }; 140 | if (ch == EOF) return ok = false; 141 | do 142 | { 143 | _v = (_v * 10) + (ch - '0'); 144 | } while (maybe_digit(ch = next_char())); 145 | static int stk[70], tp; 146 | if (ch == '.') 147 | { 148 | tp = 0; 149 | T _v2 = 0; 150 | while (maybe_digit(ch = next_char())) 151 | { 152 | stk[tp++] = ch - '0'; 153 | } 154 | while (tp--) 155 | { 156 | _v2 = _v2 / 10 + stk[tp]; 157 | } 158 | _v += _v2 / 10; 159 | } 160 | if (ch == 'e' || ch == 'E') 161 | { 162 | ch = next_char(); 163 | static bool _neg = false; 164 | if (ch == '+') 165 | ch = next_char(); 166 | else if (ch == '-') 167 | _neg = true, ch = next_char(); 168 | if (maybe_digit(ch)) 169 | { 170 | _v *= pow(10, run() * (_neg ? -1 : 1)); 171 | } 172 | } 173 | if (negative) _v = -_v; 174 | return true; 175 | } 176 | 177 | template 178 | inline T rd() 179 | { 180 | T v = T(); 181 | rd(v); 182 | return v; 183 | } 184 | 185 | /// read string 186 | 187 | inline int gets(char* s) 188 | { 189 | char* c = s; 190 | while (ch == '\n' || ch == '\r' || ch == ' ') ch = next_char(); 191 | if (ch == EOF) return (ok = false), *c = 0; 192 | do 193 | { 194 | *(c++) = ch; 195 | ch = next_char(); 196 | } while (ch != '\n' && ch != '\r' && ch != ' ' && ch != EOF); 197 | *(c++) = '\0'; 198 | return static_cast(c - s - 1); 199 | } 200 | 201 | inline int getline(char* s) 202 | { 203 | char* c = s; 204 | while (ch == '\n' || ch == '\r') ch = next_char(); 205 | if (ch == EOF) return (ok = false), *c = 0; 206 | do 207 | { 208 | *(c++) = ch; 209 | ch = next_char(); 210 | } while (ch != '\n' && ch != '\r' && ch != EOF); 211 | *(c++) = '\0'; 212 | return static_cast(c - s - 1); 213 | } 214 | 215 | inline char get_alpha() 216 | { 217 | while (!isalpha(ch)) ch = next_char(); 218 | const char ret = ch; 219 | ch = next_char(); 220 | return ret; 221 | } 222 | 223 | inline char get_nonblank() 224 | { 225 | while (isblank(ch)) ch = next_char(); 226 | const char ret = ch; 227 | ch = next_char(); 228 | return ret; 229 | } 230 | 231 | inline char get_char() 232 | { 233 | const char ret = ch; 234 | ch = next_char(); 235 | return ret; 236 | } 237 | 238 | template 239 | inline void o(T p) 240 | { 241 | static int stk[70], tp; 242 | if (p == 0) 243 | { 244 | putchar('0'); 245 | return; 246 | } 247 | if (p < 0) 248 | { 249 | p = -p; 250 | putchar('-'); 251 | } 252 | while (p) stk[++tp] = p % 10, p /= 10; 253 | while (tp) putchar(stk[tp--] + '0'); 254 | } 255 | 256 | template 257 | inline void od(T v) 258 | { 259 | static int stk[70], tp; 260 | tp = 0; 261 | if (fabs(v) < epslion / 10) 262 | { 263 | putchar('0'); 264 | if (precious > 0) 265 | { 266 | putchar('.'); 267 | for (int i = 0; i < precious; ++i) putchar('0'); 268 | } 269 | return; 270 | } 271 | else 272 | { 273 | if (v < 0) 274 | { 275 | v = -v; 276 | putchar('-'); 277 | } 278 | v += epslion / 2; 279 | T p = floor(v) + epslion / 10; 280 | while (fabs(p) > 1 - epslion) 281 | { 282 | stk[++tp] = fmod(p, 10), p /= 10; 283 | } 284 | while (tp) putchar(stk[tp--] + '0'); 285 | } 286 | if (precious == 0) return; 287 | putchar('.'); 288 | v -= floor(v); 289 | for (int i = 0; i < precious; ++i) 290 | { 291 | v *= 10; 292 | putchar((int)floor(v) + '0'); 293 | v = fmod(v, 1); 294 | } 295 | } 296 | 297 | /// Enhancement 298 | 299 | typedef void io_operator(io_s& v); 300 | typedef char* charptr; 301 | 302 | template 303 | inline io_s& operator>>(T& x) 304 | { 305 | if (numeric_limits::is_signed) 306 | rn(x); 307 | else 308 | run(x); 309 | return *this; 310 | } 311 | 312 | template 313 | inline io_s& operator<<(const T& x); 314 | 315 | inline io_s& operator<<(io_operator* v) 316 | { 317 | v(*this); 318 | return *this; 319 | } 320 | 321 | operator bool() const { return ok; } 322 | 323 | void set_precious(int x) 324 | { 325 | precious = x; 326 | epslion = pow(10, -x); 327 | } 328 | }; 329 | 330 | template <> 331 | inline io_s& io_s::operator>>(char*& x) 332 | { 333 | gets(x); 334 | return *this; 335 | } 336 | 337 | template <> 338 | inline io_s& io_s::operator>>(float& x) 339 | { 340 | rd(x); 341 | return *this; 342 | } 343 | 344 | template <> 345 | inline io_s& io_s::operator>>(double& x) 346 | { 347 | rd(x); 348 | return *this; 349 | } 350 | 351 | template <> 352 | inline io_s& io_s::operator>>(long double& x) 353 | { 354 | rd(x); 355 | return *this; 356 | } 357 | 358 | template <> 359 | inline void io_s::o(const char* p) 360 | { 361 | printf(p); 362 | } 363 | 364 | template <> 365 | inline void io_s::o(const char p) 366 | { 367 | putchar(p); 368 | } 369 | 370 | template <> 371 | inline void io_s::o(float p) 372 | { 373 | od(p); 374 | } 375 | 376 | template <> 377 | inline void io_s::o(double p) 378 | { 379 | od(p); 380 | } 381 | 382 | template <> 383 | inline void io_s::o(long double p) 384 | { 385 | od(p); 386 | } 387 | 388 | template 389 | inline io_s& io_s::operator<<(const T& x) 390 | { 391 | o(x); 392 | return *this; 393 | } 394 | 395 | inline void new_line(io_s& v) 396 | { 397 | v.o('\n'); 398 | } 399 | 400 | io_s::io_operator* nl = new_line; 401 | } // namespace io_impl 402 | 403 | using namespace io_impl; 404 | 405 | io_s io; 406 | 407 | namespace solution 408 | { 409 | 410 | #define lson (rt << 1) 411 | #define rson (rt << 1 | 1) 412 | #define Lson l, m, lson 413 | #define Rson m + 1, r, rson 414 | using ll = long long; 415 | const int INF = 1e9; 416 | const int Delt = 10000000; 417 | const int maxn = 2e5 + 5; 418 | int a[maxn]; 419 | int b[maxn]; 420 | struct Stree 421 | { 422 | int mx[maxn << 2]; 423 | 424 | inline void pushup(int rt) { mx[rt] = max(mx[lson], mx[rson]); } 425 | void build(int l, int r, int rt) 426 | { 427 | if (l == r) 428 | { 429 | mx[rt] = b[l]; 430 | return; 431 | } 432 | int m = l + r >> 1; 433 | build(Lson); 434 | build(Rson); 435 | pushup(rt); 436 | } 437 | void update(int l, int r, int rt, int p) 438 | { 439 | if (l == r) 440 | { 441 | mx[rt] = INF; 442 | return; 443 | } 444 | int m = l + r >> 1; 445 | if (p <= m) 446 | update(Lson, p); 447 | else 448 | update(Rson, p); 449 | pushup(rt); 450 | } 451 | int query(int l, int r, int rt, int L, int R) 452 | { 453 | if (L <= l && R >= r) return mx[rt]; 454 | int m = l + r >> 1; 455 | int ret = 0; 456 | if (L <= m) ret = max(ret, query(Lson, L, R)); 457 | if (m + 1 <= R) ret = max(ret, query(Rson, L, R)); 458 | return ret; 459 | } 460 | } st; 461 | 462 | bool vis[maxn]; 463 | int main() 464 | { 465 | // freopen("1.in", "r", stdin); 466 | // freopen("1.out", "w", stdout); 467 | int T; 468 | io >> T; 469 | while (T--) 470 | { 471 | int n, m; 472 | io >> n >> m; 473 | for (int i = 1; i <= n; i++) io >> a[i], b[a[i]] = i, vis[i] = 0; 474 | b[n + 1] = INF; 475 | st.build(1, n + 1, 1); 476 | 477 | int lastAns = 0; 478 | for (int i = 0, op; i < m; i++) 479 | { 480 | io >> op; 481 | int t1, t2, t3; 482 | if (op == 1) 483 | { 484 | io >> t1; 485 | int pos = t1 ^ lastAns; 486 | if (pos < 1 || pos > n) 487 | { 488 | *(int*)0 = 0; 489 | } 490 | if (vis[pos]) continue; 491 | st.update(1, n + 1, 1, a[pos]); 492 | vis[pos] = 1; 493 | } 494 | else 495 | { 496 | io >> t2 >> t3; 497 | int r = t2 ^ lastAns, k = t3 ^ lastAns; 498 | if (r < 1 || r > n || k < 1 || k > n) 499 | { 500 | *(int*)0 = 0; 501 | } 502 | int ans = -1; 503 | int L = k, R = n + 1; 504 | while (L <= R) 505 | { 506 | int mid = L + R >> 1; 507 | int res = st.query(1, n + 1, 1, k, mid); 508 | if (res > r) 509 | { 510 | ans = mid; 511 | R = mid - 1; 512 | } 513 | else 514 | L = mid + 1; 515 | } 516 | if (ans == -1) 517 | { 518 | *(int*)0 = 0; 519 | } 520 | io << ans << nl; 521 | lastAns = ans; 522 | } 523 | } 524 | } 525 | return 0; 526 | } 527 | } // namespace solution 528 | 529 | int main() 530 | { 531 | return solution::main(); 532 | } -------------------------------------------------------------------------------- /2019-Xuzhou/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #ifndef ONLINE_JUDGE 4 | #include 5 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); 6 | #else 7 | #define debug(...) 8 | #endif 9 | 10 | // base and base_digits must be consistent 11 | constexpr int base = 1000000000; 12 | constexpr int base_digits = 9; 13 | 14 | struct bigint 15 | { 16 | // value == 0 is represented by empty z 17 | vector z; // digits 18 | 19 | // sign == 1 <==> value >= 0 20 | // sign == -1 <==> value < 0 21 | int sign; 22 | 23 | bigint() : sign(1) {} 24 | 25 | bigint(long long v) { *this = v; } 26 | 27 | bigint& operator=(long long v) 28 | { 29 | sign = v < 0 ? -1 : 1; 30 | v *= sign; 31 | z.clear(); 32 | for (; v > 0; v = v / base) 33 | z.push_back((int)(v % base)); 34 | return *this; 35 | } 36 | 37 | bigint(const string& s) { read(s); } 38 | 39 | bigint& operator+=(const bigint& other) 40 | { 41 | if (sign == other.sign) 42 | { 43 | for (int i = 0, carry = 0; i < other.z.size() || carry; ++i) 44 | { 45 | if (i == z.size()) 46 | z.push_back(0); 47 | z[i] += carry + (i < other.z.size() ? other.z[i] : 0); 48 | carry = z[i] >= base; 49 | if (carry) 50 | z[i] -= base; 51 | } 52 | } 53 | else if (other != 0 /* prevent infinite loop */) 54 | { 55 | *this -= -other; 56 | } 57 | return *this; 58 | } 59 | 60 | friend bigint operator+(bigint a, const bigint& b) 61 | { 62 | return a += b; 63 | } 64 | 65 | bigint& operator-=(const bigint& other) 66 | { 67 | if (sign == other.sign) 68 | { 69 | if (sign == 1 && *this >= other || sign == -1 && *this <= other) 70 | { 71 | for (int i = 0, carry = 0; i < other.z.size() || carry; ++i) 72 | { 73 | z[i] -= carry + (i < other.z.size() ? other.z[i] : 0); 74 | carry = z[i] < 0; 75 | if (carry) 76 | z[i] += base; 77 | } 78 | trim(); 79 | } 80 | else 81 | { 82 | *this = other - *this; 83 | this->sign = -this->sign; 84 | } 85 | } 86 | else 87 | { 88 | *this += -other; 89 | } 90 | return *this; 91 | } 92 | 93 | friend bigint operator-(bigint a, const bigint& b) 94 | { 95 | return a -= b; 96 | } 97 | 98 | bigint& operator*=(int v) 99 | { 100 | if (v < 0) 101 | sign = -sign, v = -v; 102 | for (int i = 0, carry = 0; i < z.size() || carry; ++i) 103 | { 104 | if (i == z.size()) 105 | z.push_back(0); 106 | long long cur = (long long)z[i] * v + carry; 107 | carry = (int)(cur / base); 108 | z[i] = (int)(cur % base); 109 | } 110 | trim(); 111 | return *this; 112 | } 113 | 114 | bigint operator*(int v) const 115 | { 116 | return bigint(*this) *= v; 117 | } 118 | 119 | friend pair divmod(const bigint& a1, const bigint& b1) 120 | { 121 | int norm = base / (b1.z.back() + 1); 122 | bigint a = a1.abs() * norm; 123 | bigint b = b1.abs() * norm; 124 | bigint q, r; 125 | q.z.resize(a.z.size()); 126 | 127 | for (int i = (int)a.z.size() - 1; i >= 0; i--) 128 | { 129 | r *= base; 130 | r += a.z[i]; 131 | int s1 = b.z.size() < r.z.size() ? r.z[b.z.size()] : 0; 132 | int s2 = b.z.size() - 1 < r.z.size() ? r.z[b.z.size() - 1] : 0; 133 | int d = (int)(((long long)s1 * base + s2) / b.z.back()); 134 | r -= b * d; 135 | while (r < 0) 136 | r += b, --d; 137 | q.z[i] = d; 138 | } 139 | 140 | q.sign = a1.sign * b1.sign; 141 | r.sign = a1.sign; 142 | q.trim(); 143 | r.trim(); 144 | return {q, r / norm}; 145 | } 146 | 147 | friend bigint sqrt(const bigint& a1) 148 | { 149 | bigint a = a1; 150 | while (a.z.empty() || a.z.size() % 2 == 1) 151 | a.z.push_back(0); 152 | 153 | int n = a.z.size(); 154 | 155 | int firstDigit = (int)::sqrt((double)a.z[n - 1] * base + a.z[n - 2]); 156 | int norm = base / (firstDigit + 1); 157 | a *= norm; 158 | a *= norm; 159 | while (a.z.empty() || a.z.size() % 2 == 1) 160 | a.z.push_back(0); 161 | 162 | bigint r = (long long)a.z[n - 1] * base + a.z[n - 2]; 163 | firstDigit = (int)::sqrt((double)a.z[n - 1] * base + a.z[n - 2]); 164 | int q = firstDigit; 165 | bigint res; 166 | 167 | for (int j = n / 2 - 1; j >= 0; j--) 168 | { 169 | for (;; --q) 170 | { 171 | bigint r1 = (r - (res * 2 * base + q) * q) * base * base + (j > 0 ? (long long)a.z[2 * j - 1] * base + a.z[2 * j - 2] : 0); 172 | if (r1 >= 0) 173 | { 174 | r = r1; 175 | break; 176 | } 177 | } 178 | res *= base; 179 | res += q; 180 | 181 | if (j > 0) 182 | { 183 | int d1 = res.z.size() + 2 < r.z.size() ? r.z[res.z.size() + 2] : 0; 184 | int d2 = res.z.size() + 1 < r.z.size() ? r.z[res.z.size() + 1] : 0; 185 | int d3 = res.z.size() < r.z.size() ? r.z[res.z.size()] : 0; 186 | q = (int)(((long long)d1 * base * base + (long long)d2 * base + d3) / (firstDigit * 2)); 187 | } 188 | } 189 | 190 | res.trim(); 191 | return res / norm; 192 | } 193 | 194 | bigint operator/(const bigint& v) const 195 | { 196 | return divmod(*this, v).first; 197 | } 198 | 199 | bigint operator%(const bigint& v) const 200 | { 201 | return divmod(*this, v).second; 202 | } 203 | 204 | bigint& operator/=(int v) 205 | { 206 | if (v < 0) 207 | sign = -sign, v = -v; 208 | for (int i = (int)z.size() - 1, rem = 0; i >= 0; --i) 209 | { 210 | long long cur = z[i] + rem * (long long)base; 211 | z[i] = (int)(cur / v); 212 | rem = (int)(cur % v); 213 | } 214 | trim(); 215 | return *this; 216 | } 217 | 218 | bigint operator/(int v) const 219 | { 220 | return bigint(*this) /= v; 221 | } 222 | 223 | int operator%(int v) const 224 | { 225 | if (v < 0) 226 | v = -v; 227 | int m = 0; 228 | for (int i = (int)z.size() - 1; i >= 0; --i) 229 | m = (int)((z[i] + m * (long long)base) % v); 230 | return m * sign; 231 | } 232 | 233 | bigint& operator*=(const bigint& v) 234 | { 235 | *this = *this * v; 236 | return *this; 237 | } 238 | 239 | bigint& operator/=(const bigint& v) 240 | { 241 | *this = *this / v; 242 | return *this; 243 | } 244 | 245 | bool operator<(const bigint& v) const 246 | { 247 | if (sign != v.sign) 248 | return sign < v.sign; 249 | if (z.size() != v.z.size()) 250 | return z.size() * sign < v.z.size() * v.sign; 251 | for (int i = (int)z.size() - 1; i >= 0; i--) 252 | if (z[i] != v.z[i]) 253 | return z[i] * sign < v.z[i] * sign; 254 | return false; 255 | } 256 | 257 | bool operator>(const bigint& v) const 258 | { 259 | return v < *this; 260 | } 261 | 262 | bool operator<=(const bigint& v) const 263 | { 264 | return !(v < *this); 265 | } 266 | 267 | bool operator>=(const bigint& v) const 268 | { 269 | return !(*this < v); 270 | } 271 | 272 | bool operator==(const bigint& v) const 273 | { 274 | return !(*this < v) && !(v < *this); 275 | } 276 | 277 | bool operator!=(const bigint& v) const 278 | { 279 | return *this < v || v < *this; 280 | } 281 | 282 | void trim() 283 | { 284 | while (!z.empty() && z.back() == 0) 285 | z.pop_back(); 286 | if (z.empty()) 287 | sign = 1; 288 | } 289 | 290 | bool isZero() const 291 | { 292 | return z.empty(); 293 | } 294 | 295 | friend bigint operator-(bigint v) 296 | { 297 | if (!v.z.empty()) 298 | v.sign = -v.sign; 299 | return v; 300 | } 301 | 302 | bigint abs() const 303 | { 304 | return sign == 1 ? *this : -*this; 305 | } 306 | 307 | long long longValue() const 308 | { 309 | long long res = 0; 310 | for (int i = (int)z.size() - 1; i >= 0; i--) 311 | res = res * base + z[i]; 312 | return res * sign; 313 | } 314 | 315 | friend bigint gcd(const bigint& a, const bigint& b) 316 | { 317 | return b.isZero() ? a : gcd(b, a % b); 318 | } 319 | 320 | friend bigint lcm(const bigint& a, const bigint& b) 321 | { 322 | return a / gcd(a, b) * b; 323 | } 324 | 325 | void read(const string& s) 326 | { 327 | sign = 1; 328 | z.clear(); 329 | int pos = 0; 330 | while (pos < s.size() && (s[pos] == '-' || s[pos] == '+')) 331 | { 332 | if (s[pos] == '-') 333 | sign = -sign; 334 | ++pos; 335 | } 336 | for (int i = (int)s.size() - 1; i >= pos; i -= base_digits) 337 | { 338 | int x = 0; 339 | for (int j = max(pos, i - base_digits + 1); j <= i; j++) 340 | x = x * 10 + s[j] - '0'; 341 | z.push_back(x); 342 | } 343 | trim(); 344 | } 345 | 346 | friend istream& operator>>(istream& stream, bigint& v) 347 | { 348 | string s; 349 | stream >> s; 350 | v.read(s); 351 | return stream; 352 | } 353 | 354 | friend ostream& operator<<(ostream& stream, const bigint& v) 355 | { 356 | if (v.sign == -1) 357 | stream << '-'; 358 | stream << (v.z.empty() ? 0 : v.z.back()); 359 | for (int i = (int)v.z.size() - 2; i >= 0; --i) 360 | stream << setw(base_digits) << setfill('0') << v.z[i]; 361 | return stream; 362 | } 363 | 364 | static vector convert_base(const vector& a, int old_digits, int new_digits) 365 | { 366 | vector p(max(old_digits, new_digits) + 1); 367 | p[0] = 1; 368 | for (int i = 1; i < p.size(); i++) 369 | p[i] = p[i - 1] * 10; 370 | vector res; 371 | long long cur = 0; 372 | int cur_digits = 0; 373 | for (int v : a) 374 | { 375 | cur += v * p[cur_digits]; 376 | cur_digits += old_digits; 377 | while (cur_digits >= new_digits) 378 | { 379 | res.push_back(int(cur % p[new_digits])); 380 | cur /= p[new_digits]; 381 | cur_digits -= new_digits; 382 | } 383 | } 384 | res.push_back((int)cur); 385 | while (!res.empty() && res.back() == 0) 386 | res.pop_back(); 387 | return res; 388 | } 389 | 390 | typedef vector vll; 391 | 392 | static vll karatsubaMultiply(const vll& a, const vll& b) 393 | { 394 | int n = a.size(); 395 | vll res(n + n); 396 | if (n <= 32) 397 | { 398 | for (int i = 0; i < n; i++) 399 | for (int j = 0; j < n; j++) 400 | res[i + j] += a[i] * b[j]; 401 | return res; 402 | } 403 | 404 | int k = n >> 1; 405 | vll a1(a.begin(), a.begin() + k); 406 | vll a2(a.begin() + k, a.end()); 407 | vll b1(b.begin(), b.begin() + k); 408 | vll b2(b.begin() + k, b.end()); 409 | 410 | vll a1b1 = karatsubaMultiply(a1, b1); 411 | vll a2b2 = karatsubaMultiply(a2, b2); 412 | 413 | for (int i = 0; i < k; i++) 414 | a2[i] += a1[i]; 415 | for (int i = 0; i < k; i++) 416 | b2[i] += b1[i]; 417 | 418 | vll r = karatsubaMultiply(a2, b2); 419 | for (int i = 0; i < a1b1.size(); i++) 420 | r[i] -= a1b1[i]; 421 | for (int i = 0; i < a2b2.size(); i++) 422 | r[i] -= a2b2[i]; 423 | 424 | for (int i = 0; i < r.size(); i++) 425 | res[i + k] += r[i]; 426 | for (int i = 0; i < a1b1.size(); i++) 427 | res[i] += a1b1[i]; 428 | for (int i = 0; i < a2b2.size(); i++) 429 | res[i + n] += a2b2[i]; 430 | return res; 431 | } 432 | 433 | bigint operator*(const bigint& v) const 434 | { 435 | vector a6 = convert_base(this->z, base_digits, 6); 436 | vector b6 = convert_base(v.z, base_digits, 6); 437 | vll a(a6.begin(), a6.end()); 438 | vll b(b6.begin(), b6.end()); 439 | while (a.size() < b.size()) 440 | a.push_back(0); 441 | while (b.size() < a.size()) 442 | b.push_back(0); 443 | while (a.size() & (a.size() - 1)) 444 | a.push_back(0), b.push_back(0); 445 | vll c = karatsubaMultiply(a, b); 446 | bigint res; 447 | res.sign = sign * v.sign; 448 | for (int i = 0, carry = 0; i < c.size(); i++) 449 | { 450 | long long cur = c[i] + carry; 451 | res.z.push_back((int)(cur % 1000000)); 452 | carry = (int)(cur / 1000000); 453 | } 454 | res.z = convert_base(res.z, 6, base_digits); 455 | res.trim(); 456 | return res; 457 | } 458 | }; 459 | 460 | using ll = bigint; 461 | 462 | ll exgcd(ll a, ll b, ll& x, ll& y) 463 | { 464 | ll d = a; 465 | if (b != 0) 466 | d = exgcd(b, a % b, y, x), y -= x * (a / b); 467 | else 468 | x = 1, y = 0; 469 | return d; 470 | } 471 | 472 | bool excrt(ll r[], ll m[], int n, ll& re, ll& mo) 473 | { 474 | ll x, y; 475 | mo = m[0], re = r[0]; 476 | for (int i = 1; i < n; i++) 477 | { 478 | ll d = exgcd(mo, m[i], x, y); 479 | if ((r[i] - re) % d != 0) return 0; 480 | x = (r[i] - re) / d * x % (m[i] / d); 481 | re += x * mo; 482 | mo = mo / d * m[i]; 483 | re = re % mo; 484 | } 485 | re = (re + mo) % mo; 486 | return 1; 487 | } 488 | 489 | ll r[10], m[10]; 490 | ll f[80]; 491 | int main() 492 | { 493 | f[0] = 1, f[1] = 1; 494 | for (int i = 2; i < 80; i++) f[i] = f[i - 1] + f[i - 2]; 495 | // for (int i = 2; i < 80; i++) cout << f[i] << endl; 496 | int k; 497 | cin >> k; 498 | for (int i = 0; i < k; i++) cin >> m[i] >> r[i]; 499 | ll re, mo; 500 | if (!excrt(r, m, k, re, mo)) 501 | { 502 | puts("Tankernb!"); 503 | exit(0); 504 | } 505 | if (re == 0) re += mo; 506 | assert(re != 1); 507 | assert(re <= 1e15); 508 | if (binary_search(f, f + 80, re)) 509 | { 510 | puts("Lbnb!"); 511 | } 512 | else 513 | { 514 | puts("Zgxnb!"); 515 | } 516 | } 517 | --------------------------------------------------------------------------------