├── .gitignore ├── BZOJ ├── BZOJ1192.cpp ├── BZOJ2001.cpp ├── BZOJ1000.cpp ├── BZOJ4001.cpp ├── BZOJ1968.cpp ├── BZOJ2659.cpp ├── BZOJ2467.cpp ├── BZOJ1213.py ├── BZOJ3305.cpp ├── BZOJ1022.cpp ├── BZOJ2463.cpp ├── BZOJ1008.cpp ├── BZOJ1257.cpp ├── BZOJ1258.cpp ├── BZOJ2456.cpp ├── BZOJ4915.cpp ├── BZOJ1045.cpp ├── BZOJ1965.cpp ├── BZOJ5105.cpp ├── BZOJ1011.cpp ├── BZOJ4475.cpp ├── BZOJ1263.cpp ├── BZOJ2005.cpp ├── BZOJ3191.cpp ├── BZOJ1088.cpp ├── BZOJ1303.cpp ├── BZOJ4917.cpp ├── BZOJ1925.cpp ├── BZOJ1024.cpp ├── BZOJ1053.cpp ├── BZOJ1083.cpp ├── BZOJ1010.cpp ├── BZOJ2190.cpp ├── BZOJ1228.cpp ├── BZOJ2431.cpp ├── BZOJ1218.cpp ├── BZOJ1854.cpp ├── BZOJ2761.cpp ├── BZOJ2822.cpp ├── BZOJ2748.cpp ├── BZOJ2705.cpp ├── BZOJ2257.cpp ├── BZOJ1041.cpp ├── BZOJ1222.cpp ├── BZOJ1293.cpp ├── BZOJ1076.cpp ├── BZOJ1081.cpp ├── BZOJ1059.cpp ├── BZOJ1090.cpp ├── BZOJ1029.cpp ├── BZOJ3105.cpp ├── BZOJ1042.cpp ├── BZOJ1966.cpp ├── BZOJ4809.cpp ├── BZOJ3108.cpp ├── BZOJ1202.cpp ├── BZOJ1923.cpp ├── BZOJ1037.cpp ├── BZOJ2819.cpp ├── BZOJ3209.cpp ├── BZOJ1260.cpp ├── BZOJ1025.cpp ├── BZOJ4517.cpp ├── BZOJ1996.cpp ├── BZOJ1408.cpp ├── BZOJ1028.cpp ├── BZOJ1149.cpp ├── BZOJ2875.cpp ├── BZOJ5106.cpp ├── BZOJ1212.cpp ├── BZOJ1970.cpp ├── BZOJ3670.cpp ├── BZOJ1012.cpp ├── BZOJ3925.cpp ├── BZOJ1188.cpp ├── BZOJ1224.cpp ├── BZOJ1562.cpp ├── BZOJ3997.cpp ├── BZOJ1026.cpp ├── BZOJ1304.cpp ├── BZOJ4403.cpp ├── BZOJ3233.cpp ├── BZOJ1878.cpp ├── BZOJ1007.cpp ├── BZOJ4832.cpp ├── BZOJ4807.cpp ├── BZOJ2154.cpp ├── BZOJ2818.cpp ├── BZOJ4590.cpp ├── BZOJ1485.cpp ├── BZOJ3207.cpp ├── BZOJ2751.cpp ├── BZOJ1416.cpp ├── BZOJ2435.cpp ├── BZOJ4195.cpp ├── BZOJ2656.cpp ├── BZOJ1486.cpp ├── BZOJ2301.cpp ├── BZOJ1013.cpp ├── BZOJ3884.cpp ├── BZOJ4591.cpp ├── BZOJ5073.cpp ├── BZOJ1491.cpp ├── BZOJ2038.cpp ├── BZOJ1911.cpp ├── BZOJ2760.cpp ├── BZOJ1005.cpp ├── BZOJ1031.cpp ├── BZOJ1217.cpp ├── BZOJ4805.cpp ├── BZOJ4813.cpp ├── BZOJ1015.cpp ├── BZOJ2160.cpp ├── BZOJ2152.cpp ├── BZOJ1087.cpp ├── BZOJ4804.cpp ├── BZOJ3994.cpp ├── BZOJ1297.cpp ├── BZOJ2568.cpp ├── BZOJ1002.cpp ├── BZOJ4443.cpp ├── BZOJ1498.cpp ├── BZOJ2118.cpp ├── BZOJ1237.cpp ├── BZOJ1050.cpp ├── BZOJ1833.cpp ├── BZOJ4921.cpp ├── BZOJ1857.cpp ├── BZOJ5118.cpp ├── BZOJ2956.cpp ├── BZOJ4806.cpp ├── BZOJ4922.cpp ├── BZOJ1009.cpp ├── BZOJ2326.cpp ├── BZOJ5000.cpp ├── BZOJ1055.cpp ├── BZOJ2227.cpp ├── BZOJ2338.cpp ├── BZOJ1295.cpp ├── BZOJ4818.cpp ├── BZOJ1078.cpp ├── BZOJ1875.cpp ├── BZOJ4916.cpp ├── BZOJ1004.cpp ├── BZOJ1415.cpp ├── BZOJ4600.cpp ├── BZOJ1103.cpp ├── BZOJ3680.cpp ├── BZOJ2323.cpp ├── BZOJ1058.cpp ├── BZOJ4518.cpp ├── BZOJ1084.cpp ├── BZOJ3140.cpp ├── BZOJ1030.cpp ├── BZOJ1051.cpp ├── BZOJ1048.cpp ├── BZOJ2330.cpp ├── BZOJ5015.cpp ├── BZOJ1057.cpp ├── BZOJ2006.cpp ├── BZOJ3208.cpp ├── BZOJ1047.cpp └── BZOJ3143.cpp ├── Luogu ├── P1720.cpp ├── P3150.cpp ├── P1319.cpp ├── P1482.cpp ├── P1615.cpp ├── P1590.cpp ├── P2676.cpp ├── P1554.cpp ├── P1151.cpp ├── P2084.cpp ├── P2911.cpp ├── P1076.cpp ├── P3455.cpp ├── P3143.cpp ├── P2473.cpp ├── P3396.cpp ├── P1129.cpp ├── P2522.cpp ├── P2764.cpp ├── P3199.cpp ├── P2602.cpp ├── P3343.cpp ├── P2606.cpp ├── P1822.cpp ├── P1963.cpp ├── P2698.cpp ├── P2709.cpp ├── P2765.cpp ├── P3317.cpp ├── P2860.cpp ├── P2444.cpp ├── P3808.cpp ├── P2571.cpp └── P3709.cpp └── JCYZOJ ├── 1000.cpp ├── 1037.cpp ├── 1001.cpp ├── 1006.cpp └── 1042.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | temp 2 | *.swo 3 | *.swp 4 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1192.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int x, t = 1; 4 | scanf("%d", &x); 5 | while (x /= 2) ++t; 6 | return printf("%d\n", t), 0; 7 | } 8 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const int N = 20050; 6 | const int M = 50050; 7 | 8 | int main() { 9 | } 10 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1000.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int a,b; 6 | cin >> a >> b; 7 | cout << a+b << endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | double n; 5 | scanf("%lf", &n); 6 | printf("%.9lf", n * (n + 1) / 2 / (2 * n - 1)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1968.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int n, ans = 0; 4 | scanf("%d", &n); 5 | for (int i = 1; i <= n; ++i) ans += n / i; 6 | printf("%d\n", ans); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /Luogu/P1720.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | int n; 5 | scanf("%d", &n); 6 | long long a = 0, b = 1; 7 | while (n--) 8 | std::swap(a += b, b); 9 | printf("%lld.00\n", a); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Luogu/P3150.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | int n; 5 | scanf("%d", &n); 6 | while (n--) { 7 | int x; 8 | scanf("%d", &x); 9 | puts(x & 1 ? "zs wins" : "pb wins"); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2659.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | int main() { 4 | LL p, q; 5 | scanf("%lld%lld", &p, &q); 6 | if (p == q) printf("%lld\n", (p * p - 1) / 4); 7 | else printf("%lld\n", (p - 1) * (q - 1) / 4); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2467.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int T, n; 4 | scanf("%d", &T); 5 | while (T--) { 6 | scanf("%d", &n); 7 | int ans = 4 * n; 8 | while (--n) ans = ans * 5 % 2007; 9 | printf("%d\n", ans); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1213.py: -------------------------------------------------------------------------------- 1 | m, n, l, r = int(input()), int(input()), 0, 1 2 | while r**m < n: 3 | l, r = r, r * 2 4 | while l + 1 < r: 5 | mid = (l + r) // 2 6 | if mid**m < n: 7 | l = mid 8 | else: 9 | r = mid 10 | 11 | if r**m < n: 12 | print(r) 13 | else: 14 | print(l) 15 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3305.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long LL; 4 | const int mod = 1000000009; 5 | int main() { 6 | int n, ans = 1; 7 | scanf("%d", &n); 8 | for (int i = 1; i < n; ++i) 9 | ans = (LL)ans * (2 * i + 1) % mod; 10 | printf("%d\n", ans); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Luogu/P1319.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int n = 0; 4 | scanf("%d", &n); 5 | for (int i = 0, j = 0, t; j < n * n; ++i, t ^= 1) { 6 | scanf("%d", &t); 7 | while (t--) { 8 | printf("%d", i % 2); 9 | if (!((++j) % n)) puts(""); 10 | } 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Luogu/P1482.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } 4 | int main() { 5 | int a, b, c, d; 6 | scanf("%d/%d%d/%d", &a, &b, &c, &d); 7 | a *= c; 8 | b *= d; 9 | printf("%d %d", b / gcd(a, b), a / gcd(a, b)); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Luogu/P1615.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | int a, b, c, d, e, f, k; 5 | scanf("%d:%d:%d", &a, &b, &c); 6 | scanf("%d:%d:%d", &d, &e, &f); 7 | scanf("%d", &k); 8 | printf("%lld\n", (long long)(((d - a) * 60 + (e - b)) * 60 + (f - c)) * k); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1022.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int T, n, x, y, t; 4 | for (scanf("%d", &T); T; --T) { 5 | for (scanf("%d", &n), y = t = 0; n; --n) { 6 | scanf("%d", &x); 7 | y ^= x; 8 | t |= x > 1; 9 | } 10 | puts((!!y ^ t) ? "Brother" : "John"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Luogu/P1590.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | char a[30]; 4 | int main() { 5 | int T; 6 | scanf("%d", &T); 7 | while (T--) { 8 | scanf("%s", a); 9 | int b = 0, l = strlen(a); 10 | for (int i = 0; i < l; ++i) 11 | b = b * 9 + a[i] - '0' - (a[i] > '7'); 12 | printf("%d\n", b); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Luogu/P2676.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int A[20005]; 4 | int main() { 5 | int n, b; 6 | scanf("%d%d", &n, &b); 7 | for (int i = 0; i < n; ++i) scanf("%d", &A[i]); 8 | std::sort(A, A + n); 9 | for (int i = n - 1; ~i; --i) if ((b -= A[i]) <= 0) { 10 | printf("%d\n", n - i); 11 | break; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Luogu/P1554.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int A[10]; 4 | void get(int x) { 5 | if (x == 0) return; 6 | get(x / 10); ++A[x % 10]; 7 | } 8 | int main() { 9 | int M, N; 10 | scanf("%d%d", &M, &N); 11 | for (int i = M; i <= N; ++i) 12 | get(i); 13 | for (int i = 0; i < 10; ++i) 14 | printf("%d ", A[i]); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /JCYZOJ/1000.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: JCYZOJ 1000 3 | * Author: Rqy 4 | * Date: 2018 Feb 13 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | int main() { 9 | int a, b; 10 | scanf("%d%d", &a, &b); 11 | printf("%d\n", a + b); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2463.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2463 3 | * Author: Rqy 4 | * Date: 2018 Feb 28 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | int main() { 9 | int n; 10 | while (~scanf("%d", &n) && n) puts(n & 1 ? "Bob" : "Alice"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Luogu/P1151.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int k; 4 | scanf("%d", &k); 5 | bool ok = false; 6 | for (int i = 10000; i <= 30000; ++i) { 7 | int a = i / 100, b = i / 10 % 1000, c = i % 1000; 8 | if (a % k == 0 && b % k == 0 && c % k == 0) { 9 | printf("%d\n", i); 10 | ok = true; 11 | } 12 | } 13 | if (!ok) puts("No"); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Luogu/P2084.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int m; 5 | char n[1005]; 6 | int main() { 7 | scanf("%d%s", &m, n); 8 | int l = strlen(n); 9 | bool fst = true; 10 | for (int i = 0; i < l; ++i) if (n[i] != '0') { 11 | if (fst) fst = false; 12 | else printf("+"); 13 | printf("%c*%d^%d", n[i], m, l - i - 1); 14 | } 15 | if (fst) printf("0"); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1008.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int mod = 100003; 3 | typedef long long LL; 4 | LL pow_mod(LL a, LL b) { 5 | LL ans = 1; 6 | a %= mod; 7 | for (; b; b >>= 1, a = a * a % mod) 8 | if (b & 1) ans = ans * a % mod; 9 | return ans; 10 | } 11 | int main() { 12 | LL n, m; 13 | scanf("%lld%lld", &m, &n); 14 | printf("%lld\n", 15 | (pow_mod(m, n) - pow_mod(m - 1, n - 1) * m % mod + mod) % mod); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1257.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | inline LL get(LL t) { 4 | return t * (t + 1) / 2; 5 | } 6 | int main() { 7 | LL n, k; 8 | scanf("%lld%lld", &n, &k); 9 | LL ans = k * n; 10 | if (n > k) n = k; 11 | for (LL i = 1, end; i <= n; i = end + 1) { 12 | end = k / (k / i); 13 | if (end > n) end = n; 14 | ans -= k / i * (get(end) - get(i - 1)); 15 | } 16 | printf("%lld\n", ans); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1258.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int f[3], n; 4 | char s[1000]; 5 | void outPut() { 6 | s[n] = '\0'; 7 | printf("%s4\n", s); 8 | } 9 | int main() { 10 | scanf("%s", s); 11 | n = strlen(s); 12 | if (s[n - 1] == '4') { 13 | s[n - 1] = '\0'; 14 | printf("%s1\n%s2\n%s3\n", s, s, s); 15 | return 0; 16 | } 17 | int a; 18 | while (--n) 19 | if (!f[a = s[n] - '1']) { 20 | outPut(); 21 | f[a] = 1; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2456.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2456 3 | * Author: Rqy 4 | * Date: 2018 Feb 28 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | int main() { 10 | int n, a = 0, b = 0, x; 11 | scanf("%d", &n); 12 | while (n--) { 13 | scanf("%d", &x); 14 | x == a ? ++b : b ? --b : (a = x, ++b); 15 | } 16 | printf("%d\n", a); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4915.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4915 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | int main() { 11 | LL l, r; 12 | scanf("%lld%lld", &l, &r); 13 | if (!l) ++l; 14 | printf("4\n%lld\n", std::max(r / 29 - l + 1, 0LL) + std::max(r / 11 - l + 1, 0LL)); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1045.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | long long s[1000005]; 4 | int main() { 5 | int n, a; 6 | scanf("%d", &n); 7 | scanf("%lld", &s[0]); 8 | for (int i = 1; i < n; ++i) { 9 | scanf("%lld", &s[i]); 10 | s[i] += s[i - 1]; 11 | } 12 | for (int i = 0; i < n; ++i) s[i] = s[i] - s[n - 1] / n * (i + 1); 13 | std::sort(s, s + n); 14 | long long ans = 0; 15 | for (int i = 0; i < n; ++i) ans += std::abs(s[i] - s[(n + 1) / 2]); 16 | return !printf("%lld\n", ans); 17 | } 18 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1965.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | LL l, m, n, t2; 4 | const LL t = 100000; 5 | LL mul(LL a, LL b) { 6 | LL a1 = a % t, b1 = b % t, a2 = a / t, b2 = b / t; 7 | return (a1 * b1 % n + (a1 * b2 + b1 * a2) % n * t % n + a2 * b2 % n * t2 % n) % n; 8 | } 9 | int main() { 10 | scanf("%lld%lld%lld", &n, &m, &l); 11 | ++n; 12 | t2 = t * t % n; 13 | LL x = n / 2 + 1; 14 | for (; m; m >>= 1, x = mul(x, x)) 15 | if (m & 1) l = mul(l, x); 16 | printf("%lld\n", l); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Luogu/P2911.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int A[100]; 4 | int main() { 5 | int n, m, l; 6 | scanf("%d%d%d", &n, &m, &l); 7 | for (int i = 1; i <= n; ++i) 8 | for (int j = 1; j <= m; ++j) 9 | for (int k = 1; k <= l; ++k) 10 | ++A[i + j + k]; 11 | int ans = 0; 12 | for (int i = 1; i <= n + m + l; ++i) 13 | ans = std::max(ans, A[i]); 14 | for (int i = 1; i <= n + m + l; ++i) 15 | if (A[i] == ans) { 16 | printf("%d\n", i); 17 | break; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /BZOJ/BZOJ5105.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ5105 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | LL gcd(LL a, LL b) { while (b) std::swap(b, a %= b); return a; } 11 | inline LL lcm(LL a, LL b) { return a / gcd(a, b) * b; } 12 | int main() { 13 | int a, b, c; scanf("%d%d%d", &a, &b, &c); printf("%lld\n", lcm(lcm(a, b), c)); 14 | } 15 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1011.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | double M[100050], S[100050]; 3 | int main() { 4 | int n, l = 1000; 5 | double a; 6 | scanf("%d%lf", &n, &a); 7 | for (int i = 1; i <= n; ++i) { 8 | scanf("%lf", &M[i]); 9 | int j = (int)(a * i + 1e-6); 10 | if (i > l) 11 | printf("%lf\n", M[i] * S[j] / (i - j * 0.5)); 12 | else { 13 | double ans = 0.0; 14 | while (j) 15 | ans += M[i] * M[j] / (i - j), j--; 16 | printf("%lf\n", ans); 17 | } 18 | S[i] = M[i] + S[i - 1]; 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /JCYZOJ/1037.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: JCYZOJ 1037 3 | * Author: Rqy 4 | * Date: 2018 Feb 13 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | void solve(int n, char A, char B, char C) { 9 | if (!n) return; 10 | solve(n - 1, A, C, B); 11 | printf("%d from %c to %c\n", n, A, C); 12 | solve(n - 1, B, A, C); 13 | } 14 | int main() { 15 | int n; 16 | scanf("%d", &n); 17 | solve(n, 'A', 'B', 'C'); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4475.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4475 3 | * Author: Rqy 4 | * Date: 2018 Feb 22 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | typedef long long LL; 9 | const int mod = 1000000007; 10 | int main() { 11 | int n, k; 12 | scanf("%d%d", &n, &k); 13 | int ans = 1, x = 2; 14 | for (n = (LL)n * k % (mod - 1); n; n >>= 1, x = (LL)x * x % mod) 15 | if (n & 1) ans = (LL)ans * x % mod; 16 | return printf("%d\n", ans) & 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1263.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int len, ans[5050]; 3 | void mul(int x) { 4 | int i, c; 5 | for (i = 0, c = 0; i < len || c; ++i) { 6 | c = (ans[i] = ans[i] * x + c) / 10; 7 | ans[i] %= 10; 8 | } 9 | len = i; 10 | } 11 | const int e[] = {0, 0, 0, 0, 4, 6, 9, 12, 18, 27}; 12 | int main() { 13 | int n; 14 | scanf("%d", &n); 15 | len = 1, ans[0] = 1; 16 | while (n >= 10) { 17 | mul(9); 18 | n -= 6; 19 | } 20 | mul(e[n]); 21 | printf("%d\n", len); 22 | for (int i = len - 1; ~i && i >= len - 100; --i) 23 | printf("%d", ans[i]); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long LL; 5 | const int N = 100050; 6 | int phi[N]; 7 | void getPhi() { 8 | for (int i = 1; i < N; ++i) phi[i] = i; 9 | for (int i = 2; i < N; ++i) if (phi[i] == i) 10 | for (int j = i; j < N; j += i) 11 | phi[j] -= phi[j] / i; 12 | } 13 | int main() { 14 | int n, m; 15 | scanf("%d%d", &n, &m); 16 | LL ans = 0; 17 | getPhi(); 18 | for (int i = 1; i <= n && i <= m; ++i) 19 | ans += (LL)phi[i] * (n / i) * (m / i); 20 | printf("%lld\n", ans * 2 - (LL)n * m); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3191.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 60; 4 | int a[N]; 5 | double f[N][N]; 6 | int main() { 7 | int n, m; 8 | scanf("%d%d", &n, &m); 9 | double p = 1.0 / m; 10 | for (int i = 0; i < m; ++i) scanf("%d", &a[i]), --a[i]; 11 | f[1][0] = 1.0; 12 | for (int i = 2; i <= n; ++i) 13 | for (int j = 0; j < m; ++j) 14 | for (int l = a[j] % i + 1, k = 0; k < i - 1; ++k) 15 | f[i][(l + k) % i] += p * f[i - 1][k]; 16 | for (int i = 0; i < n; ++i) { 17 | if (i) putchar(' '); 18 | printf("%.2lf%%", f[n][i] * 100.0); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1088.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int a[10005], n; 4 | int main() { 5 | int i, j; 6 | scanf("%d", &n); 7 | for (i = 0; i < n; ++i) scanf("%d", &a[i]); 8 | int ans = 0; 9 | for (i = std::max(a[0] - 1, 0); i <= a[0] && i <= 1; ++i) { 10 | int t1 = i, t2 = a[0] - t1; 11 | bool ok = true; 12 | for (j = 1; j < n; ++j) { 13 | int t3 = a[j] - t2 - t1; 14 | if (t3 < 0 || t3 > 1) { 15 | ok = false; 16 | break; 17 | } 18 | t1 = t2; 19 | t2 = t3; 20 | } 21 | if (ok && !t2) ++ans; 22 | } 23 | printf("%d", ans); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1303.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int N = 100050; 3 | int A[N]; 4 | int _T[N * 2]; 5 | int *T = _T + N; 6 | int main() { 7 | int n, b, p; 8 | scanf("%d%d", &n, &b); 9 | for (int i = 0; i < n; ++i) { 10 | scanf("%d", &A[i]); 11 | if (A[i] == b) p = i; 12 | } 13 | int l = 0; 14 | ++T[0]; 15 | for (int i = p - 1; ~i; --i) { 16 | if (A[i] > b) --l; 17 | else ++l; 18 | ++T[l]; 19 | } 20 | l = 0; 21 | int ans = T[0]; 22 | for (int i = p + 1; i < n; ++i) { 23 | if (A[i] < b) --l; 24 | else ++l; 25 | ans += T[l]; 26 | } 27 | printf("%d\n", ans); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4917.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4917 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | typedef unsigned int UI; 9 | int main() { 10 | int Q; 11 | scanf("%d", &Q); 12 | while (Q--) { 13 | UI t; 14 | scanf("%u", &t); 15 | t *= 4294901761u; 16 | t ^= (t >> 11); t ^= (t >> 22); 17 | t *= 954437177u; 18 | t ^= (t >> 6); t ^= (t >> 12); t ^= (t >> 24); 19 | t *= 3222273025u; 20 | printf("%u\n", t); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1925.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | const int N = 5000; 4 | int n, p; 5 | int A[N], B[N]; 6 | int C[N]; 7 | int main() { 8 | scanf("%d%d", &n, &p); 9 | C[0] = 1; 10 | A[0] = A[1] = 1; 11 | B[0] = 1; 12 | B[1] = 0; 13 | for (int i = 2; i <= n; ++i) { 14 | A[i] = B[i] = 0; 15 | for (int j = i - 2; j >= 0; --j) 16 | B[i] = (B[i] + (LL)B[j] * A[i - j - 1] % p * (j ? C[j] = (C[j] + C[j - 1]) % p : 1)) % p; 17 | for (int j = i - 2; j; --j) 18 | A[i] = (A[i] + (LL)A[j] * A[i - j - 1] % p * C[j] % p) % p; 19 | C[i - 1] = 1; 20 | } 21 | printf("%d\n", 2 * (A[n] + B[n]) % p); 22 | } 23 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1024.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::max; 4 | using std::min; 5 | double dfs(double t, int n) { 6 | if (n == 1) { 7 | if (t > 1.0) return t; 8 | return 1.0 / t; 9 | } 10 | double ans = 1e15; 11 | for (int i = 1; i <= n / 2; ++i) { 12 | double ans1 = max(dfs(t / n * i, i), dfs(t / n * (n - i), n - i)); 13 | double ans2 = max(dfs(t * n / i, i), dfs(t * n / (n - i), n - i)); 14 | ans = min(ans, min(ans1, ans2)); 15 | } 16 | return ans; 17 | } 18 | int main() { 19 | double x, y; 20 | int n; 21 | scanf("%lf%lf%d", &x, &y, &n); 22 | printf("%.6lf\n", dfs(x / y, n)); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1053.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | int n; 4 | int ans; 5 | int divnum; 6 | const int prime[20] = { 7 | 2, 3, 5, 7, 11, 8 | 13, 17, 19, 23, 29, 9 | 31, 37, 41, 43, 47, 10 | 53, 59, 61, 67, 71 11 | }; 12 | void dfs(int x, int y, int la, int d) { 13 | if (d > divnum || d == divnum && ans > y) { 14 | ans = y; 15 | divnum = d; 16 | } 17 | if (x >= 20) return; 18 | for (int i = 0, j = 1; i <= la && n / j >= y; ++i, j *= prime[x]) 19 | dfs(x + 1, y * j, i, d * (i + 1)); 20 | } 21 | int main() { 22 | scanf("%d", &n); 23 | divnum = 0; 24 | dfs(0, 1, 10000, 1); 25 | printf("%d\n", ans); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1083.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int N = 300005; 5 | struct Edge{ 6 | int x, y, w; 7 | bool operator<(const Edge &e)const{ return w < e.w; } 8 | }e[N]; 9 | int n, m, f[N], a; 10 | int F(int &x) { return x = (~f[x] ? F(f[x]) : x); } 11 | int U(int x, int y) { return F(x) == F(y) ? 0 : (f[x] = y, 1); } 12 | int main() { 13 | scanf("%d%d", &n, &m); 14 | for (int i = (std::fill(f, f + n, -1), 0); i < m; ++i) 15 | scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].w); 16 | std::sort(e, e + m); 17 | for (int i = a = 0; i < m; ++i) a = std::max(a, U(e[i].x, e[i].y)* e[i].w); 18 | return printf("%d %d\n", n - 1, a) & 0; 19 | } 20 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1010.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int N = 50050; 3 | typedef long long LL; 4 | LL C[N], f[N]; 5 | int L; 6 | inline LL calc(int j, int i) { 7 | return f[j] + (C[i] - C[j] - L) * (C[i] - C[j] - L); 8 | } 9 | int main() { 10 | int n, i, j; 11 | scanf("%d%d", &n, &L); 12 | ++L; 13 | C[0] = 0; 14 | for (i = 1; i <= n; ++i) { 15 | scanf("%lld", &C[i]); 16 | ++C[i]; 17 | C[i] += C[i - 1]; 18 | } 19 | f[0] = 0; 20 | for (i = 1, j = 0; i <= n; ++i) { 21 | f[i] = calc(j, i); 22 | for (int q = j + 1; q < i; ++q) 23 | if (f[i] >= calc(q, i)) { 24 | j = q; 25 | f[i] = calc(q, i); 26 | } 27 | } 28 | printf("%lld", f[n]); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2190.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2190 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 40050; 11 | int phi[N]; 12 | int main() { 13 | int n; 14 | scanf("%d", &n); 15 | for (int i = 1; i < n; ++i) phi[i] = i; 16 | for (int i = 2; i < n; ++i) if (phi[i] == i) 17 | for (int j = i; j < n; j += i) 18 | phi[j] = phi[j] / i * (i - 1); 19 | LL ans = 0; 20 | for (int i = 1; i < n; ++i) ans += phi[i]; 21 | printf("%lld\n", ans * 2 + 1); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1228.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef unsigned int UI; 5 | bool t[100]; 6 | int lowbitnum[1 << 16]; 7 | inline int lbn(UI x) { 8 | return x ? x & 65535 ? lowbitnum[x & 65535] : lowbitnum[x >> 16] + 16 : 0; 9 | } 10 | int main() { 11 | int n, i, j, k, s; 12 | for (i = 0; i < 16; ++i) 13 | for (k = (j = 1 << i) << 1; j < 65536; j += k) 14 | lowbitnum[j] = i; 15 | int T; 16 | scanf("%d", &T); 17 | while (T--) { 18 | scanf("%d", &n); 19 | k = 0; 20 | while (n) { 21 | n -= 2; 22 | scanf("%d%d", &i, &j); 23 | k ^= lbn(~(UI)((i - 1) | (j - 1))); 24 | } 25 | puts(k ? "YES" : "NO"); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2431.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2431 3 | * Author: Rqy 4 | * Date: 2018 Feb 22 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | const int N = 1050; 9 | const int mod = 10000; 10 | int f[N][N]; 11 | int main() { 12 | int n, k; 13 | scanf("%d%d", &n, &k); 14 | f[0][0] = 1; 15 | for (int i = 1; i <= n; ++i) { 16 | f[i][0] = 1; 17 | for (int j = 1; j <= k; ++j) { 18 | f[i][j] = f[i][j - 1] + f[i - 1][j]; 19 | if (j >= i) f[i][j] -= f[i - 1][j - i]; 20 | f[i][j] %= mod; 21 | } 22 | } 23 | printf("%d\n", (f[n][k] + mod) % mod); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1218.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int N = 5005; 3 | int s[N][N]; 4 | int main() { 5 | int n, r, mx, my, x, y, v; 6 | scanf("%d%d", &n, &r); 7 | mx = r; my = r; 8 | for (int i = 0; i < n; ++i) { 9 | scanf("%d%d%d", &x, &y, &v); 10 | s[++y][++x] += v; 11 | if (x > mx) mx = x; 12 | if (y > my) my = y; 13 | } 14 | int ans = 0; 15 | for (int i = 1; i <= my; ++i) 16 | for (int j = 1; j <= mx; ++j) { 17 | s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]; 18 | if (i >= r && j >= r) { 19 | int a = s[i][j] - s[i - r][j] - s[i][j - r] + s[i - r][j - r]; 20 | if (a > ans) ans = a; 21 | } 22 | } 23 | printf("%d\n", ans); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1854.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1854 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | const int N = 10050; 9 | int fa[N], e[N]; 10 | int find(int x) { return fa[x] ? fa[x] = find(fa[x]) : x; } 11 | int main() { 12 | int n; 13 | scanf("%d", &n); 14 | for (int x, y; n; --n) { 15 | scanf("%d%d", &x, &y); 16 | if ((x = find(x)) != (y = find(y))) 17 | fa[x] = y, e[y] += e[x]; 18 | ++e[y]; 19 | } 20 | for (int i = 1; i < N; ++i) if (!(e[find(i)]--)) { 21 | printf("%d\n", i - 1); 22 | break; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2761.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2761 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | std::set S; 11 | int main() { 12 | int T, n, x; 13 | scanf("%d", &T); 14 | while (T--) { 15 | S.clear(); 16 | scanf("%d", &n); 17 | bool fst = true; 18 | while (n--) { 19 | scanf("%d", &x); 20 | if (S.count(x)) continue; 21 | S.insert(x); 22 | if (!fst) printf(" "); 23 | else fst = false; 24 | printf("%d", x); 25 | } 26 | if (T) puts(""); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2822.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int A[10000], lenA; 3 | bool mark[1005]; 4 | inline int get(int n, int p) { 5 | int ans = 0; 6 | while (n) ans += (n /= p); 7 | return ans; 8 | } 9 | int main() { 10 | int n; 11 | scanf("%d", &n); 12 | lenA = 1; 13 | A[0] = 1; 14 | for (int i = 2; i <= 2 * n; ++i) if (!mark[i]) { 15 | for (int j = i * i; j <= 2 * n; j += i) mark[j] = 1; 16 | for (int k = get(2 * n, i) - get(n, i) - get(n + 1, i); k; --k) { 17 | int t, j; 18 | for (t = j = 0; j < lenA || t; ++j) { 19 | t = (A[j] = A[j] * i + t) / 10; 20 | A[j] %= 10; 21 | } 22 | lenA = j; 23 | } 24 | } 25 | while (lenA--) putchar(A[lenA] + '0'); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2748.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2748 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 1050; 11 | std::bitset cur, able; 12 | int main() { 13 | int n, b, m, x; 14 | scanf("%d%d%d", &n, &b, &m); 15 | able.reset(); cur.reset(); 16 | for (int i = 0; i <= m; ++i) able.set(i); 17 | cur.set(b); 18 | while (n--) { 19 | scanf("%d", &x); 20 | cur = ((cur << x) | (cur >> x)) & able; 21 | } 22 | x = m; 23 | while (~x && !cur.test(x)) --x; 24 | printf("%d\n", x); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2705.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | const int M = 100; 4 | int n; 5 | LL ans; 6 | int pr[M], prcnt; 7 | inline void calc(int x) { 8 | int anst = x; 9 | for (int i = 0; i < prcnt; ++i) if (!(x % pr[i])) 10 | anst = anst / pr[i] * (pr[i] - 1); 11 | ans += (LL)n / x * anst; 12 | } 13 | int main() { 14 | scanf("%d", &n); 15 | int tn = n; 16 | for (int i = 2; (LL)i * i <= tn; ++i) 17 | if (!(tn % i)) { 18 | pr[prcnt++] = i; 19 | while (!(tn % i)) tn /= i; 20 | } 21 | if (tn > 1) 22 | pr[prcnt++] = tn; 23 | for (int i = 1; (LL)i * i <= n; ++i) if (!(n % i)) { 24 | calc(i); 25 | if (i != n / i) calc(n / i); 26 | } 27 | printf("%lld\n", ans); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2257.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2257 3 | * Author: Rqy 4 | * Date: 2018 Feb 28 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | int A[2000000]; 10 | int main() { 11 | int n, k, m = 0; 12 | scanf("%d%d", &n, &k); 13 | while (n--) { 14 | int x; 15 | scanf("%d", &x); 16 | for (int i = 1; i * i <= x; ++i) if (!(x % i)) { 17 | A[m++] = i; 18 | if (i * i != x) A[m++] = x / i; 19 | } 20 | } 21 | std::sort(A, A + m); 22 | for (int i = m - 1, t = 0; i; --i) 23 | if ((t = (A[i] == A[i + 1]) * t + 1) >= k) 24 | return printf("%d\n", A[i]) & 0; 25 | } 26 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1041.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long LL; 4 | LL gcd(LL a, LL b) { 5 | while (b) { 6 | LL t = b; 7 | b = a % b; 8 | a = t; 9 | } 10 | return a; 11 | } 12 | LL r; 13 | inline bool check(LL d, LL A) { 14 | LL B = (LL)sqrt(2 * r / d - A * A); 15 | return A * A + B * B == 2 * r / d && gcd(A, B) == 1 && A != B; 16 | } 17 | int main() { 18 | scanf("%lld", &r); 19 | LL ans = 0; 20 | for (LL d = 1; d * d <= 2 * r; ++d) 21 | if (!(2 * r % d)) { 22 | for (LL A = 1; A * A <= r / d; ++A) 23 | ans += check(d, A); 24 | if (d * d != 2 * r) 25 | for (LL A = 1; A * A <= d / 2; ++A) 26 | ans += check(2 * r / d, A); 27 | } 28 | printf("%lld\n", (ans + 1) * 4); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1222.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::min; 4 | const int INF = 100000000; 5 | const int N = 6050, M = 30050; 6 | int f[2][M]; 7 | int main() { 8 | int n, m, t1, t2, t3; 9 | scanf("%d", &n); 10 | m = 0; 11 | int *x = f[0], *y = f[1]; 12 | for (int i = 0; i < n; ++i, std::swap(x, y)) { 13 | scanf("%d%d%d", &t1, &t2, &t3); 14 | std::fill(y, y + M, INF); 15 | for (int j = 0; j <= m; ++j) { 16 | if (t1) y[j + t1] = min(y[j + t1], x[j]); 17 | if (t2) y[j] = min(y[j], x[j] + t2); 18 | if (t3) y[j + t3] = min(y[j + t3], x[j] + t3); 19 | } 20 | m += std::max(t1, t3); 21 | } 22 | int ans = INF; 23 | for (int i = 0; i <= m; ++i) 24 | ans = min(ans, std::max(x[i], i)); 25 | printf("%d\n", ans); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1293.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 1000050; 4 | struct Info{ 5 | int co, x; 6 | bool operator<(const Info &l) const { 7 | return x < l.x; 8 | } 9 | }A[N]; 10 | int T[65], l; 11 | int main() { 12 | int n = 0, k, t; 13 | scanf("%*d%d", &k); 14 | for (int i = 0; i < k; ++i) { 15 | scanf("%d", &t); 16 | while (t--) { 17 | scanf("%d", &A[n].x); 18 | A[n++].co = i; 19 | } 20 | } 21 | std::sort(A, A + n); 22 | l = 0; 23 | int ans = 2000000000; 24 | for (int i = 0, j = 0; j < n; ++i) { 25 | while (j < n && l < k) 26 | if (!(T[A[j++].co]++)) ++l; 27 | if (l == k) 28 | ans = std::min(ans, A[j - 1].x - A[i].x); 29 | if (!(--T[A[i].co])) --l; 30 | } 31 | printf("%d\n", ans); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1076.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 16; 4 | const int K = 105; 5 | double f[K][1 << N]; 6 | int P[K], S[K]; 7 | int main() { 8 | int n, k; 9 | scanf("%d%d", &k, &n); 10 | for (int i = 0; i < n; ++i) { 11 | scanf("%d", &P[i]); 12 | int x; 13 | S[i] = 0; 14 | while (~scanf("%d", &x) && x) 15 | S[i] |= (1 << (x - 1)); 16 | } 17 | std::fill(f[0], f[1], .0); 18 | int lim = 1 << n; 19 | double p1 = 1.0 / n; 20 | for (int i = 1; i <= k; ++i) 21 | for (int SS = 0; SS < lim; ++SS) { 22 | f[i][SS] = .0; 23 | for (int p = 0; p < n; ++p) 24 | f[i][SS] += p1 * std::max(f[i - 1][SS], 25 | ((S[p] & SS) == S[p]) * (f[i - 1][SS | (1 << p)] + P[p])); 26 | } 27 | printf("%.6lf", f[k][0]); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1081.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1081 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | const int N = 20; 9 | int T[N]; 10 | int b, n; 11 | inline int get(int x) { return x < 10 ? x + '0' : x - 10 + 'A'; } 12 | inline void put() { 13 | for (int i = 0; i < n; ++i) putchar(get(T[i])); 14 | putchar('\n'); 15 | } 16 | void dfs(int x, bool rev) { 17 | if (!x) put(); 18 | else if (rev) 19 | for (T[--x] = b - 1; ~T[x]; --T[x]) 20 | dfs(x, ~T[x] & 1); 21 | else 22 | for (T[--x] = 0; T[x] < b; ++T[x]) 23 | dfs(x, T[x] & 1); 24 | } 25 | int main() { 26 | scanf("%d%d", &n, &b); 27 | dfs(n, false); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1059.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int N = 205; 3 | bool map[N][N]; 4 | int n, my[N], vis[N], time; 5 | bool match(int x) { 6 | for (int y = 1; y <= n; ++y) if (map[x][y] && vis[y] != time) { 7 | vis[y] = time; 8 | if (!my[y] || match(my[y])) { 9 | my[y] = x; 10 | return true; 11 | } 12 | } 13 | return false; 14 | } 15 | int main() { 16 | int T; 17 | scanf("%d", &T); 18 | while (T--) { 19 | scanf("%d", &n); 20 | int x; 21 | for (int i = 1; i <= n; ++i) 22 | for (int j = 1; j <= n; ++j) { 23 | scanf("%d", &x); 24 | map[i][j] = x; 25 | my[i] = 0; 26 | } 27 | bool ok = true; 28 | for (int i = 1; i <= n; ++i) { 29 | ++time; 30 | ok = ok && match(i); 31 | } 32 | puts(ok ? "Yes" : "No"); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1090.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::min; 5 | char s[105]; 6 | inline bool eq(int b1, int b2, int len) { 7 | return !strncmp(s + b1, s + b2, len); 8 | } 9 | inline int wei(int x) { 10 | int t = 1; 11 | while (x /= 10) ++t; 12 | return t; 13 | } 14 | int f[105][105]; 15 | int main() { 16 | scanf("%s", s); 17 | int n = strlen(s); 18 | for (int len = 1; len <= n; ++len) 19 | for (int i = 0; i + len <= n; ++i) { 20 | int j = i + len; 21 | f[i][j] = len; 22 | for (int k = i + 1; k < j; ++k) { 23 | f[i][j] = min(f[i][j], f[i][k] + f[k][j]); 24 | if (!(len % (k - i)) && eq(i, k, j - k)) 25 | f[i][j] = min(f[i][j], f[i][k] + 2 + wei(len / (k - i))); 26 | } 27 | } 28 | printf("%d\n", f[0][n]); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1029.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1029 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 150050; 11 | std::priority_queue Q; 12 | struct P{ int a, b; }p[N]; 13 | bool operator<(const P &x, const P &y) { return x.b < y.b; } 14 | int main() { 15 | int n; 16 | scanf("%d", &n); 17 | for (int i = 0; i < n; ++i) scanf("%d%d", &p[i].a, &p[i].b); 18 | std::sort(p, p + n); 19 | for (int i = 0, t = 0; i < n; ++i) 20 | if (t + p[i].a < p[i].b) Q.push(p[i].a), t += p[i].a; 21 | else if (p[i].a < Q.top()) t += p[i].a - Q.top(), Q.pop(), Q.push(p[i].a); 22 | printf("%d\n", Q.size()); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3105.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ3105 3 | * Author: Rqy 4 | * Date: 2018 Feb 23 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int K = 105; 10 | int J[32], A[K]; 11 | bool mark[K]; 12 | bool cmp(int a, int b) { return b < a; } 13 | int main() { 14 | int k; 15 | scanf("%d", &k); 16 | for (int i = 0; i < k; ++i) scanf("%d", &A[i]); 17 | std::sort(A, A + k, cmp); 18 | long long ans = 0; 19 | for (int i = 0, j, l; i < k; ++i) { 20 | for (l = 31, j = A[i]; ~l; --l) 21 | if ((j >> l) & 1) { 22 | if (!J[l]) { J[l] = j; break; } 23 | else j ^= J[l]; 24 | } 25 | if (l == -1) ans += A[i]; 26 | } 27 | printf("%lld\n", ans); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1042.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | const int N = 100050; 4 | LL f[N], c[4]; 5 | LL s, d[4]; 6 | LL calc(int S) { 7 | LL t = s; 8 | for (int i = 0; i < 4; ++i) 9 | t -= ((S >> i) & 1) * (d[i] + 1) * c[i]; 10 | if (t < 0) return 0; 11 | return f[t]; 12 | } 13 | int main() { 14 | f[0] = 1; 15 | for (int i = 0; i < 4; ++i) { 16 | scanf("%lld", &c[i]); 17 | for (int j = c[i]; j < N; ++j) 18 | f[j] += f[j - c[i]]; 19 | } 20 | int T; 21 | scanf("%d", &T); 22 | while (T--) { 23 | scanf("%lld%lld%lld%lld%lld", &d[0], &d[1], &d[2], &d[3], &s); 24 | printf("%lld\n", 25 | calc(0) - calc(1) - calc(2) - calc(4) - calc(8) 26 | + calc(3) + calc(5) + calc(9) + calc(6) + calc(10) + calc(12) 27 | - calc(7) - calc(11) - calc(13) - calc(14) + calc(15)); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1966.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 1050; 4 | const int L = 505; 5 | bool f[N][L]; 6 | char s1[N]; 7 | char s2[L]; 8 | int main() { 9 | int t; 10 | scanf("%s%d", s1, &t); 11 | int ans = 0; 12 | int n = strlen(s1); 13 | while (t--) { 14 | scanf("%s", s2); 15 | int m = strlen(s2); 16 | if (!n || !m) { 17 | ++ans; 18 | continue; 19 | } 20 | memset(f, 0, sizeof f); 21 | f[0][0] = true; 22 | for (int i = 1; i <= n; ++i) 23 | for (int j = 0, f2 = 0; j <= m; ++j) { 24 | f2 = f2 || f[i - 1][j]; 25 | if (s1[i - 1] != '*') 26 | f[i][j] = j && (s1[i - 1] == '?' || s1[i - 1] == s2[j - 1]) && f[i - 1][j - 1]; 27 | else 28 | f[i][j] = f2; 29 | } 30 | ans += !f[n][m]; 31 | } 32 | printf("%d\n", ans); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4809.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4809 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 16; 10 | int l[N]; 11 | int n, ans = 0; 12 | void dfs(int x, int A, int B, int C) { 13 | if (x == n) { ++ans; return; } 14 | for (register int able = l[x] & ~(A | B | C); able; able &= (able - 1)) { 15 | int t = able & -able; 16 | dfs(x + 1, A | t, (B | t) << 1, (C | t) >> 1); 17 | } 18 | } 19 | int main() { 20 | scanf("%d", &n); 21 | for (int i = 0; i < n; ++i) 22 | for (int j = 0, x; j < n; ++j) { 23 | scanf("%d", &x); 24 | if (!x) l[i] |= (1 << j); 25 | } 26 | dfs(0, 0, 0, 0); 27 | printf("%d\n", ans); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Luogu/P1076.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P1076 3 | * Author: Rqy 4 | * Date: 2018 Feb 09 5 | * Algorithm: NONE 6 | **************************************************************/ 7 | #include 8 | const int N = 10000; 9 | const int M = 100; 10 | const int mod = 20123; 11 | bool B[N][M]; 12 | int x[N][M], g[N]; 13 | int main() { 14 | int n, m; 15 | scanf("%d%d", &n, &m); 16 | for (int i = 0; i < n; ++i) 17 | for (int j = 0, y; j < m; ++j) { 18 | scanf("%d%d", &y, &x[i][j]); 19 | g[i] += B[i][j] = y; 20 | } 21 | int p, ans = 0; 22 | scanf("%d", &p); 23 | for (int i = 0; i < n; ++i) { 24 | ans = (ans + x[i][p]) % mod; 25 | for (int j = (x[i][p] - 1) % g[i] + 1; j;) 26 | if (j -= B[i][p]) (++p) %= m; 27 | } 28 | printf("%d\n", ans); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3108.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ3108 3 | * Author: Rqy 4 | * Date: 2018 Feb 23 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 305; 10 | std::bitset out[N], zero; 11 | int main() { 12 | int T; 13 | scanf("%d", &T); 14 | while (T--) { 15 | int n, m; 16 | scanf("%d%d", &n, &m); 17 | zero.reset(); 18 | for (int i = 0; i < n; ++i) out[i].reset(); 19 | for (int x, y; m; --m) { 20 | scanf("%d%d", &x, &y); 21 | out[x].set(y); 22 | } 23 | bool ok = true; 24 | for (int i = 0; i < n; ++i) 25 | for (int j = 0; j < n; ++j) 26 | ok = ok && ((out[i] & out[j]) == zero || out[i] == out[j]); 27 | puts(ok ? "Yes" : "No"); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1202.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 105; 4 | int fa[N], dis[N]; 5 | int Find(int x) { 6 | if (fa[x]) { 7 | int t = Find(fa[x]); 8 | dis[x] += dis[fa[x]]; 9 | return fa[x] = t; 10 | } else 11 | return x; 12 | } 13 | bool Union(int s, int t, int v) { 14 | int ss = Find(s), tt = Find(t); 15 | if (ss == tt) return dis[t] - dis[s] == v; 16 | fa[tt] = ss; 17 | dis[tt] = v - dis[t] + dis[s]; 18 | return true; 19 | } 20 | int main() { 21 | int w, m; 22 | scanf("%d", &w); 23 | while (w--) { 24 | memset(fa, 0, sizeof fa); 25 | memset(dis, 0, sizeof dis); 26 | scanf("%*d%d", &m); 27 | bool ok = true; 28 | int s, t, v; 29 | while (m--) { 30 | scanf("%d%d%d", &s, &t, &v); 31 | ok = ok && Union(s, t + 1, v); 32 | } 33 | puts(ok ? "true" : "false"); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1923.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::bitset; 5 | bitset<1005> A[2005]; 6 | int main() { 7 | int n, m, i, j, k, x; 8 | scanf("%d%d", &n, &m); 9 | for (i = 0; i < m; ++i) { 10 | A[i].reset(); 11 | for (j = 0; j <= n; ++j) { 12 | scanf("%1d", &x); 13 | if (x) 14 | A[i].set(j); 15 | } 16 | } 17 | int ans = 0; 18 | for (i = 0; i < n; ++i) { 19 | for (j = i; j < m; ++j) 20 | if (A[j].test(i)) 21 | break; 22 | if (j >= m) 23 | return !puts("Cannot Determine"); 24 | if (j > ans) ans = j; 25 | std::swap(A[j], A[i]); 26 | for (j = 0; j < m; ++j) 27 | if (j != i && A[j].test(i)) 28 | A[j] ^= A[i]; 29 | } 30 | printf("%d\n", ans + 1); 31 | for (i = 0; i < n; ++i) 32 | puts(A[i].test(n) ? "?y7M#" : "Earth"); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1037.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::min; 4 | using std::max; 5 | const int N = 155; 6 | const int K = 25; 7 | const int mod = 12345678; 8 | int f[N][N][K][K]; 9 | int main() { 10 | int n, m, k; 11 | scanf("%d%d%d", &n, &m, &k); 12 | for (int i = 0; i <= k; ++i) 13 | for (int j = 0; j <= k; ++j) 14 | f[0][0][i][j] = 1; 15 | for (int ij = 1; ij <= n + m; ++ij) 16 | for (int i = max(ij - m, 0); i <= min(ij, n); ++i) { 17 | int j = ij - i; 18 | for (int a = 0; a <= k; ++a) 19 | for (int b = 0; b <= k; ++b) { 20 | f[i][j][a][b] = 0; 21 | if (i && a) f[i][j][a][b] += f[i - 1][j][a - 1][min(b + 1, k)]; 22 | if (j && b) f[i][j][a][b] += f[i][j - 1][min(a + 1, k)][b - 1]; 23 | f[i][j][a][b] %= mod; 24 | } 25 | } 26 | printf("%d\n", f[n][m][k][k]); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2819.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2819 3 | * Author: Rqy 4 | * Date: 2018 Mar 01 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 500050; 11 | int pre[N], nxt[N * 2], to[N * 2], cnt; 12 | inline void addEdge(int x, int y) { 13 | nxt[cnt] = pre[x]; 14 | to[pre[x] = cnt++] = y; 15 | nxt[cnt] = pre[y]; 16 | to[pre[y] = cnt++] = x; 17 | } 18 | int dep[N], fa[N][20], pos[N], A[N], cnt2; 19 | void dfs(int x, int f) { 20 | dep[x] = dep[fa[x][0] = f] + 1; 21 | for (int i = 0; fa[x][i]; ++i) 22 | fa[x][i + 1] = fa[fa[x][i]][i]; 23 | for (int i = pre[x]; ~i; i = nxt[i]) 24 | if (to[i] != f) dfs(to[i], x); 25 | } 26 | int LCA(int x, int y) { 27 | if (dep[x] < dep[y]) std::swap(x, y); 28 | } 29 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3209.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int mod = 10000007; 4 | typedef long long LL; 5 | const int N = 60; 6 | LL f[N][N][2]; 7 | LL pow_mod(LL a, LL b) { 8 | LL ans = 1; 9 | for (; b; b >>= 1, a = a * a % mod) 10 | if (b & 1) ans = ans * a % mod; 11 | return ans; 12 | } 13 | int main() { 14 | LL n; 15 | scanf("%lld", &n); 16 | ++n; 17 | int k = 0; 18 | while ((1LL << k) <= n) ++k; 19 | f[k][0][0] = 1; 20 | for (int i = k; i; --i) 21 | for (int a = 0; a < k; ++a) 22 | for (int b = 0; b < 2; ++b) 23 | for (int c = 0; c < 2; ++c) 24 | if (!(c == 1 && b == 0 && ((~n >> (i - 1))) & 1)) 25 | f[i - 1][a + c][b || (c < ((n >> (i - 1)) & 1))] += f[i][a][b]; 26 | LL ans = 1; 27 | for (int i = 1; i < k; ++i) 28 | ans = ans * pow_mod(i, f[0][i][1]) % mod; 29 | printf("%lld\n", ans); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1260.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1260 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 55; 11 | char s[N]; 12 | int f[N][N]; 13 | int main() { 14 | scanf("%s", s); 15 | int n = strlen(s); 16 | for (int i = 0; i < n; ++i) f[i][i] = 1; 17 | for (int l = 2; l <= n; ++l) 18 | for (int i = 0; i + l <= n; ++i) { 19 | int j = i + l - 1; 20 | f[i][j] = l; 21 | for (int k = i; k < j; ++k) 22 | f[i][j] = std::min(f[i][j], f[i][k] + f[k + 1][j]); 23 | if (s[i] == s[j]) 24 | f[i][j] = std::min(f[i][j], std::min(f[i + 1][j - 1] + 1, std::min(f[i + 1][j], f[i][j - 1]))); 25 | } 26 | printf("%d\n", f[0][n - 1]); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1025.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | const int N = 1050; 4 | bool mark[N]; 5 | int cnt, prime[500]; 6 | void getPrime(int n) { 7 | cnt = 0; 8 | for (int i = 2; i <= n; ++i) { 9 | if (!mark[i]) 10 | prime[cnt++] = i; 11 | for (int j = 0; j < cnt && prime[j] * i <= n; ++j) { 12 | mark[i * prime[j]] = 1; 13 | if (!(i % prime[j])) break; 14 | } 15 | } 16 | } 17 | LL f[N]; 18 | LL tmp[N]; 19 | int main() { 20 | int n; 21 | scanf("%d", &n); 22 | f[0] = 1; 23 | getPrime(n); 24 | for (int i = 0; i < cnt; ++i) { 25 | for (int j = 0; j <= n; ++j) { 26 | tmp[j] = 0; 27 | for (int k = prime[i]; k <= j; k *= prime[i]) 28 | tmp[j] += f[j - k]; 29 | } 30 | for (int i = 0; i <= n; ++i) 31 | f[i] += tmp[i]; 32 | } 33 | for (int i = 0; i < n; ++i) 34 | f[n] += f[i]; 35 | printf("%lld\n", f[n]); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4517.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | const int mod = 1E9 + 7; 4 | int D[1000005]; 5 | int f[1000005]; 6 | int f2[1000005]; 7 | int mul(int a, int b) 8 | { 9 | if(!a) return 1; 10 | int ans = mul(a / 2, b); 11 | ans = (LL)ans * ans % mod; 12 | if(a & 1) 13 | ans = (LL)ans * b % mod; 14 | return ans; 15 | } 16 | int main() 17 | { 18 | D[0] = 1; 19 | D[1] = 0; 20 | f[0] = f[1] = 1; 21 | for(int i = 2; i <= 1000000; i++) 22 | { 23 | f[i] = (LL)f[i-1] * i % mod; 24 | D[i] = (LL)(i-1) * ((LL)D[i-1] + D[i-2] % mod) % mod; 25 | } 26 | f2[1000000] = mul(mod - 2, f[1000000]); 27 | for(int i = 999999; i >= 0; i--) 28 | { 29 | f2[i] = (LL)f2[i+1] * (i + 1) % mod; 30 | } 31 | int T, n, m; 32 | scanf("%d", &T); 33 | while(T--) 34 | { 35 | scanf("%d%d", &n, &m); 36 | printf("%d\n", (int)(((LL)f[n] * f2[m] % mod * f2[n-m] % mod) * D[n-m] % mod)); 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1996.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 1005; 4 | const int mod = 19650827; 5 | int H[N], _f[2][N], _g[2][N]; 6 | int main() { 7 | int n; 8 | scanf("%d", &n); 9 | for (int i = 0; i < n; ++i) scanf("%d", &H[i]); 10 | int *f = _f[0], *g = _g[0], *ff = _f[1], *gg = _g[1]; 11 | for (int i = 0; i < n; ++i) f[i] = 1; 12 | for (int i = 1; i < n; ++i) { 13 | std::swap(f, ff); 14 | std::swap(g, gg); 15 | for (int j = 0; i + j < n; ++j) { 16 | f[j] = g[j] = 0; 17 | if (H[j] < H[j + 1]) f[j] += ff[j + 1]; 18 | if (H[j] < H[j + i]) f[j] += gg[j + 1]; 19 | if (H[j + i] > H[j]) g[j] += ff[j]; 20 | if (H[j + i] > H[j + i - 1]) g[j] += gg[j]; 21 | f[j] %= mod; 22 | g[j] %= mod; 23 | //printf("%d %d ", f[j], g[j]); 24 | } 25 | //printf("\n"); 26 | } 27 | printf("%d\n", (f[0] + g[0]) % mod); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1408.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1408 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 1050; 10 | const int mod = 10000; 11 | inline int sqr(int a) { return a * a % mod; } 12 | int pow(int a, int b) { 13 | return b ? (b & 1 ? a : 1) * sqr(pow(a, b >> 1)) % mod : 1; 14 | } 15 | int main() { 16 | int k, m = 1, ans1 = 0, ans2 = 0, p, e; 17 | scanf("%d", &k); 18 | while (k--) { 19 | scanf("%d%d", &p, &e); 20 | (m *= pow(p, e)) %= mod; 21 | if (p > 2) { 22 | int t1 = ans1, t2 = ans2; 23 | ans1 = (t1 + (p - 1) * t2) % mod; 24 | ans2 = ((p - 1) * (1 + t1) + t2) % mod; 25 | } 26 | } 27 | printf("%d\n%d\n%d\n", ans1, ans2, (m - 1 - ans1 - ans2 + 3 * mod) % mod); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1028.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int n, m; 3 | int num[500], tmp[500]; 4 | bool judge() { 5 | for (int d = 1; d <= n; ++d) 6 | if (num[d] >= 2) { 7 | int i; 8 | for (i = 1; i <= n + 2; ++i) tmp[i] = num[i]; 9 | tmp[d] -= 2; 10 | for (i = 1; i <= n + 2; ++i) { 11 | if (tmp[i] < 0) break; 12 | tmp[i] %= 3; 13 | tmp[i + 1] -= tmp[i]; 14 | tmp[i + 2] -= tmp[i]; 15 | } 16 | if (i == n + 3) return true; 17 | } 18 | return false; 19 | } 20 | int main() { 21 | scanf("%d%d", &n, &m); 22 | int x; 23 | for (int i = 0; i < 3 * m + 1; ++i) { 24 | scanf("%d", &x); 25 | ++num[x]; 26 | } 27 | bool f = 1; 28 | for (int i = 1; i <= n; ++i) { 29 | ++num[i]; 30 | if (judge()) { 31 | if (!f) putchar(' '); 32 | else f = 0; 33 | printf("%d", i); 34 | } 35 | --num[i]; 36 | } 37 | if (f) puts("NO"); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1149.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 100050; 4 | int s[N][2], mindep[N], maxdep[N]; 5 | int main() { 6 | int n; 7 | scanf("%d", &n); 8 | for (int i = 1; i <= n; ++i) { 9 | scanf("%d%d", &s[i][0], &s[i][1]); 10 | s[i][0] = std::max(s[i][0], 0); 11 | s[i][1] = std::max(s[i][1], 0); 12 | } 13 | for (int i = n; i; --i) { 14 | int l = s[i][0], r = s[i][1]; 15 | mindep[i] = std::min(mindep[l], mindep[r]) + 1; 16 | maxdep[i] = std::max(maxdep[l], maxdep[r]) + 1; 17 | } 18 | if (maxdep[1] - mindep[1] > 1) return puts("-1") & 0; 19 | int ans = 0; 20 | for (int o = n; o; --o) { 21 | if (mindep[o] == maxdep[o]) continue; 22 | int l = s[o][0], r = s[o][1]; 23 | if (maxdep[l] <= mindep[r]) 24 | ++ans; 25 | if (mindep[r] != maxdep[r] && mindep[l] != maxdep[l]) 26 | return puts("-1") & 0; 27 | } 28 | return printf("%d\n", ans) & 0; 29 | } 30 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2875.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2875 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | typedef long long LL; 9 | LL m, a, c, g; 10 | LL mult(LL a, LL b) { 11 | LL ans = 0; 12 | for (; b; b >>= 1, a = a * 2 % m) 13 | if (b & 1) ans = (ans + a) % m; 14 | return ans; 15 | } 16 | struct P{ 17 | LL a, b; 18 | P(LL a, LL b) : a(a), b(b) {} 19 | friend P operator*(const P &x, const P &y) { 20 | return P(mult(x.a, y.a), (mult(x.a, y.b) + x.b) % m); 21 | } 22 | }; 23 | int main() { 24 | LL x0, n; 25 | scanf("%lld%lld%lld%lld%lld%lld", &m, &a, &c, &x0, &n, &g); 26 | P x(a, 1), ans(1, 0); 27 | for (; n; n >>= 1, x = x * x) 28 | if (n & 1) ans = ans * x; 29 | printf("%lld\n", (mult(ans.a, x0) + mult(ans.b, c)) % m % g); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /BZOJ/BZOJ5106.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ5106 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 200050; 11 | LL S, L, H[N], A[N]; 12 | int n; 13 | int main() { 14 | scanf("%d%lld%lld", &n, &S, &L); 15 | LL l = 0, r = 0; 16 | for (int i = 0; i < n; ++i) scanf("%lld", &H[i]); 17 | for (int i = 0; i < n; ++i) { 18 | scanf("%lld", &A[i]); 19 | r = std::max(r, 1 + std::max(S, L) / A[i]); 20 | } 21 | while (l < r) { 22 | LL mid = (l + r) / 2; 23 | LL ans = 0; 24 | for (int i = 0; i < n && ans < S; ++i) 25 | if (H[i] + A[i] * mid >= L) ans += H[i] + A[i] * mid; 26 | if (ans >= S) r = mid; 27 | else l = mid + 1; 28 | } 29 | printf("%lld\n", l); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1212.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 250, M = 1000050; 4 | int nxt[N][26]; 5 | bool t[N]; 6 | int cnt = 1; 7 | bool f[M]; 8 | char s[M]; 9 | void addStr() { 10 | int l = strlen(s), v = 0; 11 | for (int i = 0; i < l; ++i) { 12 | if (!nxt[v][s[i] - 'a']) 13 | nxt[v][s[i] - 'a'] = cnt++; 14 | v = nxt[v][s[i] - 'a']; 15 | } 16 | t[v] = 1; 17 | } 18 | int solve() { 19 | int n = strlen(s), ans; 20 | f[0] = 1; 21 | for (int i = 1; i <= n; ++i) f[i] = 0; 22 | for (int i = 0; i <= n; ++i) if (f[i]) { 23 | ans = i; 24 | int v = 0; 25 | for (int j = i; j <= n; ++j) { 26 | if (t[v]) f[j] = 1; 27 | if (j == n || !(v = nxt[v][s[j] - 'a'])) break; 28 | } 29 | } 30 | return ans; 31 | } 32 | int main() { 33 | int n, m; 34 | scanf("%d%d", &n, &m); 35 | while (n--) scanf("%s", s), addStr(); 36 | while (m--) scanf("%s", s), printf("%d\n", solve()); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1970.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int K = 105; 4 | int A[100]; 5 | int a[K]; 6 | inline int get() { 7 | int c; 8 | while (!isdigit(c = getchar())); 9 | return c - '0'; 10 | } 11 | void solve(int dep) { 12 | int t = get(); 13 | if (t == 0) 14 | ++a[dep * 2]; 15 | else if (t == 2) { 16 | solve(dep - 1); 17 | solve(dep - 1); 18 | solve(dep - 1); 19 | solve(dep - 1); 20 | } 21 | } 22 | int main() { 23 | int k; 24 | scanf("%d", &k); 25 | solve(k); 26 | int len = 1; 27 | for (int i = 0; i < k * 2; ++i) { 28 | a[i + 1] += a[i] / 2; 29 | a[i] %= 2; 30 | } 31 | for (int i = k * 2; ~i; --i) { 32 | int t; 33 | for (int j = t = 0; j < len; ++j) { 34 | t = (A[j] = A[j] * 2 + t) / 10; 35 | A[j] %= 10; 36 | } 37 | if (t) A[len++] = t; 38 | if (a[i]) ++A[0]; 39 | } 40 | while (len--) putchar(A[len] + '0'); 41 | putchar('\n'); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3670.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | const int N = 1000050; 6 | int T, n; 7 | char s[N]; 8 | int nxt[N], num[N]; 9 | int Find(int x, int g) { 10 | if (x <= g) return x; 11 | return nxt[x] = Find(nxt[x], g); 12 | } 13 | int main() { 14 | scanf("%d", &T); 15 | while (T--) { 16 | scanf("%s", s); 17 | n = strlen(s); 18 | nxt[0] = nxt[1] = 0; 19 | num[0] = num[1] = 0; 20 | for (int i = 1, j = 0; i < n; ++i) { 21 | while (j && s[j] != s[i]) j = nxt[j]; 22 | int tj = j; 23 | nxt[i + 1] = j += s[i] == s[j]; 24 | num[i + 1] = num[j] + (s[i] == s[tj]); 25 | } 26 | for (int i = n; i; --i) 27 | nxt[i] = Find(nxt[i], i / 2); 28 | int ans = 1; 29 | for (int i = 1; i <= n; ++i) 30 | if (nxt[i]) 31 | ans = (long long)ans * (num[nxt[i]] + 2) % 1000000007; 32 | printf("%d\n", ans); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1012.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | const int N = 200050; 6 | int A[N], M[N]; 7 | inline int readInt() { 8 | int ans = 0; 9 | char c; 10 | do c = getchar(); while (!isdigit(c)); 11 | do { 12 | ans = ans * 10 + c - '0'; 13 | c = getchar(); 14 | } while (isdigit(c)); 15 | return ans; 16 | } 17 | inline char readChar() { 18 | char c; 19 | do c = getchar(); while (isspace(c)); 20 | return c; 21 | } 22 | int main() { 23 | int m = readInt(); 24 | int d = readInt(); 25 | int last_ans = 0, n = 0, cnt = 0; 26 | for (int i = 0; i < m; ++i) { 27 | char c = readChar(); 28 | int x = readInt(); 29 | if (c == 'A') { 30 | x = (x + last_ans) % d; 31 | while (cnt && A[M[cnt - 1]] < x) --cnt; 32 | A[M[cnt++] = n++] = x; 33 | } else 34 | printf("%d\n", last_ans = A[*std::lower_bound(M, M + cnt, n - x)]); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3925.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const int N = 10; 6 | const int M = 45 + 1; 7 | bool vis[1 << N][M]; 8 | int siz[1 << N], link[N]; 9 | double f[1 << N][M]; 10 | int main() { 11 | int n, m; 12 | scanf("%d%d", &n, &m); 13 | int lim = 1 << n; 14 | for (int i = 0, x, y; i < m; ++i) { 15 | scanf("%d%d", &x, &y); 16 | --x; --y; 17 | link[x] |= (1 << y); 18 | link[y] |= (1 << x); 19 | } 20 | for (int S = 1; S < lim; ++S) siz[S] = siz[S & (S - 1)] + 1; 21 | for (int S1 = 3; S1 < lim; ++S1) if (S1 & 1) { 22 | for (int S2 = (S1 - 1) & S1; S2 != 0; S2 = (S2 - 1) & S1) if (S2 & 1) { 23 | int T = 0; 24 | for (int i = 0; i < n; ++i) if ((S1 >> i) & (~S2 >> i) & 1) 25 | T += siz[link[i] & S2]; 26 | for (int i = 0; i + T <= m; ++i) 27 | f[S1][i] += 1.0 / (i + T + 1) - f[S2][i + T]; 28 | } 29 | } 30 | printf("%.6lf\n", f[lim - 1][0]); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1188.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int N = 25; 3 | int SG[N], a[N]; 4 | int f[10000]; 5 | int main() { 6 | int T, n, sg = 0; 7 | SG[0] = 0; 8 | for (int i = 1; i < N; ++i) { 9 | for (int j = 0; j < i; ++j) 10 | for (int k = 0; k < i; ++k) 11 | f[SG[j] ^ SG[k]] = i; 12 | for (SG[i] = 0; f[SG[i]] == i; ++SG[i]); 13 | } 14 | scanf("%d", &T); 15 | while (T--) { 16 | scanf("%d", &n); 17 | sg = 0; 18 | for (int i = n - 1; ~i; --i) { 19 | scanf("%d", &a[i]); 20 | if (a[i] & 1) sg ^= SG[i]; 21 | } 22 | if (!sg) { 23 | printf("-1 -1 -1\n0\n"); 24 | continue; 25 | } 26 | int ans = 0; 27 | for (int i = n - 1; ~i; --i) 28 | for (int j = i - 1; ~j; --j) 29 | for (int k = j; ~k; --k) 30 | if (a[i] && !(sg ^ SG[i] ^ SG[k] ^ SG[j]) && !(ans++)) 31 | printf("%d %d %d\n", n - i - 1, n - j - 1, n - k - 1); 32 | printf("%d\n", ans); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1224.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1224 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int M = 105; 10 | const double eps = 1e-10; 11 | inline int sgn(double x) { 12 | if (x < -eps) return -1; 13 | if (x > eps) return 1; 14 | return 0; 15 | } 16 | int m; 17 | double sum[M]; 18 | int dfs(int x, int n, double S) { 19 | if (sgn(S - (sum[x + n - 1] - sum[x - 1])) > 0) return 0; 20 | if (sgn(S - (sum[m] - sum[m - n])) < 0) return 0; 21 | if (!n) return 1; 22 | if (x > m) return 0; 23 | return dfs(x + 1, n, S) + dfs(x + 1, n - 1, S - 1.0 / x); 24 | } 25 | int main() { 26 | int n, x, y; 27 | scanf("%d%d%d%d", &n, &m, &x, &y); 28 | for (int i = 1; i <= 2 * m; ++i) sum[i] = sum[i - 1] + 1.0 / i; 29 | printf("%d\n", dfs(1, n, (double)x / y)); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Luogu/P3455.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P3455 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: 莫比乌斯反演 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 50050; 11 | int mu[N]; 12 | LL solve(int a, int b) { 13 | LL ans = 0; 14 | for (int i = 1, last; i <= a && i <= b; i = last + 1) { 15 | last = std::min(a / (a / i), b / (b / i)); 16 | ans += (LL)(a / i) * (b / i) * (mu[last] - mu[i - 1]); 17 | } 18 | return ans; 19 | } 20 | int main() { 21 | mu[1] = 1; 22 | for (int i = 1; i < N; ++i) 23 | for (int j = i * 2; j < N; j += i) 24 | mu[j] -= mu[i]; 25 | for (int i = 2; i < N; ++i) mu[i] += mu[i - 1]; 26 | int n, a, b, k; 27 | scanf("%d", &n); 28 | while (n--) { 29 | scanf("%d%d%d", &a, &b, &k); 30 | printf("%lld\n", solve(a / k, b / k)); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1562.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int N = 10050; 5 | int A[N][2]; 6 | int mx[N], my[N], vis[N], time; 7 | bool match(int x) { 8 | for (int i = 0; i < 2; ++i) { 9 | int y = A[x][i]; 10 | if (vis[y] == time) continue; 11 | vis[y] = time; 12 | if (my[y] == -1 || match(my[y])) { 13 | mx[my[y] = x] = y; 14 | return true; 15 | } 16 | } 17 | return false; 18 | } 19 | int main() { 20 | int n, x; 21 | scanf("%d", &n); 22 | for (int i = 0; i < n; ++i) { 23 | scanf("%d", &x); 24 | A[i][0] = (i - x + n) % n; 25 | A[i][1] = (i + x) % n; 26 | if (A[i][0] > A[i][1]) std::swap(A[i][0], A[i][1]); 27 | mx[i] = my[i] = -1; 28 | } 29 | for (int i = n - 1; ~i; --i) { 30 | ++time; 31 | if (!match(i)) { 32 | printf("No Answer"); 33 | return 0; 34 | } 35 | } 36 | for (int i = 0; i < n; ++i) 37 | printf("%d%c", mx[i], i == n - 1 ? '\n' : ' '); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3997.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ3997 3 | * Author: Rqy 4 | * Date: 2018 Feb 22 5 | * Algorithm: DP 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 1050; 11 | int A[N][N], f[N][N]; 12 | int main() { 13 | int T; 14 | scanf("%d", &T); 15 | while (T--) { 16 | memset(f, 0, sizeof f); 17 | int n, m; 18 | scanf("%d%d", &n, &m); 19 | for (int i = 0; i < n; ++i) 20 | for (int j = 0; j < m; ++j) 21 | scanf("%d", &A[i][j]); 22 | for (int i = 0; i < n; ++i) 23 | for (int j = m - 1; ~j; --j) { 24 | f[i][j] = std::max(A[i][j], f[i][j + 1]); 25 | if (i) { 26 | f[i][j] = std::max(f[i][j], f[i - 1][j + 1] + A[i][j]); 27 | f[i][j] = std::max(f[i][j], f[i - 1][j]); 28 | } 29 | } 30 | printf("%d\n", f[n - 1][0]); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Luogu/P3143.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P3143 3 | * Author: Rqy 4 | * Date: 2017 Dec 27 5 | * Algorithm: 尺取 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 50050; 10 | int A[N], l[N], r[N]; 11 | int main() { 12 | int n, k; 13 | scanf("%d%d", &n, &k); 14 | for (int i = 0; i < n; ++i) scanf("%d", &A[i]); 15 | std::sort(A, A + n); 16 | for (int i = 0, j = 0; i < n; ++i) { 17 | while (A[i] - A[j] > k) ++j; 18 | r[i] = i - j + 1; 19 | } 20 | for (int i = n - 1, j = n - 1; ~i; --i) { 21 | while (A[j] - A[i] > k) --j; 22 | l[i] = j - i + 1; 23 | } 24 | for (int i = 1; i < n; ++i) r[i] = std::max(r[i], r[i - 1]); 25 | for (int i = n - 2; ~i; --i) l[i] = std::max(l[i], l[i + 1]); 26 | int ans = 0; 27 | for (int i = 1; i < n; ++i) 28 | ans = std::max(ans, l[i] + r[i - 1]); 29 | printf("%d\n", ans); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1026.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int p[10]; 4 | int f[10][10]; 5 | inline bool check(int a, int b) { 6 | return std::abs(a - b) >= 2; 7 | } 8 | int count(int N) { 9 | if (!N) return 0; 10 | int n = 0; 11 | while (N) { 12 | p[n++] = N % 10; 13 | N /= 10; 14 | } 15 | p[n] = 0; 16 | bool ok = true; 17 | for (int j = 0; j < 10; ++j) f[n][j] = 0; 18 | for (int i = n - 1; ~i; --i) { 19 | for (int j = 0; j < 10; ++j) { 20 | f[i][j] = ok && (j < p[i]) && ((i == n - 1 && j) || check(p[i + 1], j)); 21 | if (i != n - 1 && j) ++f[i][j]; 22 | for (int k = 0; k < 10; ++k) 23 | if (check(k, j)) f[i][j] += f[i + 1][k]; 24 | } 25 | if (i < n - 1 && !check(p[i], p[i + 1])) ok = false; 26 | } 27 | int ans = ok; 28 | for (int i = 0; i < 10; ++i) 29 | ans += f[0][i]; 30 | return ans; 31 | } 32 | int main() { 33 | int A, B; 34 | scanf("%d%d", &A, &B); 35 | printf("%d\n", count(B) - count(A - 1)); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1304.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::min; 5 | const int N = 10050; 6 | int c[N], f[N][2]; 7 | int to[N * 2], nxt[N * 2], pre[N]; 8 | int n, m; 9 | void dfs(int x, int fa) { 10 | f[x][0] = f[x][1] = 1; 11 | if (x <= n) { 12 | f[x][c[x] ^ 1] = 1000000000; 13 | return; 14 | } 15 | for (int i = pre[x]; ~i; i = nxt[i]) 16 | if (to[i] != fa) { 17 | dfs(to[i], x); 18 | f[x][0] += min(f[to[i]][0] - 1, f[to[i]][1]); 19 | f[x][1] += min(f[to[i]][1] - 1, f[to[i]][0]); 20 | } 21 | } 22 | int main() { 23 | scanf("%d%d", &m, &n); 24 | memset(pre, -1, sizeof pre); 25 | for (int i = 1; i <= n; ++i) 26 | scanf("%d", &c[i]); 27 | int cnt = 0; 28 | for (int i = 1, x, y; i < m; ++i) { 29 | scanf("%d %d", &x, &y); 30 | nxt[cnt] = pre[x]; to[pre[x] = cnt++] = y; 31 | nxt[cnt] = pre[y]; to[pre[y] = cnt++] = x; 32 | } 33 | dfs(m, -1); 34 | printf("%d\n", min(f[m][0], f[m][1])); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4403.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4403 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int mod = 1000003; 10 | typedef long long LL; 11 | LL fac[mod], invfac[mod]; 12 | LL C(LL a, LL b) { 13 | if (a < b) return 0; 14 | return fac[a] * invfac[b] % mod * invfac[a - b] % mod; 15 | } 16 | LL Lucas(LL a, LL b) { 17 | if (!b) return 1; 18 | return C(a % mod, b % mod) * Lucas(a / mod, b / mod) % mod; 19 | } 20 | int main() { 21 | fac[0] = 1; 22 | for (int i = 1; i < mod; ++i) fac[i] = fac[i - 1] * i % mod; 23 | invfac[mod - 1] = mod - 1; 24 | for (int i = mod - 1; i; --i) invfac[i - 1] = invfac[i] * i % mod; 25 | int T; 26 | scanf("%d", &T); 27 | while (T--) { 28 | int n, l, r; 29 | scanf("%d%d%d", &n, &l, &r); 30 | printf("%lld\n", (Lucas(n + r - l + 1, n) - 1 + mod) % mod); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3233.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 100050; 4 | int n; 5 | int A[55]; 6 | bool mark[N]; 7 | int prime[N], prcnt; 8 | void getPrime() { 9 | prcnt = 0; 10 | for (int i = 2; i < N; ++i) { 11 | if (!mark[i]) prime[prcnt++] = i; 12 | for (int j = 0; j < prcnt && i * prime[j] < N; ++j) { 13 | mark[i * prime[j]] = 1; 14 | if (!(i % prime[j])) break; 15 | } 16 | } 17 | } 18 | int f[N]; 19 | int main() { 20 | scanf("%d", &n); 21 | int maxv = 0; 22 | for (int i = 0; i < n; ++i) { 23 | scanf("%d", &A[i]); 24 | maxv = std::max(maxv, A[i]); 25 | } 26 | getPrime(); 27 | for (int i = maxv; i; --i) { 28 | f[i] = 0; 29 | for (int j = 0; j < n; ++j) f[i] += A[j] / i; 30 | for (int j = 0; j < prcnt && i * prime[j] <= maxv; ++j) { 31 | int anst = f[i * prime[j]]; 32 | for (int k = 0; k < n; ++k) anst += A[k] / i % prime[j]; 33 | f[i] = std::min(f[i], anst); 34 | } 35 | } 36 | printf("%d\n", f[1]); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1878.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long LL; 5 | const int N = 1000050; 6 | int C[N], t[N]; 7 | int p[N], L[N], R[N]; 8 | LL ansv[N]; 9 | int k; 10 | bool cmp(int a, int b) { 11 | if (L[a] / k != L[b] / k) return L[a] / k < L[b] / k; 12 | return R[a] < R[b]; 13 | } 14 | int main() { 15 | int n, m; 16 | scanf("%d", &n); 17 | for (int i = 0; i < n; ++i) scanf("%d", &C[i]); 18 | scanf("%d", &m); 19 | for (int i = 0; i < m; ++i) scanf("%d%d", &L[i], &R[i]), p[i] = i; 20 | k = 0; 21 | while (k * k < n) ++k; 22 | std::sort(p, p + m, cmp); 23 | LL ans = 0, l = 0, r = -1, q, g; 24 | for (int i = 0; i < m; ++i) { 25 | while (r < R[p[i]] - 1) ans += !(t[C[++r]]++); 26 | while (l > L[p[i]] - 1) ans += !(t[C[--l]]++); 27 | while (r > R[p[i]] - 1) ans -= !(--t[C[r--]]); 28 | while (l < L[p[i]] - 1) ans -= !(--t[C[l++]]); 29 | ansv[p[i]] = ans; 30 | } 31 | for (int i = 0; i < m; ++i) 32 | printf("%d\n", ansv[i]); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Luogu/P2473.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2473 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: 期望概率 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 16; 10 | const int K = 105; 11 | double f[K][1 << N]; 12 | int P[K], S[K]; 13 | int main() { 14 | int n, k; 15 | scanf("%d%d", &k, &n); 16 | for (int i = 0; i < n; ++i) { 17 | scanf("%d", &P[i]); 18 | int x; 19 | S[i] = 0; 20 | while (~scanf("%d", &x) && x) 21 | S[i] |= (1 << (x - 1)); 22 | } 23 | std::fill(f[0], f[1], .0); 24 | int lim = 1 << n; 25 | double p1 = 1.0 / n; 26 | for (int i = 1; i <= k; ++i) 27 | for (int SS = 0; SS < lim; ++SS) { 28 | f[i][SS] = .0; 29 | for (int p = 0; p < n; ++p) 30 | f[i][SS] += p1 * std::max(f[i - 1][SS], 31 | ((S[p] & SS) == S[p]) * (f[i - 1][SS | (1 << p)] + P[p])); 32 | } 33 | printf("%.6lf", f[k][0]); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1007.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long LL; 4 | const int N = 50050; 5 | const double eps = 1e-6; 6 | struct Line{ 7 | int k, b, i; 8 | friend inline bool operator<(const Line &x, const Line &y) { 9 | if (x.k == y.k) return x.b > y.b; 10 | return x.k < y.k; 11 | } 12 | }; 13 | double crossing(const Line &x, const Line &y) { 14 | return (double)(x.b - y.b) / (y.k - x.k); 15 | } 16 | Line l[N], t[N]; 17 | bool ans[N]; 18 | int main() { 19 | int n; 20 | scanf("%d", &n); 21 | for (int i = 0; i < n; ++i) { 22 | scanf("%d%d", &l[i].k, &l[i].b); 23 | l[i].i = i; 24 | } 25 | std::sort(l, l + n); 26 | int top = 0; 27 | for (int i = 0; i < n; ++i) if (!i || l[i].k != l[i - 1].k) { 28 | while (top > 1 && crossing(t[top - 2], t[top - 1]) - crossing(l[i], t[top - 1]) > -eps) 29 | --top; 30 | t[top++] = l[i]; 31 | } 32 | for (int i = 0; i < top; ++i) ans[t[i].i] = true; 33 | for (int i = 0; i < n; ++i) if (ans[i]) 34 | printf("%d ", i + 1); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Luogu/P3396.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P3396 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: 分块 6 | **************************************************************/ 7 | #include 8 | const int N = 150050; 9 | const int sN = 400; 10 | int f[sN][sN], A[N]; 11 | char ss[5]; 12 | int main() { 13 | int n, m, s = 0; 14 | scanf("%d%d", &n, &m); 15 | while (s * s < n) ++s; 16 | for (int i = 1; i <= n; ++i) { 17 | scanf("%d", &A[i]); 18 | for (int j = 1; j <= s; ++j) 19 | f[j][i % j] += A[i]; 20 | } 21 | int x, y; 22 | while (m--) { 23 | scanf("%s%d%d", ss, &x, &y); 24 | if (*ss == 'A') { 25 | if (x <= s) printf("%d\n", f[x][y]); 26 | else { 27 | int ans = 0; 28 | for (int i = y; i <= n; i += x) 29 | ans += A[i]; 30 | printf("%d\n", ans); 31 | } 32 | } else { 33 | for (int j = 1; j <= s; ++j) 34 | f[j][x % j] += y - A[x]; 35 | A[x] = y; 36 | } 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Luogu/P1129.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P1129 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: 匈牙利 6 | **************************************************************/ 7 | #include 8 | const int N = 205; 9 | bool map[N][N]; 10 | int n, my[N], vis[N], time; 11 | bool match(int x) { 12 | for (int y = 1; y <= n; ++y) if (map[x][y] && vis[y] != time) { 13 | vis[y] = time; 14 | if (!my[y] || match(my[y])) { 15 | my[y] = x; 16 | return true; 17 | } 18 | } 19 | return false; 20 | } 21 | int main() { 22 | int T; 23 | scanf("%d", &T); 24 | while (T--) { 25 | scanf("%d", &n); 26 | int x; 27 | for (int i = 1; i <= n; ++i) 28 | for (int j = 1; j <= n; ++j) { 29 | scanf("%d", &x); 30 | map[i][j] = x; 31 | my[x] = 0; 32 | } 33 | bool ok = true; 34 | for (int i = 1; i <= n; ++i) { 35 | ++time; 36 | ok = ok && match(i); 37 | } 38 | puts(ok ? "Yes" : "No"); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4832.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4832 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | const int N = 55; 9 | const int T = 8; 10 | double f[N][T][T][T]; 11 | int main() { 12 | for (int i = 1; i <= 50; ++i) 13 | for (int a = 0; a <= 7; ++a) 14 | for (int b = 0; a + b <= 7; ++b) 15 | for (int c = 0; a + b + c <= 7; ++c) { 16 | int s = a + b + c + 1; 17 | double &ans = f[i][a][b][c]; 18 | ans = 1.0 / s * (1.0 + f[i - 1][a][b][c]); 19 | if (a) ans += ((double)a / s) * f[i - 1][a - 1][b][c]; 20 | if (b) ans += ((double)b / s) * f[i - 1][a + 1][b - 1][c + (s < 8)]; 21 | if (c) ans += ((double)c / s) * f[i - 1][a][b + 1][c - 1 + (s < 8)]; 22 | } 23 | int T, k, a, b, c; 24 | scanf("%d", &T); 25 | while (T--) { 26 | scanf("%d%d%d%d", &k, &a, &b, &c); 27 | printf("%.2lf\n", f[k][a][b][c]); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Luogu/P2522.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2522 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: 莫比乌斯反演 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 50050; 11 | int mu[N]; 12 | int n, a, b, c, d, k; 13 | LL solve(int a, int b) { 14 | a /= k; 15 | b /= k; 16 | LL ans = 0; 17 | for (int i = 1, last; i <= a && i <= b; i = last + 1) { 18 | last = std::min(a / (a / i), b / (b / i)); 19 | ans += (LL)(a / i) * (b / i) * (mu[last] - mu[i - 1]); 20 | } 21 | return ans; 22 | } 23 | int main() { 24 | mu[1] = 1; 25 | for (int i = 1; i < N; ++i) 26 | for (int j = i * 2; j < N; j += i) 27 | mu[j] -= mu[i]; 28 | for (int i = 2; i < N; ++i) mu[i] += mu[i - 1]; 29 | scanf("%d", &n); 30 | while (n--) { 31 | scanf("%d%d%d%d%d", &a, &b, &c, &d, &k); 32 | printf("%lld\n", solve(b, d) - solve(a - 1, d) - solve(b, c - 1) + solve(a - 1, c - 1)); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4807.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4807 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 1000050; 10 | bool mark[N]; 11 | const int K = 50; 12 | int A[K], len; 13 | int get(int n, int p) { return n ? n / p + get(n / p, p) : 0; } 14 | void mul(int x) { 15 | for (int i = 0, t = 0; i < len || t; ++i) { 16 | if (i >= 50) break; 17 | if (i >= len) len = i + 1; 18 | t = (A[i] = A[i] * x + t) / 10; 19 | A[i] %= 10; 20 | } 21 | } 22 | int main() { 23 | int n, m; 24 | scanf("%d%d", &n, &m); 25 | if (n < m) std::swap(n, m); 26 | len = A[0] = 1; 27 | for (int i = 2; i <= n; ++i) if (!mark[i]) { 28 | int k = get(n, i) - get(m, i) - get(n - m, i); 29 | while (k--) mul(i); 30 | for (int j = i; j <= n; j += i) 31 | mark[j] = 1; 32 | } 33 | while (!A[len - 1]) --len; 34 | for (int i = len - 1; ~i; --i) printf("%d", A[i]); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Luogu/P2764.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2764 3 | * Author: Rqy 4 | * Date: 2017 Dec 22 5 | * Algorithm: 匈牙利 6 | **************************************************************/ 7 | #include 8 | const int N = 155; 9 | bool map[N][N]; 10 | int mx[N], my[N]; 11 | int visy[N], time; 12 | int n; 13 | bool dfs(int x) { 14 | for (int y = 1; y <= n; ++y) if (visy[y] != time && map[x][y]) { 15 | visy[y] = time; 16 | if (!my[y] || dfs(my[y])) { 17 | mx[my[y] = x] = y; 18 | return true; 19 | } 20 | } 21 | return false; 22 | } 23 | int main() { 24 | int m, x, y; 25 | scanf("%d%d", &n, &m); 26 | while (m--) { 27 | scanf("%d%d", &x, &y); 28 | map[x][y] = 1; 29 | } 30 | for (int i = 1; i <= n; ++i) 31 | if (!mx[i]) ++time, dfs(i); 32 | int ans = 0; 33 | for (int i = 1; i <= n; ++i) 34 | if (!my[i]) { 35 | ++ans; 36 | for (int j = i; j; j = mx[j]) 37 | printf("%d ", j); 38 | printf("\n"); 39 | } 40 | printf("%d\n", ans); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2154.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2154 3 | * Author: Rqy 4 | * Date: 2018 Feb 28 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 10000050; 10 | const int mod = 20101009; 11 | typedef long long LL; 12 | int f[N], pr[N / 10], cnt; 13 | inline int calc(int n) { return (LL)n * (n + 1) / 2 % mod; } 14 | int main() { 15 | int n, m; 16 | scanf("%d%d", &n, &m); 17 | if (n > m) std::swap(n, m); 18 | f[1] = 1; 19 | for (int i = 2; i <= n; ++i) { 20 | if (!f[i]) 21 | f[pr[cnt++] = i] = 1 - i; 22 | for (int j = 0; j < cnt && pr[j] <= n / i; ++j) 23 | if (i % pr[j]) 24 | f[i * pr[j]] = f[i] * f[pr[j]]; 25 | else { 26 | f[i * pr[j]] = f[i]; 27 | break; 28 | } 29 | } 30 | int ans = 0; 31 | for (int i = 1; i <= n; ++i) 32 | ans = (ans + (LL)calc(n / i) * calc(m / i) % mod * i % mod * f[i] % mod) % mod; 33 | printf("%d\n", (ans + mod) % mod); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Luogu/P3199.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P3199 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: 最小权值回路 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 3050; 10 | const int M = 10050; 11 | const double INF = 1e12; 12 | double F[N][N], w[M]; 13 | int u[M], v[M]; 14 | int main() { 15 | int n, m, i, j; 16 | scanf("%d%d", &n, &m); 17 | for (i = 0; i < m; ++i) 18 | scanf("%d%d%lf", &u[i], &v[i], &w[i]); 19 | for (i = 0; i <= n; ++i) 20 | for (j = 1; j <= n; ++j) 21 | F[i][j] = i ? INF : 0; 22 | for (i = 0; i < n; ++i) 23 | for (j = 0; j < m; ++j) 24 | F[i + 1][v[j]] = std::min(F[i + 1][v[j]], F[i][u[j]] + w[j]); 25 | double ans = 1e7, ans1; 26 | for (i = 1; i <= n; ++i) if (F[n][i] < 1e11) { 27 | ans1 = -INF; 28 | for (j = 0; j < n; ++j) 29 | ans1 = std::max(ans1, (F[n][i] - F[j][i]) / (n - j)); 30 | ans = std::min(ans, ans1); 31 | } 32 | printf("%.8lf\n", ans); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2818.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2818 3 | * Author: Rqy 4 | * Date: 2018 Mar 01 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 10000050; 10 | typedef long long LL; 11 | int s[N], pr[N / 10], cnt; 12 | int main() { 13 | s[1] = 1; 14 | int n; 15 | scanf("%d", &n); 16 | for (int i = 2; i <= n; ++i) s[i] = -2; 17 | for (int i = 2; i <= n; ++i) { 18 | if (s[i] == -2) s[pr[cnt++] = i] = -1; 19 | for (int j = 0; j < cnt && pr[j] <= n / i; ++j) 20 | if (i % pr[j]) 21 | s[i * pr[j]] = -s[i]; 22 | else { 23 | s[i * pr[j]] = 0; 24 | break; 25 | } 26 | } 27 | for (int i = 1; i <= n; ++i) s[i] += s[i - 1]; 28 | LL ans = 0; 29 | for (int i = 0; i < cnt; ++i) 30 | for (int m = n / pr[i], j = 1, last; j <= m; j = last + 1) { 31 | last = m / (m / j); 32 | ans += (LL)(s[last] - s[j - 1]) * (m / j) * (m / j); 33 | } 34 | printf("%lld\n", ans); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4590.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4590 3 | * Author: Rqy 4 | * Date: 2018 Feb 23 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 100050; 11 | int x[N], n, k; 12 | int calc(LL m) { 13 | int ans = 0; 14 | for (LL i = 0, t = 0; i < n; ++i) 15 | if ((t = std::max(t + x[i], 0LL)) >= m) 16 | ++ans, t = 0; 17 | return ans; 18 | } 19 | int main() { 20 | scanf("%d%d", &n, &k); 21 | for (int i = 0; i < n; ++i) scanf("%d", &x[i]); 22 | if (calc(1) < k) return puts("-1") & 0; 23 | LL l = 1, r = 1e15; 24 | while (l < r) { 25 | LL mid = (l + r) / 2; 26 | if (calc(mid) > k) l = mid + 1; 27 | else r = mid; 28 | } 29 | if (calc(l) != k) return puts("-1") & 0; 30 | printf("%lld ", l); 31 | l = 1, r = 1e15; 32 | while (l < r) { 33 | LL mid = r + (l - r) / 2; 34 | if (calc(mid) < k) r = mid - 1; 35 | else l = mid; 36 | } 37 | printf("%lld\n", l); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Luogu/P2602.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2602 3 | * Author: Rqy 4 | * Date: 2017 Dec 26 5 | * Algorithm: 数位DP 6 | **************************************************************/ 7 | #include 8 | typedef long long LL; 9 | const int N = 15; 10 | LL p[N], a[N][2], b[N][2]; 11 | LL solve(LL t, int k) { 12 | int l = 0; 13 | while (t) p[l++] = t % 10, t /= 10; 14 | a[l][0] = 1; a[l][1] = 0; 15 | b[l][0] = b[l][1] = 0; 16 | for (int i = l - 1; ~i; --i) { 17 | a[i][0] = a[i + 1][0]; 18 | a[i][1] = a[i + 1][1] * 10 + a[i + 1][0] * p[i]; 19 | b[i][0] = b[i + 1][0] + (p[i] == k); 20 | b[i][1] = b[i + 1][1] * 10 + b[i + 1][0] * p[i] + 21 | (a[i + 1][1] - (!k && i != l - 1)) + 22 | a[i + 1][0] * (p[i] > k) * (!!k || i != l - 1); 23 | } 24 | return b[0][0] + b[0][1]; 25 | } 26 | int main() { 27 | LL A, B; 28 | scanf("%lld%lld", &A, &B); 29 | for (int i = 0; i < 10; ++i) { 30 | if (i) printf(" "); 31 | printf("%lld", solve(B, i) - solve(A - 1, i)); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1485.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | const int N = 1000050; 4 | int prime[1000000]; 5 | int cnt = 0; 6 | bool mark[2 * N]; 7 | int n, P; 8 | void getPrime() { 9 | mark[0] = 1; 10 | for (int i = 2; i <= 2 * n; ++i) { 11 | if (!mark[i]) 12 | prime[cnt++] = i; 13 | for (int j = 0; j < cnt && (LL)prime[j] * i <= 2 * n; ++j) { 14 | mark[prime[j] * i] = 1; 15 | if (!(i % prime[j])) break; 16 | } 17 | } 18 | } 19 | int get(int l, int p) { 20 | int ans = 0; 21 | while (l) ans += (l /= p); 22 | return ans; 23 | } 24 | int powMod(int x, int b) { 25 | int ans = 1; 26 | for (; b; b >>= 1, x = (LL)x * x % P) 27 | if (b & 1) ans = (LL)ans * x % P; 28 | return ans; 29 | } 30 | int main() { 31 | scanf("%d%d", &n, &P); 32 | getPrime(); 33 | int ans = 1; 34 | for (int i = 0; i < cnt; ++i) { 35 | int p = prime[i]; 36 | int g = 0; 37 | int tn = n + 1; 38 | while (!(tn % p)) --g, tn /= p; 39 | g += get(2 * n, p) - get(n, p) * 2; 40 | ans = (LL)ans * powMod(p, g) % P; 41 | } 42 | printf("%d\n", ans); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3207.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ3207 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int mod = 10000007; 10 | typedef long long LL; 11 | const int N = 60; 12 | LL f[N][N][2]; 13 | LL pow_mod(LL a, LL b) { 14 | LL ans = 1; 15 | for (; b; b >>= 1, a = a * a % mod) 16 | if (b & 1) ans = ans * a % mod; 17 | return ans; 18 | } 19 | int main() { 20 | LL n; 21 | scanf("%lld", &n); 22 | ++n; 23 | int k = 0; 24 | while ((1LL << k) <= n) ++k; 25 | f[k][0][0] = 1; 26 | for (int i = k; i; --i) 27 | for (int a = 0; a < k; ++a) 28 | for (int b = 0; b < 2; ++b) 29 | for (int c = 0; c < 2; ++c) 30 | if (!(c == 1 && b == 0 && ((~n >> (i - 1))) & 1)) 31 | f[i - 1][a + c][b || (c < ((n >> (i - 1)) & 1))] += f[i][a][b]; 32 | LL ans = 1; 33 | for (int i = 1; i < k; ++i) 34 | ans = ans * pow_mod(i, f[0][i][1]) % mod; 35 | printf("%lld\n", ans); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2751.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2751 3 | * Author: Rqy 4 | * Date: 2018 Feb 22 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | #include 11 | typedef long long LL; 12 | const int mod = 1000000007; 13 | std::map M; 14 | std::map > MS; 15 | int main() { 16 | int n, m, k, x, y; 17 | scanf("%d%d%d", &n, &m, &k); 18 | n = (LL)n * (n + 1) / 2 % mod; 19 | while (k--) { 20 | scanf("%d%d", &x, &y); 21 | if (!M.count(x)) { 22 | M[x] = n; 23 | MS[x] = std::set(); 24 | } 25 | if (!MS[x].count(y)) { 26 | (M[x] -= y) %= mod; 27 | MS[x].insert(y); 28 | } 29 | } 30 | int ans = 1; 31 | m -= M.size(); 32 | for (; m; m >>= 1, n = (LL)n * n % mod) 33 | if (m & 1) ans = (LL)ans * n % mod; 34 | for (std::map::iterator it = M.begin(); it != M.end(); ++it) 35 | ans = (LL)ans * it->second % mod; 36 | printf("%d\n", (ans + mod) % mod); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1416.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int N = 1050; 5 | const int M = 20050; 6 | int a[N]; 7 | int p[M]; 8 | void add(int n, int x) { 9 | for (int i = 2; i <= n; ++i) while (!(n % i)) { p[i] += x; n /= i; } 10 | } 11 | int A[M], len; 12 | void mul(int x) { 13 | for (int i = 0, t = 0; i < len || t; ++i) { 14 | if (i >= len) len = i + 1; 15 | t = (A[i] = A[i] * x + t) / 10; 16 | A[i] %= 10; 17 | } 18 | } 19 | int main() { 20 | int t, n, d, S = 0; 21 | scanf("%d%d%d", &t, &n, &d); 22 | for (int i = 1; i <= t; ++i) { 23 | scanf("%d", &a[i]); 24 | S += a[i]; 25 | } 26 | while (n--) { 27 | int y; 28 | scanf("%*d%d", &y); 29 | add(S, -1); add(a[y], 1); 30 | a[y] += d; S += d; 31 | } 32 | A[0] = len = 1; 33 | for (int i = 2; i < M; ++i) 34 | while (p[i] > 0) --p[i], mul(i); 35 | while (len--) printf("%d", A[len]); 36 | memset(A, 0, sizeof A); 37 | A[0] = len = 1; 38 | for (int i = 2; i < M; ++i) 39 | while (p[i] < 0) ++p[i], mul(i); 40 | putchar('/'); 41 | while (len--) printf("%d", A[len]); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2435.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2435.cpp 3 | * Author: Rqy 4 | * Date: 2018 Feb 28 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 1000050; 11 | typedef long long LL; 12 | int pre[N], to[N * 2], nxt[N * 2], c[N * 2], cnt, n; 13 | inline void addEdge(int x, int y, int v) { 14 | nxt[cnt] = pre[x]; 15 | c[cnt] = v; 16 | to[pre[x] = cnt++] = y; 17 | } 18 | LL ans = 0; 19 | LL abs(LL x) { return x < 0 ? -x : x; } 20 | int dfs(int x, int fa) { 21 | int siz = 1; 22 | for (int i = pre[x]; ~i; i = nxt[i]) 23 | if (to[i] != fa) { 24 | int u = dfs(to[i], x); 25 | ans += (LL)abs(n - 2 * u) * c[i]; 26 | siz += u; 27 | } 28 | return siz; 29 | } 30 | int main() { 31 | scanf("%d", &n); 32 | memset(pre, -1, sizeof pre); 33 | for (int i = 1, x, y, z; i < n; ++i) { 34 | scanf("%d%d%d", &x, &y, &z); 35 | addEdge(x, y, z); 36 | addEdge(y, x, z); 37 | } 38 | dfs(1, 0); 39 | printf("%lld\n", ans); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4195.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4195 3 | * Author: Rqy 4 | * Date: 2018 Feb 28 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | #include 11 | const int N = 200050; 12 | int a[N], b[N], cnt; 13 | std::map M; 14 | int fa[N]; 15 | int find(int x) { return fa[x] ? fa[x] = find(fa[x]) : x; } 16 | void merge(int x, int y) { if ((x = find(x)) != (y = find(y))) fa[x] = y; } 17 | int get(int x) { return M.count(x) ? M[x] : M[x] = cnt++; } 18 | int main() { 19 | int t; 20 | scanf("%d", &t); 21 | while (t--) { 22 | int n, m = cnt = 0; 23 | scanf("%d", &n); 24 | int x, y, e; 25 | M.clear(); 26 | memset(fa, 0, sizeof fa); 27 | while (n--) { 28 | scanf("%d%d%d", &x, &y, &e); 29 | x = get(x); y = get(y); 30 | if (e) merge(x, y); 31 | else { a[m] = x; b[m++] = y; } 32 | } 33 | bool ok = true; 34 | while (m--) ok = ok && find(a[m]) != find(b[m]); 35 | puts(ok ? "YES" : "NO"); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Luogu/P3343.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P3343 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: 概率,数学 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | 11 | const int N = 10; 12 | const int M = 45 + 1; 13 | int n, m, siz[1 << N], link[N]; 14 | double f[1 << N][M]; 15 | int main() { 16 | scanf("%d%d", &n, &m); 17 | int lim = 1 << n; 18 | for (int i = 0, x, y; i < m; ++i) { 19 | scanf("%d%d", &x, &y); 20 | --x; --y; 21 | link[x] |= (1 << y); 22 | link[y] |= (1 << x); 23 | } 24 | for (int S = 1; S < lim; ++S) siz[S] = siz[S & (S - 1)] + 1; 25 | for (int S1 = 3; S1 < lim; ++S1) if (S1 & 1) { 26 | for (int S2 = (S1 - 1) & S1; S2 != 0; S2 = (S2 - 1) & S1) if (S2 & 1) { 27 | int T = 0; 28 | for (int i = 0; i < n; ++i) if ((S1 >> i) & (~S2 >> i) & 1) 29 | T += siz[link[i] & S2]; 30 | for (int i = 0; i + T <= m; ++i) 31 | f[S1][i] += 1.0 / (i + T + 1) - f[S2][i + T]; 32 | } 33 | } 34 | printf("%.6lf\n", f[lim - 1][0]); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2656.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int N = 105; 5 | int A[N], B[N], C[N]; 6 | char s[105]; 7 | void div2() { 8 | for (int i = A[0]; i; --i) { 9 | if (i && (A[i] & 1)) A[i - 1] += 10; 10 | A[i] >>= 1; 11 | } 12 | while (A[0] && !A[A[0]]) --A[0]; 13 | } 14 | void add(int *a, int *b) { 15 | int t = 0; 16 | for (int i = 1; i <= b[0] || t; ++i) { 17 | t = (a[i] += b[i] + t) / 10; 18 | a[i] %= 10; 19 | } 20 | a[0] = std::max(a[0], b[0]); 21 | while (a[a[0] + 1]) ++a[0]; 22 | } 23 | int main() { 24 | int T; 25 | scanf("%d", &T); 26 | while (T--) { 27 | scanf("%s", s); 28 | if (!strcmp(s, "0")) { 29 | puts("0"); 30 | continue; 31 | } 32 | A[0] = strlen(s); 33 | for (int i = 0; i < A[0]; ++i) 34 | A[A[0] - i] = s[i] - '0'; 35 | memset(B, 0, sizeof B); 36 | memset(C, 0, sizeof C); 37 | B[0] = B[1] = 1; 38 | while (A[0]) { 39 | if (A[1] & 1) add(C, B); 40 | else add(B, C); 41 | div2(); 42 | } 43 | for (int i = C[0]; i; --i) 44 | putchar(C[i] + '0'); 45 | putchar('\n'); 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1486.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 3050; 4 | const int M = 10050; 5 | const double INF = 1e12; 6 | double _F[2][N], Fn[N], w[M], maxv[N]; 7 | int u[M], v[M]; 8 | int main() { 9 | int n, m, i, j; 10 | scanf("%d%d", &n, &m); 11 | for (i = 0; i < m; ++i) 12 | scanf("%d%d%lf", &u[i], &v[i], &w[i]); 13 | double *F = _F[0], *G = _F[1]; 14 | for (i = 1; i <= n; ++i) 15 | G[i] = 0; 16 | for (i = 0; i < n; ++i, std::swap(F, G)) { 17 | for (j = 1; j <= n; ++j) F[j] = INF; 18 | for (j = 0; j < m; ++j) 19 | F[v[j]] = std::min(F[v[j]], G[u[j]] + w[j]); 20 | } 21 | for (i = 1; i <= n; ++i) Fn[i] = G[i]; 22 | double ans = 1e7; 23 | for (i = 1; i <= n; ++i) 24 | maxv[i] = Fn[i] / n, G[i] = 0; 25 | for (i = 0; i < n; ++i, std::swap(F, G)) { 26 | for (j = 1; j <= n; ++j) F[j] = INF; 27 | for (j = 0; j < m; ++j) 28 | F[v[j]] = std::min(F[v[j]], G[u[j]] + w[j]); 29 | for (j = 1; j <= n; ++j) maxv[j] = std::max(maxv[j], (Fn[j] - F[j]) / (n - i - 1)); 30 | } 31 | for (i = 1; i <= n; ++i) if (Fn[i] < 1e11) ans = std::min(ans, maxv[i]); 32 | printf("%.8lf\n", ans); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Luogu/P2606.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2606 3 | * Author: Rqy 4 | * Date: 2017 Dec 28 5 | * Algorithm: DP,Lucas 6 | **************************************************************/ 7 | #include 8 | typedef long long LL; 9 | const int N = 2000050; 10 | int fac[N], inv[N]; 11 | int p; 12 | LL C(int n, int m) { 13 | return (LL)fac[n] * inv[m] % p * inv[n - m] % p; 14 | } 15 | int Lucas(int n, int m) { 16 | if (n % p < m % p) return 0; 17 | if (!n) return 1; 18 | return C(n % p, m % p) * Lucas(n / p, m / p) % p; 19 | } 20 | int siz[N]; 21 | int main() { 22 | int n; 23 | scanf("%d%d", &n, &p); 24 | fac[0] = fac[1] = 1; 25 | inv[0] = inv[1] = 1; 26 | for (int i = 2; i <= n && i < p; ++i) { 27 | fac[i] = (LL)fac[i - 1] * i % p; 28 | inv[i] = p - (LL)(p / i) * inv[p % i] % p; 29 | } 30 | for (int i = 2; i <= n && i < p; ++i) inv[i] = (LL)inv[i] * inv[i - 1] % p; 31 | for (int i = n; i; --i) siz[i / 2] += (++siz[i]); 32 | int ans = 1; 33 | for (int i = 1; i <= n; ++i) 34 | ans = (LL)ans * Lucas(siz[i] - 1, siz[i << 1]) % p; 35 | printf("%d\n", ans); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2301.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2301 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 50050; 10 | typedef long long LL; 11 | int s[N]; 12 | bool mark[N]; 13 | LL solve(int a, int b) { 14 | LL ans = 0; 15 | for (int i = 1, last; i <= a && i <= b; i = last + 1) { 16 | last = std::min(a / (a / i), b / (b / i)); 17 | ans += (LL)(s[last] - s[i - 1]) * (a / i) * (b / i); 18 | } 19 | return ans; 20 | } 21 | int main() { 22 | for (int i = 1; i < N; ++i) s[i] = 1; 23 | for (int i = 2; i < N; ++i) if (!mark[i]) 24 | for (int j = i; j < N; j += i) { 25 | mark[j] = 1; 26 | s[j] *= (j / i % i) ? -1 : 0; 27 | } 28 | for (int i = 1; i < N; ++i) s[i] += s[i - 1]; 29 | int n, a, b, c, d, k; 30 | scanf("%d", &n); 31 | while (n--) { 32 | scanf("%d%d%d%d%d", &a, &b, &c, &d, &k); 33 | (--a /= k); b /= k; (--c /= k); d /= k; 34 | printf("%lld\n", solve(b, d) - solve(a, d) - solve(b, c) + solve(a, c)); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1013.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 15; 4 | double A[N][N]; 5 | void gauss(int n) { 6 | for (int i = 0; i < n; ++i) { 7 | int k = i; 8 | for (int j = i + 1; j < n; ++j) 9 | if (A[j][i] > A[k][i]) 10 | k = j; 11 | for (int j = i; j <= n; ++j) 12 | std::swap(A[i][j], A[k][j]); 13 | for (k = i + 1; k < n; ++k) 14 | for (int j = n; j >= i; --j) 15 | A[k][j] -= A[i][j] * A[k][i] / A[i][i]; 16 | } 17 | for (int i = n - 1; ~i; --i) { 18 | A[i][n] /= A[i][i]; 19 | for (int j = i - 1; ~j; --j) 20 | A[j][n] -= A[i][n] * A[j][i]; 21 | } 22 | } 23 | double P0[N]; 24 | int main() { 25 | int n; 26 | scanf("%d", &n); 27 | for (int i = 0; i < n; ++i) 28 | scanf("%lf", &P0[i]); 29 | double x; 30 | for (int i = 0; i < n; ++i) { 31 | A[i][n] = .0; 32 | for (int j = 0; j < n; ++j) { 33 | scanf("%lf", &x); 34 | A[i][j] = 2 * (x - P0[j]); 35 | A[i][n] += x * x - P0[j] * P0[j]; 36 | } 37 | } 38 | gauss(n); 39 | printf("%.3lf", A[0][n]); 40 | for (int i = 1; i < n; ++i) 41 | printf(" %.3lf", A[i][n]); 42 | printf("\n"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3884.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ3884 3 | * Author: Rqy 4 | * Date: 2018 Feb 23 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | typedef long long LL; 9 | const int N = 10000050; 10 | int phi[N], pr[N / 10], cnt; 11 | int pow_mod(int a, int b, int p) { 12 | int ans = 1; 13 | for (; b; b >>= 1, a = (LL)a * a % p) 14 | if (b & 1) ans = (LL)ans * a % p; 15 | return ans; 16 | } 17 | int calc(int p) { 18 | // 2 ^ (2 ^ (...)) % p 19 | if (p == 1) return 0; 20 | return pow_mod(2, calc(phi[p]) + phi[p], p); 21 | } 22 | int main() { 23 | for (int i = 1; i < N; ++i) phi[i] = i; 24 | for (int i = 2; i < N; ++i) { 25 | if (phi[i] == i) 26 | --phi[pr[cnt++] = i]; 27 | for (int j = 0; j < cnt && (LL)pr[j] * i < N; ++j) { 28 | if (i % pr[j]) 29 | phi[i * pr[j]] = phi[i] * phi[pr[j]]; 30 | else { 31 | phi[i * pr[j]] = phi[i] * pr[j]; 32 | break; 33 | } 34 | } 35 | } 36 | int T, p; 37 | scanf("%d", &T); 38 | while (T--) { 39 | scanf("%d", &p); 40 | printf("%d\n", calc(p)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /JCYZOJ/1001.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: JCYZOJ 1001 3 | * Author: Rqy 4 | * Date: 2018 Feb 13 5 | * Algorithm: 01-BFS 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 55; 11 | const int M = 10050; 12 | int que[M * 2]; 13 | LL dis[M]; 14 | int v[N]; 15 | int main() { 16 | int T; 17 | scanf("%d", &T); 18 | while (T--) { 19 | int n, m = 0; 20 | LL t; 21 | scanf("%d%lld", &n, &t); 22 | for (int i = 0; i < n; ++i) { 23 | scanf("%d", &v[i]); 24 | m = std::max(m, v[i]); 25 | } 26 | std::fill(dis, dis + m, 5000000000000000000LL); 27 | int hd = M, tl = M; 28 | dis[que[tl++] = 0] = 0; 29 | while (hd < tl) { 30 | int x = que[hd++]; 31 | for (int i = 0; i < n; ++i) { 32 | int u = (x + v[i]) % m; 33 | int l = (x + v[i]) / m; 34 | if (dis[u] <= dis[x] + l) continue; 35 | dis[u] = dis[x] + l; 36 | que[l ? tl++ : --hd] = u; 37 | } 38 | } 39 | puts(dis[t % m] <= t / m ? "Possible" : "Impossible"); 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4591.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4591 3 | * Author: Rqy 4 | * Date: 2018 Feb 23 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | const int p = 2333; 9 | typedef long long LL; 10 | int CC[p][p], SS[p][p]; 11 | int C(LL n, LL k) { 12 | int ans = 1; 13 | while (n || k) { 14 | int n1 = n % p, k1 = k % p; 15 | ans = ans * CC[n1][k1] % p; 16 | n /= p; k /= p; 17 | } 18 | return ans; 19 | } 20 | int S(LL n, LL k) { 21 | if (k < 0) return 0; 22 | return (SS[n % p][p - 1] * S(n / p, k / p - 1) 23 | + C(n / p, k / p) * SS[n % p][k % p]) % p; 24 | } 25 | int main() { 26 | for (int i = 0; i < p; ++i) { 27 | CC[i][0] = 1; 28 | for (int j = 1; j <= i; ++j) 29 | CC[i][j] = (CC[i - 1][j] + CC[i - 1][j - 1]) % p; 30 | } 31 | for (int i = 0; i < p; ++i) { 32 | SS[i][0] = 1; 33 | for (int j = 1; j < p; ++j) 34 | SS[i][j] = (SS[i][j - 1] + CC[i][j]) % p; 35 | } 36 | int t; 37 | scanf("%d", &t); 38 | LL n, k; 39 | while (t--) { 40 | scanf("%lld%lld", &n, &k); 41 | printf("%d\n", S(n, k)); 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /BZOJ/BZOJ5073.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ5073 3 | * Author: Rqy 4 | * Date: 2018 Feb 28 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 100050; 10 | const int K = 105; 11 | int f[K][N], g[K][N]; 12 | char s[N], p[N]; 13 | int main() { 14 | int T; 15 | scanf("%d", &T); 16 | while (T--) { 17 | int n, m, x; 18 | scanf("%d%d%d", &n, &m, &x); 19 | scanf("%s%s", s + 1, p); 20 | ++n; 21 | s[0] = '\0'; 22 | for (int i = 0; i <= x; ++i) 23 | for (int j = 0; j < n; ++j) 24 | f[i][j] = -1, g[i][j] = 0; 25 | for (int i = 0; i <= x; ++i) 26 | for (int j = 0; j < n; ++j) { 27 | g[i][j + 1] = std::max(g[i][j + 1], g[i][j]); 28 | if (p[g[i][j]] == s[j + 1]) 29 | f[i + 1][j + 1] = std::max(f[i + 1][j + 1], g[i][j] + 1); 30 | g[i][j + 1] = std::max(g[i][j + 1], f[i][j]); 31 | if (f[i][j] >= 0 && p[f[i][j]] == s[j + 1]) 32 | f[i][j + 1] = std::max(f[i][j + 1], f[i][j] + 1); 33 | } 34 | puts(g[x][n] >= m ? "YES" : "NO"); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Luogu/P1822.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P1822 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: DFS, BFS 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | bool mm[10000050]; 11 | int p[10], A, B; 12 | int queue[40000], num, head, tail; 13 | void dfs(int x, LL y, int p10) { 14 | if (y > B) return; 15 | if (x == 0) { 16 | int last = y / (p10 / 10); 17 | if (!last) return; 18 | dfs(x, y + (LL)last * p10, p10 * 10); 19 | if (y >= A && y <= B) ++num; 20 | if (p10 < B) queue[tail++] = y; 21 | return; 22 | } 23 | int last = y / (p10 / 10), nxt = x % 10; 24 | x /= 10; 25 | if (last - nxt >= 0) dfs(x, y + p10 * (last - nxt), p10 * 10); 26 | if (nxt && last + nxt < 10) dfs(x, y + p10 * (last + nxt), p10 * 10); 27 | } 28 | int main() { 29 | scanf("%d%d", &A, &B); 30 | head = tail = num = 0; 31 | queue[tail++] = 7; 32 | if (A <= 7 && B >= 7) ++num; 33 | do 34 | for (int i = 0; i < 10; ++i) 35 | dfs(queue[head], i, 10); 36 | while (++head < tail); 37 | printf("%d\n", num); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1491.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long LL; 4 | const LL INF = 1000000000000000LL; 5 | const int N = 105; 6 | LL d[N][N], C[N][N]; 7 | int main() { 8 | int n, m; 9 | std::fill(d[0], d[N], INF); 10 | std::fill(C[0], C[N], 1); 11 | scanf("%d%d", &n, &m); 12 | for (int i = 1; i <= n; ++i) 13 | d[i][i] = 0; 14 | while (m--) { 15 | int x, y; 16 | scanf("%d%d", &x, &y); 17 | scanf("%lld", &d[x][y]); 18 | d[y][x] = d[x][y]; 19 | } 20 | for (int k = 1; k <= n; ++k) 21 | for (int i = 1; i <= n; ++i) if (i != k) 22 | for (int j = 1; j <= n; ++j) if (j != i && j != k) { 23 | if (d[i][j] > d[i][k] + d[k][j]) { 24 | d[i][j] = d[i][k] + d[k][j]; 25 | C[i][j] = C[i][k] * C[k][j]; 26 | } else if (d[i][j] == d[i][k] + d[k][j]) 27 | C[i][j] += C[i][k] * C[k][j]; 28 | } 29 | for (int v = 1; v <= n; ++v) { 30 | double I = .0; 31 | for (int i = 1; i <= n; ++i) if (v != i) 32 | for (int j = 1; j <= n; ++j) if (v != j) 33 | if (d[i][v] + d[v][j] == d[i][j]) 34 | I += (double)(C[i][v] * C[v][j]) / C[i][j]; 35 | printf("%.3lf\n", I); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2038.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long LL; 5 | const int N = 50050; 6 | int C[N], t[N]; 7 | int p[N], L[N], R[N]; 8 | LL ansv[N][2]; 9 | int k; 10 | bool cmp(int a, int b) { 11 | if (L[a] / k != L[b] / k) return L[a] / k < L[b] / k; 12 | return R[a] < R[b]; 13 | } 14 | LL gcd(LL a, LL b) { 15 | return a && b ? gcd(b, a % b) : a | b; 16 | } 17 | int main() { 18 | int n, m; 19 | scanf("%d%d", &n, &m); 20 | for (int i = 0; i < n; ++i) scanf("%d", &C[i]); 21 | for (int i = 0; i < m; ++i) scanf("%d%d", &L[i], &R[i]), p[i] = i; 22 | k = 0; 23 | while (k * k < n) ++k; 24 | std::sort(p, p + m, cmp); 25 | LL ans = 0, l = 0, r = -1, q, g; 26 | for (int i = 0; i < m; ++i) { 27 | while (r < R[p[i]] - 1) ans += t[C[++r]]++; 28 | while (l > L[p[i]] - 1) ans += t[C[--l]]++; 29 | while (r > R[p[i]] - 1) ans -= --t[C[r--]]; 30 | while (l < L[p[i]] - 1) ans -= --t[C[l++]]; 31 | q = r - l; 32 | q = q * (q + 1) / 2; 33 | g = gcd(ans, q); 34 | ansv[p[i]][0] = ans / g; 35 | ansv[p[i]][1] = q / g; 36 | } 37 | for (int i = 0; i < m; ++i) 38 | printf("%lld/%lld\n", ansv[i][0], ansv[i][1]); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1911.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1911 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 1000050; 11 | LL a, b, c; 12 | LL f[N], x[N], y[N]; 13 | LL get(int i, int j) { 14 | LL s = x[j] - x[i]; 15 | return f[i] + (a * s + b) * s + c; 16 | } 17 | bool check(int i, int j, int k) { 18 | LL x1 = x[j] - x[i], y1 = y[j] - y[i]; 19 | LL x2 = x[k] - x[i], y2 = y[k] - y[i]; 20 | // return y2 / x2 > y1 / x1 21 | return y2 * x1 > y1 * x2; 22 | } 23 | int que[N], hd, tl; 24 | int main() { 25 | int n; 26 | scanf("%d%lld%lld%lld", &n, &a, &b, &c); 27 | f[0] = 0; x[0] = 0; y[0] = 0; 28 | tl++; 29 | for (int i = 1; i <= n; ++i) { 30 | scanf("%lld", &x[i]); x[i] += x[i - 1]; 31 | while (hd < tl - 1 && get(que[hd], i) < get(que[hd + 1], i)) ++hd; 32 | f[i] = get(que[hd], i); 33 | y[i] = f[i] + (a * x[i] - b) * x[i]; 34 | while (hd < tl - 1 && check(que[tl - 2], que[tl - 1], i)) --tl; 35 | que[tl++] = i; 36 | } 37 | printf("%lld\n", f[n]); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Luogu/P1963.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P1963 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: 匈牙利 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 10050; 11 | int A[N][2]; 12 | int mx[N], my[N], vis[N], time; 13 | bool match(int x) { 14 | for (int i = 0; i < 2; ++i) { 15 | int y = A[x][i]; 16 | if (vis[y] == time) continue; 17 | vis[y] = time; 18 | if (my[y] == -1 || match(my[y])) { 19 | mx[my[y] = x] = y; 20 | return true; 21 | } 22 | } 23 | return false; 24 | } 25 | int main() { 26 | int n, x; 27 | scanf("%d", &n); 28 | for (int i = 0; i < n; ++i) { 29 | scanf("%d", &x); 30 | A[i][0] = (i - x + n) % n; 31 | A[i][1] = (i + x) % n; 32 | if (A[i][0] > A[i][1]) std::swap(A[i][0], A[i][1]); 33 | mx[i] = my[i] = -1; 34 | } 35 | for (int i = n - 1; ~i; --i) { 36 | ++time; 37 | if (!match(i)) { 38 | printf("No Answer"); 39 | return 0; 40 | } 41 | } 42 | for (int i = 0; i < n; ++i) 43 | printf("%d%c", mx[i], i == n - 1 ? '\n' : ' '); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2760.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2760 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 105; 10 | char T[N][N][N]; 11 | int m[N], len[N][N], width[N]; 12 | inline int getLen(char *s) { 13 | int ans = 1; 14 | while (*s) ans += (*(s++) == ','); 15 | return ans; 16 | } 17 | int main() { 18 | int n; 19 | scanf("%d", &n); 20 | int h = 0; 21 | for (int i = 0; i < n; ++i) { 22 | scanf("%d", &m[i]); 23 | ++m[i]; 24 | h = std::max(h, m[i]); 25 | width[i] = 0; 26 | for (int j = 0; j < m[i]; ++j) { 27 | scanf("%s", T[i][j]); 28 | len[i][j] = getLen(T[i][j]); 29 | width[i] = std::max(width[i], len[i][j]); 30 | } 31 | width[i] += (i != n - 1); 32 | } 33 | for (int i = 0; i < h; ++i) { 34 | for (int j = 0; j < n; ++j) { 35 | printf("%s", i >= m[j] ? "" : T[j][i]); 36 | int x = i >= m[j] ? 1 : len[j][i]; 37 | while (x < width[j]) { 38 | putchar(','); 39 | ++x; 40 | } 41 | } 42 | putchar('\n'); 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int N = 1005; 3 | int prime[N], cnt; 4 | int e[N]; 5 | void getPrime() { 6 | int i, j; 7 | cnt = 0; 8 | for (i = 2; i < N; ++i) { 9 | for (j = 0; j < cnt; ++j) 10 | if (!(i % prime[j])) break; 11 | if (j == cnt) prime[cnt++] = i; 12 | } 13 | } 14 | void getFac(int n, int op) { 15 | for (int i = 0, k = n; i < cnt; ++i, k = n) 16 | while (k) { 17 | k /= prime[i]; 18 | e[i] += k * op; 19 | } 20 | } 21 | int ans[100000], len; 22 | void mul(int t) { 23 | int i, c; 24 | for (i = 0, c = 0; i < len || c; ++i) { 25 | c = (ans[i] = ans[i] * t + c) / 10; 26 | ans[i] %= 10; 27 | } 28 | len = i; 29 | } 30 | int main() { 31 | getPrime(); 32 | int n; 33 | scanf("%d", &n); 34 | getFac(n - 2, 1); 35 | int tot = n - 2, m = n; 36 | for (int i = 0; i < n; ++i) { 37 | int t; 38 | scanf("%d", &t); 39 | if (t < 0) continue; 40 | --m; 41 | tot -= --t; 42 | getFac(t, -1); 43 | } 44 | if (tot < 0) return printf("0\n"), 0; 45 | getFac(tot, -1); 46 | ans[0] = len = 1; 47 | for (int i = 0; i < cnt; ++i) 48 | while (e[i]--) mul(prime[i]); 49 | while (tot--) mul(m); 50 | while (len--) printf("%d", ans[len]); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1031.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 400050; 4 | char s[N]; 5 | int sa[N]; 6 | int c[N], tmp[2][N]; 7 | int main() { 8 | scanf("%s", s); 9 | int n, m = 128, i, j; 10 | int *x = tmp[0], *y = tmp[1]; 11 | for (i = 0; i < m; ++i) c[i] = 0; 12 | for (n = 0; s[n]; ++n) ++c[x[n] = s[n]]; 13 | for (i = 0; i < n; ++i) ++c[x[n + i] = s[n + i] = s[i]]; 14 | n *= 2; 15 | for (i = 1; i < m; ++i) c[i] += c[i - 1]; 16 | for (i = n - 1; i >= 0; --i) sa[--c[x[i]]] = i; 17 | for (int k = 1; k < n; k <<= 1) { 18 | j = 0; 19 | for (i = n - k; i < n; ++i) y[j++] = i; 20 | for (i = 0; i < n; ++i) if (sa[i] >= k) y[j++] = sa[i] - k; 21 | for (i = 0; i < m; ++i) c[i] = 0; 22 | for (i = 0; i < n; ++i) ++c[x[y[i]]]; 23 | for (i = 1; i < m; ++i) c[i] += c[i - 1]; 24 | for (i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i]; 25 | std::swap(x, y); 26 | j = 0; 27 | x[sa[0]] = j++; 28 | for (i = 1; i < n; ++i) 29 | x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k] ? j - 1 : j++; 30 | if ((m = j) == n) break; 31 | } 32 | for (int i = 0; i < n; ++i) 33 | if (sa[i] < n / 2) 34 | putchar(s[(sa[i] + n / 2 - 1) % (n / 2)]); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1217.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1217 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 1050; 11 | int a[N], dep[N], p[N]; 12 | std::vector to[N]; 13 | int mark[N]; 14 | inline bool cmp(int a, int b) { return dep[a] > dep[b]; } 15 | int main() { 16 | int n; 17 | scanf("%d", &n); 18 | int ans = 0; 19 | for (int i = 2; i <= n; ++i) { 20 | scanf("%d", &a[p[i] = i]); 21 | dep[i] = dep[a[i]] + 1; 22 | to[a[i]].push_back(i); 23 | } 24 | a[1] = p[1] = 1; 25 | std::sort(p + 1, p + n + 1, cmp); 26 | for (int o = 1; o <= n; ++o) { 27 | int i = p[o]; 28 | if (mark[i]) continue; 29 | i = a[a[i]]; 30 | ++ans; 31 | mark[a[a[i]]] = mark[a[i]] = true; 32 | for (int j = 0; j < to[a[i]].size(); ++j) mark[to[a[i]][j]] = true; 33 | for (int j = 0; j < to[i].size(); ++j) { 34 | int t = to[i][j]; 35 | mark[t] = true; 36 | for (int b = 0; b < to[t].size(); ++b) 37 | mark[to[t][b]] = true; 38 | } 39 | } 40 | printf("%d\n", ans); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4805.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4805 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 2000000050; 10 | const int M = 1000050; 11 | typedef long long LL; 12 | LL s[M], s2[N / M + 50]; 13 | int pr[M], cnt; 14 | void sieve() { 15 | s[1] = 1; 16 | for (int i = 2; i < M; ++i) { 17 | if (!s[i]) s[pr[cnt++] = i] = i - 1; 18 | for (int j = 0; j < cnt && (LL)pr[j] * i < M; ++j) 19 | if (i % pr[j]) 20 | s[i * pr[j]] = s[i] * s[pr[j]]; 21 | else { 22 | s[i * pr[j]] = s[i] * pr[j]; 23 | break; 24 | } 25 | } 26 | for (int i = 2; i < M; ++i) s[i] += s[i - 1]; 27 | } 28 | int n; 29 | LL S(int d) { 30 | if (n / d < M) return s[n / d]; 31 | LL &ans = s2[d]; 32 | if (ans) return ans; 33 | int m = n / d; 34 | ans = (LL)m * (m + 1) / 2; 35 | for (int i = 2, last; i <= m; i = last + 1) { 36 | last = m / (m / i); 37 | ans -= (last - i + 1) * S(d * i); 38 | } 39 | return ans; 40 | } 41 | int main() { 42 | sieve(); 43 | scanf("%d", &n); 44 | printf("%lld\n", S(1)); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4813.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4813 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 105; 11 | typedef std::vector VI; 12 | int f[N][N], g[N][N]; 13 | int n, m; 14 | VI to[N]; 15 | void dfs(int x, int fa) { 16 | for (int i = 0; i <= m; ++i) f[x][i] = g[x][i] = 1; 17 | for (VI::iterator it = to[x].begin(); it != to[x].end(); ++it) { 18 | int u = *it; 19 | if (u == fa) continue; 20 | dfs(u, x); 21 | for (int i = m; ~i; --i) 22 | for (int j = 0; j < i; ++j) { 23 | f[x][i] = std::max(f[x][i], g[x][j] + f[u][i - j - 1]); 24 | if (i - j > 1) { 25 | f[x][i] = std::max(f[x][i], f[x][j] + g[u][i - j - 2]); 26 | g[x][i] = std::max(g[x][i], g[x][j] + g[u][i - j - 2]); 27 | } 28 | } 29 | } 30 | } 31 | int main() { 32 | scanf("%d%d", &n, &m); 33 | for (int i = 1, x, y; i < n; ++i) { 34 | scanf("%d%d", &x, &y); 35 | to[x].push_back(y); 36 | to[y].push_back(x); 37 | } 38 | dfs(0, -1); 39 | printf("%d\n", f[0][m]); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1015.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int M = 200050; 5 | int cnt; 6 | int ans[M]; 7 | std::vector G[M * 2]; 8 | int deletetime[M * 2]; 9 | int del[M * 2]; 10 | int f[M * 2]; 11 | int Find(int x) { return (f[x] >= 0) ? (f[x] = Find(f[x])) : x; } 12 | void Union(int x, int y) { 13 | int xx = Find(x), yy = Find(y); 14 | if (xx != yy) { 15 | f[xx] = yy; 16 | --cnt; 17 | } 18 | } 19 | void Union(int x) { 20 | for (int i = G[x].size() - 1; i >= 0; --i) 21 | if (deletetime[G[x][i]] <= deletetime[x]) Union(x, G[x][i]); 22 | } 23 | int main() { 24 | int n, m, x, y; 25 | scanf("%d%d", &n, &m); 26 | for (int i = 0; i < m; ++i) { 27 | scanf("%d%d", &x, &y); 28 | G[x].push_back(y); 29 | G[y].push_back(x); 30 | } 31 | int k; 32 | scanf("%d", &k); 33 | for (int i = 0; i < k; ++i) { 34 | scanf("%d", &del[i]); 35 | deletetime[del[i]] = k - i; 36 | } 37 | cnt = n; 38 | memset(f, -1, sizeof f); 39 | for (int i = 0; i < n; ++i) 40 | if (!deletetime[i]) Union(i); 41 | ans[k] = cnt - k; 42 | for (int i = k - 1; i >= 0; --i) { 43 | Union(del[i]); 44 | ans[i] = cnt - i; 45 | } 46 | for (int i = 0; i <= k; ++i) printf("%d\n", ans[i]); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2160.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2160 3 | * Author: Rqy 4 | * Date: 2018 Feb 28 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 1000050; 11 | const int mod = 19930726; 12 | typedef long long LL; 13 | int pow_mod(int a, int b) { 14 | int ans = 1; 15 | for (; b; b >>= 1, a = (LL)a * a % mod) 16 | if (b & 1) ans = (LL)ans * a % mod; 17 | return ans; 18 | } 19 | char s[N]; 20 | int c[N], f[N]; 21 | int main() { 22 | int n; 23 | LL k; 24 | scanf("%d%lld%s", &n, &k, s); 25 | ++c[f[0] = 1]; 26 | int r = 0; 27 | for (int i = 1; i < n; ++i) { 28 | f[i] = 0; 29 | if (r + f[r] > i) f[i] = std::min(f[2 * r - i], f[r] + r - i); 30 | while (f[i] <= i && i + f[i] < n && s[i - f[i]] == s[i + f[i]]) ++f[i]; 31 | if (i + f[i] > r + f[r]) r = i; 32 | ++c[f[i]]; 33 | } 34 | for (int i = n; i; --i) c[i] += c[i + 1]; 35 | int ans = 1; 36 | for (int i = n; i >= 1; --i) { 37 | int t = std::min(k, (LL)c[i]); 38 | ans = (LL)ans * pow_mod(2 * i - 1, t) % mod; 39 | k -= t; 40 | } 41 | printf("%d\n", k ? -1 : ans); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2152.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define forEdge(x, i) for (int i = pre[(x)]; ~i; i = nxt[i]) 5 | typedef long long LL; 6 | const int N = 20050; 7 | int pre[N], nxt[N * 2], to[N * 2], w[N * 2]; 8 | int cnt = 0; 9 | int n; 10 | LL f[N][3]; 11 | inline void addEdge(int x, int y, int v) { 12 | to[cnt] = y; w[cnt] = v; 13 | nxt[cnt] = pre[x]; pre[x] = cnt++; 14 | to[cnt] = x; w[cnt] = v; 15 | nxt[cnt] = pre[y]; pre[y] = cnt++; 16 | } 17 | LL dfs(int x, int fa) { 18 | LL ans = 0; 19 | f[x][0] = 1; 20 | forEdge(x, i) if (to[i] != fa) { 21 | ans += dfs(to[i], x); 22 | for (int k = 0; k < 3; ++k) 23 | ans += f[x][k] * f[to[i]][(6 - k - w[i]) % 3]; 24 | for (int k = 0; k < 3; ++k) 25 | f[x][(k + w[i]) % 3] += f[to[i]][k]; 26 | } 27 | return ans; 28 | } 29 | LL gcd(LL a, LL b) { 30 | return b ? gcd(b, a % b) : a; 31 | } 32 | int main() { 33 | memset(pre, -1, sizeof pre); 34 | int n, x, y, v; 35 | scanf("%d", &n); 36 | for (int i = 1; i < n; ++i) { 37 | scanf("%d%d%d", &x, &y, &v); 38 | addEdge(--x, --y, v %= 3); 39 | } 40 | LL ans = dfs(0, -1) * 2 + n; 41 | LL t = (LL)n * n; 42 | LL g = gcd(ans, t); 43 | printf("%lld/%lld\n", ans / g, t / g); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1087.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int N = 9; 3 | const int K = 25 + 1; 4 | typedef long long LL; 5 | LL _f[2][89][K], (*f)[K] = _f[0], (*g)[K] = _f[1], (*tmp)[K], ans; 6 | int siz[1 << N], n, k, all, m, i, j, t, l, s; 7 | inline bool check1(int x) { 8 | if (x & (x >> 1)) return false; 9 | return true; 10 | } 11 | inline bool check2(int x, int y) { 12 | if (x & y) return false; 13 | return check1(x | y); 14 | } 15 | int vaild[89]; 16 | int main() { 17 | scanf("%d%d", &n, &k); 18 | if (k >= K) return puts("0") & 0; 19 | all = (1 << n) - 1; 20 | for (s = 0; s < all; ++s) 21 | for (j = 1; j < all && (~s & j); j <<= 1) 22 | siz[s | j] = siz[s] + 1; 23 | m = 0; 24 | for (s = 0; s <= all; ++s) 25 | if (check1(s)) vaild[m++] = s; 26 | f[0][0] = 1; 27 | for (i = 0; i < n; ++i) { 28 | for (j = 0; j < m; ++j) 29 | for (t = 0; t <= k; ++t) 30 | g[j][t] = 0; 31 | for (j = 0; j < m; ++j) 32 | for (t = 0; t < m; ++t) if (check2(vaild[j], vaild[t])) 33 | for (s = siz[vaild[t]], l = 0; l + s <= k; ++l) 34 | g[t][l + s] += f[j][l]; 35 | tmp = f; 36 | f = g; 37 | g = tmp; 38 | } 39 | ans = 0; 40 | for (i = 0; i < m; ++i) 41 | ans += f[i][k]; 42 | printf("%lld\n", ans); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4804.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4804 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 10000050; 10 | typedef long long LL; 11 | LL s[N]; 12 | int pr[N], cnt; 13 | void sieve() { 14 | memset(s, -1, sizeof s); 15 | s[0] = 0; 16 | s[1] = 1; 17 | for (int i = 2; i < N; ++i) { 18 | if (!~s[i]) 19 | s[pr[cnt++] = i] = i - 2; 20 | for (int j = 0; j < cnt && (LL)i * pr[j] < N; ++j) 21 | if (i % pr[j]) s[i * pr[j]] = s[i] * s[pr[j]]; 22 | else { 23 | if (!(i / pr[j] % pr[j])) s[i * pr[j]] = s[i] * pr[j]; 24 | else s[i * pr[j]] = s[i / pr[j]] * (pr[j] - 1) * (pr[j] - 1); 25 | break; 26 | } 27 | } 28 | for (int i = 2; i < N; ++i) s[i] += s[i - 1]; 29 | } 30 | int main() { 31 | sieve(); 32 | int T; 33 | scanf("%d", &T); 34 | while (T--) { 35 | int n; 36 | scanf("%d", &n); 37 | LL ans = 0; 38 | for (int i = 1, last; i <= n; i = last + 1) { 39 | last = n / (n / i); 40 | ans += (LL)(n / i) * (n / i) * (s[last] - s[i - 1]); 41 | } 42 | printf("%lld\n", ans); 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3994.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long LL; 4 | const int N = 50050; 5 | LL t[N]; 6 | LL calcT(int n) { 7 | LL ans = 0; 8 | for (int i = 1, last; i <= n; i = last + 1) { 9 | last = n / (n / i); 10 | ans += n / i * (last - i + 1); 11 | } 12 | return ans; 13 | } 14 | bool mark[N]; 15 | int pr[N], pcnt = 0, mu[N], S[N]; 16 | void getMu() { 17 | mu[1] = 1; 18 | for (int i = 2; i < N; ++i) { 19 | if (!mark[i]) mu[pr[pcnt++] = i] = -1; 20 | for (int j = 0; j < pcnt && (LL)i * pr[j] < N; ++j) { 21 | mark[i * pr[j]] = 1; 22 | if (!(i % pr[j])) { 23 | mu[i * pr[j]] = 0; 24 | break; 25 | } 26 | mu[i * pr[j]] = -mu[i]; 27 | } 28 | } 29 | for (int i = 1; i < N; ++i) S[i] = S[i - 1] + mu[i]; 30 | } 31 | LL solve(int n, int m) { 32 | LL ans = 0; 33 | for (int i = 1, last; i <= n && i <= m; i = last + 1) { 34 | last = std::min(n / (n / i), m / (m / i)); 35 | ans += t[n / i] * t[m / i] * (S[last] - S[i - 1]); 36 | } 37 | return ans; 38 | } 39 | int main() { 40 | for (int i = 1; i < N; ++i) t[i] = calcT(i); 41 | getMu(); 42 | int T, n, m; 43 | scanf("%d", &T); 44 | while (T--) { 45 | scanf("%d%d", &n, &m); 46 | printf("%lld\n", solve(n, m)); 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Luogu/P2698.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2698 3 | * Author: Rqy 4 | * Date: 2017 Dec 22 5 | * Algorithm: 单调队列 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 100050; 10 | struct Water{ 11 | int x, h, ord; 12 | bool operator<(const Water &s) const { 13 | return x < s.x; 14 | } 15 | }ws[N]; 16 | int q1[N], q2[N], h1, h2, t1, t2; 17 | int main() { 18 | int n, d; 19 | scanf("%d%d", &n, &d); 20 | for (int i = 0; i < n; ++i) { 21 | scanf("%d%d", &ws[i].x, &ws[i].h); 22 | ws[i].ord = i; 23 | } 24 | std::sort(ws, ws + n); 25 | h1 = h2 = t1 = t2 = 0; 26 | int ans = 10000000; 27 | for (int i = 0, j = 0; i < n; ++i) { 28 | while (j < n && (i == j || ws[q1[h1]].h - ws[q2[h2]].h < d)) { 29 | while (t1 > h1 && ws[q1[t1 - 1]].h < ws[j].h) --t1; 30 | while (t2 > h2 && ws[q2[t2 - 1]].h > ws[j].h) --t2; 31 | q1[t1++] = q2[t2++] = j++; 32 | } 33 | if (ws[q1[h1]].h - ws[q2[h2]].h < d) break; 34 | ans = std::min(ans, ws[j - 1].x - ws[i].x); 35 | if (q1[h1] == i) ++h1; 36 | if (q2[h2] == i) ++h2; 37 | } 38 | if (ans >= 10000000) printf("-1\n"); 39 | else printf("%d\n", ans); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Luogu/P2709.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2709 3 | * Author: Rqy 4 | * Date: 2017 Dec 26 5 | * Algorithm: 莫队 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 50050; 11 | int A[N], cnt[N], B; 12 | struct Query{ 13 | int i, l, r; 14 | bool operator<(const Query &t)const { 15 | if (l / B != t.l / B) return l / B < t.l / B; 16 | return r < t.r; 17 | } 18 | }q[N]; 19 | LL ans[N]; 20 | int main() { 21 | int n, m; 22 | scanf("%d%d%*d", &n, &m); 23 | while (B * B < n) ++B; 24 | for (int i = 0; i < n; ++i) 25 | scanf("%d", &A[i]); 26 | for (int i = 0; i < m; ++i) { 27 | scanf("%d%d", &q[i].l, &q[i].r); 28 | --q[i].l, --q[i].r; 29 | q[i].i = i; 30 | } 31 | std::sort(q, q + m); 32 | int l = 0, r = -1; 33 | LL anst = 0; 34 | for (int i = 0; i < m; ++i) { 35 | while (l > q[i].l) anst += 2 * (cnt[A[--l]]++) + 1; 36 | while (r < q[i].r) anst += 2 * (cnt[A[++r]]++) + 1; 37 | while (l < q[i].l) anst -= 2 * (--cnt[A[l++]]) + 1; 38 | while (r > q[i].r) anst -= 2 * (--cnt[A[r--]]) + 1; 39 | ans[q[i].i] = anst; 40 | } 41 | for (int i = 0; i < m; ++i) 42 | printf("%lld\n", ans[i]); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Luogu/P2765.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2765 3 | * Author: Rqy 4 | * Date: 2017 Dec 22 5 | * Algorithm: 公式 6 | **************************************************************/ 7 | #include 8 | int n, n1, ans, i, j, s, t; 9 | inline int sqr(int x) { return x * x; } 10 | const int L = 9050; 11 | char ss[L], *l = ss; 12 | void put(int x, char c) { 13 | static int p[20]; 14 | int top = 0; 15 | while (x) { 16 | p[top++] = x % 10; 17 | x /= 10; 18 | } 19 | while (top--) *(l++) = char(p[top] + '0'); 20 | *(l++) = c; 21 | } 22 | int main() { 23 | scanf("%d", &n); 24 | n1 = (n + 1) / 2; 25 | if (n & 1) 26 | ans = 2 * n1 * n1; 27 | else 28 | ans = 2 * n1 * (n1 + 1); 29 | put(ans - 1, '\n'); 30 | for (i = 1; i <= n; ++i) { 31 | s = i / 2; 32 | if (i & 1) { 33 | if (i != 1) put(2 * s * (s + 1), ' '); 34 | for (j = 0, t = -1; j <= n - i; ++j, t *= -1) 35 | put(2 * sqr(s + j / 2 + 1) + t * (2 * s + j / 2 + 1), ' '); 36 | } else { 37 | put(2 * s * s, ' '); 38 | for (j = 0, t = -1; j < n - i; ++j, t *= -1) 39 | put(2 * sqr(s + j / 2 + 1) + t * (j / 2 + 1), ' '); 40 | } 41 | *(l++) = '\n'; 42 | } 43 | *l = '\0'; 44 | puts(ss); 45 | printf("%d\n", l - ss); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1297.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | const int N = 15; 4 | const int mod = 2009; 5 | int nn; 6 | struct Matrix{ 7 | int v[N * 9][N * 9]; 8 | Matrix() { 9 | memset(v, 0, sizeof v); 10 | } 11 | friend Matrix operator*(const Matrix &a, const Matrix &b) { 12 | Matrix ans; 13 | for (int i = 0; i < nn; ++i) 14 | for (int j = 0; j < nn; ++j) if (a.v[i][j]) 15 | for (int k = 0; k < nn; ++k) 16 | ans.v[i][k] = (ans.v[i][k] + a.v[i][j] * b.v[j][k]) % mod; 17 | return ans; 18 | } 19 | friend Matrix operator+(const Matrix &a, const Matrix &b) { 20 | Matrix ans; 21 | for (int i = 0; i < nn; ++i) 22 | for (int j = 0; j < nn; ++j) 23 | ans.v[i][j] = (a.v[i][j] + b.v[i][j]) % mod; 24 | return ans; 25 | } 26 | }; 27 | Matrix D; 28 | Matrix ans; 29 | int main() { 30 | int n, t; 31 | scanf("%d%d", &n, &t); 32 | nn = n * 9; 33 | int x; 34 | for (int i = 0; i < n; ++i) 35 | for (int j = 0; j < n; ++j) { 36 | scanf("%1d", &x); 37 | if (x) D.v[i][(x - 1) * n + j] = 1; 38 | } 39 | for (int i = 0; i < 8 * n; ++i) 40 | D.v[i + n][i] = 1; 41 | for (int i = 0; i < n; ++i) 42 | ans.v[i][i] = 1; 43 | for (; t; D = D * D, t >>= 1) 44 | if (t & 1) ans = ans * D; 45 | printf("%d\n", ans.v[0][n - 1]); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2568.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long LL; 5 | const int mod = 1000000009; 6 | inline int lowbit(int x) { return x & -x; } 7 | const int N = 1 << 16; 8 | int s[16][N]; 9 | void add(int a, int k, int x) { 10 | for (++k; k <= N; k += lowbit(k)) 11 | s[a][k] += x; 12 | } 13 | int query(int a, int k) { 14 | int ans = 0; 15 | for (++k; k; k -= lowbit(k)) ans += s[a][k]; 16 | return ans; 17 | } 18 | std::multiset S; 19 | int main() { 20 | int m, x, cnt = 0, num = 0; 21 | scanf("%d", &m); 22 | char s[10]; 23 | while (m--) { 24 | scanf("%s%d", s, &x); 25 | if (*s == 'I') { 26 | ++num; 27 | S.insert(x -= cnt); 28 | for (int i = 0; i < 16; ++i) add(i, ((1 << (i + 1)) - 1) & x, 1); 29 | } else if (*s == 'D') { 30 | int l = S.count(x -= cnt); 31 | num -= l; 32 | S.erase(x); 33 | for (int i = 0; i < 16; ++i) add(i, ((1 << (i + 1)) - 1) & x, -l); 34 | } else if (*s == 'A') { 35 | cnt += x; 36 | } else { 37 | int r = ((1 << x) - cnt - 1) & ((1 << (x + 1)) - 1), l = (-cnt) & ((1 << (x + 1)) - 1); 38 | if (r >= l) 39 | printf("%d\n", num - query(x, r) + query(x, l - 1)); 40 | else 41 | printf("%d\n", query(x, l - 1) - query(x, r)); 42 | } 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | typedef long long LL; 6 | const LL wei = 10000000; 7 | LL b[110][200]; 8 | void mul(LL *x, int a) { 9 | int i, c; 10 | for (i = 1, c = 0; i <= x[0] || c; ++i) { 11 | c = (x[i] = x[i] * a + c) / wei; 12 | x[i] %= wei; 13 | } 14 | x[0] = i - 1; 15 | } 16 | void sub(LL *x, const LL *a) { 17 | int i, c; 18 | for (i = 1, c = 0; i <= a[0] || c; ++i) { 19 | c = (wei - 1 - (x[i] = x[i] - a[i] - c)) / wei; 20 | x[i] = (x[i] + wei) % wei; 21 | } 22 | while (!x[x[0]]) --x[0]; 23 | } 24 | void sub(LL *x, int a) { 25 | int i, c = -a; 26 | for (i = 1; c; ++i) { 27 | c = (x[i] = x[i] + c) / wei; 28 | x[i] = (x[i] + wei) % wei; 29 | } 30 | while (!x[x[0]]) --x[0]; 31 | } 32 | int main() { 33 | //freopen("bzoj_1002.in", "r", stdin); 34 | //freopen("bzoj_1002.out", "w", stdout); 35 | int n, i; 36 | scanf("%d", &n); 37 | b[0][0] = b[0][1] = 0; 38 | b[1][0] = b[1][1] = 1; 39 | for (i = 2; i <= n + 1; ++i) { 40 | memcpy(b[i], b[i - 1], sizeof b[i]); 41 | mul(b[i], 3); 42 | sub(b[i], b[i - 2]); 43 | } 44 | sub(b[n + 1], b[n - 1]); 45 | sub(b[n + 1], 2); 46 | printf("%lld", b[n + 1][b[n + 1][0]]); 47 | for (i = b[n + 1][0] - 1; i; --i) 48 | printf("%07lld", b[n + 1][i]); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /BZOJ/BZOJ4443.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4443 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 300; 11 | int A[N][N], n, m; 12 | bool able[N][N], vis[N]; 13 | int my[N]; 14 | bool dfs(int x) { 15 | for (int y = 1; y <= m; ++y) if (able[x][y] && !vis[y]) { 16 | vis[y] = true; 17 | if (!my[y] || dfs(my[y])) { 18 | my[y] = x; 19 | return true; 20 | } 21 | } 22 | return false; 23 | } 24 | int calc(int x) { 25 | for (int i = 1; i <= n; ++i) 26 | for (int j = 1; j <= m; ++j) 27 | able[i][j] = (A[i][j] <= x); 28 | memset(my, 0, sizeof my); 29 | int ans = 0; 30 | for (int i = 1; i <= n; ++i) { 31 | memset(vis, 0, sizeof vis); 32 | ans += dfs(i); 33 | } 34 | return ans; 35 | } 36 | int main() { 37 | int k; 38 | scanf("%d%d%d", &n, &m, &k); 39 | k = n - k + 1; 40 | for (int i = 1; i <= n; ++i) 41 | for (int j = 1; j <= m; ++j) 42 | scanf("%d", &A[i][j]); 43 | int l = 0, r = 1000000000; 44 | while (l < r) { 45 | int mid = (l + r) / 2; 46 | if (calc(mid) >= k) r = mid; 47 | else l = mid + 1; 48 | } 49 | printf("%d\n", l); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1498.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1498 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 1050; 11 | const int M = 20050; 12 | int a[N]; 13 | int p[M]; 14 | void add(int n, int x) { 15 | for (int i = 2; i <= n; ++i) while (!(n % i)) { p[i] += x; n /= i; } 16 | } 17 | int A[M], len; 18 | void mul(int x) { 19 | for (int i = 0, t = 0; i < len || t; ++i) { 20 | if (i >= len) len = i + 1; 21 | t = (A[i] = A[i] * x + t) / 10; 22 | A[i] %= 10; 23 | } 24 | } 25 | int main() { 26 | int t, n, d, S = 0; 27 | scanf("%d%d%d", &t, &n, &d); 28 | for (int i = 1; i <= t; ++i) { 29 | scanf("%d", &a[i]); 30 | S += a[i]; 31 | } 32 | while (n--) { 33 | int y; 34 | scanf("%*d%d", &y); 35 | add(S, -1); add(a[y], 1); 36 | a[y] += d; S += d; 37 | } 38 | A[0] = len = 1; 39 | for (int i = 2; i < M; ++i) 40 | while (p[i] > 0) --p[i], mul(i); 41 | while (len--) printf("%d", A[len]); 42 | memset(A, 0, sizeof A); 43 | A[0] = len = 1; 44 | for (int i = 2; i < M; ++i) 45 | while (p[i] < 0) ++p[i], mul(i); 46 | putchar('/'); 47 | while (len--) printf("%d", A[len]); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2118.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2118 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 15; 10 | const int M = 500050; 11 | typedef long long LL; 12 | int A[N], dis[M]; 13 | bool vis[N]; 14 | std::deque Q; 15 | int main() { 16 | int n, m = 0; 17 | LL l, r; 18 | scanf("%d%lld%lld", &n, &l, &r); 19 | for (int i = 0; i < n; ++i) scanf("%d", &A[i]), m = std::max(m, A[i]); 20 | if (m == 0) return puts("0") & 0; 21 | for (int i = 0; i < m; ++i) dis[i] = -1; 22 | dis[0] = 0; 23 | Q.push_back(0); 24 | while (!Q.empty()) { 25 | int x = Q.front(); Q.pop_front(); 26 | if (vis[x]) continue; 27 | vis[x] = true; 28 | for (int i = 0; i < n; ++i) { 29 | int t = x + A[i]; 30 | if (~dis[t % m] && dis[t % m] <= dis[x] + (t / m)) continue; 31 | dis[t % m] = dis[x] + (t / m); 32 | if (t >= m) Q.push_back(t - m); 33 | else Q.push_front(t); 34 | } 35 | } 36 | LL ans = 0; 37 | for (int i = 0; i < m; ++i) if (~dis[i]) { 38 | LL t = (LL)dis[i] * m + i; 39 | if (t <= r) ans += (r - t) / m + 1; 40 | if (t < l) ans -= (l - 1 - t) / m + 1; 41 | } 42 | printf("%lld\n", ans); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1237.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::min; 5 | typedef long long LL; 6 | LL INF = 1000000000000000LL; 7 | const int N = 100050; 8 | inline int readInt() { 9 | int ans = 0; 10 | char c; 11 | do c = getchar(); while (!isdigit(c)); 12 | while (isdigit(c)) { 13 | ans = ans * 10 + c - '0'; 14 | c = getchar(); 15 | } 16 | return ans; 17 | } 18 | LL A[N], B[N]; 19 | LL f[N]; 20 | inline LL cost(int i, int j) { 21 | if (A[i] == B[j]) return INF; 22 | return std::abs(A[i] - B[j]); 23 | } 24 | int main() { 25 | int n = readInt(); 26 | for (int i = 1; i <= n; ++i) { 27 | A[i] = readInt(); 28 | B[i] = readInt(); 29 | } 30 | if (n == 1 && A[1] == B[1]) return puts("-1"), 0; 31 | std::sort(A + 1, A + n + 1); 32 | std::sort(B + 1, B + n + 1); 33 | f[0] = 0; 34 | for (int i = 1; i <= n; ++i) { 35 | f[i] = f[i - 1] + cost(i, i); 36 | if (i > 1) f[i] = min(f[i], f[i - 2] + cost(i, i - 1) + cost(i - 1, i)); 37 | if (i > 2) { 38 | f[i] = min(f[i], f[i - 3] + cost(i, i - 2) + cost(i - 1, i - 1) + cost(i - 2, i)); 39 | f[i] = min(f[i], f[i - 3] + cost(i, i - 2) + cost(i - 1, i) + cost(i - 2, i - 1)); 40 | f[i] = min(f[i], f[i - 3] + cost(i, i - 1) + cost(i - 2, i) + cost(i - 1, i - 2)); 41 | } 42 | } 43 | printf("%lld\n", f[n]); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1050.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long LL; 4 | const int N = 505, M = 5050; 5 | struct Edge{ 6 | int u, v, w; 7 | bool operator<(const Edge &x)const{ 8 | return w < x.w; 9 | } 10 | }; 11 | Edge e[M]; 12 | int fa[N]; 13 | int find(int x) { 14 | if (fa[x]) return fa[x] = find(fa[x]); 15 | return x; 16 | } 17 | inline void Union(int x, int y) { 18 | if ((x = find(x)) != (y = find(y))) 19 | fa[x] = y; 20 | } 21 | int gcd(int a, int b) { 22 | return b ? gcd(b, a % b) : a; 23 | } 24 | int main() { 25 | int n, m, s, t; 26 | scanf("%d%d", &n, &m); 27 | for (int i = 0; i < m; ++i) 28 | scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w); 29 | std::sort(e, e + m); 30 | scanf("%d%d", &s, &t); 31 | int ansn = 10000000, ansd = 1; 32 | for (int l = 0; l + 1 < m; ++l) { 33 | for (int i = 1; i <= n; ++i) fa[i] = 0; 34 | Union(e[l].u, e[l].v); 35 | int r; 36 | for (r = l + 1; r < m && find(s) != find(t); ++r) 37 | Union(e[r].u, e[r].v); 38 | if (find(s) == find(t)) { 39 | int an = e[r - 1].w, ad = e[l].w; 40 | if ((LL)an * ansd < (LL)ansn * ad) 41 | ansn = an, ansd = ad; 42 | } 43 | } 44 | if (ansn == 10000000) return printf("IMPOSSIBLE"), 0; 45 | int g = gcd(ansn, ansd); 46 | ansn /= g, ansd /= g; 47 | printf("%d", ansn); 48 | if (ansd > 1) printf("/%d", ansd); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1833.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1833 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 20; 11 | LL f[N][2], g[N][2], n[N]; 12 | LL solve(LL m, int k) { 13 | int l = 0; 14 | while (m) n[l++] = m % 10, m /= 10; 15 | f[l][0] = 1; f[l][1] = g[l][0] = g[l][1] = 0; 16 | if (k) { 17 | for (int i = l - 1; ~i; --i) { 18 | f[i][0] = f[i + 1][0]; 19 | f[i][1] = f[i + 1][0] * n[i] + f[i + 1][1] * 10; 20 | g[i][0] = g[i + 1][0] + (n[i] == k); 21 | g[i][1] = g[i + 1][0] * n[i] + (k < n[i]) + g[i + 1][1] * 10 + f[i + 1][1]; 22 | } 23 | return g[0][0] + g[0][1]; 24 | } else { 25 | for (int i = l - 1; ~i; --i) { 26 | f[i][0] = f[i + 1][0]; 27 | f[i][1] = f[i + 1][0] * n[i] + f[i + 1][1] * 10; 28 | g[i][0] = g[i + 1][0] + (n[i] == k); 29 | g[i][1] = g[i + 1][0] * n[i] + (i + 1 != l && k < n[i]) + g[i + 1][1] * 10 + f[i + 1][1] - (i + 1 != l); 30 | } 31 | return g[0][0] + g[0][1]; 32 | } 33 | } 34 | int main() { 35 | LL a, b; 36 | scanf("%lld%lld", &a, &b); 37 | for (int i = 0; i < 10; ++i) { 38 | if (i) printf(" "); 39 | printf("%lld", solve(b, i) - solve(a - 1, i)); 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4921.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4921 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const LL N = 100050; 11 | const LL mod = 998244353; 12 | LL A[N]; 13 | LL a[100], b[100], c[100], d[100]; 14 | inline LL gcd(LL a, LL b) { while (b) std::swap(a %= b, b); return a; } 15 | int main() { 16 | LL n; 17 | scanf("%lld", &n); 18 | for (LL i = 0; i < n; ++i) 19 | scanf("%lld", &A[i]); 20 | LL m = 0, k = 0; 21 | a[m] = A[0]; b[m] = 1; 22 | for (LL i = 1; i < n; ++i) { 23 | if (A[i] % a[m]) a[m + 1] = gcd(A[i], a[m]), ++m; 24 | ++b[m]; 25 | } 26 | c[k] = A[n - 1]; d[k] = 1; 27 | for (LL i = n - 2; ~i; --i) { 28 | if (A[i] % c[k]) c[k + 1] = gcd(A[i], c[k]), ++k; 29 | ++d[k]; 30 | } 31 | LL ans = 0; 32 | for (LL i = 0; i <= m; ++i) 33 | for (LL j = 0; j <= k; ++j) 34 | ans = (ans + (LL)b[i] * d[j] % mod * gcd(a[i], c[j])) % mod; 35 | ans = (ans - (n * n + 3 * n - 2) / 2 % mod * a[m]) % mod; 36 | for (LL i = 0; i <= m; ++i) 37 | ans = (ans + (b[i] - !i - (i == m)) * a[i]) % mod; 38 | for (LL i = 0; i <= k; ++i) 39 | ans = (ans + (d[i] - !i - (i == k)) * c[i]) % mod; 40 | printf("%lld\n", (ans + mod) % mod); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1857.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int Ax, Ay, Bx, By, Cx, Cy, Dx, Dy; 5 | int P, Q, R; 6 | inline double get(double a, double b, double t) { 7 | return a * t + b * (1 - t); 8 | } 9 | inline double sqr(double x) { return x * x; } 10 | inline double dis(double x1, double y1, double x2, double y2) { 11 | return sqrt(sqr(x1 - x2) + sqr(y1 - y2)); 12 | } 13 | inline double calc(double t1, double t2) { 14 | double x1 = get(Ax, Bx, t1), y1 = get(Ay, By, t1); 15 | double x2 = get(Cx, Dx, t2), y2 = get(Cy, Dy, t2); 16 | return dis(Ax, Ay, x1, y1) / P + 17 | dis(Dx, Dy, x2, y2) / Q + 18 | dis(x1, y1, x2, y2) / R; 19 | } 20 | inline double work2(double t1) { 21 | double l = 0.0, r = 1.0; 22 | for (int _ = 0; _ < 100; ++_) { 23 | double m1 = l / 3 + r * 2 / 3; 24 | double m2 = l * 2 / 3 + r / 3; 25 | if (calc(t1, m1) > calc(t1, m2)) r = m1; 26 | else l = m2; 27 | } 28 | return calc(t1, l); 29 | } 30 | double work1() { 31 | double l = 0.0, r = 1.0; 32 | for (int _ = 0; _ < 100; ++_) { 33 | double m1 = l / 3 + r * 2 / 3; 34 | double m2 = l * 2 / 3 + r / 3; 35 | if (work2(m1) > work2(m2)) r = m1; 36 | else l = m2; 37 | } 38 | return work2(l); 39 | } 40 | int main() { 41 | scanf("%d%d%d%d%d%d%d%d%d%d%d", 42 | &Ax, &Ay, &Bx, &By, &Cx, &Cy, &Dx, &Dy, &P, &Q, &R); 43 | printf("%.2lf\n", work1()); 44 | } 45 | -------------------------------------------------------------------------------- /BZOJ/BZOJ5118.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ5118.cpp 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const LL mod = 1125899839733759LL; 11 | LL mul(LL a, LL n, LL p) { 12 | LL ans = 0; 13 | for (; n; n >>= 1, a = a * 2 % p) 14 | if (n & 1) ans = (ans + a) % p; 15 | return ans; 16 | } 17 | LL pow_mod(LL a, LL n, LL p) { 18 | LL ans = 1; 19 | for (; n; n >>= 1, a = mul(a, a, p)) 20 | if (n & 1) ans = mul(ans, a, p); 21 | return ans; 22 | } 23 | struct M{ 24 | LL a, b, c, d; 25 | M(LL a, LL b, LL c, LL d) : a(a % mod), b(b % mod), c(c % mod), d(d % mod) {} 26 | friend M operator*(const M &a, const M &b) { 27 | return M( 28 | mul(a.a, b.a, mod) + mul(a.b, b.c, mod), 29 | mul(a.a, b.b, mod) + mul(a.b, b.d, mod), 30 | mul(a.c, b.a, mod) + mul(a.d, b.c, mod), 31 | mul(a.c, b.b, mod) + mul(a.d, b.d, mod)); 32 | } 33 | }; 34 | int main() { 35 | int T; 36 | LL n; 37 | scanf("%d", &T); 38 | while (T--) { 39 | scanf("%lld", &n); 40 | M x(1, 1, 1, 0), ans(1, 0, 0, 1); 41 | n = pow_mod(2, n, mod - 1); 42 | for (; n; n >>= 1, x = x * x) 43 | if (n & 1) ans = ans * x; 44 | printf("%lld\n", ans.b); 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2956.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2956 3 | * Author: Rqy 4 | * Date: 2018 Feb 23 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int mod = 19940417; 10 | const int inv6 = 3323403; 11 | typedef long long LL; 12 | inline LL calc(int n) { n %= mod; return (LL)n * (n + 1) / 2 % mod; } 13 | inline LL calc2(int n) { 14 | n %= mod; 15 | return (LL)n * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod; 16 | } 17 | int solve(int n) { 18 | int ans = (LL)n * n % mod; 19 | for (int i = 1, last; i <= n; i = last + 1) { 20 | last = n / (n / i); 21 | ans = (ans - (LL)(calc(last) - calc(i - 1)) * (n / i) % mod) % mod; 22 | } 23 | return ans; 24 | } 25 | int solve2(int n, int m) { 26 | if (n > m) std::swap(n, m); 27 | int ans = (LL)n * n % mod * m % mod; 28 | for (int i = 1, last; i <= n; i = last + 1) { 29 | last = std::min(n / (n / i), m / (m / i)); 30 | ans = (ans 31 | - (LL)(calc(last) - calc(i - 1)) * ((LL)(n / i) * m % mod + (LL)(m / i) * n % mod) % mod 32 | + (LL)(calc2(last) - calc2(i - 1)) * (n / i) % mod * (m / i) % mod) % mod; 33 | } 34 | return ans; 35 | } 36 | int main() { 37 | int n, m; 38 | scanf("%d%d", &n, &m); 39 | printf("%d\n", (int)((((LL)solve(n) * solve(m) - solve2(n, m))% mod + mod) % mod)); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4806.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4806 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 105; 10 | const int mod = 999983; 11 | typedef long long LL; 12 | #define add(x, y) ((x) = ((x) + (y)) % mod) 13 | #define C2(x) ((LL)(x) * ((x) - 1) / 2 % mod) 14 | int n, m, f[N][N][N]; 15 | int main() { 16 | scanf("%d%d", &n, &m); 17 | memset(f, 0, sizeof f); 18 | f[0][0][0] = 1; 19 | for (int i = 0; i < n; ++i) 20 | for (int b = 0; b <= 2 * i && b <= m; ++b) 21 | for (int c = 0; b + 2 * c <= 2 * i && b + c <= m; ++c) { 22 | LL t = f[i][b][c]; 23 | int a = m - b - c; 24 | // 0 25 | add(f[i + 1][b][c], t); 26 | // 1 27 | if (a) add(f[i + 1][b + 1][c], t * a % mod); 28 | if (b) add(f[i + 1][b - 1][c + 1], t * b % mod); 29 | // 2 30 | if (a > 1) add(f[i + 1][b + 2][c], t * C2(a) % mod); 31 | if (a && b) add(f[i + 1][b][c + 1], t * a % mod * b % mod); 32 | if (b > 1) add(f[i + 1][b - 2][c + 2], t * C2(b) % mod); 33 | } 34 | int ans = 0; 35 | for (int b = 0; b <= 2 * n && b <= m; ++b) 36 | for (int c = 0; b + 2 * c <= 2 * n && b + c <= m; ++c) 37 | add(ans, f[n][b][c]); 38 | printf("%d\n", ans); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4922.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4922.cpp 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 305; 10 | const int M = N * N * 10; 11 | struct P{ 12 | char s[N]; 13 | int a, b, len; 14 | void input() { 15 | scanf("%s", s); 16 | len = a = b = 0; 17 | while (s[len]) { 18 | if (s[len] == '(') ++b; 19 | else if (b) --b; 20 | else ++a; 21 | ++len; 22 | } 23 | } 24 | bool operator<(const P &p) const { 25 | if ((a < b) != (p.a < p.b)) return a < b; 26 | if (a < b) return a < p.a; 27 | else return b > p.b; 28 | } 29 | }p[N]; 30 | int _f[2][M]; 31 | int main() { 32 | int n; 33 | scanf("%d", &n); 34 | for (int i = 0; i < n; ++i) p[i].input(); 35 | std::sort(p, p + n); 36 | int *f = _f[0], *g = _f[1]; 37 | int m = 0; 38 | f[0] = 0; 39 | for (int i = 0; i < n; ++i, std::swap(f, g)) { 40 | for (int j = 0; j <= m; ++j) g[j] = f[j]; 41 | int m1 = m; 42 | if (p[i].a < p[i].b) { 43 | m += p[i].b - p[i].a; 44 | for (int j = m1 + 1; j <= m; ++j) g[j] = -10000000; 45 | } 46 | for (int j = p[i].b, k = p[i].a; j <= m && k <= m1; ++j, ++k) 47 | g[j] = std::max(g[j], f[k] + p[i].len); 48 | } 49 | printf("%d\n", f[0]); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1009.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int n, m, kk; 4 | int fail[30]; 5 | int p[30]; 6 | void getfail() { 7 | fail[0] = fail[1] = 0; 8 | int i, j; 9 | for (i = 1; i < m; ++i) { 10 | j = fail[i]; 11 | while (j && p[i] != p[j]) j = fail[j]; 12 | fail[i + 1] = p[i] == p[j] ? j + 1 : 0; 13 | } 14 | } 15 | typedef int matrix[100][100]; 16 | matrix d; 17 | void mul(matrix a, matrix b, matrix c, int siz) { 18 | memset(d, 0, sizeof d); 19 | for (int i = 0; i < siz; ++i) 20 | for (int k = 0; k < siz; ++k) 21 | for (int j = 0; j < siz; ++j) 22 | d[i][j] = (d[i][j] + a[i][k] * b[k][j]) % kk; 23 | memcpy(c, d, sizeof d); 24 | } 25 | matrix A, B; 26 | int main() { 27 | // freopen("bzoj_1009.in", "r", stdin); 28 | // freopen("bzoj_1009.out", "w", stdout); 29 | scanf("%d%d%d", &n, &m, &kk); 30 | for (int i = 0; i < m; ++i) scanf("%1d", &p[i]); 31 | getfail(); 32 | memset(A, 0, sizeof A); 33 | for (int i = 0; i < m; ++i) 34 | for (int j = 0; j < 10; ++j) { 35 | int k = i; 36 | while (k && p[k] != j) k = fail[k]; 37 | ++A[i][p[k] == j ? k + 1 : k]; 38 | } 39 | if (!n) { 40 | printf("1"); 41 | return 0; 42 | } 43 | memcpy(B, A, sizeof A); 44 | for (--n; n; n >>= 1, mul(A, A, A, m)) 45 | if (n & 1) mul(A, B, B, m); 46 | int ans = 0; 47 | for (int i = 0; i < m; ++i) ans = (ans + B[0][i]) % kk; 48 | printf("%d", ans); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2326.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long LL; 4 | LL n, m, ans; 5 | LL powmod(LL a, LL b) { 6 | LL ans = 1; 7 | for (; b; b >>= 1, a = a * a % m) 8 | if (b & 1) ans = ans * a % m; 9 | return ans; 10 | } 11 | LL calc1(LL x, LL t) { 12 | //return (1 + x + ... + x^(t-1)) % m 13 | if (t == 0) return 0; 14 | LL ans = (1 + powmod(x, t / 2)) % m * calc1(x, t / 2) % m; 15 | if (t & 1) ans = (ans * x + 1) % m; 16 | return ans; 17 | } 18 | LL calc2(LL x, LL t) { 19 | //return (t + (t - 1)x + (t - 2)x^2 + ... + 1x^(t - 1)) % m 20 | if (t == 0) return 0; 21 | LL ans = ((1 + powmod(x, t / 2)) % m * calc2(x, t / 2) % m + 22 | (t / 2) % m * calc1(x, t / 2) % m) % m; 23 | if (t & 1) ans = (ans + calc1(x, t)) % m; 24 | return ans; 25 | } 26 | void calc(LL l, LL r, int p) { 27 | //ans = ans : l : (l+1) : ... : r, 10^(p+1) <= l <= r < 10^(p+2) 28 | // = ans * 10^(p(r-l+1)) + (l-1)(1+10^p+...+10^(r-l)) + 29 | // 1*10^(p(r-l)) + 2*10^(p(r-l-1)) + ... + (r-l+1)*10^(p*0) 30 | LL t = powmod(10, p); 31 | ans = ans * powmod(t, r - l + 1) % m; 32 | ans = (ans + (l - 1) % m * calc1(t, r - l + 1) % m) % m; 33 | ans = (ans + calc2(t, r - l + 1)) % m; 34 | } 35 | 36 | int main() { 37 | scanf("%lld%lld", &n, &m); 38 | ans = 0; 39 | for (LL p = 1, l = 1; l <= n; ++p, l *= 10) 40 | calc(l, std::min(l * 10 - 1, n), p); 41 | printf("%lld\n", ans); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /BZOJ/BZOJ5000.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ5000 3 | * Author: Rqy 4 | * Date: 2018 Feb 28 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 10050; 11 | int a[32][N][30], b[32][N][30]; 12 | char s[200000]; 13 | int main() { 14 | int n, k; 15 | scanf("%d%d", &n, &k); 16 | for (int i = 1; i <= n; ++i) 17 | for (int j = 0; j < k; ++j) 18 | scanf("%d%d", &a[0][i][j], &b[0][i][j]); 19 | int p; 20 | scanf("%s%d", s, &p); 21 | for (int t = 1; t < 32; ++t) 22 | for (int i = 1; i <= n; ++i) 23 | for (int j = 0; j < k; ++j) { 24 | a[t][i][j] = a[t - 1][a[t - 1][i][j]][j]; 25 | b[t][i][j] = b[t - 1][i][j] + b[t - 1][a[t - 1][i][j]][j]; 26 | if (b[t][i][j] >= p) b[t][i][j] -= p; 27 | } 28 | int o = 1, ans = 0; 29 | char *c = s; 30 | while (*c) { 31 | int cnt, cc; 32 | if (*c == '[') { 33 | int t; 34 | sscanf(++c, "%d%n", &cnt, &t); 35 | c += t; 36 | cc = *(c++) - 'A'; 37 | c++; 38 | } else { 39 | cnt = 1; 40 | cc = *(c++) - 'A'; 41 | } 42 | for (int i = 0; i < 32; ++i) 43 | if ((cnt >> i) & 1) { 44 | if ((ans += b[i][o][cc]) >= p) ans -= p; 45 | o = a[i][o][cc]; 46 | } 47 | } 48 | printf("%d\n", ans); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Luogu/P3317.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P3317 3 | * Author: Rqy 4 | * Date: 2017 Dec 23 5 | * Algorithm: 矩阵树定理,高斯消元 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 55; 11 | const double eps = 1e-7; 12 | double A[N][N]; 13 | double calc(int n) { 14 | for (int i = 0; i < n; ++i) { 15 | int c = i; 16 | for (int j = i + 1; j < n; ++j) 17 | if (fabs(A[j][i]) > fabs(A[c][i])) c = j; 18 | if (fabs(A[c][i]) < eps) return .0; 19 | for (int j = i; j < n; ++j) 20 | std::swap(A[i][j], A[c][j]); 21 | for (int j = i + 1; j < n; ++j) 22 | for (int k = n - 1; k >= i; --k) 23 | A[j][k] -= A[j][i] / A[i][i] * A[i][k]; 24 | } 25 | double ans = 1.0; 26 | for (int i = 0; i < n; ++i) 27 | ans *= A[i][i]; 28 | return ans; 29 | } 30 | int main() { 31 | int n; 32 | scanf("%d", &n); 33 | double t = 1.0; 34 | for (int i = 0; i < n; ++i) 35 | for (int j = 0; j < n; ++j) { 36 | double x; 37 | scanf("%lf", &x); 38 | if (i != j) { 39 | if (fabs(x - 1.0) < eps) x = 1.0 - eps; 40 | if (fabs(x) < eps) x = eps; 41 | if (i < j) t *= 1 - x; 42 | x = x / (1 - x); 43 | A[i][i] -= (A[i][j] = x); 44 | } 45 | } 46 | printf("%lf\n", fabs(calc(n - 1) * t)); 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1055.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int readWING() { 3 | char c; 4 | do c = getchar(); 5 | while (c != EOF && c != 'W' && c != 'I' && c != 'N' && c != 'G'); 6 | if (c == 'W') return 0; 7 | if (c == 'I') return 1; 8 | if (c == 'N') return 2; 9 | if (c == 'G') return 3; 10 | return EOF; 11 | } 12 | const int N = 205; 13 | char s[N]; 14 | int A[N]; 15 | bool f[N][N][4]; 16 | int num[4], to[4][20][2]; 17 | int main() { 18 | for (int i = 0; i < 4; ++i) 19 | scanf("%d", &num[i]); 20 | for (int i = 0; i < 4; ++i) 21 | for (int j = 0; j < num[i]; ++j) { 22 | to[i][j][0] = readWING(); 23 | to[i][j][1] = readWING(); 24 | } 25 | int n; 26 | scanf("%s", s); 27 | for (n = 0; s[n]; ++n) 28 | A[n] = s[n] == 'W' ? 0 : s[n] == 'I' ? 1 : s[n] == 'N' ? 2 : 3; 29 | for (int i = 0; i < n; ++i) f[i][i + 1][A[i]] = 1; 30 | for (int l = 2; l <= n; ++l) 31 | for (int i = 0; i + l <= n; ++i) 32 | for (int j = i + l, k = 0; k < 4; ++k) 33 | for (int l = 0; l < num[k]; ++l) 34 | for (int t = i + 1; t < j; ++t) 35 | if (f[i][t][to[k][l][0]] && f[t][j][to[k][l][1]]) { 36 | f[i][j][k] = 1; 37 | l = num[k]; 38 | break; 39 | } 40 | bool c = false; 41 | for (int i = 0; i < 4; ++i) 42 | if (f[0][n][i]) { 43 | putchar("WING"[i]); 44 | c = true; 45 | } 46 | if (!c) puts("The name is wrong!"); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2227.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2227 3 | * Author: Rqy 4 | * Date: 2018 Feb 22 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 205; 11 | int p[N]; 12 | int A[10000], len; 13 | void add(int n, int m) { 14 | for (int i = 2; i <= n; ++i) if (!(n % i)) { 15 | while (!(n % i)) { 16 | p[i] += m; 17 | n /= i; 18 | } 19 | } 20 | } 21 | void mul(int x) { 22 | for (int i = 0, t = 0; i < len || t; ++i) { 23 | t = (A[i] = A[i] * x + t) / 10; 24 | A[i] %= 10; 25 | if (i >= len) len = i + 1; 26 | } 27 | } 28 | int main() { 29 | int T; 30 | scanf("%d", &T); 31 | while (T--) { 32 | int n, k; 33 | scanf("%d%d", &n, &k); 34 | if (n > k) { puts("0 1"); continue; } 35 | memset(p, 0, sizeof p); 36 | add(k - n + 1, 1); 37 | add(k + 1, n - 1); 38 | add(k, -n); 39 | memset(A, 0, sizeof A); 40 | A[0] = len = 1; 41 | for (int i = 1; i <= k + 1; ++i) 42 | for (int j = 0; j < p[i]; ++j) 43 | mul(i); 44 | while (len--) printf("%d", A[len]); 45 | putchar(' '); 46 | memset(A, 0, sizeof A); 47 | A[0] = len = 1; 48 | for (int i = 1; i <= k + 1; ++i) 49 | for (int j = p[i]; j < 0; ++j) 50 | mul(i); 51 | while (len--) printf("%d", A[len]); 52 | putchar('\n'); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2338.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long LL; 4 | const int N = 1600; 5 | int X[N], Y[N]; 6 | inline LL sqr(LL a) { 7 | return a * a; 8 | } 9 | inline LL S(LL x1, LL y1, LL x2, LL y2) { 10 | return std::abs(x1 * y2 - x2 * y1); 11 | } 12 | inline LL S(int p1, int p2, int p3) { 13 | return S(X[p1] - X[p2], Y[p1] - Y[p2], X[p3] - X[p2], Y[p3] - Y[p2]); 14 | } 15 | struct Line{ 16 | LL len2; 17 | int mx, my; 18 | int p1, p2; 19 | Line(int p1 = 0, int p2 = 0) 20 | : p1(p1), p2(p2) { 21 | mx = X[p1] + X[p2]; 22 | my = Y[p1] + Y[p2]; 23 | len2 = sqr(X[p1] - X[p2]) + sqr(Y[p1] - Y[p2]); 24 | } 25 | bool operator < (const Line &x) const { 26 | if (len2 != x.len2) return len2 < x.len2; 27 | if (mx != x.mx) return mx < x.mx; 28 | return my < x.my; 29 | } 30 | bool operator == (const Line &x) const { 31 | return !(*this < x || x < *this); 32 | } 33 | }; 34 | Line lines[N * N]; 35 | int main() { 36 | int n; 37 | scanf("%d", &n); 38 | int c = 0; 39 | for (int i = 0; i < n; ++i) scanf("%d%d", &X[i], &Y[i]); 40 | for (int i = 0; i < n; ++i) 41 | for (int j = 0; j < n; ++j) if (i != j) 42 | lines[c++] = Line(i, j); 43 | std::sort(lines, lines + c); 44 | LL ans = 0; 45 | for (int i = 0; i < c; ++i) 46 | for (int j = i - 1; j && lines[j] == lines[i]; --j) 47 | ans = std::max(ans, S(lines[i].p1, lines[j].p1, lines[i].p2)); 48 | return !printf("%lld", ans); 49 | } 50 | -------------------------------------------------------------------------------- /Luogu/P2860.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int N = 5000; 4 | const int M = 10000; 5 | 6 | int n, pre[N], nxt[M * 2], to[M * 2], cnt; 7 | 8 | inline void addEdge(int x, int y) { 9 | nxt[cnt] = pre[x]; 10 | to[pre[x] = cnt++] = y; 11 | nxt[cnt] = pre[y]; 12 | to[pre[y] = cnt++] = x; 13 | } 14 | 15 | int stack[N], top; 16 | int dfn[N], cnt2, bcnt; 17 | bool in[N]; 18 | int ans = 0; 19 | 20 | inline int min(int x, int y) { return x < y ? x : y; } 21 | int Tarjan(int x, int fe) { 22 | int lowx = dfn[x] = ++cnt2; 23 | stack[top++] = x; 24 | for (int i = pre[x]; i >= 0; i = nxt[i]) 25 | if (dfn[to[i]] == 0) 26 | lowx = min(lowx, Tarjan(to[i], i)); 27 | else if ((i ^ 1) != fe) 28 | lowx = min(lowx, dfn[to[i]]); 29 | if (lowx >= dfn[x]) { 30 | int tt = top; 31 | while (stack[--top] != x); 32 | int k = 0; 33 | for (int i = top; i < tt; ++i) in[stack[i]] = true; 34 | for (int i = top; i < tt; ++i) 35 | for (int j = pre[stack[i]]; j >= 0; j = nxt[j]) 36 | if (!in[to[j]]) 37 | ++k; 38 | if (k == 1) ++ans; 39 | for (int i = top; i < tt; ++i) in[stack[i]] = false; 40 | } 41 | return lowx; 42 | } 43 | 44 | int main() { 45 | int m; 46 | scanf("%d%d", &n, &m); 47 | for (int i = 1; i <= n; ++i) pre[i] = -1; 48 | while (m--) { 49 | int x, y; 50 | scanf("%d%d", &x, &y); 51 | addEdge(x, y); 52 | } 53 | Tarjan(1, -1); 54 | printf("%d\n", (ans + 1) / 2); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1295.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | const int N = 50; 6 | int map[N][N]; 7 | struct HeapNode{ 8 | int i, j, d; 9 | HeapNode(int i = 0, int j = 0, int d = 0) : i(i), j(j), d(d) {} 10 | bool operator<(const HeapNode &x)const{ 11 | return d > x.d; 12 | } 13 | }; 14 | std::priority_queue Q; 15 | int dis[N][N], n, m, T, ans; 16 | const int d[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; 17 | void Dijkstra(int si, int sj) { 18 | std::fill(dis[0], dis[n], 10000); 19 | while (!Q.empty()) Q.pop(); 20 | Q.push(HeapNode(si, sj, dis[si][sj] = map[si][sj])); 21 | while (!Q.empty()) { 22 | HeapNode x = Q.top(); Q.pop(); 23 | if (x.d != dis[x.i][x.j]) continue; 24 | if (x.d > T) return; 25 | int ui = x.i, uj = x.j; 26 | ans = std::max(ans, (ui - si) * (ui - si) + (uj - sj) * (uj - sj)); 27 | for (int k = 0; k < 4; ++k) { 28 | int vi = ui + d[k][0], vj = uj + d[k][1]; 29 | if (~vi && ~vj && vi < n && vj < m && dis[vi][vj] > x.d + map[vi][vj]) 30 | Q.push(HeapNode(vi, vj, dis[vi][vj] = x.d + map[vi][vj])); 31 | } 32 | } 33 | } 34 | int main() { 35 | scanf("%d%d%d", &n, &m, &T); 36 | for (int i = 0; i < n; ++i) 37 | for (int j = 0; j < m; ++j) 38 | scanf("%1d", &map[i][j]); 39 | ans = 0; 40 | for (int i = 0; i < n; ++i) 41 | for (int j = 0; j < m; ++j) 42 | Dijkstra(i, j); 43 | printf("%.6f", sqrt((float)ans)); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4818.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4818 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | typedef long long LL; 11 | const int N = 20000050; 12 | const int mod = 20170408; 13 | bool mark[N]; 14 | int pr[N / 10], cnt; 15 | int n, m, p; 16 | struct P{ 17 | int A[100]; 18 | P() { memset(A, 0, sizeof A); } 19 | friend P operator*(const P &x, const P &y) { 20 | P ans; 21 | for (int i = 0; i < p; ++i) 22 | for (int j = 0; j < p; ++j) 23 | (ans.A[(i + j) % p] += (LL)x.A[i] * y.A[j] % mod) %= mod; 24 | return ans; 25 | } 26 | }x, ans; 27 | int main() { 28 | scanf("%d%d%d", &n, &m, &p); 29 | mark[1] = 1; 30 | for (int i = 2; i <= m; ++i) { 31 | if (!mark[i]) pr[cnt++] = i; 32 | for (int j = 0; j < cnt && pr[j] <= m / i; ++j) { 33 | mark[i * pr[j]] = 1; 34 | if (!i % pr[j]) break; 35 | } 36 | } 37 | ans.A[0] = 1; 38 | for (int i = 1; i <= m; ++i) x.A[i % p] += mark[i]; 39 | for (int t = n; t; t >>= 1, x = x * x) 40 | if (t & 1) ans = ans * x; 41 | int t1 = ans.A[0]; 42 | ans = P(); x = P(); 43 | ans.A[0] = 1; 44 | for (int i = 1; i <= m; ++i) ++x.A[i % p]; 45 | for (int t = n; t; t >>= 1, x = x * x) 46 | if (t & 1) ans = ans * x; 47 | printf("%d\n", (ans.A[0] - t1 + mod) % mod); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1078.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int fa[200], lc[200], rc[200]; 4 | int siz[200]; 5 | int p[200][200]; 6 | int main() { 7 | int n; 8 | scanf("%d", &n); 9 | ++n; 10 | for (int i = 1; i < n; ++i) { 11 | int x; 12 | scanf("%d", &x); 13 | if (x < 100) lc[fa[i] = x] = i; 14 | else rc[fa[i] = x - 100] = i; 15 | } 16 | for (int i = n - 1; i >= 0; --i) { 17 | siz[i] = siz[lc[i]] + siz[rc[i]] + 1; 18 | if (!rc[i] || siz[lc[i]] >= siz[rc[i]]) { 19 | int k = siz[lc[i]] - (rc[i] == 0 ? 0 : siz[rc[i]]); 20 | if (k == 1) { 21 | p[i][0] = i; 22 | for (int j = 0; j < siz[i]; j += 2) { 23 | p[i][j + 1] = p[lc[i]][j / 2]; 24 | p[i][j + 2] = p[rc[i]][j / 2]; 25 | } 26 | continue; 27 | } 28 | for (int j = 0; j < k; ++j) 29 | p[i][j] = p[lc[i]][j]; 30 | p[i][k] = i; 31 | for (int j = 0; j < siz[i] - k; j += 2) { 32 | p[i][j + k + 1] = p[rc[i]][j / 2]; 33 | p[i][j + k + 2] = p[lc[i]][j / 2 + k]; 34 | } 35 | } else if (siz[lc[i]] < siz[rc[i]]) { 36 | int k = siz[rc[i]] - siz[lc[i]] + 1; 37 | for (int j = 0; j < k; ++j) 38 | p[i][j] = p[rc[i]][j]; 39 | p[i][k] = i; 40 | for (int j = 0; j < siz[i] - k; j += 2) { 41 | p[i][j + k + 1] = p[lc[i]][j / 2]; 42 | p[i][j + k + 2] = p[rc[i]][j / 2 + k]; 43 | } 44 | } 45 | } 46 | for (int i = 0; i < n; ++i) 47 | printf("%d ", p[0][i]); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1875.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int mod = 45989; 5 | typedef long long LL; 6 | int A, B, N, M; 7 | LL t; 8 | struct Martix { 9 | int n; 10 | int a[200][200]; 11 | Martix(int nn = 0) : n(nn) { 12 | memset(a, 0, sizeof a); 13 | } 14 | friend Martix operator * (Martix a, Martix b) { 15 | Martix c(a.n); 16 | for (int k = 0; k < a.n; ++k) 17 | for (int i = 0; i < a.n; ++i) 18 | for (int j = 0; j < a.n; ++j) 19 | c.a[i][j] = (c.a[i][j] + (LL)a.a[i][k] * b.a[k][j]) % mod; 20 | return c; 21 | } 22 | }; 23 | Martix a, b; 24 | int x[150], y[150]; 25 | int main() { 26 | int i, j; 27 | scanf("%d%d%lld%d%d", &N, &M, &t, &A, &B); 28 | if (t == 0) { 29 | printf("%d", A == B); 30 | return 0; 31 | } else if (t < 0) { 32 | printf("0"); 33 | return 0; 34 | } 35 | a.n = b.n = M * 2; 36 | for (i = 0; i < M; ++i) { 37 | scanf("%d%d", &x[i], &y[i]); 38 | x[M + i] = y[i]; 39 | y[M + i] = x[i]; 40 | } 41 | for (i = 0; i < M * 2; ++i) 42 | for (j = 0; j < M * 2; ++j) 43 | if (j != i + M && i != j + M && y[i] == x[j]) 44 | ++a.a[i][j]; 45 | for (i = 0; i < M * 2; ++i) 46 | b.a[i][i] = 1; 47 | for (--t; t; t /= 2, a = a * a) 48 | if (t & 1) b = a * b; 49 | int ans = 0; 50 | for (i = 0; i < M * 2; ++i) if (x[i] == A) 51 | for (j = 0; j < M * 2; ++j) if (y[j] == B) 52 | ans = (ans + b.a[i][j]) % mod; 53 | printf("%d", ans); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4916.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ4916 3 | * Author: Rqy 4 | * Date: 2018 Feb 26 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | typedef long long LL; 10 | const int N = 1000050; 11 | const int mod = 1000000007; 12 | const int inv2 = (mod + 1) / 2; 13 | const int inv6 = (mod + 1) / 6; 14 | int B[N], pr[N / 5], cnt, n, B2[1050]; 15 | int s1(int i) { return (LL)i * (i + 1) % mod * inv2 % mod; } 16 | int s2(int i) { return (LL)i * (i + 1) % mod * (2 * i + 1) % mod * inv6 % mod; } 17 | int get(int d) { 18 | if (n / d < N) return B[n / d]; 19 | //if (n / d == 1) return 1; 20 | int &ans = B2[d]; 21 | if (ans) return ans; 22 | int m = n / d; 23 | ans = s2(m); 24 | for (int i = 2, last; i <= m; i = last + 1) { 25 | last = m / (m / i); 26 | ans = (ans - (LL)(s1(last) - s1(i - 1)) * get(i * d)) % mod; 27 | } 28 | return ans; 29 | } 30 | int main() { 31 | scanf("%d", &n); 32 | B[1] = 1; 33 | for (int i = 2; i < N; ++i) { 34 | if (!B[i]) 35 | B[pr[cnt++] = i] = i - 1; 36 | for (int j = 0; j < cnt && pr[j] <= (N - 1) / i; ++j) 37 | if (i % pr[j]) B[i * pr[j]] = B[i] * (pr[j] - 1); 38 | else { 39 | B[i * pr[j]] = B[i] * pr[j]; 40 | break; 41 | } 42 | } 43 | for (int i = 2; i < N; ++i) 44 | B[i] = ((LL)B[i] * i + B[i - 1]) % mod; 45 | printf("1\n%d\n", (get(1) + mod) % mod); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1004.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int n, p, cir[70]; 3 | int Sr, Sb, Sg; 4 | bool vis[70]; 5 | int siz[70], f[25][25][25]; 6 | int getNum() { 7 | for (int i = 1; i <= n; ++i) 8 | vis[i] = 0; 9 | int cnt = 0; 10 | for (int i = 1; i <= n; ++i) if (!vis[i]) { 11 | siz[cnt] = 0; 12 | for (int j = i; !vis[j]; j = cir[j]) { 13 | ++siz[cnt]; 14 | vis[j] = 1; 15 | } 16 | ++cnt; 17 | } 18 | for (int i = 0; i <= Sr; ++i) 19 | for (int j = 0; j <= Sb; ++j) 20 | for (int k = 0; k <= Sg; ++k) 21 | f[i][j][k] = 0; 22 | f[0][0][0] = 1; 23 | while (cnt--) 24 | for (int i = Sr; i >= 0; --i) 25 | for (int j = Sb; j >= 0; --j) 26 | for (int k = Sg; k >= 0; --k) { 27 | if (i >= siz[cnt]) f[i][j][k] = (f[i][j][k] + f[i - siz[cnt]][j][k]) % p; 28 | if (j >= siz[cnt]) f[i][j][k] = (f[i][j][k] + f[i][j - siz[cnt]][k]) % p; 29 | if (k >= siz[cnt]) f[i][j][k] = (f[i][j][k] + f[i][j][k - siz[cnt]]) % p; 30 | } 31 | return f[Sr][Sb][Sg]; 32 | } 33 | int main() { 34 | int m; 35 | scanf("%d%d%d%d%d", &Sr, &Sb, &Sg, &m, &p); 36 | n = Sr + Sb + Sg; 37 | int ans = 0; 38 | for (int i = 0; i < m; ++i) { 39 | for (int j = 1; j <= n; ++j) 40 | scanf("%d", &cir[j]); 41 | ans = (ans + getNum()) % p; 42 | } 43 | for (int i = 1; i <= n; ++i) cir[i] = i; 44 | ans = (ans + getNum()) % p; 45 | for (int i = 1; i < p; ++i) if (i * (m + 1) % p == 1) { 46 | printf("%d 47 | ", ans * i % p); 48 | return 0; 49 | } 50 | } -------------------------------------------------------------------------------- /BZOJ/BZOJ1415.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1415 3 | * Author: Rqy 4 | * Date: 2018 Feb 24 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 1050; 10 | bool e[N][N]; 11 | int n, nxt[N][N]; 12 | void bfs(int x) { 13 | static bool vis[N]; 14 | static int que[N]; 15 | memset(vis, 0, sizeof vis); 16 | int hd = 0, tl = 0; 17 | vis[que[tl++] = x] = true; 18 | while (hd < tl) { 19 | int y = que[hd++]; 20 | for (int z = 1; z <= n; ++z) 21 | if (e[y][z] && !vis[z]) { 22 | nxt[x][z] = (y == x ? z : nxt[x][y]); 23 | vis[que[tl++] = z] = true; 24 | } 25 | } 26 | } 27 | bool vis[N][N]; 28 | double f[N][N]; 29 | double F(int s, int t) { 30 | if (vis[s][t]) return f[s][t]; 31 | double &ans = f[s][t]; 32 | vis[s][t] = true; 33 | if (s == t) return ans = .0; 34 | if ((s = nxt[s][t]) == t) return ans = 1.0; 35 | if ((s = nxt[s][t]) == t) return ans = 1.0; 36 | int d = 0; 37 | for (int i = 1; i <= n; ++i) if (e[t][i]) 38 | ans += F(s, i), ++d; 39 | return (ans /= d) += 1; 40 | } 41 | int main() { 42 | int m, s, t; 43 | scanf("%d%d%d%d", &n, &m, &s, &t); 44 | while (m--) { 45 | int x, y; 46 | scanf("%d%d", &x, &y); 47 | e[x][y] = e[y][x] = true; 48 | } 49 | for (int i = 1; i <= n; ++i) e[i][i] = true; 50 | for (int i = 1; i <= n; ++i) bfs(i); 51 | printf("%.3lf", F(s, t)); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4600.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int c[30050]; 4 | int SG[30050]; 5 | int cnt[100]; 6 | int pow[2][20]; 7 | int main() { 8 | //freopen("new1.txt", "r", stdin); 9 | int n, maxq, T, i, j, p, q, k; 10 | scanf("%d", &T); 11 | for (i = 1; i <= 30000; ++i) { 12 | c[i] = i; 13 | while (!(c[i] % 2)) c[i] /= 2; 14 | while (!(c[i] % 3)) c[i] /= 3; 15 | } 16 | pow[0][0] = pow[1][0] = 1; 17 | for (i = 1; i < 16; ++i) { 18 | pow[0][i] = pow[0][i - 1] * 2; 19 | pow[1][i] = pow[1][i - 1] * 3; 20 | } 21 | while (T--) { 22 | scanf("%d%d", &n, &maxq); 23 | SG[0] = 0; 24 | memset(cnt, 0, sizeof cnt); 25 | for (i = 0; pow[0][i] <= n; ++i) 26 | for (j = 0; pow[0][i] * pow[1][j] <= n; ++j) { 27 | for (p = 1; p <= i; ++p) 28 | for (q = 1, k = 0; p * q <= i && q <= maxq; ++q) 29 | cnt[k ^= SG[pow[0][i - p * q] * pow[1][j]]] = pow[0][i] * pow[1][j]; 30 | 31 | for (p = 1; p <= j; ++p) 32 | for (q = 1, k = 0; p * q <= j && q <= maxq; ++q) 33 | cnt[k ^= SG[pow[0][i] * pow[1][j - p * q]]] = pow[0][i] * pow[1][j]; 34 | 35 | k = 0; 36 | while (cnt[k] == pow[0][i] * pow[1][j]) ++k; 37 | SG[pow[0][i] * pow[1][j]] = k; 38 | } 39 | int ans = 0; 40 | for (i = 1; i <= n; ++i) { 41 | scanf("%d", &j); 42 | if (!j) 43 | ans ^= SG[i / c[i]]; 44 | } 45 | if (ans) 46 | puts("win"); 47 | else 48 | puts("lose"); 49 | } 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1103.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int N = 250050; 5 | inline int readInt() { 6 | int ans = 0; 7 | char c; 8 | do c = getchar(); while (!isdigit(c)); 9 | do { 10 | ans = ans * 10 + c - '0'; 11 | c = getchar(); 12 | } while (isdigit(c)); 13 | return ans; 14 | } 15 | inline char readChar() { 16 | char c; 17 | do c = getchar(); while (isspace(c)); 18 | return c; 19 | } 20 | int left[N], siz[N], fa[N]; 21 | int A[N], n; 22 | inline void add(int i, int x) { 23 | while (i <= n) { 24 | A[i] += x; 25 | i += i & -i; 26 | } 27 | } 28 | inline int query(int i) { 29 | int ans = 0; 30 | while (i) { 31 | ans += A[i]; 32 | i -= i & -i; 33 | } 34 | return ans; 35 | } 36 | int main() { 37 | n = readInt(); 38 | for (int i = 1; i < n; ++i) { 39 | int a = readInt(), b = readInt(); 40 | if (a > b) std::swap(a, b); 41 | fa[b] = a; 42 | } 43 | fa[1] = 0; 44 | for (int i = n; i; --i) { 45 | left[i] = siz[fa[i]] + 1; 46 | siz[fa[i]] += ++siz[i]; 47 | } 48 | for (int i = 1; i <= n; ++i) 49 | left[i] += left[fa[i]]; 50 | for (int i = 2; i <= n; ++i) { 51 | add(left[i], 1); 52 | add(left[i] + siz[i], -1); 53 | } 54 | int m = readInt() + n - 1; 55 | while (m--) { 56 | if (readChar() == 'W') 57 | printf("%d\n", query(left[readInt()])); 58 | else { 59 | int a = std::max(readInt(), readInt()); 60 | add(left[a], -1); 61 | add(left[a] + siz[a], 1); 62 | } 63 | } 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3680.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ3680 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | #include 11 | const double eps = 1e-5; 12 | inline double abs(double x) { return x < 0 ? -x : x; } 13 | inline int sgn(double x) { return x < -eps ? -1 : x > eps; } 14 | const int N = 10050; 15 | int n; 16 | double x[N], y[N], w[N]; 17 | double E(double x0, double y0) { 18 | double ans = .0; 19 | for (int i = 0; i < n; ++i) { 20 | double xx = x[i] - x0, yy = y[i] - y0; 21 | ans += sqrt(xx * xx + yy * yy) * w[i]; 22 | } 23 | return ans; 24 | } 25 | double randf() { return (double)rand() / RAND_MAX; } 26 | int main() { 27 | srand((unsigned)(new char)); 28 | double Sx = .0, Sy = .0, Sw = .0; 29 | scanf("%d", &n); 30 | for (int i = 0; i < n; ++i) { 31 | scanf("%lf%lf%lf", &x[i], &y[i], &w[i]); 32 | Sx += x[i] * w[i]; Sy += y[i] * w[i]; Sw += w[i]; 33 | } 34 | double x0 = Sx / Sw, y0 = Sy / Sw; 35 | for (int i = 0; i < n; ++i) w[i] /= Sw; 36 | double ll = 1e5, ans = E(x0, y0); 37 | while (sgn(ll)) { 38 | for (int _ = 0; _ < 10; ++_) { 39 | double x1 = x0 + (randf()-0.5) * ll, y1 = y0 + (randf()-0.5) * ll; 40 | double anst = E(x1, y1); 41 | if (anst < ans) x0 = x1, y0 = y1, ans = anst; 42 | } 43 | ll *= 0.9; 44 | } 45 | printf("%.3lf %.3lf\n", x0, y0); 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Luogu/P2444.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2444 3 | * Author: Rqy 4 | * Date: 2017 Dec 28 5 | * Algorithm: AC自动机 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 30050; 10 | bool d[N], vis1[N], vis2[N]; 11 | int nxt[N][2], fail[N], cnt = 0; 12 | char s[N]; 13 | int que[N]; 14 | bool dfs(int x) { 15 | if (vis1[x]) return true; 16 | if (vis2[x]) return false; 17 | if (d[x]) return false; 18 | vis1[x] = vis2[x] = true; 19 | for (int i = 0; i < 2; ++i) 20 | if (dfs(nxt[x][i])) return true; 21 | vis1[x] = false; 22 | return false; 23 | } 24 | int main() { 25 | int n; 26 | scanf("%d", &n); 27 | memset(nxt, 0, sizeof nxt); 28 | memset(d, 0, sizeof d); 29 | cnt = 1; 30 | for (int i = 0; i < n; ++i) { 31 | scanf("%s", s); 32 | int o = 0; 33 | for (int j = 0; s[j]; ++j) { 34 | int c = s[j] - '0'; 35 | if (!nxt[o][c]) nxt[o][c] = cnt++; 36 | o = nxt[o][c]; 37 | } 38 | d[o] = true; 39 | } 40 | int head = 0, tail = 0; 41 | for (int i = 0; i < 2; ++i) if (nxt[0][i]) 42 | fail[que[tail++] = nxt[0][i]] = 0; 43 | while (head < tail) 44 | for (int i = 0, o = que[head++]; i < 2; ++i) 45 | if (nxt[o][i]) 46 | d[nxt[o][i]] |= d[fail[que[tail++] = nxt[o][i]] = nxt[fail[o]][i]]; 47 | else 48 | nxt[o][i] = nxt[fail[o]][i]; 49 | memset(vis1, 0, sizeof vis1); 50 | memset(vis2, 0, sizeof vis2); 51 | printf("%s\n", dfs(0) ? "TAK" : "NIE"); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2323.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long LL; 3 | const int mod = 1000000007; 4 | struct Matrix{ 5 | int v[2][2]; 6 | Matrix() { 7 | v[0][0] = v[0][1] = v[1][0] = v[1][1] = 0; 8 | } 9 | friend Matrix operator*(const Matrix &a, const Matrix &b) { 10 | Matrix c; 11 | for (int i = 0; i < 2; ++i) 12 | for (int k = 0; k < 2; ++k) 13 | for (int j = 0; j < 2; ++j) 14 | c.v[i][j] = ((LL)a.v[i][k] * b.v[k][j] + c.v[i][j]) % mod; 15 | return c; 16 | } 17 | friend Matrix operator+(const Matrix &a, const Matrix &b) { 18 | Matrix c; 19 | for (int i = 0; i < 2; ++i) 20 | for (int j = 0; j < 2; ++j) 21 | c.v[i][j] = (a.v[i][j] + b.v[i][j]) % mod; 22 | return c; 23 | } 24 | }; 25 | Matrix pow_mod(Matrix a, int b) { 26 | Matrix c; 27 | c.v[0][0] = c.v[1][1] = 1; 28 | for (; b; a = a * a, b >>= 1) 29 | if (b & 1) c = c * a; 30 | return c; 31 | } 32 | Matrix f[2000]; 33 | int t[2000]; 34 | Matrix F[2000]; 35 | int main() { 36 | int n, i; 37 | scanf("%d", &n); 38 | for (i = 1; i <= n; ++i) 39 | scanf("%1d", &t[i]); 40 | F[0].v[0][0] = F[0].v[0][1] = F[0].v[1][0] = 1; 41 | for (i = 1; i <= n; ++i) F[i] = pow_mod(F[i - 1], 10); 42 | f[0].v[0][0] = 1; 43 | f[0].v[0][1] = f[0].v[1][0] = -1; 44 | f[0].v[1][1] = 2; 45 | for (i = 1; i <= n; ++i) { 46 | Matrix k; 47 | k.v[0][0] = k.v[1][1] = 1; 48 | for (int j = i; j; --j) { 49 | k = k * pow_mod(F[i - j], t[j]); 50 | f[i] = f[i] + k * f[j - 1]; 51 | } 52 | } 53 | printf("%d\n", (f[n].v[0][0] + mod) % mod); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Luogu/P3808.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P3796 3 | * Author: Rqy 4 | * Date: 2017 Dec 28 5 | * Algorithm: AC自动机 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 1000050; 11 | bool vis[N]; 12 | int v[N], fail[N], nxt[N][26], cnt; 13 | int que[N]; 14 | char s[N]; 15 | int main() { 16 | int n; 17 | scanf("%d", &n); 18 | cnt = 1; 19 | memset(nxt, 0, sizeof nxt); 20 | for (int i = 0; i < n; ++i) { 21 | scanf("%s", s); 22 | int o = 0; 23 | for (int j = 0; s[j]; ++j) { 24 | int c = s[j] - 'a'; 25 | if (!nxt[o][c]) nxt[o][c] = cnt++; 26 | o = nxt[o][c]; 27 | } 28 | v[i] = o; 29 | } 30 | memset(fail, -1, sizeof fail); 31 | fail[0] = 0; 32 | int head = 0, tail = 0; 33 | for (int i = 0; i < 26; ++i) if (nxt[0][i]) { 34 | fail[nxt[0][i]] = 0; 35 | que[tail++] = nxt[0][i]; 36 | } 37 | while (head < tail) 38 | for (int o = que[head++], i = 0; i < 26; ++i) 39 | if (nxt[o][i]) { 40 | fail[nxt[o][i]] = nxt[fail[o]][i]; 41 | que[tail++] = nxt[o][i]; 42 | } else { 43 | nxt[o][i] = nxt[fail[o]][i]; 44 | } 45 | scanf("%s", s); 46 | int o = 0; 47 | vis[o] = true; 48 | for (int i = 0; s[i]; ++i) { 49 | int c = s[i] - 'a'; 50 | o = nxt[o][c]; 51 | for (int j = o; !vis[j]; j = fail[j]) vis[j] = true; 52 | } 53 | int ans = 0; 54 | for (int i = 0; i < n; ++i) if (vis[v[i]]) ++ans; 55 | printf("%d\n", ans); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1058.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1058 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | #include 11 | const int N = 500050; 12 | int A[N], B[N], ans2; 13 | std::set S; 14 | std::map M; 15 | void updS(int x) { 16 | if (!S.empty()) { 17 | std::set::iterator it = S.end(); 18 | if (x > *(--it)) { 19 | ans2 = std::min(ans2, x - *it); 20 | } else { 21 | it = S.lower_bound(x); 22 | ans2 = std::min(ans2, *it - x); 23 | if (it != S.begin()) ans2 = std::min(ans2, x - *(--it)); 24 | } 25 | } 26 | S.insert(x); 27 | } 28 | inline void delM(int x) { if (!--M[x]) M.erase(x); } 29 | inline void addM(int x) { if (!M.count(x)) M[x] = 0; ++M[x]; } 30 | int main() { 31 | int n, m, x, y; 32 | scanf("%d%d", &n, &m); 33 | ans2 = 10000000; 34 | for (int i = 1; i <= n; ++i) scanf("%d", &A[i]), updS(B[i] = A[i]); 35 | for (int i = 1; i < n; ++i) addM(std::abs(A[i + 1] - A[i])); 36 | char s[20]; 37 | while (m--) { 38 | scanf("%s", s); 39 | if (*s == 'I') { 40 | scanf("%d%d", &x, &y); 41 | updS(y); 42 | if (x < n) { 43 | delM(std::abs(A[x + 1] - B[x])); 44 | addM(std::abs(A[x + 1] - y)); 45 | } 46 | addM(std::abs(y - B[x])); 47 | B[x] = y; 48 | } else if (s[4] == 'S') 49 | printf("%d\n", ans2); 50 | else 51 | printf("%d\n", M.begin()->first); 52 | } 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /JCYZOJ/1006.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: JCYZOJ 1006 3 | * Author: Rqy 4 | * Date: 2018 Feb 13 5 | * Algorithm: RMQ 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 500050; 10 | int n, A[N], L[N], R[N], gcdv[20][N]; 11 | bool t[N]; 12 | int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } 13 | inline int gcdr(int l, int r) { 14 | int k = 0; 15 | while ((r - l + 1) >> (k + 1)) ++k; 16 | return gcd(gcdv[k][l], gcdv[k][r - (1 << k) + 1]); 17 | } 18 | int main() { 19 | scanf("%d", &n); 20 | for (int i = 0; i < n; ++i) { 21 | scanf("%d", &A[i]); 22 | gcdv[0][i] = A[i]; 23 | } 24 | for (int i = 0; i < 19; ++i) 25 | for (int j = 0; j + (2 << i) <= n; ++j) 26 | gcdv[i + 1][j] = gcd(gcdv[i][j], gcdv[i][j + (1 << i)]); 27 | int ans = 0, ans2 = 0; 28 | for (int i = 0; i < n; ++i) { 29 | int l = 0, r = i; 30 | while (l < r) { 31 | int mid = (l + r) / 2; 32 | if (gcdr(mid, i) == A[i]) r = mid; 33 | else l = mid + 1; 34 | } 35 | L[i] = l; 36 | l = i, r = n - 1; 37 | while (l < r) { 38 | int mid = r + (l - r) / 2; 39 | if (gcdr(i, mid) == A[i]) l = mid; 40 | else r = mid - 1; 41 | } 42 | R[i] = l; 43 | ans = std::max(ans, R[i] - L[i]); 44 | } 45 | for (int i = 0; i < n; ++i) if (R[i] - L[i] == ans) t[L[i]] = true; 46 | for (int i = 0; i < n; ++i) if (t[i]) ++ans2; 47 | printf("%d %d\n", ans2, ans); 48 | for (int i = 0; i < n; ++i) if (t[i]) printf("%d ", i + 1); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /BZOJ/BZOJ4518.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define N 3003 7 | #define LL long long 8 | #define inf 1e9 9 | using namespace std; 10 | int n, m, q[N]; 11 | LL a[N], sum[N], f[N], g[N]; 12 | #define K(x) ((LL)-2 * sum[(x)]) 13 | #define B(x) ((LL)g[(x)] + sum[(x)] * sum[(x)]) 14 | inline LL calc(int x, int y) { 15 | return K(x) * sum[y] + B(x) + sum[y] * sum[y]; 16 | } 17 | inline bool pd(int x1, int x2, int x3) { 18 | LL w1 = (LL)(K(x2) - K(x3)) * (B(x2) - B(x1)); 19 | LL w2 = (LL)(K(x1) - K(x2)) * (B(x3) - B(x2)); 20 | return w1 <= w2; 21 | } 22 | inline LL read() { 23 | LL ans = 0; 24 | char c; 25 | do c = getchar(); 26 | while (c > '9' || c < '0'); 27 | do { 28 | ans = ans * 10 + c - '0'; 29 | c = getchar(); 30 | } while (c <= '9' && c >= '0'); 31 | return ans; 32 | } 33 | int main() { 34 | int i, j; 35 | scanf("%d%d", &n, &m); 36 | sum[0] = 0; 37 | for (i = 1; i <= n; ++i) 38 | sum[i] = sum[i - 1] + read(); 39 | LL ans = inf; 40 | for (i = 1; i <= n; ++i) 41 | g[i] = inf; 42 | g[0] = 0; 43 | for (i = 1; i <= m; ++i) { 44 | int head = 0, tail = 0; 45 | for (j = 1; j <= n; ++j) { 46 | while (head < tail && calc(q[head + 1], j) <= calc(q[head], j)) 47 | ++head; 48 | f[j] = calc(q[head], j); 49 | while (head < tail && pd(q[tail - 1], j, q[tail])) 50 | --tail; 51 | ++tail; 52 | q[tail] = j; 53 | } 54 | ans = min(ans, f[n]); 55 | memcpy(g, f, sizeof f); 56 | } 57 | printf("%lld\n", ans * m - sum[n] * sum[n]); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /JCYZOJ/1042.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: JCYZOJ 1042 3 | * Author: Rqy 4 | * Date: 2018 Feb 13 5 | * Algorithm: BigNum, Sort 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 1050; 10 | struct P{ 11 | int a, b; 12 | bool operator<(const P &x) const { 13 | return a * b < x.a * x.b; 14 | } 15 | }p0, p[N]; 16 | struct BigNum{ 17 | int v[N * 10], len; 18 | BigNum operator*=(int x) { 19 | for (int i = 0, t = 0; i < len || t; ++i) { 20 | t = (v[i] = v[i] * x + t) / 10; 21 | v[i] %= 10; 22 | len = std::max(len, i + 1); 23 | } 24 | return *this; 25 | } 26 | BigNum operator/=(int x) { 27 | for (int i = len - 1, t = 0; ~i; --i) { 28 | t = (v[i] += t * 10) % x; 29 | v[i] /= x; 30 | } 31 | while (len && !v[len - 1]) --len; 32 | return *this; 33 | } 34 | bool operator<(const BigNum &x) const { 35 | if (len != x.len) return len < x.len; 36 | for (int i = len - 1; ~i; --i) 37 | if (v[i] != x.v[i]) 38 | return v[i] < x.v[i]; 39 | return false; 40 | } 41 | }A, B, ans; 42 | int main() { 43 | int n; 44 | scanf("%d%d%d", &n, &p0.a, &p0.b); 45 | for (int i = 0; i < n; ++i) 46 | scanf("%d%d", &p[i].a, &p[i].b); 47 | std::sort(p, p + n); 48 | A.len = A.v[0] = 1; 49 | A *= p0.a; 50 | for (int i = 0; i < n; ++i) { 51 | (B = A) /= p[i].b; 52 | if (!i || ans < B) ans = B; 53 | A *= p[i].a; 54 | } 55 | for (int i = ans.len - 1; ~i; --i) 56 | printf("%d", ans.v[i]); 57 | return 0; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /Luogu/P2571.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P2571 3 | * Author: Rqy 4 | * Date: 2017 Dec 26 5 | * Algorithm: 三分 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | double Ax, Ay, Bx, By, Cx, Cy, Dx, Dy; 11 | double P, Q, R; 12 | inline double get(double a, double b, double t) { 13 | return a * t + b * (1 - t); 14 | } 15 | inline double sqr(double x) { return x * x; } 16 | inline double dis(double x1, double y1, double x2, double y2) { 17 | return sqrt(sqr(x1 - x2) + sqr(y1 - y2)); 18 | } 19 | double calc(double t1, double t2) { 20 | double x1 = get(Ax, Bx, t1), y1 = get(Ay, By, t1); 21 | double x2 = get(Cx, Dx, t2), y2 = get(Cy, Dy, t2); 22 | return dis(Ax, Ay, x1, y1) / P + 23 | dis(Dx, Dy, x2, y2) / Q + 24 | dis(x1, y1, x2, y2) / R; 25 | } 26 | double work2(double t1) { 27 | double l = 0.0, r = 1.0; 28 | for (int _ = 0; _ < 100; ++_) { 29 | double m1 = l / 3 + r * 2 / 3; 30 | double m2 = l * 2 / 3 + r / 3; 31 | if (calc(t1, m1) > calc(t1, m2)) r = m1; 32 | else l = m2; 33 | } 34 | return calc(t1, l); 35 | } 36 | double work1() { 37 | double l = 0.0, r = 1.0; 38 | for (int _ = 0; _ < 100; ++_) { 39 | double m1 = l / 3 + r * 2 / 3; 40 | double m2 = l * 2 / 3 + r / 3; 41 | if (work2(m1) > work2(m2)) r = m1; 42 | else l = m2; 43 | } 44 | return work2(l); 45 | } 46 | int main() { 47 | scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", 48 | &Ax, &Ay, &Bx, &By, &Cx, &Cy, &Dx, &Dy, &P, &Q, &R); 49 | printf("%.2lf\n", work1()); 50 | } 51 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1084.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::max; 4 | int f[105][15][5]; 5 | inline int max(int a, int b, int c) { 6 | return max(max(a, b), c); 7 | } 8 | inline int max(int a, int b, int c, int d) { 9 | return max(max(a, b), max(c, d)); 10 | } 11 | inline int max(int a, int b, int c, int d, int e) { 12 | return max(e, max(max(a, b), max(c, d))); 13 | } 14 | int main() { 15 | int n, m, k; 16 | scanf("%d%d%d", &n, &m, &k); 17 | if (m == 1) { 18 | int x; 19 | for (int i = 1; i <= n; ++i) { 20 | scanf("%d", &x); 21 | for (int j = 0; j <= k; ++j) { 22 | f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1]); 23 | if (j) f[i][j][1] = max(f[i - 1][j - 1][0], f[i - 1][j][1]) + x; 24 | } 25 | } 26 | printf("%d\n", max(f[n][k][0], f[n][k][1])); 27 | } else { 28 | int x, y; 29 | for (int i = 1; i <= n; ++i) { 30 | scanf("%d%d", &x, &y); 31 | for (int j = 0; j <= k; ++j) { 32 | int *now = f[i][j], *las = f[i - 1][j], *las1 = f[i - 1][j - 1], *las2 = f[i - 1][j - 2]; 33 | now[0] = max(las[0], las[1], las[2], las[3], las[4]); 34 | if (j) { 35 | now[1] = max(las1[0], las[1], las1[2], las1[3], las[4]) + x; 36 | now[2] = max(las1[0], las1[1], las[2], las1[3], las[4]) + y; 37 | now[3] = max(las1[0], las1[1], las1[2], las[3], las1[4]) + x + y; 38 | if (j > 1) 39 | now[4] = max(las2[0], las1[1], las1[2], las2[3], las[4]) + x + y; 40 | } 41 | } 42 | } 43 | int *ans = f[n][k]; 44 | printf("%d\n", max(ans[0], ans[1], ans[2], ans[3], ans[4])); 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3140.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int N = 5000; 5 | int A[N][N], S; 6 | int gg[N]; 7 | int my[N], vis[N], time, time2; 8 | int a, b, c; 9 | bool match(int x) { 10 | for (int y = 1; y <= c; ++y) if (vis[y] < time && (A[x][y] & ~S)) { 11 | vis[y] = time; 12 | if (gg[y] < time2 || !my[y] || match(my[y])) { 13 | gg[y] = time2; 14 | my[y] = x; 15 | return true; 16 | } 17 | } 18 | return false; 19 | } 20 | int solve() { 21 | ++time2; 22 | int ans = 0; 23 | for (int x = 1; x <= b; ++x) 24 | ++time, ans += match(x); 25 | return ans; 26 | } 27 | int siz[1 << 20]; 28 | int main() { 29 | int T; 30 | scanf("%d", &T); 31 | for (int i = 1; i < (1 << 20); ++i) 32 | siz[i] = siz[i & (i - 1)] + 1; 33 | while (T--) { 34 | scanf("%d%d%d", &a, &b, &c); 35 | int t = (a < b) ? ((a < c) ? 0 : 2) : ((b < c) ? 1 : 2); 36 | int x; 37 | for (int i = 1; i <= a; ++i) 38 | for (int j = 1; j <= b; ++j) 39 | for (int k = 1; k <= c; ++k) { 40 | scanf("%d", &x); 41 | if (t == 0 && i == 1) A[j][k] = 0; 42 | if (t == 1 && j == 1) A[i][k] = 0; 43 | if (t == 2 && k == 1) A[i][j] = 0; 44 | if (x) { 45 | if (t == 0) A[j][k] |= (1 << (i - 1)); 46 | else if (t == 1) A[i][k] |= (1 << (j - 1)); 47 | else A[i][j] |= (1 << (k - 1)); 48 | } 49 | } 50 | if (t == 1) std::swap(a, b); 51 | else if (t == 2) std::swap(a, c); 52 | int ans = a; 53 | for (S = 0; S < (1 << a); ++S) 54 | ans = std::min(ans, solve() + siz[S]); 55 | printf("%d\n", ans); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1030.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | const int MAXN = 10000; 5 | const int mod = 10007; 6 | int ch[MAXN][26], cnt, match[MAXN]; 7 | int fail[MAXN]; 8 | char s[1000]; 9 | void addstring() { 10 | int r = 0; 11 | for (int i = 0; s[i] != '\0'; ++i) { 12 | if (!ch[r][s[i] - 'A']) match[ch[r][s[i] - 'A'] = cnt++] = false; 13 | r = ch[r][s[i] - 'A']; 14 | } 15 | match[r] = true; 16 | } 17 | std::queue Q; 18 | void getfail() { 19 | while (!Q.empty()) Q.pop(); 20 | fail[0] = 0; 21 | for (int i = 0; i < 26; ++i) 22 | if (ch[0][i]) { 23 | fail[ch[0][i]] = 0; 24 | Q.push(ch[0][i]); 25 | } 26 | while (!Q.empty()) { 27 | int x = Q.front(); 28 | Q.pop(); 29 | for (int i = 0; i < 26; ++i) 30 | if (ch[x][i]) { 31 | match[ch[x][i]] |= match[ch[fail[x]][i]]; 32 | fail[ch[x][i]] = ch[fail[x]][i]; 33 | Q.push(ch[x][i]); 34 | } else { 35 | ch[x][i] = ch[fail[x]][i]; 36 | } 37 | } 38 | } 39 | int f[MAXN][105]; 40 | int solve(int x, int k) { 41 | if (match[x]) return 0; 42 | if (!k) return 1; 43 | if (f[x][k] >= 0) return f[x][k]; 44 | int &ans = f[x][k]; 45 | ans = 0; 46 | for (int i = 0; i < 26; ++i) ans = (ans + solve(ch[x][i], k - 1)) % mod; 47 | return ans; 48 | } 49 | int main() { 50 | int n, m; 51 | cnt = 1; 52 | scanf("%d%d", &n, &m); 53 | for (int i = 0; i < n; ++i) { 54 | scanf("%s", s); 55 | addstring(); 56 | } 57 | getfail(); 58 | int ans = 1; 59 | for (int i = 0; i < m; ++i) ans = ans * 26 % mod; 60 | memset(f, -1, sizeof f); 61 | printf("%d", (ans - solve(0, m) + mod) % mod); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1051.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ1051 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 10050; 11 | const int M = 50050; 12 | int pre[N], to[M], nxt[M], cnt; 13 | inline void addEdge(int x, int y) { 14 | nxt[cnt] = pre[x]; 15 | to[pre[x] = cnt++] = y; 16 | } 17 | int dfn[N], scc[N], sccnum, dfscnt, stack[N], top; 18 | int tarjan(int x) { 19 | int lowx = dfn[x] = ++dfscnt; 20 | stack[top++] = x; 21 | for (int i = pre[x]; ~i; i = nxt[i]) { 22 | int u = to[i]; 23 | if (!dfn[u]) lowx = std::min(lowx, tarjan(u)); 24 | else if (!scc[u]) lowx = std::min(lowx, dfn[u]); 25 | } 26 | if (lowx == dfn[x]) { 27 | scc[x] = ++sccnum; 28 | while (stack[--top] != x) scc[stack[top]] = sccnum; 29 | } 30 | return lowx; 31 | } 32 | int out[N]; 33 | int main() { 34 | int n, m; 35 | scanf("%d%d", &n, &m); 36 | memset(pre, -1, sizeof pre); 37 | while (m--) { 38 | int x, y; 39 | scanf("%d%d", &x, &y); 40 | addEdge(x, y); 41 | } 42 | for (int i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i); 43 | for (int i = 1; i <= n; ++i) { 44 | if (!scc[i]) while(1); 45 | for (int j = pre[i]; ~j; j = nxt[j]) 46 | if (scc[to[j]] != scc[i]) ++out[scc[i]]; 47 | } 48 | int r = 0; 49 | for (int i = 1; i <= sccnum; ++i) 50 | r += !out[i]; 51 | if (r > 1) printf("0\n"); 52 | else { 53 | int ans = 0; 54 | for (int i = 1; i <= n; ++i) ans += !out[scc[i]]; 55 | printf("%d\n", ans); 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1048.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::min; 5 | typedef double LL; 6 | const int N = 15; 7 | LL s[N][N], f[N][N][N][N][N]; 8 | inline LL sqr(LL x) { 9 | return x * x; 10 | } 11 | inline LL sum(int l, int r, int u, int d) { 12 | return s[d][r] - s[d][l - 1] - s[u - 1][r] + s[u - 1][l - 1]; 13 | } 14 | int main() { 15 | int a, b, n; 16 | scanf("%d%d%d", &a, &b, &n); 17 | for (int i = 1; i <= a; ++i) 18 | for (int j = 1; j <= b; ++j) { 19 | scanf("%lf", &s[i][j]); 20 | s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]; 21 | } 22 | for (int u = 1; u <= a; ++u) 23 | for (int d = u; d <= a; ++d) 24 | for (int l = 1; l <= b; ++l) 25 | for (int r = l; r <= b; ++r) 26 | f[1][u][d][l][r] = sqr(sum(l, r, u, d)); 27 | for (int k = 2; k <= n; ++k) 28 | for (int hei = 1; hei <= a; ++hei) 29 | for (int u = 1; u + hei - 1 <= a; ++u) { 30 | int d = u + hei - 1; 31 | for (int wid = 1; wid <= b; ++wid) 32 | for (int l = 1; l + wid - 1 <= b; ++l) { 33 | int r = l + wid - 1; 34 | LL &ans = f[k][u][d][l][r]; 35 | ans = 1000000000000000000LL; 36 | for (int k1 = 1; k1 < k; ++k1) { 37 | int k2 = k - k1; 38 | for (int i = u + 1; i <= d; ++i) 39 | ans = min(ans, f[k1][u][i - 1][l][r] + f[k2][i][d][l][r]); 40 | for (int j = l + 1; j <= r; ++j) 41 | ans = min(ans, f[k1][u][d][l][j - 1] + f[k2][u][d][j][r]); 42 | } 43 | } 44 | } 45 | printf("%.2lf\n", sqrt(f[n][1][a][1][b] / n - sqr(s[a][b] / n))); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2330.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2330 3 | * Author: Rqy 4 | * Date: 2018 Feb 22 5 | * Algorithm: 差分约束 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 100050; 11 | int pre[N], nxt[N * 2], to[N * 2], c[N * 2], cnt; 12 | void addEdge(int a, int b, int v) { 13 | nxt[cnt] = pre[a]; 14 | c[cnt] = v; 15 | to[pre[a] = cnt++] = b; 16 | } 17 | bool inque[N]; 18 | int dis[N]; 19 | int que[N], hd, tl; 20 | int main() { 21 | int n, m; 22 | scanf("%d%d", &n, &m); 23 | memset(pre, -1, sizeof pre); 24 | int x, a, b; 25 | while (m--) { 26 | scanf("%d%d%d", &x, &a, &b); 27 | if (x == 5) addEdge(a, b, 0); 28 | else if (x == 4) addEdge(b, a, 1); 29 | else if (x == 3) addEdge(b, a, 0); 30 | else if (x == 2) addEdge(a, b, 1); 31 | else { addEdge(a, b, 0); addEdge(b, a, 0); } 32 | } 33 | hd = tl = 0; 34 | for (int i = 1; i <= n; ++i) { 35 | dis[i] = 1; 36 | inque[que[tl++] = i] = true; 37 | } 38 | int maxv = 0; 39 | while (hd != tl && maxv <= n) { 40 | int x = que[hd++]; 41 | inque[x] = false; 42 | hd %= N; 43 | for (int i = pre[x]; ~i; i = nxt[i]) 44 | if (dis[to[i]] < dis[x] + c[i]) { 45 | maxv = std::max(maxv, dis[to[i]] = dis[x] + c[i]); 46 | if (!inque[to[i]]) { 47 | inque[que[tl++] = to[i]] = true; 48 | tl %= N; 49 | } 50 | } 51 | } 52 | if (maxv > n) { puts("-1"); return 0; } 53 | long long ans = 0; 54 | for (int i = 1; i <= n; ++i) ans += dis[i]; 55 | printf("%lld\n", ans); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Luogu/P3709.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: Luogu P3709 3 | * Author: Rqy 4 | * Date: 2017 Dec 26 5 | * Algorithm: 莫队 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 200050; 11 | int bs, A[N], cnt[N], ccnt[N], maxt, ans[N]; 12 | struct Query{ 13 | int l, r, i; 14 | bool operator<(const Query &t)const{ 15 | if (l / bs != t.l / bs) return l / bs < t.l / bs; 16 | return r < t.r; 17 | } 18 | }q[N]; 19 | int get(int x) { 20 | static int p = 0; 21 | static std::map M; 22 | if (!M.count(x)) M[x] = p++; 23 | return M[x]; 24 | } 25 | void add(int x) { 26 | --ccnt[cnt[x]]; 27 | ++ccnt[++cnt[x]]; 28 | maxt = std::max(maxt, cnt[x]); 29 | } 30 | void remove(int x) { 31 | --ccnt[cnt[x]]; 32 | if (maxt == cnt[x] && !ccnt[cnt[x]]) 33 | --maxt; 34 | ++ccnt[--cnt[x]]; 35 | } 36 | int main() { 37 | int n, m; 38 | scanf("%d%d", &n, &m); 39 | while (bs * bs < n) ++bs; 40 | for (int i = 0; i < n; ++i) { 41 | scanf("%d", &A[i]); 42 | A[i] = get(A[i]); 43 | } 44 | for (int i = 0; i < m; ++i) { 45 | scanf("%d%d", &q[i].l, &q[i].r); 46 | --q[i].l; --q[i].r; 47 | q[i].i = i; 48 | } 49 | std::sort(q, q + m); 50 | int l = 0, r = -1; 51 | ccnt[0] = n; 52 | for (int i = 0; i < m; ++i) { 53 | while (l > q[i].l) add(A[--l]); 54 | while (r < q[i].r) add(A[++r]); 55 | while (l < q[i].l) remove(A[l++]); 56 | while (r > q[i].r) remove(A[r--]); 57 | ans[q[i].i] = maxt; 58 | } 59 | for (int i = 0; i < n; ++i) 60 | printf("%d\n", -ans[i]); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /BZOJ/BZOJ5015.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ5015 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int mod = 1000000007; 10 | typedef long long LL; 11 | const int N = 15; 12 | LL pow_mod(LL a, LL b) { 13 | LL ans = 1; 14 | for (a %= mod; b; b >>= 1, a = a * a % mod) 15 | if (b & 1) ans = ans * a % mod; 16 | return ans; 17 | } 18 | LL g[N]; 19 | LL A[N][N]; 20 | void solve(int n) { 21 | for (int i = 0; i < n; ++i) { 22 | int j = i; 23 | while (!A[j][i]) ++j; 24 | LL inv = pow_mod(A[j][i], mod - 2); 25 | for (int k = i; k <= n; ++k) std::swap((A[j][k] *= inv) %= mod, A[i][k]); 26 | for (int j = i + 1; j < n; ++j) 27 | for (int k = n; k >= i; --k) 28 | A[j][k] = (A[j][k] - A[i][k] * A[j][i]) % mod; 29 | } 30 | for (int i = n - 1; i; --i) 31 | for (int j = i - 1; ~j; --j) 32 | A[j][n] = (A[j][n] - A[i][n] * A[j][i]) % mod; 33 | } 34 | int main() { 35 | LL n; 36 | int k; 37 | scanf("%lld%d", &n, &k); 38 | g[0] = 0; 39 | for (int i = 1; i <= k; ++i) { 40 | g[i] = (2 * g[i - 1] - pow_mod(i - 1, k) + pow_mod(i, k)) % mod; 41 | A[i - 1][k] = g[i]; 42 | A[i - 1][0] = (1 << i) - 1; 43 | LL pi = i; 44 | for (int j = 1; j < k; ++j, pi = pi * i % mod) 45 | A[i - 1][j] = pi; 46 | } 47 | solve(k); 48 | LL ans = A[0][k] * (pow_mod(2, n) - 1); 49 | LL pn = n %= mod; 50 | for (int i = 1; i < k; ++i, pn = pn * n % mod) 51 | ans = (ans + pn * A[i][k]) % mod; 52 | printf("%lld\n", (ans + mod) % mod); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1057.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::stack; 5 | stack S; 6 | const int maxn = 2010; 7 | int high[maxn], L[maxn], R[maxn]; 8 | int A[maxn], B[maxn]; 9 | inline int max(int a, int b) { 10 | return a > b ? a : b; 11 | } 12 | inline int min(int a, int b) { 13 | return a < b ? a : b; 14 | } 15 | int main() { 16 | int n, m; 17 | scanf("%d%d", &n, &m); 18 | int ans1 = 0, ans2 = 0; 19 | memset(high, 0, sizeof(high)); 20 | for(int i = 0; i < n; i++) 21 | { 22 | while (!S.empty()) S.pop(); 23 | S.push(0); 24 | for(int j = 1; j <= m; j++) 25 | { 26 | scanf("%1d", &A[j]); 27 | high[j] = (!i || B[j] != A[j]) ? high[j] + 1 : 1; 28 | B[j] = A[j]; 29 | if (j > 1 && A[j] == A[j - 1]) { 30 | while (!S.empty()) S.pop(); 31 | L[j] = j; 32 | S.push(j); 33 | } else { 34 | while (S.size() > 1 && high[S.top()] >= high[j]) S.pop(); 35 | L[j] = high[S.top()] >= high[j] ? S.top() : S.top() + 1; 36 | S.push(j); 37 | } 38 | } 39 | while (!S.empty()) S.pop(); 40 | S.push(m + 1); 41 | for(int j = m; j; j--) 42 | { 43 | if (j < m && A[j] == A[j + 1]) { 44 | while (!S.empty()) S.pop(); 45 | R[j] = j; 46 | S.push(j); 47 | } else { 48 | while (S.size() > 1 && high[S.top()] >= high[j]) S.pop(); 49 | R[j] = high[S.top()] >= high[j] ? S.top() + 1 : S.top(); 50 | S.push(j); 51 | } 52 | ans1 = max(ans1, min(R[j] - L[j], high[j]) * min(R[j] - L[j], high[j])); 53 | ans2 = max(ans2, (R[j] - L[j]) * high[j]); 54 | } 55 | } 56 | printf("%d\n%d\n", ans1, ans2); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /BZOJ/BZOJ2006.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ2006 3 | * Author: Rqy 4 | * Date: 2018 Feb 25 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 500050; 11 | typedef long long LL; 12 | LL A[N]; 13 | int maxn[30][N]; 14 | int query(int l, int r) { 15 | int k = 0; 16 | while ((1 << (k + 1)) <= (r - l + 1)) ++k; 17 | int t1 = maxn[k][l], t2 = maxn[k][r - (1 << k) + 1]; 18 | return A[t1] > A[t2] ? t1 : t2; 19 | } 20 | struct HeapNode{ 21 | int x, l, r, y; 22 | HeapNode(int x, int l, int r) : x(x), l(l), r(r) { y = query(l, r); } 23 | LL getV() const { return A[y] - A[x - 1]; } 24 | bool operator<(const HeapNode &t) const { return getV() < t.getV(); } 25 | }; 26 | std::priority_queue PQ; 27 | int main() { 28 | int n, k, L, R; 29 | scanf("%d%d%d%d", &n, &k, &L, &R); 30 | for (int i = 1; i <= n; ++i) scanf("%lld", &A[i]); 31 | for (int i = 1; i <= n; ++i) A[maxn[0][i] = i] += A[i - 1]; 32 | for (int i = 0; i + 1 < 30; ++i) 33 | for (int j = 1; (1 << i) + j <= n + 1; ++j) { 34 | int t1 = maxn[i][j], t2 = maxn[i][j + (1 << i)]; 35 | maxn[i + 1][j] = A[t1] > A[t2] ? t1 : t2; 36 | } 37 | while (!PQ.empty()) PQ.pop(); 38 | for (int i = 1; i + L - 1 <= n; ++i) 39 | PQ.push(HeapNode(i, i + L - 1, std::min(i + R - 1, n))); 40 | LL ans = 0; 41 | while (k--) { 42 | HeapNode x = PQ.top(); PQ.pop(); 43 | ans += x.getV(); 44 | if (x.y != x.l) PQ.push(HeapNode(x.x, x.l, x.y - 1)); 45 | if (x.y != x.r) PQ.push(HeapNode(x.x, x.y + 1, x.r)); 46 | } 47 | printf("%lld\n", ans); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3208.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ3208 3 | * Author: Rqy 4 | * Date: 2018 Feb 27 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | #include 10 | const int N = 705; 11 | int h[N][N]; 12 | bool mark[N][N]; 13 | int f[N][N]; 14 | int dfs(int i, int j) { 15 | if (mark[i][j]) return 0; 16 | if (f[i][j]) return f[i][j]; 17 | f[i][j] = 1; 18 | if (h[i - 1][j] < h[i][j]) f[i][j] = std::max(f[i][j], 1 + dfs(i - 1, j)); 19 | if (h[i + 1][j] < h[i][j]) f[i][j] = std::max(f[i][j], 1 + dfs(i + 1, j)); 20 | if (h[i][j - 1] < h[i][j]) f[i][j] = std::max(f[i][j], 1 + dfs(i, j - 1)); 21 | if (h[i][j + 1] < h[i][j]) f[i][j] = std::max(f[i][j], 1 + dfs(i, j + 1)); 22 | return f[i][j]; 23 | } 24 | int main() { 25 | int n, m; 26 | scanf("%d", &n); 27 | memset(h, 0x3f, sizeof h); 28 | for (int i = 1; i <= n; ++i) 29 | for (int j = 1; j <= n; ++j) 30 | scanf("%d", &h[i][j]); 31 | scanf("%d", &m); 32 | while (m--) { 33 | char s[2]; 34 | scanf("%s", s); 35 | if (*s == 'Q') { 36 | memset(f, 0, sizeof f); 37 | int ans = 0; 38 | for (int i = 1; i <= n; ++i) 39 | for (int j = 1; j <= n; ++j) 40 | ans = std::max(ans, dfs(i, j)); 41 | printf("%d\n", ans); 42 | } else if (*s == 'C') { 43 | int x, y; 44 | scanf("%d%d", &x, &y); 45 | scanf("%d", &h[x][y]); 46 | } else { 47 | int a, b, c, d; 48 | scanf("%d%d%d%d", &a, &b, &c, &d); 49 | for (int i = a; i <= c; ++i) 50 | for (int j = b; j <= d; ++j) 51 | mark[i][j] = (*s == 'S'); 52 | } 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /BZOJ/BZOJ1047.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::deque; 5 | const int N = 1050; 6 | int A[N][N]; 7 | int _ans[2][N][N]; 8 | int a, b, n; 9 | struct MQ{ 10 | int v[N]; 11 | deque DQ; 12 | int ttop, end; 13 | void clear() { 14 | DQ.clear(); 15 | ttop = end = 0; 16 | } 17 | void push(int x) { 18 | v[end] = x; 19 | while (!DQ.empty() && x < v[DQ.back()]) DQ.pop_back(); 20 | DQ.push_back(end++); 21 | } 22 | int top() { 23 | return v[DQ.front()]; 24 | } 25 | void pop() { 26 | if (DQ.front() == ttop++) 27 | DQ.pop_front(); 28 | } 29 | }; 30 | MQ C[N], R; 31 | void solve(int x) { 32 | int (*ans)[N] = _ans[x]; 33 | for (int i = 0; i < b; ++i) { 34 | C[i].clear(); 35 | for (int j = 0; j < n; ++j) 36 | C[i].push(A[j][i]); 37 | } 38 | for (int i = 0; i + n - 1 < a; ++i) { 39 | R.clear(); 40 | for (int j = 0; j < n; ++j) R.push(C[j].top()); 41 | for (int j = 0; j + n - 1 < b; ++j) { 42 | ans[i][j] = R.top(); 43 | R.pop(); 44 | if (j + n < b) R.push(C[j + n].top()); 45 | } 46 | for (int j = 0; j < b; ++j) { 47 | C[j].pop(); 48 | C[j].push(A[i + n][j]); 49 | } 50 | } 51 | } 52 | int main() { 53 | scanf("%d%d%d", &a, &b, &n); 54 | for (int i = 0; i < a; ++i) 55 | for (int j = 0; j < b; ++j) 56 | scanf("%d", &A[i][j]); 57 | solve(0); 58 | for (int i = 0; i < a; ++i) 59 | for (int j = 0; j < b; ++j) 60 | A[i][j] *= -1; 61 | solve(1); 62 | int ans = 1000000000; 63 | for (int i = 0; i + n - 1 < a; ++i) 64 | for (int j = 0; j + n - 1 < b; ++j) 65 | ans = std::min(ans, -(_ans[0][i][j] + _ans[1][i][j])); 66 | printf("%d\n", ans); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /BZOJ/BZOJ3143.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * Problem: BZOJ3143 3 | * Author: Rqy 4 | * Date: 2018 Feb 23 5 | * Algorithm: 6 | **************************************************************/ 7 | #include 8 | #include 9 | const int N = 505; 10 | const int M = 250050; 11 | int n, deg[N], x[M], y[M]; 12 | bool map[N][N]; 13 | double A[N][N]; 14 | double abs(double x) { return x < 0 ? -x : x; } 15 | void solve() { 16 | for (int i = 1; i <= n; ++i) { 17 | int j = i; 18 | for (int k = i + 1; k <= n; ++k) if (abs(A[k][i]) > abs(A[j][i])) j = k; 19 | for (int k = i; k <= n + 1; ++k) std::swap(A[i][k], A[j][k]); 20 | for (int j = i + 1; j <= n; ++j) 21 | for (int k = n + 1; k >= i; --k) 22 | A[j][k] -= A[i][k] * A[j][i] / A[i][i]; 23 | } 24 | for (int i = n; i; --i) { 25 | A[i][n + 1] /= A[i][i]; 26 | for (int j = i - 1; j; --j) 27 | A[j][n + 1] -= A[i][n + 1] * A[j][i]; 28 | } 29 | } 30 | double p[M]; 31 | int main() { 32 | int m; 33 | scanf("%d%d", &n, &m); 34 | for (int i = 0; i < m; ++i) { 35 | scanf("%d%d", &x[i], &y[i]); 36 | map[x[i]][y[i]] = map[y[i]][x[i]] = true; 37 | ++deg[x[i]]; ++deg[y[i]]; 38 | } 39 | for (int i = 1; i <= n; ++i) { 40 | A[i][i] = 1.0; 41 | if (i != n) 42 | for (int j = 1; j <= n; ++j) if (i != j && map[i][j]) 43 | A[i][j] = -1.0 / deg[j]; 44 | if (i == 1) 45 | A[i][n + 1] = 1.0; 46 | } 47 | solve(); 48 | for (int i = 0; i < m; ++i) 49 | p[i] = A[x[i]][n + 1] / deg[x[i]] + A[y[i]][n + 1] / deg[y[i]]; 50 | std::sort(p, p + m); 51 | double ans = .0; 52 | for (int i = 0; i < m; ++i) 53 | ans += p[i] * (m - i); 54 | printf("%.3lf\n", ans); 55 | return 0; 56 | } 57 | --------------------------------------------------------------------------------