标签:技术分享 i++ color erase 大小 open ras 记忆 长度
1000.a+b。
#include<bits/stdc++.h> using namespace std; int a,b; int main() { ios::sync_with_stdio(false); while(cin >> a >> b) cout << a+b << endl; return 0; }
1001.直接开数组回爆内存,于是自己开个一维数组存放元素,大小需要自己斟酌。(或者动态数组)
直接处理对应位置,根据最后对应的位置得出答案。
#include<bits/stdc++.h> using namespace std; int n,m,q,a[1000005],r[1000005],c[1000005]; string s; int main() { ios::sync_with_stdio(false); int z = 0; while(cin >> n >> m) { cout << "Case #" << ++z << ":" << endl; for(int i = 1;i <= n;i++) r[i] = i; for(int i = 1;i <= m;i++) c[i] = i; int cnt = 0; for(int i = 1;i <= n;i++) { cin >> s; for(int j = 0;j < m;j++) { if(s[j] == ‘T‘) a[++cnt] = 1; else if(s[j] == ‘i‘) a[++cnt] = 2; else a[++cnt] = 0; } } cin >> q; while(q--) { int x,y,z; cin >> x >> y >>z; if(x == 1) { int t = (r[y]-1)*m+c[z]; switch(a[t]) { case 0: cout << "Empty" << endl;break; case 1: cout << "Tree" << endl;break; case 2: cout << "Phone" << endl;break; } } else if(x == 2) swap(r[y],r[z]); else swap(c[y],c[z]); } } return 0; }
1002.dp[i][j][k]代表前i段,j个绿,k个蓝造成的最大伤害。
#include<bits/stdc++.h> #define LL long long using namespace std; LL n,x,y,z,t; LL dp[105][105][105]; int main() { while(cin >> n) { cin >> x >> y >> z >> t; memset(dp,0,sizeof(dp)); dp[1][0][0] = x*t; for(int i = 2;i <= n;i++) { for(int g = 0;g <= i;g++) { int b = 0; for(b = 0;b+g < i;b++) dp[i][g][b] = dp[i-1][g][b]+(g*y+x)*(b*z+t); if(g > 0) dp[i][g][b] = max(dp[i][g][b],dp[i-1][g-1][b]+y*(g-1)*(b*z+t)); if(b > 0) dp[i][g][b] = max(dp[i][g][b],dp[i-1][g][b-1]+g*y*((b-1)*z+t)); } } LL ans = 0; for(int i = 0;i <= n;i++) { for(int j = 0;j+i <= n;j++) ans = max(ans,dp[n][i][j]); } cout << ans << endl; } return 0; }
1003.转化到10进制,模拟加法,转换回去。
#include<bits/stdc++.h> using namespace std; int change1(char a) { switch(a) { case ‘A‘:return 10; case ‘B‘:return 11; case ‘C‘:return 12; case ‘D‘:return 13; case ‘E‘:return 14; case ‘F‘:return 15; default :return a-‘0‘; } } char change2(int a) { switch(a) { case 10:return ‘A‘; case 11:return ‘B‘; case 12:return ‘C‘; case 13:return ‘D‘; case 14:return ‘E‘; case 15:return ‘F‘; default:return a+‘0‘; } } string s1,s2; int n,ans[100]; int main() { while(cin >> n) { cin >> s1 >> s2; int p1 = s1.length()-1,p2 = s2.length()-1,cnt = 0,temp = 0; while(p1 >= 0 && p2 >= 0) { int now = change1(s1[p1])+change1(s2[p2])+temp; ans[++cnt] = now%n; temp = now/n; p1--; p2--; } while(p1 >= 0) { int now = change1(s1[p1])+temp; ans[++cnt] = now%n; temp = now/n; p1--; } while(p2 >= 0) { int now = change1(s2[p2])+temp; ans[++cnt] = now%n; temp = now/n; p2--; } if(temp) ans[++cnt] = temp; for(int i = cnt;i > 0;i--) cout << change2(ans[i]); cout << endl; } return 0; }
1004.每次固定一个对应点,旋转5次,暴力比较。
#include<bits/stdc++.h> using namespace std; int a[15],b[15],A,B,C,D,E,F,G,H,I,J; int point[12][2] = {{1,12},{2,7},{3,8},{4,9},{5,10},{6,11},{7,2},{8,3},{9,4},{10,5},{11,6},{12,1}}; int x[12][10] = {{2,3,4,5,6,10,11,7,8,9},{1,6,9,10,3,5,8,12,11,4},{11,4,1,2,10,7,5,6,9,12},{1,3,11,7,5,2,10,12,8,6}, {1,4,7,8,6,3,11,12,9,2},{1,5,8,9,2,4,7,12,10,3},{4,11,12,8,5,3,10,9,6,1},{12,9,6,5,7,10,2,1,4,11}, {6,8,12,10,2,5,7,11,3,1},{2,9,12,11,3,6,8,7,4,1},{3,10,12,7,4,2,9,8,5,1},{9,8,7,11,10,6,5,4,3,2}}; int fun(int AA,int BB,int CC,int DD,int EE,int FF,int GG,int HH,int II,int JJ) { if(AA==A && BB==B && CC==C && DD==D && EE==E && FF==F && GG==G && HH==H && II==I && JJ==J) return 1; if(AA==B && BB==C && CC==D && DD==E && EE==A && FF==G && GG==H && HH==I && II==J && JJ==F) return 1; if(AA==C && BB==D && CC==E && DD==A && EE==B && FF==H && GG==I && HH==J && II==F && JJ==G) return 1; if(AA==D && BB==E && CC==A && DD==B && EE==C && FF==I && GG==J && HH==F && II==G && JJ==H) return 1; if(AA==E && BB==A && CC==B && DD==C && EE==D && FF==J && GG==F && HH==G && II==H && JJ==I) return 1; return 0; } int main() { int T; cin >>T; while(T--) { int flag = 1; for(int i = 1;i <= 12;i++) cin >> a[i]; for(int i = 1;i <= 12;i++) cin >> b[i]; A = b[2],B = b[3],C = b[4],D = b[5],E = b[6]; F = b[10],G = b[11],H = b[7],I = b[8],J = b[9]; for(int i = 0;i < 12;i++) { if(a[point[i][0]] == b[1] && a[point[i][1]] == b[12]) { if(fun(a[x[i][0]],a[x[i][1]],a[x[i][2]],a[x[i][3]],a[x[i][4]], a[x[i][5]],a[x[i][6]],a[x[i][7]],a[x[i][8]],a[x[i][9]])) { flag = 0; cout << "Identical" << endl; break; } } } if(flag) cout << "Different" << endl; } return 0; }
1005.每次除2向上取整,直到最后一根。
#include<bits/stdc++.h> using namespace std; long long n; int main() { while(cin >> n) { int cnt = 0; while(n > 1) { n = n/2+n%2; cnt++; } cout << cnt << endl; } return 0; }
1006.按位运算,a[i],b[i],c[i]分别对应^,|,&,表示从头到i当前位的累和。
#include<bits/stdc++.h> using namespace std; int bin[100005][35],two[35],n; double a[100005],b[100005],c[100005]; int main() { two[0] = 1; for(int i = 1;i <= 30;i++) two[i] = two[i-1]*2; while(cin >> n) { memset(bin,0,sizeof(bin)); int maxcnt = 0; double ans1 = 0,ans2 = 0,ans3 = 0; for(int i = 1,x;i <= n;i++) { cin >> x; ans1 -= x; ans2 -= x; ans3 -= x; int cnt = 0; while(x) { bin[i][++cnt] = x%2; x /= 2; } maxcnt = max(cnt,maxcnt); } for(int j = 1;j <= maxcnt;j++) { int last0 = 0,last1 = 0; for(int i = 1;i <= n;i++) { if(bin[i][j] == 1) { if(last1 > 0) a[i] = a[last1-1]+i-last1; else a[i] = i; b[i] = i-last0; c[i] = i; last1 = i; } else { if(i > 1) a[i] = a[i-1]; else a[i] = 0; b[i] = 0; c[i] = last1; last0 = i; } ans1 += a[i]*two[j]; ans2 += b[i]*two[j]; ans3 += c[i]*two[j]; } } cout << fixed << setprecision(3) << ans1/n/n << " " << fixed << setprecision(3) << ans2/n/n << " " << fixed << setprecision(3) << ans3/n/n << endl; } return 0; }
1007.dp[i][j]表示n层k个蛋需要的最多次数。
#include<bits/stdc++.h> using namespace std; int dp[1005][1005]; int main() { int n,k; while(cin >> n >> k) { memset(dp,0x3f,sizeof(dp)); for(int i=0;i<=n;i++) { dp[i][0]=0; dp[i][1]=1; } for(int i=0;i<=k;i++) dp[1][i]=i; for(int j=2;j<=k;j++) { for(int i=2;i<=n;i++) { for(int t=1;t<=j;t++) dp[i][j]=min(dp[i][j],max(dp[i-1][t-1],dp[i][j-t])+1); } } cout << dp[n][k] << endl; } return 0; }
1008.约瑟夫环。
#include<bits/stdc++.h> using namespace std; int n,k; int nextt[200005],lastt[200005]; int main() { while(cin >> n >> k) { for(int i = 1;i < n;i++) nextt[i] = i+1; for(int i = 2;i <= n;i++) lastt[i] = i-1; nextt[n] = 1; lastt[1] = n; cout << k;; nextt[lastt[k]] = nextt[k]; lastt[nextt[k]] = lastt[k]; int cnt = n-1,now = nextt[k]; while(cnt) { int left = (k-1)%cnt; while(left--) now = nextt[now]; cout << " " << now; nextt[lastt[now]] = nextt[now]; lastt[nextt[now]] = lastt[now]; now = nextt[now]; cnt--; } cout << endl; } return 0; }
1009.约瑟夫环线段树实现。
#include<bits/stdc++.h> using namespace std; struct segtree { int left,right,sum; }tree[800005]; void build(int pos,int l,int r) { tree[pos].left = l; tree[pos].right = r; if(l == r) { tree[pos].sum = 1; return; } int mid = (l+r)/2; build(pos*2,l,mid); build(pos*2+1,mid+1,r); tree[pos].sum = tree[pos*2].sum+tree[pos*2+1].sum; } int update(int pos,int num) { tree[pos].sum--; if(tree[pos].left == tree[pos].right) return tree[pos].left; if(num <= tree[pos*2].sum) return update(pos*2,num); else return update(pos*2+1,num-tree[pos*2].sum); } int main() { int n,m; while(cin >> n >> m) { build(1,1,n); cout << m; int now = m; update(1,now); for(int i = 2;i <= n;i++) { now = (now+m-1)%tree[1].sum; if(now == 0) now = tree[1].sum; cout << " " << update(1,now); } cout << endl; } return 0; }
1010.分为两段,预处理,枚举中点,求两段的和的最大值。
#include<bits/stdc++.h> using namespace std; int lmax[100005],rmax[100005],a[100005],n; int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; int minn = a[1]; lmax[1] = -1e9; for(int i = 2;i <= n;i++) { lmax[i] = max(lmax[i-1],a[i]-minn); minn = min(minn,a[i]); } int maxx = a[n]; rmax[n] = -1e9; for(int i = n-1;i >= 1;i--) { rmax[i] = max(rmax[i+1],maxx-a[i]); maxx = max(maxx,a[i]); } int ans = -1e9; for(int i = 2;i < n-2;i++) ans = max(lmax[i]+rmax[i+1],ans); cout << ans << endl; } return 0; }
1011.规律。
#include<bits/stdc++.h> using namespace std; long long n; int main() { ios::sync_with_stdio(false); while(cin >> n) { long long y = sqrt(n); if(y*y <= n) y++; if(y%2) cout << "even" << endl; else cout << "odd" << endl; } return 0; }
1012.KMP的运用。
#include<bits/stdc++.h> using namespace std; int nextt[400005]; string s; void get_next(int len) { int i = 0,j = -1; nextt[0] = -1; while(i < len) { if(j == -1 || s[len-i-1] == s[len-j-1]) nextt[++i] = ++j; else j = nextt[j]; } } int main () { while(cin >> s) { int endd = s.length(); get_next(endd); int maxlen = 0,maxcnt = 0; for(int i = 1;i <= endd;i++) { int len = i-nextt[i]; if(len && i%len == 0) { int cnt = i/len; if(cnt >= maxcnt) { maxcnt = cnt; maxlen = len; } } } if(maxcnt > 1) cout << s.substr(endd-maxlen) << " " << maxcnt << endl; else cout << -1 << endl; } return 0; }
1013.求前缀和。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m,a[100005],b[100005]; int main() { while(cin >> n) { long long sum = 0; memset(b,0,sizeof(b)); for(int i = 1;i <= n;i++) cin >> a[i]; cin >> m; for(int i = 1,l,r,k;i <= m;i++) { cin >> l >> r >> k; b[l+1] -= k; b[r+1] += k; } for(int i = 1;i <= n;i++) { b[i] += b[i-1]; a[i] += b[i]; sum += a[i]; } int ave = sum/n; for(int i = 1;i <= n;i++) a[i] -= ave; sum = 0; for(int i = 1;i <= n;i++) sum += (long long)a[i]*a[i]; cout << sum << endl; } return 0; }
1014.二分或者概率论知识直接算。
先将l,r和210转化为1-n的数,问题就成了求k次取1-n中的数,求最大值的数学期望。
参考:http://www.zhihu.com/question/30463955?sort=created
#include<bits/stdc++.h> using namespace std; int l,r; double x[305]; int main() { while(cin >> l >> r) { if(r <= 210) { cout << "stupid" << endl; continue; } int m = r-l+1,ok = 210-l+1; for(int i = 1;i <= m;i++) x[i] = 1; double now = 300; int ans = 0; while(m - now <= ok-1e-6) { now = 0; for(int i = 1;i <= m-1;i++) { x[i] *= (double)i/m; now += x[i]; } ans++; } cout << ans << endl; } return 0; }
1015.先确定最大的位数,然后从高位开始贪心。
#include<bits/stdc++.h> using namespace std; struct aaa { int num,w; }a[10]; int w[10]; bool cmp(aaa x,aaa y) { if(x.w == y.w) return x.num > y.num; return x.w < y.w; } int main() { int n; while(cin >> n) { for(int i = 1;i <= 9;i++) { scanf("%d",&a[i].w); w[i] = a[i].w; a[i].num = i; } sort(a+1,a+10,cmp); if(n < a[1].w) { cout << -1 << endl; continue; } int cnt = n/a[1].w,temp = n%a[1].w,now = 9; while(n) { if(n < a[1].w) break; for(;temp+a[1].w < w[now];now--); cout << now; n -= w[now]; temp = temp+a[1].w-w[now]; } cout << endl; } return 0; }
1016.a+b。
#include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long a,b; while(cin >> a >> b) cout << a+b << endl; return 0; }
1017.排序。
#include<bits/stdc++.h> using namespace std; int n; struct aa { string name; int rating; friend bool operator <(aa x,aa y) { if(x.rating != y.rating) return x.rating > y.rating; return x.name < y.name; } }a[10005]; int main() { ios::sync_with_stdio(false); while(cin >> n && n) { for(int i = 0;i < n;i++) cin >> a[i].name >> a[i].rating; sort(a,a+n); for(int i = 0;i < n;i++) cout << a[i].name << " " << a[i].rating << endl; } return 0; }
1018.约瑟夫环数学结论。
#include<bits/stdc++.h> using namespace std; int n,k,t; int fun(int x) { int a = (n-x+k)%(n-x+1); for(int i = 2;i <= x;) { int temp = min(x+1-i,(n-x+i-a-1)/k+1); a = (a+k*temp)%(n-x+i+temp-1); i += temp; } return a; } int main() { while(cin >> n >> k >> t) { int x; cin >> x; cout << fun(x)+1; while(--t) { cin >> x; cout << " " << fun(x)+1; } cout << endl; } return 0; }
1019.末尾0由2*5构成,2足够多,看5的数量就可以了。注意25是2个5,125是3个5...先用等比公式推出趋于极限时,n = m*4,但实际可能比这个数大一点,再向上找。
#include<bits/stdc++.h> using namespace std; int n; int f(int x) { int now = 5,ans = 0; while(now <= x) { ans += x/now; now *= 5; } return ans; } int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { cin >> n; int ans = n*4; while(f(ans) < n) ans++; if(f(ans) == n) cout << ans << endl; else cout << "No solution" << endl; } return 0; }
1020.概率dp,dp[i][j]表示前i题对j题的概率。
#include<bits/stdc++.h> using namespace std; int n,k; double a[1005],dp[1005][1005]; int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { cin >> n >> k; for(int i = 1;i <= n;i++) cin >> a[i]; dp[1][0] = 1-a[1]; dp[1][1] = a[1]; for(int i = 2;i <= n;i++) { dp[i][0] = dp[i-1][0]*(1-a[i]); for(int k = 1;k <= n;k++) dp[i][k] = dp[i-1][k]*(1-a[i])+dp[i-1][k-1]*a[i]; } double ans = 0; for(int i = k;i <= n;i++) ans += dp[n][i]; cout << fixed << setprecision(4) << ans << endl; } return 0; }
1021.划成三角形,公式。
#include<bits/stdc++.h> #define PI atan(1)*4 using namespace std; int main() { int T; cin >> T; while(T--) { int n; double x; cin >> n >> x; cout << fixed << setprecision(4) << 0.25*n*x*x/tan(PI/n) <<endl; } return 0; }
1022.统计每个数被加了多少次,两次前缀和。
#include<bits/stdc++.h> using namespace std; int sum[1000005]; int main() { ios::sync_with_stdio(false); for(int i = 1;i <= 1000000;i++) { for(int j = i;j <= 1000000;j += i) sum[j]++; } for(int i = 2;i <= 1000000;i++) sum[i] = (sum[i]+sum[i-1])%1007; for(int i = 2;i <= 1000000;i++) sum[i] = (sum[i]+sum[i-1])%1007; int a,b; while(cin >> a >> b) cout << (sum[b]-sum[a-1]+1007)%1007 << endl; return 0; }
1023.区间不交叉,排序后下标是对应的,直接二分在r中找,确定是否在某个区间中。
#include<bits/stdc++.h> using namespace std; int n,m,r[100005]; struct city { int l,id; friend bool operator <(city x,city y) { return x.l < y.l; } }a[100005]; int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { cin >> n; for(int i = 1;i <= n;i++) cin >> a[i].l >> r[i] >> a[i].id; sort(a+1,a+n+1); sort(r+1,r+n+1); scanf("%d",&m); while(m--) { int ip; cin >> ip; int ans = lower_bound(r+1,r+1+n,ip)-r; if(ans == n+1 || ip < a[ans].l) cout << -1 << endl; else cout << a[ans].id << endl; } } return 0; }
1024.线段树求逆序对数。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; int n; struct segtree { int left,right,sum; }tree[45]; void build(int pos,int l,int r) { tree[pos].left = l; tree[pos].right = r; tree[pos].sum = 0; if(l < r) { int mid = (l+r)/2; build(pos*2,l,mid); build(pos*2+1,mid+1,r); } } int getsum(int pos,int l,int r) { if(tree[pos].left == l && tree[pos].right == r) return tree[pos].sum; int mid = (tree[pos].right+tree[pos].left)/2; if(r <= mid) return getsum(pos*2,l,r); if(l > mid) return getsum(pos*2+1,l,r); return getsum(pos*2,l,mid)+getsum(pos*2+1,mid+1,r); } void update(int pos,int num) { tree[pos].sum++; if(tree[pos].left == tree[pos].right) return; int mid = (tree[pos].left+tree[pos].right)/2; if(num <= mid) update(pos*2,num); else update(pos*2+1,num); } int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { build(1,0,9); cin >> n; long long ans = 0; int temp; while(n--) { cin >> temp; update(1,temp); if(temp == 9) continue; ans = (ans+getsum(1,temp+1,9))%MOD; } cout << ans << endl; } return 0; }
1025.类似树形dp,期间记忆化一下。
#include<bits/stdc++.h> using namespace std; vector<int> make[10005]; int v[10005],c[10005],sum[10005],n,m,w; int dfs(int x) { if(sum[x] == -1) { if(make[x].empty()) sum[x] = v[x]; else { int ans = w; for(int i = 0;i < make[x].size();i++) ans += dfs(make[x][i]); sum[x] = min(ans,v[x]); } } return sum[x]; } int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { memset(sum,-1,sizeof(sum)); cin >> n >> m >> w; for(int i = 0;i < n;i++) make[i].clear(); for(int i = 0;i < n;i++) { cin >> v[i] >> c[i]; for(int j = 0;j < c[i];j++) { int temp; cin >> temp; make[i].push_back(temp); } } int ans = 0; for(int i = 0;i < m;i++) { int temp; cin >> temp; ans += dfs(temp); } cout << ans << endl; } return 0; }
1026.快速幂模版。
#include<bits/stdc++.h> using namespace std; long long PowerMod(int a, int b, int c) { long long ans = 1; a = a%c; while(b > 0) { if(b % 2 == 1) ans = (ans*a)%c; b = b/2; a = (a*a)%c; } return ans; } int main() { int a,b,c; while(cin >> a >> b >> c) cout << PowerMod(a,b,c) << endl; return 0; }
1027.矩阵快速幂模版。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; int n; struct matrix { long long m[2][2]; }; matrix one = { 1,0, 0,1 }; matrix base = { 2,1, 1,0 }; matrix mul(matrix a, matrix b) { matrix tmp; for(int i = 0; i < 2;i++) { for(int j = 0; j < 2;j++) { tmp.m[i][j] = 0; for(int k = 0; k < 2;k++) tmp.m[i][j] = (tmp.m[i][j]+a.m[i][k]*b.m[k][j])%MOD; } } return tmp; } long long fast_mod(int n) { matrix ans = one,y = base; while(n) { if(n&1) ans = mul(ans,y); y = mul(y,y); n /= 2; } return ans.m[0][0]; } int main() { while(cin >> n) cout << fast_mod(n-1) << endl; return 0; }
1028.素数筛,dp。
#include<bits/stdc++.h> using namespace std; int n,dp[1000005] = {0},prime[100000]; int main() { ios::sync_with_stdio(false); int cnt = 0; for(int i = 2;i <= 1000000;i++) { if(dp[i]) continue; for(int j = 2*i;j <= 1000000;j += i) dp[j] = 1; } for(int i = 2;i <= 1000000;i++) { if(!dp[i]) prime[++cnt] = i; } memset(dp,0x3f,sizeof(dp)); dp[1] = 0; for(int i = 1;i <= 1000000;i++) { dp[i+1] = min(dp[i]+1,dp[i+1]); for(int j = 1;j <= cnt;j++) { long long k = (long long)i*prime[j]; if(k > 1000000) break; dp[k] = min(dp[i]+1,dp[k]); } } while(cin >> n) cout << dp[n] << endl; return 0; }
1029.首位化log再化回来,末尾快速幂模10。
#include<bits/stdc++.h> using namespace std; int n; long long PowerMod(int a, int b, int c) { long long ans = 1; a = a%c; while(b > 0) { if(b % 2 == 1) ans = (ans*a)%c; b = b/2; a = (a*a)%c; } return ans; } int main() { int T; cin >> T; while(T--) { cin >> n; double x = log10(2)*n; int ans = pow(10,x-(int)x)+1e-6; cout << ans << " " << PowerMod(2,n,10) << endl; } return 0; }
1030.排序后枚举前两个,二分第三个。
#include<bits/stdc++.h> using namespace std; int a[1005],n; int main() { ios::sync_with_stdio(false); while(cin >> n) { int flag = 1; for(int i = 1;i <= n;i++) cin >> a[i]; sort(a+1,a+1+n); for(int i = 1;i <= n-2;i++) { for(int j = i+1;j <= n-1;j++) { int sum = -a[i]-a[j]; int pos = lower_bound(a+j+1,a+n+1,sum)-a; if(pos != n+1 && a[pos] == sum) { flag = 0; cout << a[i] << " " << a[j] << " " << a[pos] << endl; } } } if(flag) cout << "Ren Chou Jiu Gai Duo Du Shu!" << endl; } return 0; }
1031.规律,a[i][j] = (i+j)/gcd(i,j)。
#include<bits/stdc++.h> using namespace std; int n,m; int main() { ios::sync_with_stdio(false); while(cin >> n >> m) { int ans = 0; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) { ans += (i+j)/__gcd(i,j); } } cout << ans << endl; } return 0; }
1032.规律,组合数,lacus模版。
#include<bits/stdc++.h> #define LL long long using namespace std; LL n,m; LL PowerMod(LL a, LL b, LL c) { LL ans = 1; a = a % c; while(b) { if(b&1) ans = (ans*a)%c; b = b>>1; a = (a*a)%c; } return ans; } LL c(LL m,LL n) { if(m < n) return 0; if(m == n) return 1; if(n > m-n) n = m-n; LL mm = 1,nn = 1; for(LL i = 0;i < n;i++) { mm = mm*(m-i)%10007; nn = nn*(n-i)%10007; } return mm*PowerMod(nn,10005,10007)%10007; } LL lucas(LL m,LL n) { LL ans = 1; while(m && n && ans) { ans = ans%10007*c(m%10007,n%10007)%10007; n /= 10007; m /= 10007; } return ans; } int main() { ios::sync_with_stdio(false); while(cin >> n >> m) cout << lucas(m,n) << endl; return 0; }
1033.直接比较。
#include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { int a; cin >> a; if(a <= 540) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
1034.直接一次次减。
#include<bits/stdc++.h> using namespace std; int n; int main() { int n,T; cin >> T; while(T--) { cin >> n; int sub = 1,ans = 1; while(n > sub) { n -= sub; ans++; sub *= 2; } if(n <= sub/2) cout << ans-1 << " " << sub/2 << endl; else cout << ans << " " << n << endl; } return 0; }
1035.暴力判断。
#include<bits/stdc++.h> using namespace std; int a[10][10]; int x[9] = {1,1,1,4,4,4,7,7,7},y[9] = {1,4,7,1,4,7,1,4,7}; set<int> s; int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { int flag = 1; for(int i = 1;i <= 9;i++) { s.clear(); for(int j = 1;j <= 9;j++) { cin >> a[i][j]; if(a[i][j] == 0 || a[i][j] > 9) flag = 0; s.insert(a[i][j]); } if(s.size() != 9) flag = 0; } for(int j = 1;j <= 9;j++) { s.clear(); for(int i = 1;i <= 9;i++) s.insert(a[i][j]); if(s.size() != 9) flag = 0; } for(int k = 0;k < 9;k++) { s.clear(); for(int i = x[k],cnt1 = 0;cnt1 < 3;i++,cnt1++) { for(int j = y[k],cnt2 = 0;cnt2 < 3;j++,cnt2++) s.insert(a[i][j]); } if(s.size() != 9) flag = 0; } there: if(flag) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
1036.容量为sum/2的01背包。
#include<bits/stdc++.h> using namespace std; int n,w[205],dp[200005]; int main() { int T; cin >> T; while(T--) { memset(dp,0,sizeof(dp)); int sum = 0; cin >> n; for(int i = 1;i <= n;i++) { cin >> w[i]; sum += w[i]; } int endd = sum/2; for(int i = 1;i <= n;i++) { for(int j = endd;j >= w[i];j--) { dp[j] = max(dp[j],dp[j-w[i]]+w[i]); } } cout << sum-dp[endd]*2 << endl; } return 0; }
1037.构造逆向逆数b,然后找a的后缀和b的前缀相同的部分。
#include<bits/stdc++.h> using namespace std; int a[55],b[55]; string s; int main() { int T; cin >> T; while(T--) { cin >> s; int n = s.length(); for(int i = 1;i <= n;i++) { a[i] = s[i-1]-‘0‘; b[n-i+1] = a[i]^1; } int pos; for(pos = 1;pos <= n;pos++) { int flag = 1; for(int i = pos,j = 1;i <= n;i++,j++) { if(a[i] != b[j]) { flag = 0; break; } } if(flag) break; } for(int i = 1;i < pos;i++) cout << a[i]; for(int i = 1;i <= n;i++) cout << b[i]; cout << endl; } return 0; }
1038.状压dp。
#include<bits/stdc++.h> using namespace std; int a[15][15],sta[100],dp[15][100],sum[15][100],n,m; int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { memset(dp,0,sizeof(dp)); memset(sum,0,sizeof(sum)); cin >> n >> m; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) cin >> a[i][j]; } int cnt = 0,endd = 1<<(m-1); for(int i = 0;i < endd;i++) { if(i & (i<<1)) continue; sta[++cnt] = i; } for(int i = 1;i < n;i++) { for(int j = 1;j <= cnt;j++) { for(int k = 1;k <= m-1;k++) { if(1<<(k-1) & sta[j]) { if(a[i][k] && a[i][k+1] && a[i+1][k] && a[i+1][k+1]) sum[i][j]++; } } } } for(int i = 1;i <= cnt;i++) dp[1][i] = sum[1][i]; for(int i = 1;i < n;i++) { for(int j = 1;j <= cnt;j++) { for(int k = 1;k <= cnt;k++) { dp[i+2][k] = max(dp[i+2][k],dp[i][j]+sum[i+2][k]); if(sta[j] & sta[k]) continue; if(sta[j] & (sta[k]<<1)) continue; if(sta[j] & (sta[k]>>1)) continue; dp[i+1][k] = max(dp[i+1][k],dp[i][j]+sum[i+1][k]); } } } int ans = 0; for(int i = 1;i <= cnt;i++) ans = max(ans,dp[n-1][i]); cout << ans << endl; } return 0; }
1039.大模拟。
#include<bits/stdc++.h> using namespace std; struct yy { int num,y; friend bool operator<(yy A,yy B) { return A.y > B.y; } }y; struct zz { int num,z; friend bool operator<(zz A,zz B) { return A.z > B.z; } }z; struct xx { int x,last,next; }a[1005]; int eat[1005],die[1005],n,m,live,turn; priority_queue<yy> qy; priority_queue<zz> qz; void fun(int i) { a[i].x--; if(a[i].x == 0) { die[i] = 1; eat[i] = 0; live--; a[a[i].last].next = a[i].next; a[a[i].next].last = a[i].last; } } void eatt(int num) { if(!eat[num]) fun(num); for(int i = 1;i <= n;i++) { if(eat[i]) fun(i); } } int main() { int T; cin >> T; while(T--) { while(!qy.empty()) qy.pop(); while(!qz.empty()) qz.pop(); memset(die,0,sizeof(die)); memset(eat,0,sizeof(eat)); cin >> n >> m; for(int i = 1;i <= n;i++) { a[i].last = i-1; a[i].next = i+1; cin >> a[i].x; } a[1].last = n; a[n].next = 1; for(int i = 1;i <= n;i++) { y.num = i; cin >> y.y; qy.push(y); } for(int i = 1;i <= n;i++) { z.num = i; cin >> z.z; qz.push(z); } turn = 1,live = n; while(m--) { int card,now; cin >> card; if(n == 1) break; switch(card) { case 1: now = qy.top().num; while(die[now]) { qy.pop(); now = qy.top().num; } eatt(now); break; case 2: now = qz.top().num; while(die[now]) { qz.pop(); now = qz.top().num; } eatt(now); break; case 3: eat[turn] = 1; break; case 4: eatt(a[turn].last); break; case 5: eatt(a[turn].next); break; case 6: eatt(turn); break; } if(live == 0) break; turn = a[turn].next; while(die[turn]) turn = a[turn].next; if(live == 1) break; } for(int i = 1;i <= m;i++) { int card; cin >> card; } switch(live) { case 0: cout << 0 << endl; break; case 1: cout << turn << endl; break; default: cout << -1 << endl; break; } } return 0; }
1040.解方程组判断。
#include<bits/stdc++.h> using namespace std; int a,b,c; int main() { int T; cin >> T; while(T--) { cin >> a >> b >> c; int x = (a+b-c)/2,y = (a+c-b)/2,z = (b+c-a)/2; if((x+y+z)*2==(a+b+c)&&1<=x&&x<=100000&&1<=y&&y<=100000&&1<=z&&z<=100000) cout << x << " " << y << " " << z << endl; else cout << "Impossible" << endl; } return 0; }
1041.奇偶性规律。
#include<bits/stdc++.h> using namespace std; int n,m; int main() { int T; cin >> T; while(T--) { cin >> n >> m; if(n%2 || m%2) cout << 1 << endl; else cout << 2 << endl; } return 0; }
1042.每次操作最小的两个数。
#include<bits/stdc++.h> using namespace std; priority_queue<double,vector<double>,greater<double> > q; int n; int main() { int T; cin >> T; while(T--) { cin >> n; for(int i = 1;i <= n;i++) { double temp; cin >> temp; q.push(temp); } for(int i = 1;i < n;i++) { double a = q.top(); q.pop(); double b = q.top(); q.pop(); q.push((a+b)/2); } cout << fixed << setprecision(2) << q.top()+0.00001 << endl; q.pop(); } return 0; }
1043.概率dp,等比求和。
#include<bits/stdc++.h> using namespace std; double p[25],dp[25][25]; int n,k; int main() { int T; cin >> T; while(T--) { cin >> n >> k; for(int i = 1;i <= n;i++) cin >> p[i]; dp[0][0] = 1; for(int i = 1;i <= n;i++) { dp[i][0] = dp[i-1][0]*(1-p[i]); for(int j = 1;j <= n;j++) dp[i][j] = dp[i-1][j-1]*p[i]+dp[i-1][j]*(1-p[i]); } double x = 0; for(int i = k;i <= n;i++) x += dp[n][i]; cout << x/(1-x) << endl; } return 0; }
1044.枚举所有情况就可以了,注意每种牌最多只有4张。
#include<bits/stdc++.h> using namespace std; int dp[15][15][15] = {0},a[3]; void init() { for(int i = 9;i >= 1;i--) dp[i][i][i] = dp[i+1][i+1][i+1]+1; dp[7][8][9] = 9; for(int i = 6;i >= 1;i--) dp[i][i+1][i+2] = dp[i+1][i+2][i+3]+1; int x = 1,y = 2,z = 3; dp[1][2][3] -= 1; for(int i = 9;i >= 1;i--) { int j; for(j = 9;j > i;j--) { dp[i][i][j] = dp[x][y][z]+1; x = i; y = i; z = j; } for(j--;j >= 1;j--) { dp[j][i][i] = dp[x][y][z]+1; x = j; y = i; z = i; } } dp[1][2][3] += 1; dp[1][1][2] += 1; for(int k = 9;k >= 1;k--) { for(int j = k-1;j >= 1;j--) { for(int i = j-1;i >= 1;i--) { if(dp[i][j][k]) continue; dp[i][j][k] = dp[x][y][z]+1; x = i; y = j; z = k; } } } dp[1][1][2] -= 1; dp[2][3][5] += 9; } int main() { ios::sync_with_stdio(false); init(); int T; cin >> T; while(T--) { cin >> a[0] >> a[1] >> a[2]; sort(a,a+3); cout << dp[a[0]][a[1]][a[2]] << endl; } return 0; }
1045.博弈dfs,注意dfs过程中还原原图。
#include<bits/stdc++.h> using namespace std; string a[10]; int dir[4][2] = {-1,0,0,-1,1,0,0,1}; bool dfs(int x,int y) { a[x][y] = ‘1‘; for(int i = 0;i < 4;i++) { int xx = x+dir[i][0],yy = y+dir[i][1]; if(xx > 5 || xx < 1 || yy > 5 || yy < 1 || a[xx][yy] == ‘1‘) continue; if(dfs(xx,yy)) { a[x][y] = ‘0‘; return 0; } } a[x][y] = ‘0‘; return 1; } int main() { int T; cin >> T; while(T--) { for(int i = 1;i <= 5;i++) { cin >> a[i]; a[i] = " "+a[i]; } int flag = 0; for(int i = 1;i <= 5;i++) { for(int j = 1;j <= 5;j++) { if(a[i][j] == ‘0‘ && dfs(i,j)) { flag = 1; break; } } if(flag) break; } if(flag) cout << "win" << endl; else cout << "lose" << endl; } return 0; }
1046.string类型的高精度。
#include<bits/stdc++.h> using namespace std; int compare(string str1,string str2) { if(str1.length() > str2.length()) return 1; else if(str1.length() < str2.length()) return -1; else return str1.compare(str2); } string add(string str1,string str2) { string str; int len1 = str1.length(),len2 = str2.length(); if(len1 < len2) { for(int i = 1;i <= len2-len1;i++) str1 = "0"+str1; } else { for(int i = 1;i <= len1-len2;i++) str2 = "0"+str2; } int cf = 0,temp; for(int i = str1.length()-1;i >= 0;i--) { temp = str1[i]-‘0‘+str2[i]-‘0‘+cf; cf = temp/10; temp %= 10; str = char(temp+‘0‘)+str; } if(cf != 0) str = char(cf+‘0‘)+str; return str; } string sub(string str1,string str2) { string str; int flag = 0; if(compare(str1,str2) < 0) { flag = 1; swap(str1,str2); } int tmp = str1.length()-str2.length(),cf = 0; for(int i = str2.length()-1;i >= 0;i--) { if(str1[tmp+i] < str2[i]+cf) { str = char(str1[tmp+i]-str2[i]-cf+‘0‘+10)+str; cf = 1; } else { str = char(str1[tmp+i]-str2[i]-cf+‘0‘)+str; cf = 0; } } for(int i = tmp-1;i >= 0;i--) { if(str1[i]-cf >= ‘0‘) { str = char(str1[i]-cf)+str; cf = 0; } else { str = char(str1[i]-cf+10)+str; cf = 1; } } str.erase(0,str.find_first_not_of(‘0‘)); if(str.empty()) str = "0"; if(flag) str = "-"+str; return str; } string mul(string str1,string str2) { string str; int len1 = str1.length(); int len2 = str2.length(); string tempstr; for(int i = len2-1;i >= 0;i--) { tempstr = ""; int temp = str2[i]-‘0‘,t = 0,cf = 0; if(temp != 0) { for(int j = 1;j <= len2-1-i;j++) tempstr += "0"; for(int j = len1-1;j >= 0;j--) { cf = (temp*(str1[j]-‘0‘)+cf); t = cf%10; cf /= 10; tempstr = char(t+‘0‘)+tempstr; } if(cf != 0) tempstr = char(cf+‘0‘)+tempstr; str=add(str,tempstr); } } str.erase(0,str.find_first_not_of(‘0‘)); if(str.empty()) str = "0"; return str; } void div(string str1,string str2,string "ient,string &residue) { quotient = ""; residue = ""; if(str2 == "0") { quotient = "ERROR"; residue = "ERROR"; return; } if(str1 == "0") { quotient = "0"; residue = "0"; return; } int res = compare(str1,str2); if(res < 0) { quotient = "0"; residue = str1; return; } else { int len1 = str1.length(); int len2 = str2.length(); string tempstr; tempstr.append(str1,0,len2-1); for(int i = len2-1;i < len1;i++) { tempstr = tempstr+str1[i]; tempstr.erase(0,tempstr.find_first_not_of(‘0‘)); if(tempstr.empty()) tempstr = "0"; for(char ch = ‘9‘;ch >= ‘0‘;ch--) { string str,tmp; str = str+ch; tmp = mul(str2,str); if(compare(tmp,tempstr) <= 0) { quotient = quotient+ch; tempstr = sub(tempstr,tmp); break; } } } residue = tempstr; } quotient.erase(0,quotient.find_first_not_of(‘0‘)); if(quotient.empty()) quotient = "0"; } int main() { string str1,str2; string str3,str4; while(cin >> str1 >> str2) { div(str1,str2,str3,str4); cout << add(str1,str2) << ‘ ‘ << sub(str1,str2) << ‘ ‘ << mul(str1,str2) << ‘ ‘ << str3 << ‘ ‘ << str4 << endl; } return 0; }
1048.二分匹配模版。
#include<bits/stdc++.h> using namespace std; int n,m,l; int linker[505]; bool used[505]; vector<int> v[505]; bool dfs(int u) { for(int i = 0;i < v[u].size();i++) { int t = v[u][i]; if(used[t]) continue; used[t] = 1; if(linker[t] == -1 || dfs(linker[t])) { linker[t] = u; return 1; } } return 0; } int MaxMatch() { int ans = 0; memset(linker,-1,sizeof(linker)); for(int i = 0;i < n;i++) { memset(used,0,sizeof(used)); if(dfs(i)) ans++; } return ans; } int main() { ios::sync_with_stdio(false); while(cin >> n >> m) { for(int i = 0;i < n;i++) v[i].clear(); cin >> l; for(int i = 0,a,b;i < l;i++) { cin >> a >> b; v[a].push_back(b); } cout << MaxMatch() << endl; } return 0; }
1053.直接判断。
#include<bits/stdc++.h> using namespace std; int n; int main() { while(cin >> n) { if(n > 0) cout << "yes" << endl; else if(n < 0) cout << "no" << endl; else cout << "light" << endl; } return 0; }
1054.权值累加。
#include<bits/stdc++.h> using namespace std; int n,a[10] = {1,0,0,0,0,0,1,0,2,1}; int main() { ios::sync_with_stdio(false); while(cin >> n) { int ans = 0; while(n) { ans += a[n%10]; n /= 10; } cout << ans << endl; } return 0; }
1055.模拟遍历。
include<bits/stdc++.h> using namespace std; int a[55][55],ans[2505],n,m; int main() { ios::sync_with_stdio(false); while(cin >> n >> m) { int total = n*m; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) cin >> a[i][j]; } int now = 1,x = 1,y = 1; ans[1] = a[1][1]; while(now < total) { if(x < n) ans[++now] = a[++x][y]; else ans[++now] = a[x][++y]; while(x > 1 && y < m) ans[++now] = a[--x][++y]; if(y < m) ans[++now] = a[x][++y]; else ans[++now] = a[++x][y]; while(x < n && y > 1) ans[++now] = a[++x][--y]; } cout << ans[1]; for(int i = 2;i <= total;i++) cout << " " << ans[i]; cout << endl; } return 0; }
1056.dp。
#include<bits/stdc++.h> using namespace std; int dp[1005][1005] = {0},n; int main() { ios::sync_with_stdio(false); dp[0][0] = 1; for(int i = 1;i <= 1000;i++) { dp[i][0] = 1; for(int j = 1;j < i;j++) dp[i][j] = (dp[i-1][j]+dp[i][j-1])%10007; dp[i][i] = dp[i][i-1]; } while(cin >> n) cout << dp[n][n] << endl; return 0; }
1057.lucas模版。
#include<bits/stdc++.h> #define LL long long using namespace std; LL n,m; LL PowerMod(LL a, LL b, LL c) { LL ans = 1; a = a % c; while(b) { if(b&1) ans = (ans*a)%c; b = b>>1; a = (a*a)%c; } return ans; } LL c(LL m,LL n) { if(m < n) return 0; if(m == n) return 1; if(n > m-n) n = m-n; LL mm = 1,nn = 1; for(LL i = 0;i < n;i++) { mm = mm*(m-i)%10007; nn = nn*(n-i)%10007; } return mm*PowerMod(nn,10005,10007)%10007; } LL lucas(LL m,LL n) { LL ans = 1; while(m && n && ans) { ans = ans%10007*c(m%10007,n%10007)%10007; n /= 10007; m /= 10007; } return ans; } int main() { ios::sync_with_stdio(false); while(cin >> n >> m) cout << lucas(m+n-1,m) << endl; return 0; }
1058.把边的贡献算到点上,贡献大于0的点在子图内,小于0的点在子图外。
#include<bits/stdc++.h> #define LL long long using namespace std; LL a[100005]; int n,m,u,v,w; int main() { ios::sync_with_stdio(false); while(cin >> n >> m) { memset(a,0,sizeof(a)); for(int i = 1;i <= m;i++) { cin >> u >> v >> w; a[u] += w; a[v] += w; } LL ans = 0; for(int i = 1;i <= n;i++) ans += a[i] > 0?a[i]:-a[i]; cout << ans/2 << endl; } return 0; }
*1059.大模拟,待补。
1060.手动处理整数。
#include<bits/stdc++.h> using namespace std; int a[25]; long long x; int main() { while(cin >> x) { int cnt = 0; if(x%10 >= 5) x = x/10+1; else x = x/10; if(x == 0) { cout << "0.00" << endl; continue; } while(x) { a[++cnt] = x%10; x /= 10; } if(cnt > 2) { for(;cnt > 2;cnt--) cout << a[cnt]; } else { cout << "0"; } cout << "."; if(cnt == 1) cout << "0"; for(;cnt > 0;cnt--) cout << a[cnt]; cout << endl; } return 0; }
1061.同分计算约分。
#include<bits/stdc++.h> using namespace std; int a1,a2,b1,b2; char c; int main() { while(cin >> a1 >> c >> a2 >> b1 >> c >> b2) { int x = a1*b2+a2*b1,y = a2*b2,g = __gcd(x,y);; cout << x/g << "/" << y/g << endl; } return 0; }
1062.最大连续k段和,注意k小于n的情况。
#include<bits/stdc++.h> using namespace std; int n,t,a[100005]; int main() { while(cin >> n >> t) { for(int i = 1;i <= n;i++) cin >> a[i]; int sum = 0; if(n <= t) { for(int i = 1;i <= n;i++) sum += a[i]; cout << sum << endl; continue; } for(int i = 1;i <= t;i++) sum += a[i]; int ans = sum; for(int l = 1,r = t+1;r <= n;l++,r++) { sum += a[r]; sum -= a[l]; ans = max(ans,sum); } cout << ans << endl; } return 0; }
1063.数学结论。
http://www.dxhx.pku.edu.cn/fileup/PDF/20060324.pdf
#include<bits/stdc++.h> #define LL long long #define MOD 1000000007 using namespace std; LL PowerMod(LL a, LL b, LL c) { LL ans = 1; a = a % c; while(b) { if(b&1) ans = (ans*a)%c; b = b>>1; a = (a*a)%c; } return ans; } int main() { int n; while(cin >> n) { if(n == 1) cout << 1 << endl; else if(n%2) cout << PowerMod(2,n-1,MOD) << endl; else cout << (PowerMod(2,n-1,MOD)+PowerMod(2,n/2-1,MOD))%MOD << endl; } return 0; }
1064.将坐标旋转45度,注意长度的变化,然后处理一下重叠面积和方格数量的转化。
#include<bits/stdc++.h> #define LL long long using namespace std; int main() { LL x1,y1,x2,y2,r,X1,Y1,X2,Y2; while(cin >> x1 >> y1 >> x2 >> y2 >> r) { X1 = y1+x1; Y1 = y1-x1; X2 = y2+x2; Y2 = y2-x2; LL a = max(0LL,2*r+2-abs(X1-X2)),b = max(0LL,2*r+2-abs(Y1-Y2)); LL sub = a*b?(a*b-a-b+2)/2:0; cout << 2*(2*r*r+2*r+1)-sub << endl; } return 0; }
*1065.动态开点线段树。
#include<bits/stdc++.h> using namespace std; int n,q,cnt = 0,a[100005]; struct segtree { int l,r,lazy,len,x,lx,rx; segtree *lson,*rson; }*root,tree[3000005]; segtree *newnode(int l,int r) { segtree *t = &tree[++cnt]; t->l = l; t->r = r; t->lson = NULL; t->rson = NULL; t->lazy = -1; t->len = r-l+1; t->x = t->len; t->lx = t->len; t->rx = t->len; return t; } segtree *newlson(segtree *pos) { int mid = (pos->l+pos->r)/2; return newnode(pos->l,mid); } segtree *newrson(segtree *pos) { int mid = (pos->l+pos->r)/2; return newnode(mid+1,pos->r); } void pushup(segtree *pos) { pos->lx = pos->lson->lx; pos->rx = pos->rson->rx; pos->x = max(pos->lson->x,pos->rson->x); if(pos->lx == pos->lson->len) pos->lx += pos->rson->lx; if(pos->rx == pos->rson->len) pos->rx += pos->lson->rx; pos->x = max(pos->x,pos->lson->rx+pos->rson->lx); } void pushdown(segtree *pos) { if(!pos->lson) pos->lson = newlson(pos); if(!pos->rson) pos->rson = newrson(pos); if(pos->lazy != -1) { pos->lson->lazy = pos->lazy; pos->rson->lazy = pos->lazy; pos->lson->x = pos->lson->lx = pos->lson->rx = pos->lazy*pos->lson->len; pos->rson->x = pos->rson->lx = pos->rson->rx = pos->lazy*pos->rson->len; pos->lazy = -1; } } void update(segtree *pos,int l,int r,int x) { if(r < pos->l || pos->r < l) return; if(l <= pos->l && pos->r <= r) { pos->x = pos->lx = pos->rx = pos->len*x; pos->lazy = x; return; } pushdown(pos); update(pos->lson,l,r,x); update(pos->rson,l,r,x); pushup(pos); } int getl(segtree *pos,int x) { if(pos->x < x) return 2e9;; if(pos->lx >= x) return pos->l; pushdown(pos); int minn = 2e9; if(pos->lson->rx+pos->rson->lx >= x) minn = pos->lson->r-pos->lson->rx+1; minn = min(minn,getl(pos->lson,x)); minn = min(minn,getl(pos->rson,x)); return minn; } int main() { while(~scanf("%d%d",&n,&q)) { set<int> s; map<int,int> mp; cnt = 0; root = newnode(1,n); while(q--) { char ss[20]; int x; scanf("%s",ss); if(ss[0] == ‘m‘) { sscanf(ss,"malloc(%d)",&x); int t = getl(root,x); if(t == 2e9) printf("0\n"); else { printf("%d\n",t); update(root,t,t+x-1,0); s.insert(t); mp[t] = t+x-1; } } else { sscanf(ss,"free(%d)",&x); auto it = s.lower_bound(x); if(it == s.end() || *it != x) continue; update(root,*it,mp[*it],1); s.erase(it); } } } return 0; }
1066.a^b%c = a^(b%phi[c]+phi[c])%c。
#include<bits/stdc++.h> #define LL long long #define MOD 1000000007 using namespace std; LL a,b,n; LL PowerMod(LL a, LL b, LL c) { LL ans = 1; a = a % c; while(b) { if(b&1) ans = (ans*a)%c; b = b>>1; a = (a*a)%c; } return ans; } int main() { int T; cin >> T; while(T--) { cin >> a >> n >> b; int ans = b; for(int i = 2;i <= n;i++) { b = (b*b+MOD-2)%(MOD-1); ans = ans*b%(MOD-1); } cout << PowerMod(a,ans,MOD) << endl; } return 0; }
*1067.待补。
1068.二分答案,判断的时候先排序,然后O(n)。
#include<bits/stdc++.h> #define LL long long using namespace std; LL a[10005],b[10005]; int n,m,k; int f(LL x) { int ans = 0,now = m; for(int i = 1;i <= n;i++) { while(now) { if(a[i]*b[now] > x) now--; else break; } ans += now; } return n*m-ans; } int main() { int T; cin >> T; while(T--) { cin >> n >> m >> k; k--; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 1;i <= m;i++) cin >> b[i]; sort(a+1,a+1+n); sort(b+1,b+1+m); LL l = a[1]*b[1],r = a[n]*b[m]; while(l < r) { LL mid = (l+r)/2; int t = f(mid); if(t <= k) r = mid; else l = mid+1; } cout << l << endl; } return 0; }
1069.建一个虚点连接所有人,再连接人和人之间的关系,求最小生成树。
#include<bits/stdc++.h> using namespace std; struct line { int to,w; line(int a,int b):to(a),w(b){} friend bool operator<(line X,line Y) { return X.w > Y.w; } }; int n,m,k,x,dis[20005],vis[20005]; vector<line> v[20005]; int prim() { memset(vis,0,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); int ans = 0,cnt = 0; priority_queue<line> q; q.push(line(0,0)); int endd = n+m; while(cnt <= endd && !q.empty()) { while(!q.empty() && vis[q.top().to]) q.pop(); ans += q.top().w; int now = q.top().to; vis[now] = 1; cnt++; for(int i = 0;i < v[now].size();i++) { if(vis[v[now][i].to]) continue; if(dis[v[now][i].to] <= v[now][i].w) continue; dis[v[now][i].to] = v[now][i].w; q.push(line(v[now][i].to,v[now][i].w)); } } return ans; } int main() { int T; cin >> T; while(T--) { cin >> m >> n >> k >> x; int endd = n+m; for(int i = 0;i <= endd;i++) v[i].clear(); for(int i = 1;i <= endd;i++) v[0].push_back(line(i,k)); while(x--) { int a,b,c; cin >> a >> b >> c; a += 1; b += m+1; c = k-c; v[a].push_back(line(b,c)); v[b].push_back(line(a,c)); } cout << prim() << endl; } return 0; }
1070.树形dp。
#include<bits/stdc++.h> using namespace std; int dp[105][105],n,m; vector<int> v[105]; void dfs(int pos,int pre) { for(int i = 0;i < v[pos].size();i++) { if(v[pos][i] == pre) continue; int x = v[pos][i]; dfs(x,pos); for(int j = m;j > 1;j--) { for(int k = 1;k <= j;k++) { dp[pos][j] = max(dp[pos][j],dp[x][j-k]+dp[pos][k]); } } } } int main() { int T; cin >> T; while(T--) { cin >> n >> m; memset(dp,0,sizeof(dp)); for(int i = 0;i < n;i++) v[i].clear(); for(int i = 0;i < n;i++) cin >> dp[i][1]; for(int i = 1;i < n;i++) { int a,b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } dfs(0,-1); int ans = 0; for(int i = 0;i < n;i++) ans = max(ans,dp[i][m]); cout << ans << endl; } return 0; }
1071.枚举长宽。
#include<bits/stdc++.h> using namespace std; int n,m; int main() { int T; cin >> T; while(T--) { cin >> n >> m; int ans =0; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) ans += i*j; } cout << ans << endl; } return 0; }
1072.求关键边的数量,即强连通分量-1,tarjan模版。
#include<bits/stdc++.h> using namespace std; int n,m,dfn[10005],low[10005],num,cnt; vector<int> v[10005]; void tarjan(int pos,int pre) { dfn[pos] = low[pos] = ++num; for(int i = 0;i < v[pos].size();i++) { int x = v[pos][i]; if(x == pre) continue; if(dfn[x] == 0) { tarjan(x,pos); low[pos] = min(low[pos],low[x]); if(dfn[pos] < low[x]) cnt++; } else low[pos] = min(low[pos],dfn[x]); } } int main() { int T; cin >> T; while(T--) { num = 0; cnt = 0; memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); cin >> n >> m; for(int i = 0;i < n;i++) v[i].clear(); while(m--) { int x,y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } tarjan(0,-1); cout << cnt << endl; } return 0; }
1073.01背包,先判断奇偶。
#include<bits/stdc++.h> using namespace std; int n,w[1005],dp[5005]; int main() { int T; cin >> T; while(T--) { memset(dp,0,sizeof(dp)); cin >> n; int sum = 0; for(int i = 1;i <= n;i++) { cin >> w[i]; sum += w[i]; } if(sum&1) { cout << "No" << endl; continue; } int endd = sum/2; for(int i = 1;i <= n;i++) { for(int j = endd;j >= w[i];j--) dp[j] = max(dp[j],dp[j-w[i]]+w[i]); } if(dp[endd] == endd) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
*1074.待补。
*1075.待补。
1076.每位的权值加起来。
#include<bits/stdc++.h> #define LL long long using namespace std; LL three[40] = {0},sum[40] = {0}; string s; int change(char x) { switch(x) { case ‘1‘: return 1; case ‘3‘: return 2; case ‘5‘: return 3; } } int main() { three[0] = 1; for(int i = 1;i <= 36;i++) three[i] = three[i-1]*3; for(int i = 1;i <= 36;i++) sum[i] = sum[i-1]+three[i]; while(cin >> s) { LL ans = sum[s.length()-1]; for(int i = 0;i < s.length();i++) ans += (change(s[i])-1)*three[s.length()-i-1]; cout << ans+1 << endl; } return 0; }
1077.循环节仅看分母,先把分母2,5除完后为m,求欧拉数,循环节长度为欧拉数约数中最小数x的使10^x%m == 1的值。
#include<bits/stdc++.h> #define LL long long using namespace std; LL PowerMod(LL a, LL b, LL c) { LL ans = 1; a = a % c; while(b) { if(b&1) ans = (ans*a)%c; b = b>>1; a = (a*a)%c; } return ans; } int euler(int n) { int res = n,a = n; for(int i = 2;i*i <= a;i++) { if(a%i == 0) { res = res/i*(i-1); while(a%i == 0) a /= i; } } if(a > 1) res = res/a*(a-1); return res; } int gcd(int a,int b) { return b?gcd(b,a%b):a; } int main() { int p,q; while(cin >> p >> q) { q /= gcd(p,q); while(q%2 == 0) q /= 2; while(q%5 == 0) q /= 5; if(q == 1) { cout << 0 << endl; continue; } int x = euler(q),now = 1; int xx = sqrt(x),flag = 0; for(int i = 1;i <= xx;i++) { if(x%i) continue; if(PowerMod(10,i,q) == 1) { cout << i << endl; flag = 1; break; } } if(flag) continue; for(int i = xx;i >= 1;i--) { if(x%i) continue; if(PowerMod(10,x/i,q) == 1) { cout << x/i << endl; break; } } } return 0; }
1078.分层图最短路模版。
#include<bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; struct edge { int to,cost; friend bool operator>(edge x,edge y) { return x.cost > y.cost; } }e; vector<edge> v[11005]; int n,m,k,a[1005][1005],dis[11005],vis[11005]; priority_queue< edge,vector<edge>,greater<edge> > q; int dij() { while(!q.empty()) q.pop(); memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[1] = 0; e.to = 1; e.cost = 0; q.push(e); while(!q.empty()) { int now = q.top().to,cost = q.top().cost; if(now == (k+1)*n) return dis[(k+1)*n]; q.pop(); if(vis[now]) continue; vis[now] = 1; for(int i = 0;i < v[now].size();i++) { e = v[now][i]; if(e.cost+dis[now] < dis[e.to]) { dis[e.to] = e.cost+dis[now]; e.cost = dis[e.to]; q.push(e); } } } } int main() { while(cin >> n >> m >> k) { for(int i = 1;i <= 11000;i++) v[i].clear(); memset(a,0x3f,sizeof(a)); for(int i = 1;i <= m;i++) { int u,v,w; cin >> u >> v >> w; a[u][v] = min(a[u][v],w); a[v][u] = min(a[v][u],w); } for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { if(i == j || a[i][j] == INF) continue; for(int l = 0;l <= k;l++) { e.to = l*n+j; e.cost = a[i][j]; v[l*n+i].push_back(e); if(l != k) { e.to = (l+1)*n+j; e.cost = 0; v[l*n+i].push_back(e); } } } } cout << dij() << endl; } return 0; }
1079.枚举两个数。
#include<bits/stdc++.h> using namespace std; int a[100005],n,m,has[15]; int main() { int T; cin >> T; while(T--) { memset(has,0,sizeof(has)); cin >> n >> m; int cnt = 0; for(int i = 1;i <= n;i++) { cin >> a[i]; if(!has[a[i]]) has[a[i]] = 1; } int ans = 0; for(int i = 1;i <= m;i++) { for(int j = 1;j <= m;j++) { if(i == j) continue; if(!has[i] || !has[j]) continue; int sum = 0,flag = 0; for(int k = 1;k <= n;k++) { if(a[k] == i) sum++; else if(a[k] == j) { sum--; flag = 1; } if(sum < 0) { sum = 0; flag = 0; } if(flag) ans = max(ans,sum); else ans = max(ans,sum-1); } } } cout << ans << endl; } return 0; }
1080.规律。
#include<bits/stdc++.h> using namespace std; int main() { int n; while(cin >> n) cout << n*n*4 << endl; return 0; }
1081.判断两个范围是否有交集。
#include<bits/stdc++.h> using namespace std; long long a1,a2,b1,b2,c1,c2,d1,d2; int main() { while(cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2 >> d1 >> d2) { long long x1 = a1*d1,x2 = a2*d2,y1 = b1*c1,y2 = b2*c2; if(x1 > y2 || x2 < y1) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
1082.若场数最多的人比其余的和要大,则ans为其余的和,否则ans为sum/2。
#include<bits/stdc++.h> using namespace std; int n; int main() { int T; cin >> T; while(T--) { long long maxx = 0,sum = 0,temp; cin >> n; while(n--) { cin >> temp; maxx = max(maxx,temp); sum += temp; } cout << min(sum/2,sum-maxx) << endl; } return 0; }
1083.从左到右遍历,记录左括号个数,有右括号时ans++就可以了。
#include<bits/stdc++.h> using namespace std; int main() { int T; string s; cin >> T; while(T--) { cin >> s; int len = s.length(),cnt = 0,ans = 0; for(int i = 0;i < len;i++) { if(s[i] == ‘(‘) cnt++; else if(cnt) { cnt--; ans += 2; } } cout << ans << endl; } return 0; }
1084.树形dp。
#include<bits/stdc++.h> using namespace std; int n,u,v,vis[15],sco[15],ans,k; vector<int> pre[15]; void dfs(int x,int sum) { if(x == 0) { ans = max(ans,sum); return; } for(int i = 1;i <= 10;i++) { if(vis[i]) continue; int flag = 1; for(int j = 0;j < pre[i].size();j++) { if(!vis[pre[i][j]]) { flag = 0; break; } } if(!flag) continue; vis[i] = 1; dfs(x-1,sum+sco[i]); vis[i] = 0; } } int main() { ios::sync_with_stdio(0); while(cin >> n) { memset(vis,0,sizeof(vis)); for(int i = 1;i <= 10;i++) pre[i].clear(); while(n--) { cin >> u >> v; pre[v].push_back(u); } for(int i = 1;i <= 10;i++) cin >> sco[i]; cin >> k; ans = 0; dfs(k/3,0); if(ans >= 60) cout << ans << endl; else cout << "I chose to die" << endl; } return 0; }
*1085.待补。
1086.模拟。
#include<bits/stdc++.h> using namespace std; int n,m,h,op[1005],a[1005],hh[1005]; int main() { int T; cin >> T; while(T--) { memset(a,-1,sizeof(a)); cin >> n; for(int i = 0;i < n;i++) cin >> op[i]; cin >> m; for(int i = 1;i <= m;i++) cin >> hh[i]; int pos; for(int i = 1;i <= m;i++) { cin >> pos; if(pos <= n) a[pos] = hh[i]; } cin >> h; int flag = 1,now = 0; while(now < n) { if(op[now] == 1) h++; else if(op[now] == 0) h--; if(h == 0 || a[now] != -1 && h >= a[now]) { flag = 0; break; } now++; } if(flag) cout << "V8Orz" << endl; else cout << now << endl; } return 0; }
1087.贪心单位体积的价值。
#include<bits/stdc++.h> using namespace std; int n,w,V; struct aa { int v; double a; }a[1005]; bool cmp(aa x,aa y) { return x.a < y.a; } int main() { int T; cin >> T; while(T--) { cin >> n; for(int i = 1;i <= n;i++) { cin >> a[i].v >> w; a[i].a = (double)w/a[i].v; } cin >> V; sort(a+1,a+1+n,cmp); int now = n; double ans = 0; while(V && now >= 1) { if(V > a[now].v) { ans += a[now].a*a[now].v; V -= a[now].v; now--; } else { ans += V*a[now].a; break; } } cout << fixed << setprecision(4) << ans << endl; } return 0; }
1088.sort。
#include<bits/stdc++.h> using namespace std; string s[100005]; int n; bool cmp(string x,string y) { string X = x+y,Y = y+x; return X > Y; } int main() { while(cin >> n) { for(int i = 1;i <= n;i++) cin >> s[i]; sort(s+1,s+1+n,cmp); for(int i = 1;i <= n;i++) cout << s[i]; cout << endl; } return 0; }
1089.时间顺序的bfs,注意多个水珠同时到一点的情况。
#include<bits/stdc++.h> using namespace std; int n,m,nn,tt,a[105][2],sta[105][105],t[105][105],dir[4][2] = {-1,0,0,-1,1,0,0,1}; struct water { int x,y,d,t; water(int _x,int _y,int _d,int _t):x(_x),y(_y),d(_d),t(_t){}; friend bool operator <(water X,water Y) { return X.t > Y.t; } }; void bfs(int x,int y) { priority_queue<water> q; q.push(water(x,y,0,1)); q.push(water(x,y,1,1)); q.push(water(x,y,2,1)); q.push(water(x,y,3,1)); while(!q.empty()) { water temp = q.top(); q.pop(); if(temp.t > tt) return; int xx = temp.x+dir[temp.d][0],yy = temp.y+dir[temp.d][1]; if(xx < 1 || xx > n || yy < 1 || yy > m) continue; if(t[xx][yy] == temp.t) continue; if(sta[xx][yy] == 0) q.push(water(xx,yy,temp.d,temp.t+1)); else if(sta[xx][yy] == 4) { sta[xx][yy] = 0; t[xx][yy] = temp.t; q.push(water(xx,yy,0,temp.t+1)); q.push(water(xx,yy,1,temp.t+1)); q.push(water(xx,yy,2,temp.t+1)); q.push(water(xx,yy,3,temp.t+1)); } else sta[xx][yy]++; } } int main() { while(cin >> n >> m >> nn >> tt) { memset(sta,0,sizeof(sta)); memset(t,0,sizeof(t)); for(int i = 1;i <= nn;i++) { int x; cin >> a[i][0] >> a[i][1] >> x; sta[a[i][0]][a[i][1]] = x; } int x,y; cin >> x >> y; bfs(x,y); for(int i = 1;i <= nn;i++) { if(t[a[i][0]][a[i][1]] == 0) cout << 1 << " " << sta[a[i][0]][a[i][1]] << endl; else cout << 0 << " " << t[a[i][0]][a[i][1]] << endl; } } return 0; }
1090.树形dp。
#include<bits/stdc++.h> using namespace std; struct line { int next,w; }l; vector<line> v[10005]; int n; int dfs(int x) { int sum = 0; for(int i = 0;i < v[x].size();i++) sum = max(sum,dfs(v[x][i].next)+v[x][i].w); return sum; } int main() { int T; cin >> T; while(T--) { cin >> n; for(int i = 1;i <= 10000;i++) v[i].clear(); for(int i = 1;i <= n;i++) { int u; cin >> u >> l.next >> l.w; v[u].push_back(l); } cout << dfs(1) << endl; } return 0; }
1091.按结束时间排序,贪心。
#include<bits/stdc++.h> using namespace std; struct vedio { int l,r; }a[1005]; int n; bool cmp(vedio x,vedio y) { return x.r < y.r; } int main() { while(cin >> n && n) { for(int i = 1;i <= n;i++) cin >> a[i].l >> a[i].r; sort(a+1,a+1+n,cmp); int ans = 1,time = a[1].r,now = 2; for(;now <= n;now++) { if(time > a[now].l) continue; time = a[now].r; ans++; } cout << ans << endl; } return 0; }
*1092.待补。
1093.确定区间,二分答案。
#include<bits/stdc++.h> using namespace std; const double eps=0.0001; double a,b,c,d; double ans[5]; int sum; inline double calc(double x) { return a*x*x*x+b*x*x+c*x+d; } void find(double l,double r) { while(1) { double mid = (l+r)/2; if(abs(calc(mid)) < eps) { ans[++sum] = mid; return; } if(calc(mid)*calc(l) < 0) r = mid; else l = mid; } } int main() { while(cin >> a >> b >> c >> d) { sum = 0; for(int i=-101;i<=101;i++) { double tmp1 = calc(i),tmp2 = calc(i+1); if(abs(tmp1) < eps) ans[++sum] = i; else if(abs(tmp2) < eps) { ans[++sum] = i+1; i++; } else if(tmp1*tmp2 < 0) find(i,i+1); if(sum == 3) break; } sort(ans+1,ans+4); cout << fixed << setprecision(2) << ans[1] << " " << ans[2] << " " << ans[3] << endl; } return 0; }
1094.递归输出。
#include<bits/stdc++.h> using namespace std; int n; void printff(int x) { if(x == 1) { cout <<"2(0)"; return; } if(x == 2) { cout << "2"; return; } int sum = 1,b = -1; while(sum <= x) { sum *= 2; b++; } if(sum/2 < x) { if(sum/2 == 2) { cout << "2+"; printff(x-sum/2); } else { cout << "2("; printff(b); cout << ")+"; printff(x-sum/2); } } else { cout << "2("; printff(b); cout << ")"; } } int main() { ios::sync_with_stdio(0); while(cin >> n) { printff(n); cout << endl; } return 0; }
1095.暴力dfs。
#include<bits/stdc++.h> using namespace std; int vis[15],a[15],n,k,ans; void dfs(int num,int v) { if(num == n) { if(abs(v-a[1]) <= k) ans++; return; } for(int i = 2;i <= n;i++) { if(vis[i]) continue; if(abs(v-a[i]) > k) continue; vis[i] = 1; dfs(num+1,a[i]); vis[i] = 0; } } int main() { ios::sync_with_stdio(0); while(cin >> n >> k) { memset(vis,0,sizeof(vis)); ans = 0; for(int i = 1;i <= n;i++) cin >> a[i]; dfs(1,a[1]); cout << ans << endl; } return 0; }
1096.dp[i][j]表示i拆分成最大为j的数。
#include<bits/stdc++.h> using namespace std; int dp[85][85],n; int main() { ios::sync_with_stdio(0); for(int i = 1;i <= 80;i++) { dp[i][1] = 1; dp[1][i] = 1; } for(int i = 2;i <= 80;i++) { for(int j = 2;j <= 80;j++) { if(i == j) dp[i][j] = dp[i][j-1]+1; else if(i < j) dp[i][j] = dp[i][j-1]; else dp[i][j] = dp[i-j][j]+dp[i][j-1]; } } while(cin >> n) cout << dp[n][n]-1 << endl; return 0; }
1097.dp[i] = dp[i-1]+dp[i-2]+dp[i-3]。
#include<bits/stdc++.h> using namespace std; int n; long long dp[75]; int main() { dp[1] = 1; dp[2] = 2; dp[3] = 4; for(int i = 4;i <= 70;i++) dp[i] = dp[i-1]+dp[i-2]+dp[i-3]; while(cin >> n) cout << dp[n] << endl; return 0; }
1098.求欧拉函数模版。
#include<bits/stdc++.h> using namespace std; int n; int euler(int n) { int res = n,a = n; for(int i = 2;i*i <= a;i++) { if(a%i == 0) { res = res/i*(i-1); while(a%i == 0) a /= i; } } if(a > 1) res = res/a*(a-1); return res; } int main() { ios::sync_with_stdio(0); while(cin >> n) cout << euler(n) << endl; return 0; }
1099.线性求累加和,sum > k时增加ans。
#include<bits/stdc++.h> using namespace std; int a[100005],n,k; int main() { ios::sync_with_stdio(0); int T; cin >> T; while(T--) { cin >> n >> k; for(int i = 1;i <= n;i++) cin >> a[i]; int l = 0,now = 0; long long ans = 0; for(int i = 1;i <= n;i++) { now += a[i]; while(now > k) now -= a[++l]; ans += i-l; } cout << ans << endl; } return 0; }
1100.单调栈模版。
#include<bits/stdc++.h> using namespace std; int n; stack<int> s; long long a[100005],b[100005]; int main() { ios::sync_with_stdio(0); while(cin >> n) { while(!s.empty()) s.pop(); long long ans = 0; b[1] = 0; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 2;i <= n;i++) { cin >> b[i]; b[i] += b[i-1]; } a[n+1] = 0; for(int i = 1;i <= n+1;i++) { while(!s.empty() && a[s.top()] > a[i]) { int temp = s.top(); s.pop(); long long len = s.empty()?b[i-1]:b[i-1]-b[s.top()+1]; ans = max(ans,len*a[temp]); } s.push(i); } cout << ans << endl; } return 0; }
1101.模拟进制加法。
#include<bits/stdc++.h> using namespace std; int k,n; int main() { ios::sync_with_stdio(0); while(cin >> k >> n) { long long ans = 0,temp = 1; while(n) { ans += temp*(n%2); n /= 2; temp *= k; } cout << ans << endl; } return 0; }
*1102.待补。
*1103.待补。
1104.dfs两侧点的个数, 相乘。
#include<bits/stdc++.h> using namespace std; int n,m,from[1005],to[1005]; vector<int> v[1005]; int dfs(int s,int t) { int ans = 1; for(int i = 0;i < v[s].size();i++) { if(v[s][i] == t) continue; ans += dfs(v[s][i],s); } return ans; } int main() { ios::sync_with_stdio(0); int T; cin >> T; while(T--) { cin >> n >> m; for(int i = 1;i <= n;i++) v[i].clear(); for(int i = 1;i < n;i++) { int a,b; cin >> a >> b; from[i] = a; to[i] = b; v[a].push_back(b); v[b].push_back(a); } for(int i = 1;i <= m;i++) { int x; cin >> x; int t = dfs(to[x],from[x]); cout << t*(n-t) << endl; } } return 0; }
1105.约数个数。
#include<bits/stdc++.h> using namespace std; int n; int main() { int T; cin >> T; while(T--) { cin >> n; int ans = 0; for(int i = 1;i <= sqrt(n+0.1);i++) { if(n%i == 0) { if(i == n/i) ans++; else ans += 2; } } cout << ans << endl; } return 0; }
*1106.待补。
1107.最长上升子序列。
#include<bits/stdc++.h> using namespace std; struct point { int x,y; }a[100005]; int b[100005],n; bool cmp(point X,point Y) { if(X.x == Y.x) return X.y > Y.y; return X.x < Y.x; } int LIS() { int len = 1,j; b[1] = a[1].y; for(int i = 2;i <= n;i++) { if(a[i].y > b[len]) j = ++len; else j = lower_bound(b+1,b+1+len,a[i].y)-b; b[j] = a[i].y; } return len; } int main() { ios::sync_with_stdio(0); while(cin >> n) { for(int i = 1;i <= n;i++) cin >> a[i].x; for(int i = 1;i <= n;i++) cin >> a[i].y; sort(a+1,a+1+n,cmp); cout << LIS() << endl; } return 0; }
1108.打表,前缀和。
#include<bits/stdc++.h> using namespace std; int x,y,a[1000005] = {0}; int main() { a[0] = a[1] = 0; for(int i = 2;i <= 1000000;i++) { if(i%2) a[i] = a[i-1]+1; else a[i] = a[i/2]+1; } for(int i = 2;i <= 1000000;i++) a[i] += a[i-1]; while(cin >> x >> y) cout << a[y]-a[x-1] << endl; return 0; }
1109.dp[i] = dp[i-1]+dp[i-2]+dp[i-3]。
#include<bits/stdc++.h> using namespace std; int n,a[1000005]; int main() { ios::sync_with_stdio(0); a[1] = 1; a[2] = 2; a[3] = 4; for(int i = 4;i <= 1000000;i++) a[i] = (a[i-1]+a[i-2]+a[i-3])%10007; while(cin >> n) cout << a[n] << endl; return 0; }
1111.f(n)=f(n-1)+f(n-2)+n-2推出f(n)=3*f(n-1)-2*f(n-2)-f(n-3)+f(n-4),矩阵快速幂。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; int n,anss[6] = {0,0,0,1,3,7}; struct matrix { long long m[4][4]; }; matrix ans = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1,}; matrix base = { 3,1,0,0, MOD-2,0,1,0, MOD-1,0,0,1, 1,0,0,0,}; matrix mul(matrix a, matrix b) { matrix tmp; for(int i = 0; i < 4;i++) { for(int j = 0; j < 4;j++) { tmp.m[i][j] = 0; for(int k = 0; k < 4;k++) tmp.m[i][j] = (tmp.m[i][j]+a.m[i][k]*b.m[k][j])%MOD; } } return tmp; } long long fast_mod(int n) { matrix x = ans,y = base; while(n) { if(n & 1) { x = mul(x,y); } y = mul(y,y); n >>= 1; } return (anss[5]*x.m[0][0]+anss[4]*x.m[1][0]+anss[3]*x.m[2][0]+anss[2]*x.m[3][0])%MOD; } int main() { ios::sync_with_stdio(0); while(cin >> n) { if(n <= 5) cout << anss[n] << endl; else cout << fast_mod(n-5) << endl; } }
1112.dp[i][j]表示i个人j派的方案数。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; long long dp[1005][1005] = {0},ans[1005] = {0}; int n; int main() { ios::sync_with_stdio(0); dp[1][1] = 1; for(int i = 2;i <= 1000;i++) { for(int j = 1;j <= i;j++) dp[i][j] = (dp[i-1][j-1]+dp[i-1][j]*j)%MOD; } for(int i = 1;i <= 1000;i++) { for(int j = 1;j <= i;j++) ans[i] = (ans[i]+dp[i][j])%MOD; } while(cin >> n) cout << ans[n] << endl; return 0; }
*1113.待补。
1114.先离线,再树状数组。
#include<bits/stdc++.h> using namespace std; struct arr { int o,x,pos,l,r; }a[300005]; int n,m,tree[300005] = {0},ans1[100005],ans2[100005]; bool cmp(arr X,arr Y) { if(X.x == Y.x) return X.o < Y.o; return X.x < Y.x; } inline int lowbit(int x) { return x & (-x); } void update(int pos,int x) { while(pos <= n) { tree[pos] += x; pos += lowbit(pos); } } int getsum(int pos) { int sum = 0; while(pos > 0) { sum += tree[pos]; pos -= lowbit(pos); } return sum; } int main() { while(~scanf("%d%d",&n,&m)) { memset(tree,0,sizeof(tree)); for(int i = 1;i <= n;i++) { scanf("%d",&a[i].x); a[i].pos = i; a[i].o = 1; } int cnt = n; for(int i = 1;i <= m;i++) { scanf("%d%d%d%d",&a[cnt+1].l,&a[cnt+1].r,&a[cnt+1].x,&a[cnt+2].x); a[cnt+1].o = 0; a[cnt+2].o = 2; a[cnt+1].pos = a[cnt+2].pos = i; a[cnt+2].l = a[cnt+1].l; a[cnt+2].r = a[cnt+1].r; cnt += 2; } sort(a+1,a+1+cnt,cmp); for(int i = 1;i <= cnt;i++) { if(a[i].o == 1) update(a[i].pos,1); else if(a[i].o == 0) ans1[a[i].pos] = getsum(a[i].r)-getsum(a[i].l-1); else ans2[a[i].pos] = getsum(a[i].r)-getsum(a[i].l-1); } for(int i = 1;i <= m;i++) printf("%d\n",ans2[i]-ans1[i]); } return 0; }
1115.x^n+y^n = (x^(n-1)+y^(n-1))*(x+y)-(x^(n-2)+y^(n-2))*x*y。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; int a,b,n; long long ans[10005]; int main() { ios::sync_with_stdio(0); ans[0] = 2; while(cin >> a >> b >> n) { ans[1] = a; for(int i = 2;i <= n;i++) ans[i] = ((ans[i-1]*a-ans[i-2]*b%MOD)+MOD)%MOD; cout << ans[n] << endl; } return 0; }
*1116.待补。
1117.状压dp。
#include<bits/stdc++.h> #define LL long long #define MOD 1000000007 using namespace std; int n,m; LL dp[105][1<<11]; vector<int> v[105]; LL dfs(int left,int sta) { if(dp[left][sta] != -1) return dp[left][sta]; if(left == 0) return !sta; dp[left][sta] = 0; for(int i = 0;i < v[left].size();i++) { dp[left][sta] = (dp[left][sta]+dfs(left-1,sta^v[left][i]))%MOD; } return dp[left][sta]; } int main() { while(~scanf("%d%d",&n,&m)) { memset(dp,-1,sizeof(dp)); for(int i = 1;i <= m;i++) v[i].clear(); for(int i = 1;i <= m;i++) { int t,endd = 1<<n; scanf("%d",&t); for(int j = 0;j < endd;j++) { int sum = 0; for(int k = 0;k < n;k++) { if(j&(1<<k)) sum++; } if(sum == t) v[i].push_back(j); } } int a = 0; for(int i = 0;i < n;i++) { int t; scanf("%d",&t); a |= (1<<i)*t; } printf("%lld\n",dfs(m,a)); } return 0; }
1118.延长边,判断是否正三角形。
#include<bits/stdc++.h> #define PI (atan(1)*4) using namespace std; int a[6],b[6],vis[6] = {0}; double ans; void dfs(int cnt) { if(cnt == 6) { if(b[0]+b[1]+b[3] == b[0]+b[2]+b[4] && b[0]+b[2]+b[4] == b[1]+b[2]+b[5]) { ans = sqrt(3)/4*((b[0]+b[2]+b[4])*(b[0]+b[2]+b[4])-b[0]*b[0]-b[1]*b[1]-b[2]*b[2]); } } for(int i = 0;i < 6;i++) { if(vis[i]) continue; b[cnt] = a[i]; vis[i] = 1; dfs(cnt+1); vis[i] = 0; } } int main() { ios::sync_with_stdio(false); while(cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5]) { ans = 0; dfs(0); if(ans > 1e-5) cout << fixed << setprecision(2) << ans << endl; else cout << 0 << endl; } return 0; }
1119.递增枚举最小公倍数。
#include<bits/stdc++.h> using namespace std; int a[1005],n,k; bool ok(int x) { int cnt = 0; for(int i = 1;i <= n;i++) { if(x%a[i] == 0) cnt++; } if(cnt >= k) return 1; return 0; } int main() { ios::sync_with_stdio(0); while(cin >> n >> k) { for(int i = 1;i <= n;i++) cin >> a[i]; int ans = 1; for(;ans <=1000;ans++) { if(ok(ans)) break; } if(ans <= 1000) cout << ans << endl; else cout << "Orz" << endl; } return 0; }
1120.贪心,时间后往前,大的先考虑。
#include<bits/stdc++.h> using namespace std; struct task { int t,v; }a[1005]; int n,vis[1005] = {0}; bool cmp(task x,task y) { return x.v > y.v; } int main() { ios::sync_with_stdio(0); cin >> n; int t = 0,sum = 0,ans = 0; for(int i = 1;i <= n;i++) { cin >> a[i].t; t = max(a[i].t,t); } for(int i = 1;i <= n;i++) { cin >> a[i].v; sum += a[i].v; } sort(a+1,a+1+n,cmp); for(int i = t;i >= 1;i--) { for(int j = 1;j <= n;j++) { if(!vis[j] && i <= a[j].t) { ans += a[j].v; vis[j] = 1; break; } } } cout << sum-ans << endl; return 0; }
1121.60个刚好超1e18,60之前直接递归算,60之后的用60的来算。
#include<bits/stdc++.h> using namespace std; long long a[65],n,k; int f(long long x,long long y) { if(x == 1) return 1; if(y <= a[x-1]) return f(x-1,y)^1; if(y == a[x-1]+1) return 1; return f(x-1,a[x]-y+1); } int main() { ios::sync_with_stdio(0); a[1] = 1; for(int i = 2;i <= 64;i++) a[i] = a[i-1]*2+1; while(cin >> n >> k) { int ans; if(n <= 60) ans = f(n,k); else { ans = f(60,k); if(n&1) ans ^= 1; } if(ans) cout << "srO" << endl; else cout << "Orz\n" << endl; } return 0; }
1123.二分查找。
#include<bits/stdc++.h> using namespace std; struct orz { int id,x; friend bool operator<(struct orz X,struct orz Y) { if(X.x == Y.x) return X.id < Y.id; return X.x < Y.x; } }a[100005],t; int n,m; int main() { ios::sync_with_stdio(0); t.id = 0; while(cin >> n >> m) { for(int i = 1;i <= n;i++) { cin >> a[i].x; a[i].id = i; } sort(a+1,a+1+n); int flag = 1; while(m--) { cin >> t.x; int ans = lower_bound(a+1,a+1+n,t)-a; if(flag) { if(ans > n) cout << -1; else cout << a[ans].id; flag = 0; } else { if(ans > n) cout << " -1"; else cout << " " << a[ans].id; } } cout << endl; } return 0; }
1124.找第一个可以替代的数,用后面最大且最后的数替代。
#include<bits/stdc++.h> using namespace std; char a[1005]; int main() { while(cin >> a) { int len = strlen(a),first = 0,last = 0,now = 0; while(now < len) { first = last = now; char maxx = a[now]; for(int i = len-1;i > now;i--) { if(a[i] > maxx) { maxx = a[i]; last = i; } } if(first != last) break; now++; } swap(a[first],a[last]); cout << a << endl; } return 0; }
1125.分成三个三角形,由面积判断。
#include<bits/stdc++.h> using namespace std; double f(double x1,double y1,double x2,double y2,double x3,double y3) { double a,b,c,p; a = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); b = sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); c = sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); p = (a+b+c)/2; return sqrt(p*(p-a)*(p-b)*(p-c)); } int main() { double x1,x2,x3,y1,y2,y3,x,y; while(cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x >> y) { if(abs(f(x1,y1,x2,y2,x3,y3)-f(x,y,x2,y2,x3,y3)-f(x1,y1,x,y,x3,y3)-f(x1,y1,x2,y2,x,y)) < 1e-6) cout << "Orz" << endl; else cout << "stO" << endl; } return 0; }
*1128.待补。
1129.记录交换后的映射。
#include<bits/stdc++.h> using namespace std; int a[1005][1005],x[1005],y[1005],n,m,k; int main() { while(~scanf("%d%d%d",&n,&m,&k)) { for(int i = 1;i <= n;i++) x[i] = i; for(int i = 1;i <= m;i++) y[i] = i; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) scanf("%d",&a[i][j]); } int o,xx,yy; while(k--) { scanf("%d%d%d",&o,&xx,&yy); if(o == 0) swap(x[xx],x[yy]); else swap(y[xx],y[yy]); } for(int i = 1;i <= n;i++) { printf("%d",a[x[i]][y[1]]); for(int j = 2;j <= m;j++) printf(" %d",a[x[i]][y[j]]); printf("\n"); } } return 0; }
1130.C(n,0)+C(n,1)+......+C(n,n) =2^n。
#include<bits/stdc++.h> #define LL long long #define MOD 542 using namespace std; int p,d,n; LL qmod(LL a, LL b, LL c) { LL ans = 1; a = a%c; while(b) { if(b&1) ans = ans*a%c; b >>= 1; a = a*a%c; } return ans; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&p,&d,&n); LL ans = qmod(2,n,MOD)*p%MOD; ans = (qmod(2,n-1,MOD)*d*n+ans)%MOD; printf("%lld\n",ans); } return 0; }
*1131.待补。
*1132.待补。
1138.a+b。
#include<bits/stdc++.h> using namespace std; char s; int a,b,c,d; int main() { ios::sync_with_stdio(0); while(cin >> a >> s >> b >> s >> c >> s >> d >> s) cout << a+c << "+" << b+d << "i" << endl; return 0; }
1139.打表二分。
#include<bits/stdc++.h> using namespace std; long long n,f[100],sum[100]; int main() { ios::sync_with_stdio(0); f[0] = 0; f[1] = 1; sum[1] = 1; for(int i = 2;i <= 100;i++) { f[i] = f[i-1]+f[i-2]; sum[i] = sum[i-1]+f[i]; } while(cin >> n) { if(n == 1 || n == 2 || n == 3) cout << "1 1" << endl; else { int x = lower_bound(sum,sum+100,n)-sum; long long y = n-sum[x-1]; if(y > f[x-1]) cout << x << " " << y << endl; else cout << x-1 << " " << f[x-1] << endl; } } return 0; }
1140.字符串操作。
#include<bits/stdc++.h> using namespace std; int n; int main() { while(cin >> n;) { int ans = 0; char a[50000],b[50000]; while(n--) { cin >> a; strcat(b,a); char *p = b,*o; while(o = strstr(p,"wanshen")) { p = o+7; ans++; } strcpy(b,p); } cout << ans << endl; } return 0; }
1141.取余模拟。
#include<bits/stdc++.h> using namespace std; long long x,y,a; int main() { while(cin >> x >> y >> a) { a %= x+y; if(a == 0) { cout << "light" << endl; continue; } while(a) { a -= x; if(a <= 0) { cout << "wanshen" << endl; break; } a -= y; if(a <= 0) { cout << "light" << endl; break; } } } return 0; }
1142.记录字符串中出现的字符。
#include<bits/stdc++.h> using namespace std; char a[100005],b[100005]; int s[128]; int main() { while(cin >> a >> b) { memset(s,0,sizeof(s)); int flag=0,lena = strlen(a),lenb = strlen(b); for(int i = 0;i < lenb;i++) s[b[i]] = 1; for(int i = 0;i < lena;i++) { if(!s[a[i]]) { putchar(a[i]); flag = 1; } } if(!flag) cout << "EMPTY"; cout << endl; } return 0; }
1143.暴力dfs。
#include<bits/stdc++.h> using namespace std; int a[10],vis[10],num[10],ans; bool ok(int x,int z) { if(x == 1) return 1; if(x > 3 && __gcd(z,num[x-3]) != 1) return 0; if(x%3 != 1 && __gcd(z,num[x-1]) != 1) return 0; return 1; } void dfs(int x) { if(x == 9) { ans++; return; } for(int i = 1;i <= 9;i++) { if(vis[i]) continue; if(ok(x+1,a[i])) { vis[i] = 1; num[x+1] = a[i]; dfs(x+1); vis[i] = 0; } } } int main() { while(cin >> a[1]) { for(int i = 2;i <= 9;i++) cin >> a[i]; ans = 0; dfs(0); cout << ans << endl; } return 0; }
1144.合并小的,不一定每一次都是合并k个,第一次先合并不足k的次数,后面都合并k个。
#include<bits/stdc++.h> using namespace std; priority_queue< long long,vector<long long>,greater<long long> > q; int n,k; int main() { ios::sync_with_stdio(0); while(cin >> n >> k) { while(!q.empty()) q.pop(); long long x,ans = 0; for(int i = 1;i <= n;i++) { cin >> x; q.push(x); } while((n-1)%(k-1)) { q.push(0); n++; } while(q.size() != 1) { long long t = 0,sum = 0; while(t++ < k) { sum += q.top(); q.pop(); } ans += sum; q.push(sum); } cout << ans << endl; } return 0; }
1145.矩阵快速幂。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; long long l,r,anss[5] = {1,2,3,6,11}; struct matrix { long long m[4][4]; }; matrix ans = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1,}; matrix base = { 1,1,1,0, 1,0,0,0, 0,1,0,0, 1,0,0,1,}; matrix mul(matrix a, matrix b) { matrix tmp; for(int i = 0; i < 4;i++) { for(int j = 0; j < 4;j++) { tmp.m[i][j] = 0; for(int k = 0; k < 4;k++) tmp.m[i][j] = (tmp.m[i][j]+a.m[i][k]*b.m[k][j])%MOD; } } return tmp; } long long fast_mod(long long n) { matrix x = ans,y = base; while(n) { if(n & 1) { x = mul(x,y); } y = mul(y,y); n >>= 1; } return (x.m[3][0]+x.m[3][1]+x.m[3][2]+2*x.m[3][3])%MOD; } int main() { ios::sync_with_stdio(0); while(cin >> l >> r) { long long x,y; if(l == 0) x = 0; else x = l < 6?anss[l-1]:fast_mod(l-2); y = r < 5?anss[r]:fast_mod(r-1); cout << (y-x+MOD)%MOD << endl; } return 0; }
1146.01背包,达成每种V所需的最小体力。
#include<bits/stdc++.h> using namespace std; int dp[50005],v[1005],w[1005],n,W; int main() { ios::sync_with_stdio(0); while(cin >> n >> W) { memset(dp, 0x3f, sizeof(dp)); dp[0] = 0; int sum = 0; for(int i = 1;i <= n;i++) { cin >> w[i] >> v[i]; sum += v[i]; } for(int i = 1;i <= n;i++) { for(int j = sum;j >= v[i];j--) dp[j] = min(dp[j-v[i]]+w[i],dp[j]); } for(int i = sum;i >= 0;i--) { if(dp[i] <= W) { cout << i << endl; break; } } } return 0; }
1147.先判断和在不在最大和最小的区间内。若在,从后往前贪心。
#include<bits/stdc++.h> using namespace std; int n,l[100005],r[100005],ans[100005],s; int main() { while(~scanf("%d%d",&n,&s)) { int sum1 = 0,sum2 = 0; for(int i = 1;i <= n;i++) { scanf("%d%d",&l[i],&r[i]); sum1 += l[i]; sum2 += r[i]; } if(s < sum1 || s > sum2) { printf("Xue Beng\n"); continue; } int x = s-sum1; for(int i = n;i >= 1;i--) { ans[i] = x >= r[i]-l[i]?r[i]:l[i]+x; x -= ans[i]-l[i]; } printf("%d",ans[1]); for(int i = 2;i <= n;i++) printf(" %d",ans[i]); printf("\n"); } return 0; }
1148.b ≥ phi[c]时,a^b%c = a^(b%phi[c]+phi[c])%c,否则直接算。
#include<bits/stdc++.h> #define LL long long using namespace std; LL a,b,c,y; LL gcd(LL a,LL b) { return b?gcd(b,a%b):a; } LL PowerMod(LL a, LL b, LL c) { LL ans = 1; a = a % c; while(b) { if(b&1) ans = (ans*a)%c; b = b>>1; a = (a*a)%c; } return ans; } LL euler(LL n) { LL res = n,a = n; for(LL i = 2;i*i <= a;i++) { if(a%i == 0) { res = res/i*(i-1); while(a%i == 0) a /= i; } } if(a > 1) res = res/a*(a-1); return res; } int main() { while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&y)) { LL t = euler(y); if(log(b) < 9/c) { printf("%lld\n",PowerMod(a,PowerMod(b,c,1e9+7),y)); } else printf("%lld\n",PowerMod(a,PowerMod(b,c,t)+t,y)); } return 0; }
1149.打表预处理,容斥。
#include<bits/stdc++.h> #define MOD 1000000007 #define LL long long using namespace std; LL fac[2000005],inv[2000005],n,m,k; LL PowerMod(LL a, LL b, LL c) { LL ans = 1; a = a % c; while(b) { if(b&1) ans = (ans*a)%c; b = b>>1; a = (a*a)%c; } return ans; } inline LL c(LL n,LL m) { if(m > n) return 0; return fac[n]*inv[m]%MOD*inv[n-m]%MOD; } int main() { ios::sync_with_stdio(0); fac[0] = 1; for(int i = 1;i <= 2000000;i++) fac[i] = fac[i-1]*i%MOD; inv[2000000] = PowerMod(fac[2000000],MOD-2,MOD); for(int i = 1999999;i >= 0;i--) inv[i] = inv[i+1]*(i+1)%MOD; while(cin >> n >> m >> k) { LL ans = c(n+m-1,m),x = -1; for(int i = 1;i <= n;i++,x = -x) { LL t = m-i*(k+1); if(t < 0) break; ans = (ans+x*(c(n,i)*c(n+t-1,t)%MOD)+MOD)%MOD; } cout << ans << endl; } return 0; }
1150.减法。
#include<bits/stdc++.h> using namespace std; int a; int main() { while(cin >> a) cout << a-10 << endl; return 0; }
1151.分情况讨论。
#include<bits/stdc++.h> using namespace std; long long a,b,c; int main() { while(cin >> a >> b >> c) { if(a==0&&b==0&&c==0) { cout << "inf" << endl; continue; } if(a==0&&b!=0) { cout << 1 << endl; continue; } if(a==0&&b==0&&c!=0) { cout << 0 << endl; continue; } long long x=b*b-4*a*c; if(x==0) cout << 1 << endl; else if(x>0) cout << 2 << endl; else cout << 0 << endl; } return 0; }
1152.map记录。
#include<bits/stdc++.h> using namespace std; struct st { string name,course; }; map<int,st> mp; map<int,st>::iterator it; int main() { int n,m; while(cin >> n >> m) { mp.clear(); string a,c,d; int b; cin >> a >> c; for(int i = 1;i <= n;i++) { cin >> a >> b; mp[b].name = a; mp[b].course = "NULL"; } cin >> a >> c >> d; for(int i = 1;i <= m;i++) { cin >> a >> b >> c; mp[b].course = c; } cout << "Name StuNum CourseName" << endl; for(it = mp.begin();it != mp.end();it++) cout << it->second.name << ‘ ‘ << it->first << ‘ ‘ << it->second.course << endl; } return 0; }
1153.比较斜率排序。
#include<bits/stdc++.h> #define LL long long using namespace std; struct line { LL x,y; }a[100005]; int n; bool cmp(line X,line Y) { return X.y*Y.x < X.x*Y.y; } int main() { while(~scanf("%d",&n)) { int cnt = 0; LL x,y,xx,yy,temp = 0; for(int i = 1;i <= n;i++) { scanf("%lld%lld%lld%lld",&x,&y,&xx,&yy); if(x == xx) temp++; else if(x < xx) { a[cnt].x = xx-x; a[cnt].y = yy-y; cnt++; } else { a[cnt].x = x-xx; a[cnt].y = y-yy; cnt++; } } sort(a,a+cnt,cmp); LL ans = temp*(temp-1)/2; LL num = 1; for(int i = 1;i < cnt;i++) { if(a[i].x*a[i-1].y == a[i].y*a[i-1].x) num++; else { ans += num*(num-1)/2; num = 1; } } ans += num*(num-1)/2; printf("%lld\n",ans); } return 0; }
1154.统计连续0串的个数。
#include<bits/stdc++.h> using namespace std; string s; int main() { ios::sync_with_stdio(0); while(cin >> s) { int len = s.length(); int ans = 0,flag = 0; for(int now = 0;now < len;now++) { if(flag) { while(now < len) { if(s[now] == ‘1‘) { ans++; flag = 0; break; } else now++; } } else if(s[now] == ‘0‘) flag = 1; } if(flag) ans++; cout << ans << endl; } return 0; }
1155.先确定在往里数的第几个圈上,然后根据位置确定数字。
#include<bits/stdc++.h> using namespace std; long long k,x,y,ans; int main() { ios::sync_with_stdio(0); while(cin >> k >> x >> y) { long long p = min(x,y); p = min(p,k-x+1); p = min(p,k-y+1); long long dis = x-p+y-p; if(x <= y) ans = 4*(p-1)*(k-p+1)+1+dis; else ans = 4*p*(k-p)+1-dis; cout << ans << endl; } return 0; }
1156.递减的单调队列,保存人的编号和入队耐心-入队时间。
#include<bits/stdc++.h> using namespace std; struct st { int num,t; }s; deque<st> q; int n,x; int main() { while(~scanf("%d",&n)) { while(!q.empty()) q.pop_back(); int out = 1,cnt = 1; for(int i = 1;i <= n;i++) { scanf("%d",&x); switch(x) { case 1: int tt; scanf("%d",&tt); s.t = tt-i; s.num = cnt++; while(!q.empty() && q.back().t <= s.t) q.pop_back(); q.push_back(s); break; case 2: if(q.front().num == out++) q.pop_front(); break; case 3: printf("%d\n",q.front().t+i); } } } return 0; }
1157.求max和从小边往大边加,求min和从大边往小边加,用并查集计点数。
#include<bits/stdc++.h> using namespace std; int point[100005]; struct line { int v,u,w; }l[1000005]; int n,pre[100005]; bool cmp(line x,line y) { return x.w < y.w; } int findd(int x) { int root = x; while(root != pre[root]) root = pre[root]; int temp; while(x != pre[x]) { temp = pre[x]; pre[x] = root; x = temp; } return root; } int main() { while(~scanf("%d",&n)) { long long ans = 0; for(int i = 1;i < n;i++) scanf("%d%d%d",&l[i].u,&l[i].v,&l[i].w); sort(l+1,l+n,cmp); for(int i = 1;i <= n;i++) { pre[i] = i; point[i] = 1; } for(int i = 1;i < n;i++) { int x = findd(l[i].u),y = findd(l[i].v); ans += (long long)point[x]*point[y]*l[i].w; pre[x] = y; point[y] += point[x]; } for(int i = 1;i <= n;i++) { pre[i] = i; point[i] = 1; } for(int i = n-1;i >= 1;i--) { int x = findd(l[i].u),y = findd(l[i].v); ans -= (long long)point[x]*point[y]*l[i].w; pre[x] = y; point[y] += point[x]; } printf("%lld\n",ans); } return 0; }
1158.规律C(x+k,x+y),打表预处理。
#include<bits/stdc++.h> #define LL long long #define MOD (long long)(1e9+7) using namespace std; int x,y,k; LL fac[2000005]; LL PowerMod(LL a, LL b, LL c) { LL ans = 1; a = a % c; while(b) { if(b&1) ans = (ans*a)%c; b = b>>1; a = (a*a)%c; } return ans; } LL inv(LL x) { return PowerMod(x,MOD-2,MOD); } int main() { fac[1] = 1; for(int i = 2;i <= 2000000;i++) fac[i] = fac[i-1]*i%MOD; while(~scanf("%d%d%d",&x,&y,&k)) { if(y == k) printf("1\n"); else if(y < k) printf("0\n"); else printf("%lld\n",fac[x+y]*inv(fac[x+k])%MOD*inv(fac[y-k])%MOD); } return 0; }
1159.首要要考虑重边和环,然后开放一个点就要更新相关最短路,而且更新的顺序有要求。
#include<bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; int n,m,t,a[305][305],ok[305]; int main() { while(~scanf("%d%d%d",&n,&m,&t)) { memset(a,0x3f,sizeof(a)); memset(ok,0,sizeof(ok)); for(int i = 0;i < n;i++) a[i][i] = 0; while(m--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); if(u == v) continue; a[u][v] = min(a[u][v],w); } while(t--) { int o; scanf("%d",&o); if(o == 0) { int x; scanf("%d",&x); if(ok[x]) printf("lab %d has been repaired!\n",x); else { ok[x] = 1; for(int i = 0;i < n;i++) { for(int j = 0;j < n;j++) { if(!ok[i] || !ok[j]) continue; a[j][x] = min(a[j][x],a[j][i]+a[i][x]); } } for(int i = 0;i < n;i++) { for(int j = 0;j < n;j++) { if(!ok[i] || !ok[j]) continue; a[x][j] = min(a[x][j],a[x][i]+a[i][j]); } } for(int i = 0;i < n;i++) { for(int j = 0;j < n;j++) { if(!ok[i] || !ok[j]) continue; a[i][j] = min(a[i][j],a[i][x]+a[x][j]); } } } } else { int u,v; scanf("%d%d",&u,&v); if(!ok[u] || !ok[v]) printf("help %d %d\n",u,v); else if(a[u][v] == INF) printf("no path\n"); else printf("%d\n",a[u][v]); } } } return 0; }
1160.set模拟,貌似代码有点问题,直接过了,好像是询问都是按时间顺序来的。
#include<bits/stdc++.h> using namespace std; int n,m; struct ss { int num,t,o; }a[1000005]; set<int> s; bool cmp(ss x,ss y) { return x.t < y.t; } int main() { scanf("%d%d",&n,&m); for(int i = 1;i <= n;i++) scanf("%d%d%d",&a[i].o,&a[i].t,&a[i].num); sort(a+1,a+1+n,cmp); int cnt = 1; for(int i = 1;i <= m;i++) { int p,q; scanf("%d%d",&p,&q); while(cnt <= n&& a[cnt].t < p) { if(a[cnt].o == 1) s.insert(a[cnt].num); else s.erase(a[cnt].num); cnt++; } if(s.count(q)) printf("YES\n"); else printf("NO\n"); } return 0; }
1161.规律,列方程推一下,其中某一个村庄假设不动,其他n-1个,最后转化成求绝对值最小问题。
#include<bits/stdc++.h> #define LL long long using namespace std; LL a[1000005] = {0}; int n; int main() { while(~scanf("%d",&n) && n > 0) { LL sum1 = 0,sum2 = 0; for(int i = 1;i <= n;i++) { scanf("%lld",&a[i]); sum1 += a[i]; } for(int i = 1;i <= n;i++) { LL t; scanf("%lld",&t); sum2 += t; a[i] = a[i-1]+t-a[i]; } if(sum1 != sum2) { printf("No way\n"); continue; } sort(a,a+n); LL t = a[n/2],ans = 0; for(int i = 0;i < n;i++) ans += abs(t-a[i]); printf("%lld\n",ans); } return 0; }
*1162.待补。
*1163.待补。
1164.因为存孩子会爆,只能用孩子存父节点,然后逆向的dfs,期间需要一个数组保存累加值。
#include<bits/stdc++.h> using namespace std; int a[10000005],pre[10000005] = {0},sum[10000005] = {0},n,u,v; bool vis[10000005] = {0}; long long dfs(int pos) { if(vis[pos]) return 0; vis[pos] = 1; if(!pre[pos]) { sum[pos] -= a[pos]; return abs(sum[pos]); } long long t = dfs(pre[pos]); a[pos] += sum[pre[pos]]; t += abs(a[pos]); sum[pos] += sum[pre[pos]]-a[pos]; return t; } int main() { scanf("%d",&n); for(int i = 1;i < n;i++) { scanf("%d%d",&u,&v); pre[v] = u; } for(int i = 1;i <= n;i++) scanf("%d",&a[i]); long long ans = 0; for(int i = 1;i <= n;i++) ans += dfs(i); printf("%lld\n",ans); return 0; }
*1165.待补。
*1166.待补。
*1167.待补。
1168.都取出现次数最少的那个字母就可以了。
#include<bits/stdc++.h> using namespace std; char a[100005]; int x[30],y[30]; int main() { int T; scanf("%d",&T); for(int c = 1;c <= T;c++) { memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); scanf("%s",a); int len = strlen(a); for(int i = 0;i < len;i++) { if(‘a‘ <= a[i] && a[i] <= ‘z‘) x[a[i]-‘a‘]++; else y[a[i]-‘A‘]++; } sort(x,x+26); sort(y,y+26); printf("Case %d: %d\n",c,min(x[0],y[0])); } return 0; }
1169.不断除以2,直到奇数,操作次数的奇偶性。
#include<bits/stdc++.h> using namespace std; int f(long long x) { if(x&1) return 0; else return f(x/2)^1; } int main() { int T; scanf("%d",&T); while(T--) { long long x; scanf("%d",&x); if(f(x) == 0) printf("First Box\n"); else printf("Second Box\n"); } }
1170.优化一下暴力的过程,将数量一样的放一起。
#include<bits/stdc++.h> #define LL long long using namespace std; LL n,m; LL gcd(LL x,LL y) { return y?gcd(y,x%y):x; } LL c(LL x,LL y) { return (y*(y+1)*(2*y+1))/6-((x-1)*x*(2*x-1))/6; } int main() { while(~scanf("%lld%lld",&n,&m) && n && m) { LL ans = 0; int now = 1; while(now <= n) { int t = n/now,endd = min(m,n/t); ans += t*c(now,endd); if(endd == m) break; now = endd +1; } LL x = n*m; LL g = gcd(ans,x); printf("%lld/%lld\n",ans/g,x/g); } }
*1171.待补。
1172.sum/2,01背包。
#include<bits/stdc++.h> using namespace std; int n,w[205],dp[200005]; int main() { ios::sync_with_stdio(0); while(cin >> n) { memset(dp,0,sizeof(dp)); int sum = 0; for(int i = 1;i <= n;i++) { cin >> w[i]; sum += w[i]; } int endd = sum/2; for(int i = 1;i <= n;i++) { for(int j = endd;j >= w[i];j--) { dp[j] = max(dp[j],dp[j-w[i]]+w[i]); } } cout << sum-dp[endd]*2 << endl; } return 0; }
1173.是否为3的倍数。
#include<bits/stdc++.h> using namespace std; int n; int main() { ios::sync_with_stdio(0); while(cin >> n) { if(n%3) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
*1174.待补。
1175.不断更新l。
#include<stdio.h> int a[200005]; int main() { int n,k; while(scanf("%d%d",&n,&k)!=EOF) { int i,j,l = 0,sum = 0; for(i = 0;i < n;i++) { scanf("%d",&a[i]); for(j = l;j < i;j++) { if(a[j] == a[i]) { l = j+1; break; } } if(i-l+1 == k) { sum++; l++; } } printf("%d\n",sum); } }
1176.渡河dp。
#include<bits/stdc++.h> using namespace std; int a[100005],n; int main() { while(cin >> n) { for(int i = 1;i <= n;i++) cin >> a[i]; sort(a+1,a+1+n); long long ans = 0; while(n) { if(n == 1) { ans += a[1]; break; } if(n == 2) { ans += a[2]; break; } if(n == 3) { ans += a[1]+a[2]+a[3]; break; } else { ans += min(2*a[1]+a[n]+a[n-1],a[1]+2*a[2]+a[n]); n -= 2; } } cout << ans << endl; } return 0; }
1177.排序后一维树状数组。
#include<bits/stdc++.h> using namespace std; int tree[1000005],n,m,cnt = 1,ans[100005]; struct star { int num,x,y,o; }a[200005]; bool cmp(star X,star Y) { if(X.x == Y.x) return X.y < Y.y; return X.x < Y.x; } inline int lowbit(int x) { return x & (-x); } void update(int pos,int x) { while(pos <= 1000001) { tree[pos] += x; pos += lowbit(pos); } } int getsum(int pos) { int sum = 0; while(pos > 0) { sum += tree[pos]; pos -= lowbit(pos); } return sum; } int main() { while(~scanf("%d%d",&n,&m)) { printf("Case #%d:\n",cnt++); memset(tree,0,sizeof(tree)); for(int i = 1;i <= n;i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].o = 1; } for(int i = n+1;i <= n+m;i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].num = i-n; a[i].o = 0; } sort(a+1,a+1+n+m,cmp); for(int i = 1;i <= n+m;i++) { if(a[i].o) update(a[i].y+1,1); else ans[a[i].num] = getsum(a[i].y+1); } for(int i = 1;i <= m;i++) printf("%d\n",ans[i]); } return 0; }
1179.map实现。
#include<bits/stdc++.h> using namespace std; int a[5005],ans[10000005] = {0},n,s,q; int main() { memset(ans,0x3f,sizeof(ans)); cin >> n >> s; for(int i = 1;i <= n;i++) { cin >> a[i]; for(int j = 1;j <= s;j++) ans[a[i]*j] = min(ans[a[i]*j],j); } cin >> q; while(q--) { int t,x = 2e9; cin >> t; for(int i = 1;i <= n;i++) { int endd = min(s,t/a[i]); for(int j = 1;j <= endd;j++) { int left = t-j*a[i]; if(left == 0) x = min(x,j); else if(ans[left] && ans[left]+j <= s) x = min(x,ans[left]+j); } } if(x == 2e9) cout << -1 << endl; else cout << x << endl; } return 0; }
1180.dp[i][j][k]表示取第i个珠子颜色为j且末尾有k个连续的珠子的总数。
k = 1时,dp[i][j][k] = sumx≠j且1≤y≤k(dp[i-1][x][y])
k ≠ 1时,dp[i][j][k] = dp[i-1][j][k-1]
由于对称性,j不影响dp[i][j][k]结果,则忽略第二维,dp[i][1] = sum1≤y≤k(dp[i-1][y])*(m-1)
利用矩阵快速幂,计算sum1≤x≤k-1(dp[n][x]),再乘以m种颜色即为结果。
#include<bits/stdc++.h> #define MOD 23333 using namespace std; long long n,m,k; struct matrix { long long m[105][105]; }; matrix one = {0},base = {0}; matrix mul(matrix a, matrix b) { matrix tmp; for(int i = 0; i < k-1;i++) { for(int j = 0; j < k-1;j++) { tmp.m[i][j] = 0; for(int K = 0;K < k-1;K++) tmp.m[i][j] = (tmp.m[i][j]+a.m[i][K]*b.m[K][j])%MOD; } } return tmp; } long long fast_mod(long long n) { matrix ans = one,y = base; while(n) { if(n&1) ans = mul(ans,y); y = mul(y,y); n /= 2; } long long res = 0; for(int i = 0;i < k-1;i++) res = (res+ans.m[i][0])%MOD; return res*m%MOD; } int main() { ios::sync_with_stdio(false); for(int i = 0;i < 100;i++) one.m[i][i] = 1; for(int i = 1;i < 100;i++) base.m[i][i-1] = 1; while(cin >> n >> m >> k) { for(int i = 0;i < k-1;i++) base.m[0][i] = m-1; cout << fast_mod(n-1) << endl; } return 0; }
1181.因为只有一组数据,直接开vector存每一次的状态,把树状数组改一下就可以了。
#include<bits/stdc++.h> using namespace std; int a[1000005] = {0},n,q,o,t = 0,c = 0; vector< pair<int,int> > v[1000005]; vector< pair<int,int> >::iterator it; inline int lowbit(int x) { return x&(-x); } void update(int pos,int x) { while(pos <= n) { int temp = 0; if(!v[pos].empty()) temp = v[pos].back().second; v[pos].push_back(make_pair(t,temp+x)); pos += lowbit(pos); } } int getsum(int pos,int tt) { int sum = 0; while(pos > 0) { it = upper_bound(v[pos].begin(),v[pos].end(),make_pair(tt,0)); if(it != v[pos].end() && it->first == tt) sum += it->second; else if(it != v[pos].begin()) { it--; sum += it->second; } pos -= lowbit(pos); } return sum; } int main() { ios::sync_with_stdio(false); cin >> n >> q; while(q--) { cin >> o; if(o == 1) { t++; int i,x; cin >> i >> x; c = i^c; update(c,x); a[c] += x; c = a[c]; } else { int l,r,tt; cin >> l >> r >> tt; c = getsum(r^c,tt)-getsum((l^c)-1,tt); cout << c << endl; } } return 0; }
1182.快速乘。
#include<bits/stdc++.h> #define LL long long #define MOD 23333333333 using namespace std; LL n,m; LL qmul(LL a, LL b, LL c) { LL ans = 0; a = a%c; while(b) { if(b&1) ans = (ans+a)%c; b >>= 1; a = (a+a)%c; } return ans; } int main() { while(cin >> n >> m) { LL x = m/3,y = m%3,ans = 0; if(x%2) ans = qmul(x*3,(2*n+x+1)/2,MOD); else ans = qmul(x*3/2,2*n+x+1,MOD); for(int i = 0;i < y;i++) ans = (ans+n+x+i)%MOD; cout << ans << endl; } return 0; }
1183.划分问题,dp[i][j] = dp[i-1][j-1]+dp[i-j][j]。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; long long dp[1005][1005] = {0}; int n,m; int main() { for(int i = 1;i <= 1000;i++) dp[i][1] = 1; for(int i = 2;i <= 1000;i++) { for(int j = 2;j <= i;j++) dp[i][j] = (dp[i-1][j-1]+dp[i-j][j])%MOD; } for(int i = 1;i <= 1000;i++) { for(int j = 1;j <= 1000;j++) dp[i][j] = (dp[i][j-1]+dp[i][j])%MOD; } while(scanf("%d%d",&n,&m) && n && m) printf("%lld\n",dp[n][m]); return 0; }
1184.Sprague-Grundy定理。
#include<bits/stdc++.h> using namespace std; int n; int main() { while(~scanf("%d",&n)) { int ans = 0,t; for(int i = 1;i <= n;i++) { scanf("%d",&t); ans ^= t%3; } if(ans) printf("Yes\n"); else printf("No\n"); } return 0; }
1185.二分答案。
#include<bits/stdc++.h> using namespace std; int m,k,t[100005]; bool ok(int x) { int cnt = 0,sum = 0; for(int i = 1;i <= m;i++) { if(sum+t[i] < x) sum += t[i]; else if(sum+t[i] == x) { cnt++; sum = 0; } else { cnt++; sum = t[i]; } } if(sum) cnt++; if(cnt > k) return 1; return 0; } int main() { ios::sync_with_stdio(false); while(cin >> m >> k) { for(int i = 1;i <= m;i++) cin >> t[i]; int l = 1,r = 100000000; while(l < r) { int mid = (l+r)/2; if(ok(mid)) l = mid+1; else r = mid; } cout << l << endl; } return 0; }
1186.dfs。
#include<bits/stdc++.h> using namespace std; int n = 0,m,len[55],vis[55][55] = {0},sum,ok; int dx[4] = {-1,0,1,0}; int dy[4] = {0,-1,0,1}; string a[55]; void dfs(int x,int y) { vis[x][y] = 1; sum++; for(int i = 0;i < 4;i++) { int xx = x+dx[i],yy = y+dy[i]; if(xx < 1 || xx > n || yy < 1 || yy > len[x]) { ok = 1; continue; } if(vis[xx][yy]) continue; if(a[xx][yy] == ‘#‘) continue; dfs(xx,yy); } } int main() { ios::sync_with_stdio(false); while(cin >> a[++n]) { len[n] = a[n].length(); a[n] = ‘ ‘+a[n]; } int ans = 0; for(int i = 1;i <= n;i++) { for(int j = 1;j <= len[i];j++) { if(vis[i][j] || a[i][j] == ‘#‘) continue; sum = 0; ok = 0; dfs(i,j); if(!ok) ans += sum; } } cout << ans << endl; return 0; }
1187.每个点按度排序,从度小的点开始,把每个点邻接表中在该点前面的点都删掉。然后对于每一条边,统计公共点的个数和。
#include<bits/stdc++.h> using namespace std; int n,m,vis[100005],x[100005],y[100005]; vector<int> v[100005]; struct xx { int cnt,id; friend bool operator<(xx a,xx b) { return a.cnt < b.cnt; } }a[100005]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); memset(vis,0,sizeof(vis)); for(int i = 1;i <= n;i++) v[i].clear(); for(int i = 1;i <= n;i++) a[i].id = i,a[i].cnt = 0; for(int i = 1;i <= m;i++) { scanf("%d%d",&x[i],&y[i]); v[x[i]].push_back(y[i]); v[y[i]].push_back(x[i]); a[x[i]].cnt++; a[y[i]].cnt++; } sort(a+1,a+1+n); for(int i = 1;i <= n;i++) { int tt = a[i].id; for(int j = 0;j < v[tt].size();j++) { int t = v[tt][j]; if(vis[t]) v[tt].erase(v[tt].begin()+j),j--; } sort(v[tt].begin(),v[tt].end()); vis[tt] = 1; } long long ans = 0; for(int i = 1;i <= m;i++) { int t = x[i],tt = y[i]; int now1 = 0,now2 = 0; while(now1 < v[t].size() && now2 < v[tt].size()) { if(v[t][now1] < v[tt][now2]) now1++; else if(v[t][now1] > v[tt][now2]) now2++; else { ans++; now1++; now2++; } } } printf("%lld\n",ans); } return 0; }
1188.结论。
#include<bits/stdc++.h> using namespace std; long long n; int main() { ios::sync_with_stdio(false); while(cin >> n) cout << 1+n*(n+1)/2 << endl; return 0; }
1189.输出注意空格。
#include<bits/stdc++.h> using namespace std; int n; int main() { ios::sync_with_stdio(false); while(cin >> n) { for(int i = 1;i <= n;i++) { int t = 26-i; while(t--) cout << " "; for(int j = 0;j < i;j++) cout << (char)(‘A‘+j); for(int j = i-2;j >= 0;j--) cout << (char)(‘A‘+j); cout << endl; } } return 0; }
1190.连续平方数求和。
#include<bits/stdc++.h> #define LL long long using namespace std; long long n,p; LL qmod(LL a, LL b, LL c) { LL ans = 1; a = a%c; while(b) { if(b&1) ans = ans*a%c; b >>= 1; a = a*a%c; } return ans; } LL inv(LL a,LL b) { return qmod(a,b-2,b); } int main() { ios::sync_with_stdio(false); while(cin >> n >> p) { cout << n%p*(n+1)%p*(2*n+1)%p*inv(6,p)%p << endl; } return 0; }
1191.给每一类记录一个标准。
#include<bits/stdc++.h> #define LL long long using namespace std; long long n; vector<string> v[5005]; int main() { ios::sync_with_stdio(false); while(cin >> n) { int cnt= 0; for(int i = 1;i <= n;i++) v[i].clear(); for(int i = 1;i <= n;i++) { string s; cin >> s; string ss = s; sort(ss.begin(),ss.end()); int p = 1,flag = 0; for(;p <= cnt;p++) { if(v[p][0] == ss) { v[p].push_back(s); flag = 1; break; } } if(!flag) { v[++cnt].push_back(ss); v[cnt].push_back(s); } } for(int i = 1;i <= cnt;i++) { cout << v[i][1]; for(int j = 2;j <v[i].size();j++) cout << " " << v[i][j]; cout << endl; } } return 0; }
1192.比较每一段。
#include<bits/stdc++.h> #define LL long long using namespace std; int n,k,a[200005]; int main() { ios::sync_with_stdio(false); while(cin >> n >> k) { long long ans = 0,now = 0; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 1;i <= k;i++) now += a[i]; ans = now; for(int i = k+1;i <= n;i++) { now = now+a[i]-a[i-k]; ans = min(now,ans); } cout << ans << endl; } return 0; }
1193.Sprague-Grundy定理。
#include<bits/stdc++.h> #define LL long long using namespace std; int a,b,c,d; int main() { ios::sync_with_stdio(false); while(cin >> a >> b >> c >> d) { int t = a%(c+1),tt = b%(d+1); if(t^tt) cout << "NUO!" << endl; else cout << "NO!" << endl; } return 0; }
1194.求解模方程a^x=b(mod n),n为素数。大步小步模版。
#include<bits/stdc++.h> #define LL long long using namespace std; LL pow_mod(LL a,LL b,LL n) { LL s=1; while(b) { if(b&1) s=(s*a)%n; a=(a*a)%n; b=b>>1; } return s; } LL log_mod (LL a,LL b,LL n) { LL m,v,e=1,i; m=ceil(sqrt(n+0.5)); //x=i*m+j //v=inv(pow_mod(a,m,n),n); //a^m*v=1(mod n) v=pow_mod(a,n-m-1,n); map<LL,LL>x; x[1]=m; for(i=1;i<m;i++) { e=(e*a)%n; if(!x[e])x[e]=i; } for(i=0;i<m;i++) { if(x[b]) { LL num=x[b]; x.clear(); return i*m+(num==m?0:num); } b=(b*v)%n; //b=b/(a^m) } return -1; } int main() { LL a,b; while(scanf("%lld%lld",&a,&b)!=EOF) { LL ans1=log_mod(3,a,2147483647),ans2 = log_mod(3,b,2147483647); if(ans1==-1 || ans2 == -1) printf("No Solution\n"); else printf("%lld\n",pow_mod(3,ans1*ans2,2147483647)); } return 0; }
1195.最短路。
#include<bits/stdc++.h> using namespace std; int n,m,a[105][105],vis[105],dis[105]; int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { cin >> m; n = 0; memset(a,0x3f,sizeof(a)); while(m--) { int x,y,z; cin >> x >> y >> z; a[x][y] = a[y][x] = min(a[x][y],z); n = max(n,x); n = max(n,y); } int s,d; cin >> s >> d; memset(vis,0,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); dis[s] = 0; for(int i = 1;i <= n;i++) { int k = -1,minn = 0x3f3f3f3f; for(int j = 1;j <= n;j++) { if(vis[j]) continue; if(minn > dis[j]) { minn = dis[j]; k = j; } } if(k == -1) break; vis[k] = 1; for(int j = 1;j <= n;j++) { if(vis[j]) continue; if(dis[j] > dis[k]+a[j][k]) dis[j] = dis[k]+a[j][k]; } } cout << 2*dis[d] << endl; } return 0; }
1196.判断。
#include <bits/stdc++.h> using namespace std; int n; int main() { ios::sync_with_stdio(false); while(cin >> n) { if(n <= 1000) cout << "XiaoZhen" << endl; else if(n < 10000) cout << "Zh0ngshen" << endl; else cout << "DaNuo" << endl; } return 0; }
1197.判断。
#include <bits/stdc++.h> using namespace std; string s; int main() { ios::sync_with_stdio(false); while(cin >> s) { if(s == "v8") cout << "SingleDog&YangRouHuoGuo" << endl; else if(s == "qsqx") cout << "Couple&Program" << endl; else cout << "SingleDog&GoodGoodStud" << endl; } return 0; }
1198.结论。
#include <bits/stdc++.h> using namespace std; long long a[10000005]; int n; int main() { ios::sync_with_stdio(false); a[0] = 1; a[1] = 2; for(int i = 2;i <= 10000000;i++) a[i] = a[i-1]+2*(i-1); while(cin >> n) cout << a[n] << endl; return 0; }
1199.圆排列。
#include <bits/stdc++.h> using namespace std; long long n,m,p; int main() { ios::sync_with_stdio(false); while(cin >> n >> m >> p) { long long ans = 1; int flag = 0; for(int i = n-m+1;i <= n;i++) { if(!flag && i%m == 0) { ans = ans*i/m%p; flag = 1; } else ans = ans*i%p; } cout << ans << endl; } return 0; }
1200.汉诺塔结论。
#include <bits/stdc++.h> using namespace std; long long n; long long qpower(long long a,long long b,long long c) { a %= c; long long ans = 1; while(b) { if(b%2) ans = ans*a%c; a = a*a%c; b /= 2; } return ans; } int main() { ios::sync_with_stdio(false); while(cin >> n) cout << ((qpower(2,n,2097151)-1)*2+2097151)%2097151 << endl; return 0; }
1201.素数筛。
#include <bits/stdc++.h> using namespace std; int vis[1000005] = {0},prime[1000005],num[105] = {0}; long long n; int main() { int cnt = 0; for(int i = 2;i < 1000005;i++) { if(!vis[i]) prime[++cnt] = i; for(int j = 1;i*prime[j] < 1000005 && j <= cnt;j++) { vis[prime[j]*i] = 1; if(!(i%prime[j])) break; } } for(int i = 1;i <= 100;i++) { for(int j = 0;j <= i;j++) num[i] += (j+1)*(i-j+1); } while(~scanf("%lld",&n)) { long long ans = 1; for(int i = 1;(long long)prime[i]*prime[i] <= n;i++) { if(n%prime[i] == 0) { int t = 0; while(n%prime[i] == 0) { t++; n /= prime[i]; } ans *= num[t]; } } if(n != 1) ans *= 4; printf("%lld\n",ans); } return 0; }
1202.最短路构造虚点。
#include<bits/stdc++.h> using namespace std; struct xx { int to; long long w; xx(int a,long long b):to(a),w(b){}; friend bool operator <(xx X,xx Y) { return X.w > Y.w; } }; vector<xx> v[16005]; int n,m,r,h[105][105]; long long dis[16005]; bool vis[16005],has[105]; void add(int a,int b,int x) { v[a].push_back(xx(b,x)); v[b].push_back(xx(a,x)); } void dij(int beg) { priority_queue<xx> q; memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[beg] = 0; q.push(xx(beg,0)); while(!q.empty()) { int now = q.top().to; q.pop(); if(vis[now]) continue; vis[now] = 1; for(int i = 0;i < v[now].size();i++) { int tt = v[now][i].to,ww = v[now][i].w; if(!vis[tt] && dis[now]+ww < dis[tt]) { dis[tt] = dis[now]+ww; q.push(xx(tt,dis[tt])); } } } } int main() { ios::sync_with_stdio(false); int T; cin >> T; while(T--) { for(int i = 1;i <= 16000;i++) v[i].clear(); cin >> n >> m >> r; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) cin >> h[i][j]; } for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) { if(i+1 <= n) add(105*i+j,105*i+j+105,2*(h[i][j]+h[i+1][j])); if(j+1 <= m) add(105*i+j,105*i+j+1,2*(h[i][j]+h[i][j+1])); } } for(int k = 1;k <= r;k++) { memset(has,0,sizeof(has)); int ax,ay,bx,by,t,kk; cin >> ax >> ay >> bx >> by >> t >> kk; for(int i = ax;i <= bx;i++) { for(int j = ay;j <= by;j++) { add(11000+105*k+h[i][j],105*i+j,t); v[12500+105*k+h[i][j]].push_back(xx(105*i+j,0)); v[105*i+j].push_back(xx(14000+105*k+h[i][j],0)); has[h[i][j]] = 1; } } for(int i = 0;i <= 100;i++) { if(!has[i]) continue; for(int j = i+1;j <= min(i+kk,100);j++) { if(!has[j]) continue; add(12500+105*k+i,14000+105*k+j,2*t); add(14000+105*k+i,12500+105*k+j,2*t); } } } int sx,sy,ox,oy; cin >> sx >> sy >> ox >> oy; dij(105*sx+sy); cout << dis[105*ox+oy]/2 << endl; } }
1203.状压dp。
#include<bits/stdc++.h> using namespace std; #define MOD 1000000007 int n,m,k,cnt,sta[1<<18],a[45][1<<18]; void dfs(int l,int now,int pre1,int pre2) { if(l == m) { sta[now] = 1; return; } for(int i = 0;i < k;i++) { if(i == pre1 && i == pre2) continue; dfs(l+1,(now<<3)|i,pre2,i); dfs(l+1,(now<<3)|(i+4),pre2,i); } } bool ok(int x) { int t = x%4; x >>= 3; int tt = x%4; if(t != tt) return 1; x >>= 3; int ttt = x%4; if(t != ttt) return 1; return 0; } int main() { ios::sync_with_stdio(false); while(cin >> n >> m >> k) { if(n < m) swap(n,m); memset(sta,0,sizeof(sta)); memset(a,0,sizeof(a)); cnt = 0; dfs(0,0,-1,-1); int endd = 1<<(3*m); for(int i = 0;i < endd;i++) { if(!sta[i]) continue; int flag = 0; for(int j = 0;j < m;j++) { if(i&(4<<(j*3))) flag = 1; } if(!flag) a[m][i] = 1; } for(int i = 1;i < n;i++) { for(int j = 0;j < m;j++) { int now = i*m+j+1; for(int l = 0;l < endd;l++) { if(a[now-1][l] == 0) continue; if((l&(4<<(j*3))) == 0 && (j < 2 || ok(l>>(3*j-6)))) a[now][l|(4<<(j*3))] = (a[now][l|(4<<(j*3))]+a[now-1][l])%MOD; for(int t = 0;t < k;t++) { if(t == (l>>(j*3))%4) continue; int tt = (l-(l&(7<<(j*3))))|(t<<(j*3)); if(j > 1 && !ok(tt>>(3*j-6))) continue; a[now][tt] = (a[now][tt]+a[now-1][l])%MOD; } } } } int t = n*m; int ans = 0; for(int i = 0;i < endd;i++) ans = (ans+a[t][i])%MOD; cout << ans << endl; } return 0; }
*1204.待补。
1205.构造x为10。
#include<bits/stdc++.h> using namespace std; string s; stack<char> ss; int main() { ios::sync_with_stdio(false); while(cin >> s) { if(s.length() == 1) { cout << "1 10" << endl; cout << 0 << " " << s[0] << endl; continue; } for(int i = 0;i < s.length();i++) ss.push(s[i]); cout << s.length()-1 << " " << 10 << endl; cout << ss.top(); ss.pop(); while(!ss.empty()) { cout << " " << ss.top();; ss.pop(); } cout << endl; } return 0; }
1206.等比求和推。
#include<bits/stdc++.h> #define LL long long using namespace std; string s; int main() { ios::sync_with_stdio(false); while(cin >> s) { LL t = 0,x = 0,y = 0; int i = 0,len = s.length(); int a = 0,b = 0; for(;i < len && s[i] != ‘.‘;i++) t = t*10+s[i]-‘0‘; i++; for(;i < len && s[i] != ‘_‘;i++) { x = x*10+s[i]-‘0‘; a++; } i++; y = x; for(;i < len && s[i] != ‘.‘;i++) { y = y*10+s[i]-‘0‘; b++; } LL sub,mom,z = 0; if(a == 0 && b != 0) { while(b--) z = z*10+9; LL g = __gcd(y,z); sub = y/g,mom = z/g; } else if(a == 0 && b == 0) { sub = 0; mom = 1; } else if(b == 0) { z = 1; while(a--) z *= 10; LL g = __gcd(x,z); sub = x/g,mom = z/g; } else { y -= x; while(b--) z = z*10+9; while(a--) z *= 10; LL g = __gcd(y,z); sub = y/g,mom = z/g; } sub += t*mom; cout << sub << "/" << mom << endl; } return 0; }
1207.dp。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; int n,l,r,sum[10005],t[10005]; int main() { ios::sync_with_stdio(false); while(cin >> n >> l >> r) { memset(sum,0,sizeof(sum)); memset(t,0,sizeof(t)); t[0] = 1; sum[0] = 1; while(n--) { int c,a; cin >> c >> a; for(int i = 0;i+c <= r;i++) { t[i+c] = (t[i]+t[i+c])%MOD; int tt = i+(a+1)*c; if(tt <= r) t[tt] = (t[tt]-sum[i]+MOD)%MOD; } memcpy(sum,t,(r+1)*sizeof(int)); } for(int i = l+1;i <= r;i++) sum[i] = (sum[i-1]+sum[i])%MOD; cout << sum[r] << endl; } return 0; }
1208.每次少k-1个瓶盖。
#include<bits/stdc++.h> #define MOD 1000000007 #define LL long long using namespace std; int a,b,k; LL qmod(LL a, LL b, LL c) { LL ans = 1; a = a%c; while(b) { if(b&1) ans = ans*a%c; b >>= 1; a = a*a%c; } return ans; } int main() { ios::sync_with_stdio(false); while(cin >> a >> b >> k) { LL x = qmod(a,b,k-1); if(x == 0) x = k-1; cout << (qmod(a,b,MOD)-x+MOD)%MOD*qmod(k-1,MOD-2,MOD)%MOD << endl; } return 0; }
*1209.待补。
1210.
#include<bits/stdc++.h> #define MOD 19260817 using namespace std; int n,q,a[55]; long long euler(long long n) { long long ans = 1; for(int i = 2;(long long)i*i <= n;i++) { if(n%i == 0) { n /= i; ans *= i-1; while(n%i == 0) { n /= i; ans *= i; } } } if(n > 1) ans *= n-1; return ans; } int main() { while(~scanf("%d",&n)) { for(int i = 1;i <= n;i++) { int x; scanf("%d",&x); a[i] = euler(x); } scanf("%d",&q); while(q--) { long long ans = 1; int x; scanf("%d",&x); for(int i = 1;i <= n;i++) ans = ans*(__gcd(x-1,a[i])+1)%MOD; printf("%lld\n",ans); } } return 0; }
1211.首项系数为1,根肯定为整数,且为an因子。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; int n,a[25]; long long qpower(long long a, long long b, long long c) { long long ans = 1; a = a%c; while(b) { if(b%2) ans = ans*a%c; a = a*a%c; b /= 2; } return ans; } bool ok(long long x) { long long t = 0; for(int i = 0;i <= n;i++) t = (t+a[i]*qpower(x,n-i,MOD)+MOD)%MOD; return t == 0; } int main() { ios::sync_with_stdio(0); a[0] = 1; while(cin >> n) { for(int i = 1;i <= n;i++) cin >> a[i]; long long x = abs(a[n]),ans = 0; for(long long i = 1;i*i <= x;i++) { if(x%i) continue; if(ok(i)) ans += i; if(ok(-i)) ans -= i; if(i*i == x) continue; if(ok(x/i)) ans += x/i; if(ok(-x/i)) ans -= x/i; } cout << fixed << setprecision(2) << (double)ans << endl; } return 0; }
1213.对m排序,二分找m符合的序列,树状数组维护w。
#include<bits/stdc++.h> using namespace std; int n,X,Y,tree[100005]; inline int lowbitt(int x) { return x&-x; } void add(int pos,int x) { while(pos <= 100000) { tree[pos] += x; pos += lowbitt(pos); } } int getsum(int pos) { int ans = 0; while(pos > 0) { ans += tree[pos]; pos -= lowbitt(pos); } return ans; } struct xx { int x,y; friend bool operator<(xx a,xx b) { return a.x < b.x; } }a[100005]; int main() { ios::sync_with_stdio(false); while(~scanf("%d%d%d",&n,&X,&Y)) { memset(tree,0,sizeof(tree)); for(int i = 1;i <= n;i++) scanf("%d",&a[i].x); for(int i = 1;i <= n;i++) scanf("%d",&a[i].y); sort(a+1,a+1+n); int now = 1,r = 1; long long ans = 0; for(int i = n;i >= 1;i--) { while(a[r].x <= X-a[i].x) r++; while(now > i) { --now; add(a[now].y,-1); } while(now < i && now < r) { add(a[now].y,1); now++; } ans += getsum(Y-a[i].y); } printf("%lld\n",ans); } return 0; }
1216.dfs序+划分树。
#include<bits/stdc++.h> #define N 100005 using namespace std; int n,m,cnt,l[N],r[N],pos[N]; int a[N]; vector<int> v[N]; void dfs(int now,int pre) { l[now] = ++cnt; pos[cnt] = now; for(int i = 0;i < v[now].size();i++) { int t = v[now][i]; if(t == pre) continue; dfs(t,now); } r[now] = cnt; } int tree[20][N],sorted[N],toleft[20][N]; void build(int l,int r,int dep) { if(l == r)return; int mid = (l+r)/2,same = mid-l+1; for(int i = l;i <= r;i++) { if(tree[dep][i] < sorted[mid]) same--; } int lpos = l,rpos = mid+1; for(int i = l;i <= r;i++) { if(tree[dep][i] < sorted[mid]) tree[dep+1][lpos++] = tree[dep][i]; else if(tree[dep][i] == sorted[mid] && same > 0) { tree[dep+1][lpos++] = tree[dep][i]; same--; } else tree[dep+1][rpos++] = tree[dep][i]; toleft[dep][i] = toleft[dep][l-1]+lpos-l; } build(l,mid,dep+1); build(mid+1,r,dep+1); } int query(int l,int r,int ql,int qr,int dep,int k) { if(ql == qr) return tree[dep][ql]; int mid = (l+r)/2,cnt = toleft[dep][qr]-toleft[dep][ql-1]; if(cnt >= k) { int ll = l+toleft[dep][ql-1]-toleft[dep][l-1],rr = ll+cnt-1; return query(l,mid,ll,rr,dep+1,k); } else { int rr = qr+toleft[dep][r]-toleft[dep][qr],ll = rr-(qr-ql-cnt); return query(mid+1,r,ll,rr,dep+1,k-cnt); } } int main() { while(~scanf("%d",&n)) { for(int i = 1;i <= n;i++) scanf("%d",&a[i]); for(int i = 1;i <= n;i++) v[i].clear(); cnt = 0; for(int i = 1;i < n;i++) { int x,y; scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } dfs(1,-1); for(int i = 1;i <= n;i++) sorted[i] = tree[0][i] = a[pos[i]]; sort(sorted+1,sorted+n+1); build(1,n,0); scanf("%d",&m); while(m--) { int x,y; scanf("%d%d",&x,&y); printf("%d\n",query(1,n,l[x],r[x],0,y)); } } return 0; }
1226.待补。
1127.lucas+中国剩余。
#include<bits/stdc++.h> using namespace std; int n; long long a[100005]; long long qmod(long long a,long long b,long long c) { long long ans = 1; a = a%c; while(b) { if(b%2) ans = (ans*a)%c; a = (a*a)%c; b /= 2; } return ans; } long long c(long long m,long long n,long long MOD) { if(m < n) return 0; if(m == n) return 1; if(n > m-n) n = m-n; long long mm = 1,nn = 1; for(long long i = 0;i < n;i++) { mm = mm*(m-i)%MOD; nn = nn*(n-i)%MOD; } return mm*qmod(nn,MOD-2,MOD)%MOD; } long long lucas(long long m,long long n,long long MOD) { long long ans = 1; while(m && n && ans) { ans = ans%MOD*c(m%MOD,n%MOD,MOD)%MOD; n /= MOD; m /= MOD; } return ans; } long long e_gcd(long long a,long long b,long long &x,long long &y) { if(!b) { x = 1; y = 0; return a; } long long d = e_gcd(b,a%b,y,x); y -= a/b*x; return d; } long long solve(long long *m,long long *a,long long n) { long long M = m[1],A = a[1],x,y; for(int i = 2;i <= n;i++) { long long d = e_gcd(M,m[i],x,y); if((a[i]-A)%d) return -1; x = (a[i]-A)/d*x%(m[i]/d); A += x*M; M = M/d*m[i]; A %= M; } if(A < 0) A += M; return A; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1;i <= n;i++) scanf("%lld",&a[i]); long long t1 = 0,t2 = 0,t3 = 0; for(int i = 1;i <= n;i++) { t1 = (t1+lucas(n-1,i-1,7)*a[i])%7; t2 = (t2+lucas(n-1,i-1,11)*a[i])%11; t3 = (t3+lucas(n-1,i-1,13)*a[i])%13; } long long a[4],m[4]; a[1] = t1; a[2] = t2; a[3] = t3; m[1] = 7; m[2] = 11; m[3] = 13; printf("%lld\n",solve(m,a,3)); } return 0; }
1129.len-串和反串的LCS。
#include<bits/stdc++.h> using namespace std; int dp[5005][5005]; char a[5005],b[5005]; int main() { ios::sync_with_stdio(false); int T; scanf("%d",&T); while(T--) { scanf("%s",&a); int len = strlen(a); for(int i = 0;i < len;i++) b[i] = a[len-i-1]; memset(dp,0,sizeof(dp)); for(int i = 1;i <= len;i++) { for(int j = 1;j <= len;j++) { if(a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1]+1; else dp[i][j] = max(dp[i-1][j],dp[i][j-1]); } } printf("%d\n",len-dp[len][len]); } return 0; }
1233.转化为LIS,nlogn。
#include<bits/stdc++.h> using namespace std; int n,a[100005],b[100005],mp[100005]; int main() { ios::sync_with_stdio(0); int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1;i <= n;i++) scanf("%d",&a[i]); for(int i = 1;i <= n;i++) scanf("%d",&b[i]); for(int i = 1;i <= n;i++) mp[b[i]] = i; for(int i = 1;i <= n;i++) a[i] = mp[a[i]]; b[1] = a[1]; int len = 1; for(int i = 2;i <= n;i++) { int t = lower_bound(b+1,b+len+1,a[i])-b; b[t] = a[i]; if(t == len+1) len++; } printf("%d\n",len); } return 0; }
1234.规律。
#include<bits/stdc++.h> using namespace std; int n; double a[25],b[25],R; int main() { ios::sync_with_stdio(0); cin >> n; if(n == 0) { cout << "0.00" << endl; return 0; } for(int i = 1;i <= n;i++) cin >> a[i]; cin >> R; sort(a+1,a+1+n); reverse(a+1,a+1+n); int l = 1,r = n,cnt = 1; while(l <= r) { if(cnt%2) b[r--] = a[cnt++]; else b[l++] = a[cnt++]; } double ans = b[1]+b[n]; for(int i = 1;i < n;i++) ans += 2*sqrt(R*(b[i]+b[i+1]-R)); cout << fixed << setprecision(2) << ans << endl; return 0; }
1237.二分。
#include<bits/stdc++.h> using namespace std; int n,maxx = 0,cnt[1000005] = {0}; bool ok(int x) { if(x < maxx) return 0; if(x-maxx > 30) return 1; int now = 1; for(int i = 0;i <= x;i++) { if(now > 1e6) break; if(cnt[x-i] > now) return 0; now -= cnt[x-i]; now *= 2; } return 1; } int main() { scanf("%d",&n); for(int i = 1;i <= n;i++) { int x; scanf("%d",&x); cnt[x]++; maxx = max(maxx,x); } int l = 0,r = 1e6+50; while(l < r) { int mid = (l+r)/2; if(ok(mid)) r = mid; else l = mid+1; } printf("%d\n",l); return 0; }
标签:技术分享 i++ color erase 大小 open ras 记忆 长度
原文地址:http://www.cnblogs.com/zhurb/p/7253277.html