等比求和


1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1e5 + 10; 5 const LL mod = 1e9 + 9; 6 char s[maxn]; 7 8 LL qpow(LL a, LL b) 9 { 10 LL ret = 1LL; 11 while(b) 12 { 13 if(b & 1) ret = ret * a % mod; 14 a = a * a % mod; 15 b >>= 1; 16 } 17 return ret; 18 } 19 20 LL inv(LL x) 21 { 22 return qpow(x, mod - 2); 23 } 24 25 int main(){ 26 LL n, a, b, k; 27 scanf("%I64d %I64d %I64d %I64d %s", &n, &a, &b, &k, s); 28 LL base = qpow(a, n), sum = 0, r = (n + 1) / k, ans; 29 for(int i = 0; i < k; ++i){ 30 if(s[i] == '+') sum = (sum + base) % mod; 31 else sum = (sum - base + mod) % mod; 32 base = base * b % mod * inv(a) % mod; 33 } 34 LL tmp = qpow(b, k) * inv(qpow(a, k)) % mod; 35 if(tmp == 1) ans = r * sum % mod; 36 else ans = sum * (1 - qpow(tmp, r) + mod) % mod * inv((1 - tmp + mod) % mod) % mod; 37 printf("%I64d\n", ans); 38 return 0; 39 }
大家的贪心水平都很高阿


1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 2e5 + 10; 4 vector<int> G[maxn], ans; 5 6 vector<int> son[maxn][2][2]; // d/nd y/n 7 int df[maxn], ndf[maxn]; 8 void dfs1(int x, int f){ 9 for(int i = 0; i < G[x].size(); ++i){ 10 int to = G[x][i]; 11 if(to == f) continue; 12 dfs1(to, x); 13 son[x][df[to]][ndf[to]].push_back(to); 14 } 15 if(son[x][0][0].size() || son[x][1][0].size() % 2 == 1 && son[x][1][1].size() == 0) df[x] = 0; 16 else df[x] = 1; 17 if(son[x][0][0].size() || son[x][1][0].size() % 2 == 0 && son[x][1][1].size() == 0) ndf[x] = 0; 18 else ndf[x] = 1; 19 } 20 21 void dfs2(int x, int del_f){ 22 int cnt = 0; 23 for(int i = 0; i < son[x][0][1].size(); ++i){ 24 dfs2(son[x][0][1][i], 0); 25 } 26 if(del_f && son[x][1][0].size() % 2 == 1 || !del_f && son[x][1][0].size() % 2 == 0){ 27 int tmp = son[x][1][1][son[x][1][1].size()-1]; 28 son[x][1][1].pop_back(); 29 dfs2(tmp, 0); 30 } 31 ans.push_back(x); 32 for(int i = 0; i < son[x][1][1].size(); ++i){ 33 dfs2(son[x][1][1][i], 1); 34 } 35 for(int i = 0; i < son[x][1][0].size(); ++i){ 36 dfs2(son[x][1][0][i], 1); 37 } 38 } 39 40 int main(){ 41 int n; 42 scanf("%d", &n); 43 for(int i = 1; i <= n; ++i){ 44 int p; 45 scanf("%d", &p); 46 if(p) G[i].push_back(p), G[p].push_back(i); 47 } 48 dfs1(1, 0); 49 if(df[1] == 0) puts("NO"); 50 else{ 51 dfs2(1, 1); 52 puts("YES"); 53 for(int i = 0; i < n; ++i) printf("%d\n", ans[i]); 54 } 55 return 0; 56 }
随便枚举


1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 2e5 + 10; 5 LL w[maxn], h[maxn], c[maxn]; 6 map< LL, vector<int> > M; 7 map< LL, LL > G; 8 map< LL, vector<int> > :: iterator it, itt; 9 10 bool cmp(int i, int j){ 11 return h[i] < h[j]; 12 } 13 14 LL gcd(LL a, LL b){ 15 return a % b ? gcd(b, a % b) : b; 16 } 17 18 int main(){ 19 int n; 20 scanf("%d", &n); 21 for(int i = 1; i <= n; ++i) { 22 cin >> w[i] >> h[i] >> c[i]; 23 M[w[i]].push_back(i); 24 } 25 LL m = 1e18; 26 for(it = M.begin(); it != M.end(); ++it){ 27 LL W = (*it).first; 28 vector<int> & t = (*it).second; 29 sort(t.begin(), t.end(), cmp); 30 G[W] = c[t[0]]; 31 for(int j = 0; j < t.size(); ++j) G[W] = gcd(G[W], c[t[j]]); 32 LL sum = 0; 33 for(int j = 0; j < t.size(); ++j) sum += c[t[j]] / G[W]; 34 if(sum < m) m = sum, itt = it; 35 } 36 LL ok = 1, g = G[(*itt).first], ans = 0; 37 for(it = M.begin(); it != M.end(); ++it){ 38 vector<int> & t = (*it).second; 39 vector<int> & tt = (*itt).second; 40 LL W = (*itt).first; 41 if(t.size() != tt.size()) {ok = 0; break;} 42 for(int j = 0; j < t.size(); ++j){ 43 if(h[t[j]] != h[tt[j]]) ok = 0; 44 if(c[t[j]] % (c[tt[j]] / G[W]) != 0) ok = 0; 45 if(c[t[j]] / (c[tt[j]] / G[W]) != c[t[0]] / (c[tt[0]] / G[W])) ok = 0; 46 } 47 if(!ok) break; 48 g = gcd(g, c[t[0]] / (c[tt[0]] / G[W])); 49 } 50 for(LL i = 1; i * i <= g; ++i){ 51 if(g % i == 0){ 52 ans += 2; 53 if(i * i == g) ans--; 54 } 55 } 56 cout << ok * ans << endl; 57 return 0; 58 }
蛤习天下无敌


1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1e5 + 10; 5 const int INF = 1e9; 6 char s[maxn], T[maxn]; 7 vector<int> pos[maxn], id[maxn]; 8 int k[maxn], ans[maxn]; 9 10 // Hash 11 const int seed = 131; 12 LL mod = 1000000007; 13 LL po[maxn], P[maxn], PT[maxn], t0[maxn]; 14 15 void init(int len) 16 { 17 P[0] = 0, po[0] = 1; 18 for(int i = 1; i <= len; i++) 19 P[i] = (P[i-1] * seed + s[i]) % mod, po[i] = po[i-1] * seed % mod; 20 } 21 map<LL, int> M; 22 23 int main(){ 24 scanf("%s", s + 1); 25 int len = strlen(s + 1); 26 init(len); 27 int n; 28 scanf("%d", &n); 29 for(int kase = 1; kase <= n; ++kase) { 30 scanf("%d%s", k + kase, T + 1); 31 int lt = strlen(T + 1); 32 for (int t = 0; t <= 0; t++) { 33 PT[0] = 0; 34 for (int i = 1; i <= lt; i++) 35 PT[i] = (PT[i - 1] * seed + T[i]) % mod; 36 } 37 t0[kase] = PT[lt]; 38 ans[kase] = INF; 39 id[lt].push_back(kase); 40 } 41 for(int i = 1; i <= len; ++i) { 42 if(id[i].size() == 0) continue; 43 M.clear(); 44 for(int j = 0; j < id[i].size(); ++j){ 45 int x = id[i][j]; 46 M[t0[x]] = x; 47 } 48 for(int j = i; j <= len; ++j){ 49 LL T0 = (P[j] - P[j-i] * po[i] % mod + mod) % mod; 50 if(M.find(T0) == M.end()) continue; 51 int x = M[T0]; 52 pos[x].push_back(j); 53 if(pos[x].size() >= k[x]) ans[x] = min(pos[x][pos[x].size() - 1] - pos[x][pos[x].size() - k[x]] + i, ans[x]); 54 } 55 } 56 for(int i = 1; i <= n; ++i) 57 printf("%d\n", ans[i] == INF ? -1 : ans[i]); 58 return 0; 59 }
稀疏矩阵求个逆


1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const LL mod = 1e9 + 7; 5 typedef pair<int, int> pii; 6 int sqr(int x){return x * x;} 7 vector<pii> v; 8 map<pii, int> id; 9 int m[8000][8000], X[8000]; 10 int dx[] = {1, 0, -1, 0}; 11 int dy[] = {0, 1, 0, -1}; 12 int cnt; 13 14 LL qpow(LL a, LL b) { 15 LL ret = 1LL; 16 while(b) { 17 if(b & 1) ret = ret * a % mod; 18 a = a * a % mod; 19 b >>= 1; 20 } 21 return ret; 22 } 23 24 LL inv(LL x) { 25 return qpow(x, mod - 2); 26 } 27 28 void CAL(){ 29 for(int i = 0; i < cnt; ++i){ 30 LL x = inv(m[i][i]); 31 for(int j = i; j < min(cnt, i + 111); ++j) m[i][j] = m[i][j] * x % mod; 32 X[i] = X[i] * x % mod; 33 vector<int> O; 34 vector<LL> T; 35 for(int j = i + 1; j < min(i + 111, cnt); ++j){ 36 if(j == i) continue; 37 if(!m[j][i]) continue; 38 O.push_back(j), T.push_back(m[j][i]); 39 X[j] = (X[j] - (LL) X[i] * m[j][i] % mod + mod) % mod; 40 } 41 for(int j = i; j < min(cnt, i + 111); ++j){ 42 if(!m[i][j]) continue; 43 for(int k = 0; k < O.size(); ++k) m[O[k]][j] = (m[O[k]][j] - T[k] * m[i][j] % mod + mod) % mod; 44 } 45 } 46 for(int i = cnt - 1; i >= 0; --i) { 47 for (int j = i - 1; j >= max(0, i - 111); --j) { 48 if (m[j][i] == 0) continue; 49 X[j] = (X[j] - (LL) X[i] * m[j][i] % mod + mod) % mod; 50 } 51 } 52 } 53 54 int main(){ 55 int R, a[4]; 56 scanf("%d", &R); 57 for(int i = 0; i < 4; ++i) scanf("%d", a + i); 58 59 for(int i = 0; i <= 200; ++i){ 60 for(int j = 0; j <= 200; ++j){ 61 if(sqr(100 - i) + sqr(100 - j) <= sqr(R)) v.push_back(pii(i, j)), id[pii(i, j)] = cnt++; 62 } 63 } 64 65 for(int i = 0; i < cnt; ++i){ 66 m[i][i] = 1; 67 for(int k = 0; k < 4; ++k){ 68 int xx = v[i].first + dx[k]; 69 int yy = v[i].second + dy[k]; 70 if(id.find(pii(xx, yy)) == id.end()) continue; 71 m[i][id[pii(xx, yy)]] = (mod - a[k] * inv(a[0] + a[1] + a[2] + a[3]) % mod + mod) % mod; 72 } 73 } 74 75 X[id[pii(100, 100)]] = 1; 76 CAL(); 77 78 LL ans = 0; 79 for(int i = 0; i < cnt; ++i) ans = (ans + X[i]) % mod; 80 cout << ans << endl; 81 82 return 0; 83 }