CodeForce——Deltix Round, Autumn 2021 (open for everyone, rated, Div. 1 + Div. 2)前三道题目题解
目录
题目链接
A | standard input/output 1 s, 256 MB | 正在上传…重新上传取消 x13036 | |
B | standard input/output 2 s, 256 MB | 正在上传…重新上传取消 x10007 | |
C | standard input/output 2 s, 256 MB | 正在上传…重新上传取消 x7544 | |
A:
这道题目使用了一个数学知识,使得可以更加快速地求出最后一位1后的0的个数
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll H[37]; ll a[20]; ll b[20];//0的数目 ll c[20]; ll num; void Init() { for(int i = 0; i <= 31; i++) { H[(1ll << i) % 37] = i; } } void solve() { c[0] = 0; for(int i = 1; i <= num; i++) { b[i] = H[(a[i] & (-a[i]) )%37]; } for(int i = 1; i <= num; i++) { c[i] = a[i] >> b[i]; } int maxpos = 0; for(int i = 1; i <= num; i++) { if(c[i] >= c[maxpos]) maxpos = i; } for(int i = 1; i <= num; i++) { if(i != maxpos) { a[i] >>= b[i]; a[maxpos] <<= b[i]; } } ll ans = 0; for(int i = 1; i <= num; i++) { ans += a[i]; } printf("%lld\n", ans); } int main() { Init(); int T; cin >> T; while(T--) { scanf("%lld", &num); for(int i = 1; i <= num; i++) scanf("%lld", a+i); solve(); } return 0; }
B:
注意子序列与子串的区别
#include <bits/stdc++.h> using namespace std; char s[100007]; int n, q; bool sub(int pos) { switch (s[pos]) { case 'a': if(pos+2 > n) return false; if(s[pos+1] == 'b' && s[pos+2] == 'c') return true; else return false; break; case 'b': if(pos + 1 > n || pos-1 < 1) return false; if(s[pos-1]=='a' && s[pos+1]=='c') return true; else return false; break; case 'c': if(pos-2 < 1) return false; if(s[pos-2]=='a' && s[pos-1]=='b') return true; else return false; break; } return false; } int main() { int cnt = 0; scanf("%d%d", &n, &q); scanf("%s", s+1); for(int i = 1; i <= n-2; i++) if(s[i]=='a'&&s[i+1]=='b'&&s[i+2]=='c') cnt++; while(q--) { int pos; char buf[10]; scanf("%d%s",&pos, buf); if(sub(pos)) cnt--; s[pos] = buf[0]; if(sub(pos)) cnt++; printf("%d\n", cnt); } }
C:
#include <bits/stdc++.h> using namespace std; #define MAX 1000007 typedef long long ll; bitset<MAX>notprime; int prime[MAX]; void Init() { int k = 0; notprime[0] = true; notprime[1] = true; for(int i = 2; i <= 1000005; i++) { if(!notprime.test(i)) prime[++k] = i; for(int j = 1; j <= k; j++) { if(prime[j] * i > 1000005) break; notprime[prime[j] * i] = true; if(i % prime[j]==0) break; } } } int s[MAX]; int n, e; inline int next(int x) { return x+e; } inline int before(int x) { return x-e; } inline int dis(int x, int y) { return abs(x-y) / e; } /* int main() { Init(); for(int i = 1; i <= 100; i++) printf("%d\t", prime[i]); return 0; } */ //欧拉筛写错了,导致出了大问题。 int main() { Init(); int T; cin >> T; while(T--) { ll ans = 0; scanf("%d%d", &n, &e); for(int i = 1; i <= n; i++) { scanf("%d", s+i); } for(int i = 1; i <= e; i++) { int p = i; while(p <= n) { if(!notprime.test(s[p])) { int l = before(p); int r = next(p); while(l >= 1 && s[l] == 1) l = before(l); while(r <= n && s[r] == 1) r = next(r); ans += (ll)dis(p, l) * dis(r, p) - 1; } p = next(p); } } printf("%lld\n", ans); } return 0; }
本文来自博客园,作者:心坚石穿,转载请注明原文链接:https://www.cnblogs.com/xjsc01/p/16438223.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话