#include<iostream>usingnamespace std;typedeflonglong ll;
ll qmi(ll a, ll b, ll p){
ll res =1;while(b){if(b &1) res = res * a % p;
a = a * a % p;
b >>=1;}return res;}intmain(){int _;scanf("%d",&_);while(_ --){
ll a, b, p;scanf("%lld%lld%lld",&a,&b,&p);printf("%lld\n",qmi((a + b)% p, p, p));}}
阿巴阿巴
#include<iostream>usingnamespace std;constint N =1e3+10;int cnta[N], cntb[N];intmain(){int _;scanf("%d",&_);while(_ --){int n;
string s;
cin >> n >> s;for(int i =0; i < n; i ++){
cnta[i]= cnta[i -1]+(s[i]=='a');
cntb[i]= cntb[i -1]+(s[i]=='b');}bool ok =false;for(int l =0; l +7< n; l ++){int r = l +7;if(cnta[r]- cnta[l -1]==4&& cntb[r]- cntb[l -1]==4){
ok =true;
cout << l +1<<' '<< r +1<< endl;break;}}if(!ok) cout <<"impossible"<< endl;for(int i =0; i < n; i ++) cnta[i]= cntb[i]=0;}}
#include<iostream>#include<unordered_map>usingnamespace std;intmain(){int _;scanf("%d",&_);while(_ --){
unordered_map<int,int> ma;int n, x;scanf("%d%d",&n,&x);
string s; cin >> s;
ma[0]=-1;bool ok =false;int cnt =0;for(int i =0; i < n; i ++){if(s[i]=='a') cnt ++;else cnt --;if(ma.count(cnt)){if(i -(ma[cnt]+1)+1>= x){
ok =true;printf("%d %d\n", ma[cnt]+2, i +1);break;}}else ma[cnt]= i;}if(!ok)puts("impossible");}}
瓜瓜的特别任务
字符串哈希时注意 1.字符串下标要从1开始;2.p[0] = 1初始化
#include<iostream>usingnamespace std;typedefunsignedlonglong ull;constint N =5e3+10, P =131;
ull h[N], p[N];
ull get(int l,int r){return h[r]- h[l -1]* p[r - l +1];}intmain(){int _;scanf("%d",&_);while(_ --){
string s; cin >> s; s =" "+ s;int n =(int)s.size();
p[0]=1;for(int i =1; i <= n -1; i ++){
h[i]= h[i -1]* P + s[i];
p[i]= p[i -1]* P;}int mx =1;for(int len =1; len <= n -1; len ++){for(int i =1; i + len -1<= n -1; i ++){int cnt =1, j = i + len;while(j + len -1<= n -1&&get(i, i + len -1)==get(j, j + len -1)){
cnt ++; j += len;}
mx =max(mx, cnt);}}printf("%d\n", mx);}}
磊爷与佣兵战记
#include<iostream>#include<cmath>#include<vector>usingnamespace std;typedeflonglong ll;constint N =2e5+10;
ll w[N];
ll sum[N];bool is_lazy[N];
vector<int> g[N];
vector<int> lazy[N];intmain(){int n, m;scanf("%d%d",&n,&m);for(int i =0, a, b; i < m &&scanf("%d%d",&a,&b); i ++){
g[a].push_back(b);
g[b].push_back(a);}int t =sqrt(n);for(int i =1; i <= n; i ++){if(g[i].size()>= t){
is_lazy[i]=true;for(auto u : g[i])
lazy[u].push_back(i);}}int q;scanf("%d",&q);int op, p;while(q --){scanf("%d%d",&op,&p);if(op ==1){int x;scanf("%d",&x);
w[p]+= x;if(is_lazy[p]) sum[p]+= x;else{for(auto v : g[p])
w[v]+= x;}}else{
ll res = w[p];for(auto v : lazy[p])
res += sum[v];printf("%lld\n", res);}}}
瓜瓜的春天
dp时注意是否要使用滚动数组
使用滚动数组的时候,注意,在初始化和最后输出结果的时候也要&1
#include<iostream>usingnamespace std;typedeflonglong ll;constint N =1e4+10;const ll mod =998244353;
ll f[2][N];
ll qmi(ll a, ll b, ll p){
ll res =1;while(b){if(b &1) res = res * a % p;
a = a * a % p;
b >>=1;}return res;}intmain(){int ii, jj, a, b;scanf("%d%d%d%d",&ii,&jj,&a,&b);
ll inv3 =qmi(3, mod -2, mod);
f[1&1][1]= inv3;for(int i =1; i <= ii; i ++)for(int j =1; j <= jj; j ++){if(i ==1&& j ==1)continue;
f[i &1][j]=((f[i &1][j -1]* inv3 % mod + f[i -1&1][j]* inv3 % mod)% mod + f[i -1&1][j -1]* inv3 % mod)% mod;}printf("%lld\n", f[ii &1][jj]);}