牛客练习赛20
背包
1 #include <bits/stdc++.h> 2 using namespace std; 3 int f[1111]; 4 5 int main() { 6 int T; 7 scanf("%d", &T); 8 while(T--) { 9 int N, M, K, P; 10 scanf("%d %d %d %d", &N, &M, &K, &P); 11 memset(f, 0, sizeof(f)); 12 f[0] = 1; 13 for(int i = 1; i <= N; ++i) { 14 for(int j = 0; j + 1 <= K; ++j) { 15 f[j+1] = (f[j+1] + f[j]) % P; 16 } 17 } 18 for(int i = 1; i <= M; ++i) { 19 for(int j = 0; j + 2 <= K; ++j) { 20 f[j+2] = (f[j+2] + f[j]) % P; 21 } 22 } 23 printf("%d\n", f[K]); 24 } 25 return 0; 26 }
推下式子发现只要包含至少一个0.5即可
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 5 int main() { 6 int T; 7 scanf("%d", &T); 8 while(T--) { 9 int n, cnt = 0; 10 scanf("%d", &n); 11 for(int i = 1; i <= n; ++i) { 12 string s; 13 cin >> s; 14 if(s == "0.500000") cnt++; 15 } 16 LL ans = ((1LL << cnt) - 1) * (1LL << (n - cnt)); 17 cout << ans << endl; 18 } 19 return 0; 20 }
一个环套树,卡内存只能dfs找环
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1 << 24; 5 int to[maxn], f[maxn], vis[maxn]; 6 7 int st = -1; 8 vector<int> v; 9 void dfs(int x) { 10 vis[x] = -1; 11 f[x] = 1; 12 if(to[x] != x && vis[to[x]] == 0) { 13 dfs(to[x]), f[x] += f[to[x]]; 14 if(st == x) { 15 for(int i = 0; i < v.size(); ++i) f[v[i]] = f[x]; 16 st = -1; 17 v.clear(); 18 } 19 else if(st != -1) v.push_back(x); 20 } 21 else if(to[x] != x && vis[to[x]] == -1) { 22 st = to[x]; 23 v.push_back(x); 24 } 25 else if(to[x] != x) f[x] += f[to[x]]; 26 vis[x] = 1; 27 } 28 29 int main() { 30 int T; 31 scanf("%d", &T); 32 while(T--) { 33 LL a, b, c, n; 34 scanf("%lld %lld %lld %lld", &a, &b, &c, &n); 35 for(int i = 0; i < n; ++i) vis[i] = 0; 36 for(int i = 0; i < n; ++i) { 37 to[i] = (a * i * i + b * i + c) % n; 38 } 39 int ans = 0; 40 for(int i = 0; i < n; ++i) { 41 if(!vis[i]) dfs(i); 42 ans = max(ans, f[i]); 43 } 44 printf("%d\n", ans); 45 } 46 return 0; 47 }
坐标范围较小,枚举交点相邻加边跑一个你喜欢的最短路
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 vector<int> G[5555]; 5 vector<double> d[5555]; 6 7 typedef pair<double, double> pdd; 8 typedef pair<pdd, int> pddi; 9 vector<pddi> v; 10 11 int in[5555]; 12 double dist[5555]; 13 void spfa(int S) { 14 for(int i = 0; i < 5555; ++i) dist[i] = 1e18; 15 dist[S] = 0; 16 queue<int> q; 17 q.push(S); 18 in[S] = 1; 19 while(!q.empty()) { 20 int x = q.front(); q.pop(); in[x] = 0; 21 for(int i = 0; i < G[x].size(); ++i) { 22 int to = G[x][i]; 23 double dis = d[x][i]; 24 if(dist[to] > dist[x] + dis) { 25 dist[to] = dist[x] + dis; 26 if(!in[to]) q.push(to), in[to] = 1; 27 } 28 } 29 } 30 } 31 32 double sqr(double x) {return x * x;} 33 34 int main() { 35 int T; 36 scanf("%d", &T); 37 while(T--) { 38 v.clear(); 39 for(int i = 0; i < 5555; ++i) G[i].clear(), d[i].clear(); 40 int Ax, Ay, Bx, By; 41 double P, Q, R; 42 scanf("%d %d %d %d %lf %lf %lf", &Ax, &Ay, &Bx, &By, &P, &Q, &R); 43 int S = 1, T = 2, cnt = 2; 44 for (int i = -1010; i <= 1010; ++i) { 45 if (Q == 0) continue; 46 double x = i, y = (R - P * x) / Q; 47 v.push_back(pddi(pdd(x, y), ++cnt)); 48 G[S].push_back(cnt), d[S].push_back(abs(Ax - x) + abs(Ay - y)); 49 G[cnt].push_back(T), d[cnt].push_back(abs(Bx - x) + abs(By - y)); 50 } 51 for (int i = -1010; i <= 1010; ++i) { 52 if (P == 0) continue; 53 double y = i, x = (R - Q * y) / P; 54 v.push_back(pddi(pdd(x, y), ++cnt)); 55 G[S].push_back(cnt), d[S].push_back(abs(Ax - x) + abs(Ay - y)); 56 G[cnt].push_back(T), d[cnt].push_back(abs(Bx - x) + abs(By - y)); 57 } 58 sort(v.begin(), v.end()); 59 for (int i = 1; i < v.size(); ++i) { 60 G[v[i-1].second].push_back(v[i].second), G[v[i].second].push_back(v[i-1].second); 61 double dis = sqrt(sqr(v[i-1].first.first - v[i].first.first) + sqr(v[i-1].first.second - v[i].first.second)); 62 d[v[i-1].second].push_back(dis), d[v[i].second].push_back(dis); 63 } 64 spfa(S); 65 printf("%.3f\n", dist[T]); 66 } 67 return 0; 68 }
随便模拟
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5 + 10; 4 5 int main() { 6 int n, x, ok = 1, c25 = 0, c50 = 0; 7 scanf("%d", &n); 8 for(int i = 1; i <= n; ++i) { 9 scanf("%d", &x); 10 if(x == 25) c25++; 11 else if(x == 50) { 12 if(c25) c25--, c50++; 13 else ok = 0; 14 } 15 else if(c25 && c50) c25--, c50--; 16 else if(c25 >= 3) c25 -=3; 17 else ok = 0; 18 } 19 puts(ok ? "YES" : "NO"); 20 return 0; 21 }
随便贪心
1 #include <bits/stdc++.h> 2 using namespace std; 3 int a[10]; 4 5 int main() { 6 int n; 7 scanf("%d", &n); 8 for(int i = 1; i <= 9; ++i) scanf("%d", a + i); 9 int m = a[1], p = 1; 10 for(int i = 1; i <= 9; ++i) 11 if(a[i] <= m) m = a[i], p = i; 12 int l = n / m; 13 if(l == 0) puts("-1"); 14 else { 15 n %= m; 16 for(int i = 1; i <= l; ++i) { 17 for(int j = 9; j >= p; --j) { 18 if(n >= a[j] - a[p]) { 19 n -= a[j] - a[p]; 20 printf("%d", j); 21 break; 22 } 23 } 24 } 25 puts(""); 26 } 27 return 0; 28 }