日常是搬砖砖写写题划划水摸摸鱼看动画片画萌妹纸
以及好多好多好多好多的坑www
3.5
offer8
A.小Ho的强迫症
gcd,感觉对这种东西一点都不敏感阿之前那个青蛙跳石头的容斥也是
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 const int maxn = 1e5+5; 8 9 int gcd(int a, int b) 10 { 11 return a % b ? gcd(b, a % b) : b; 12 } 13 14 int main() 15 { 16 int T; 17 scanf("%d",&T); 18 while(T--) 19 { 20 int l,f,d; 21 scanf("%d %d %d",&l,&f,&d); 22 int g = gcd(l,d); 23 if(f <= g) puts("YES"); 24 else puts("NO"); 25 } 26 return 0; 27 }
B.拆字游戏
dfs输出读题要仔细
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 const int maxn = 1e5+5; 8 9 char g[505][505]; 10 int n,m,vis[505][505]; 11 int xmn,xmx,ymx,ymn,ax,ay; 12 int a,b,c,d; 13 int dx[4] = {-1,1,0,0}; 14 int dy[4] = {0,0,1,-1}; 15 16 bool in(int x,int y) {return x >= 1 && x <= n && y >= 1 && y <= m;} 17 18 struct node{ 19 int xmn,xmx,ymx,ymn; 20 int ax,ay; 21 int col; 22 }p[505*505]; 23 24 int cmp(node n1,node n2) 25 { 26 if(n1.ay != n2.ay) return n1.ay < n2.ay; 27 return n1.ax < n2.ax; 28 } 29 30 int tot; 31 32 void dfs(int x,int y) 33 { 34 vis[x][y] = tot; 35 if(y < ay) {ax = x;ay = y;} 36 if(y == ay) 37 { 38 if(x < ax) {ax= x;ay = y;} 39 } 40 xmn = min(xmn,x);xmx = max(xmx,x); 41 ymn = min(ymn,y);ymx = max(ymx,y); 42 // printf("x = %d y = %d\n",x,y); 43 for(int i = 0;i < 4;i++) 44 { 45 int xx = x + dx[i]; 46 int yy = y + dy[i]; 47 if(!in(xx,yy)) continue; 48 if(g[xx][yy] == '0' || vis[xx][yy]) continue; 49 dfs(xx,yy); 50 } 51 } 52 53 void print(int xmn,int ymx,int xmx,int ymn,int col) 54 { 55 printf("%d %d\n",xmx-xmn+1,ymx-ymn+1); 56 for(int i = xmn;i <= xmx;i++) 57 { 58 for(int j = ymn;j <= ymx;j++) 59 { 60 if(vis[i][j] == col) printf("%c",g[i][j]); 61 else printf("0"); 62 } 63 printf("\n"); 64 } 65 } 66 67 int main() 68 { 69 scanf("%d %d",&n,&m); 70 for(int i = 1;i <= n;i++) scanf("%s",g[i]+1); 71 memset(vis,0,sizeof(vis)); 72 tot = 0; 73 for(int i = 1;i <= n;i++) 74 { 75 for(int j = 1;j <= m;j++) 76 { 77 if(g[i][j] == '1' && vis[i][j] == 0) 78 { 79 // printf("g[%d][%d] = %c\n",i,j,g[i][j]); 80 xmn = 1000;ymn = 1000; 81 xmx = 0;ymx = 0; 82 ax = 1000;ay = 1000; 83 tot++; 84 dfs(i,j); 85 p[tot].xmx = xmx; 86 p[tot].xmn = xmn; 87 p[tot].ymx = ymx; 88 p[tot].ymn = ymn; 89 p[tot].col = tot; 90 p[tot].ax = ax; 91 p[tot].ay = ay; 92 //print(xmn,ymx,xmx,ymn); 93 94 } 95 } 96 } 97 sort(p+1,p+tot+1,cmp); 98 for(int i = 1;i <= tot;i++) print(p[i].xmn,p[i].ymx,p[i].xmx,p[i].ymn,p[i].col); 99 // printf("tot = %d\n",tot); 100 101 return 0; 102 }
C.数组分拆
暴力的n2 dp就是见过很多次的dp[i]以i结尾的怎样怎样,枚举上一段结尾的为j来转移,这个的转化可以变成加上前缀和不等于当前前缀和的所有dp值
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <map> 6 using namespace std; 7 typedef long long LL; 8 const int maxn = 5e6+5; 9 const LL mod = 1e9+7; 10 11 int n,a[maxn]; 12 LL dp[maxn]; 13 14 int main() 15 { 16 scanf("%d",&n); 17 for(int i = 1;i <= n;i++) scanf("%d",&a[i]); 18 map<int,int> h; 19 map<int,long long> f; 20 LL all = 0LL; 21 int sum = 0; 22 f[0] = 1LL;dp[0] = 1LL; 23 for(int i = 1;i <= n;i++) 24 { 25 all = (all + dp[i-1]) % mod; 26 sum = sum + a[i]; 27 dp[i] = (all - f[sum] + mod) % mod; 28 f[sum] = (f[sum] + dp[i]) % mod; 29 } 30 //for(int i = 1;i <= n;i++) printf("dp[%d] = %lld\n",i,dp[i]); 31 printf("%lld\n",dp[n]); 32 return 0; 33 }
D.矩形计数
容斥,仍然是对这种东西不敏感T.T
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 const int maxn = 1e5+5; 8 const int INF = 1e9+7; 9 10 int x[maxn],y[maxn],n,m,k; 11 12 int main() 13 { 14 scanf("%d %d %d",&n,&m,&k); 15 for(int i = 0;i < k;i++) scanf("%d %d",&x[i],&y[i]); 16 LL ans = (LL) (n*(n+1)/2 ) * (LL) (m*(m+1)/2); 17 //printf("ans = %lld\n",ans); 18 for(int i = 1;i < (1<<k);i++) 19 { 20 int xmin = INF,ymin = INF,xmax = 0,ymax = 0,cnt = 0; 21 for(int j = 0;j < k;j++) 22 { 23 if(i & (1<<j)) 24 { 25 cnt++; 26 xmin = min(xmin,x[j]);ymin = min(ymin,y[j]); 27 xmax = max(xmax,x[j]);ymax = max(ymax,y[j]); 28 } 29 } 30 LL tmp = (LL) (ymin * (m - ymax + 1)) * (LL)(xmin * (m - xmax + 1)); 31 //printf("i = %d xmin = %d ymin = %d xmax = %d ymax = %d tmp = %lld\n",i,xmin,ymin,xmax,ymax,tmp); 32 if(cnt&1) ans -= tmp; 33 else ans += tmp; 34 } 35 printf("%lld\n",ans); 36 return 0; 37 }
3.12
排序
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 const int maxn = 1e5+5; 8 9 int a[maxn],n,c,k; 10 11 int main() 12 { 13 scanf("%d %d %d",&n,&c,&k); 14 for(int i = 1;i <= n;i++) scanf("%d",&a[i]); 15 sort(a+1,a+n+1); 16 int ans = 1; 17 int ed = a[1] + k,cnt = 1; 18 for(int i = 2;i <= n;i++) 19 { 20 if(a[i] > ed || cnt == c) {ans++;ed = a[i]+k;cnt = 0;} 21 cnt++; 22 //printf("i = %d cnt = %d ans = %d\n",i,cnt,ans); 23 } 24 printf("%d\n",ans); 25 return 0; 26 }
感觉贪心还是需要证明清楚才敢写..
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 const int maxn = 1e5+5; 8 9 int n; 10 LL a[maxn],sum[maxn]; 11 12 int main() 13 { 14 scanf("%d",&n); 15 memset(sum,0,sizeof(sum)); 16 for(int i = 1;i <= n;i++) scanf("%lld",&a[i]); 17 sort(a+1,a+n+1); 18 for(int i = 1;i <= n;i++) sum[i] = sum[i-1] + a[i]; 19 int mx = 0; 20 for(int i = 1;i < n;i++) 21 { 22 if(2*sum[i] < a[i+1]) mx = max(mx,i); 23 } 24 printf("%d\n",n-mx); 25 return 0; 26 }
tls 在群里说教教大家 组合数取模 先码在这里>.<
offer 9
整场暴力骗分QwQ,要补题的喔!恩恩!
A.闰秒
这题得80..不知道错哪儿..不想补了TwT
B.水陆距离
bfs把所有的0当起点
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 typedef long long LL; 8 const int maxn = 1e5+5; 9 const int INF = 1e9+7; 10 int d[1005][1005],n,m; 11 char g[1005][1005]; 12 int dx[4] = {1,-1,0,0}; 13 int dy[4] = {0,0,1,-1}; 14 15 int in(int x,int y) {return x >= 1 && x <= n && y >= 1 && y <= m;} 16 17 void bfs() 18 { 19 queue<pair<int,int> > q; 20 for(int i = 1;i <= n;i++) 21 { 22 for(int j = 1;j <= m;j++) 23 { 24 if(g[i][j] == '0') 25 { 26 d[i][j] = 0; 27 q.push(make_pair(i,j)); 28 } 29 else d[i][j] = INF; 30 } 31 } 32 while(!q.empty()) 33 { 34 pair<int,int> u = q.front();q.pop(); 35 int x = u.first,y = u.second; 36 for(int i = 0;i < 4;i++) 37 { 38 int xx = x + dx[i]; 39 int yy = y + dy[i]; 40 if(!in(xx,yy)) continue; 41 if(g[xx][yy] == '0') continue; 42 if(d[x][y]+1 < d[xx][yy]) 43 { 44 d[xx][yy] = d[x][y] + 1; 45 q.push(make_pair(xx,yy)); 46 } 47 48 } 49 } 50 } 51 52 int main() 53 { 54 scanf("%d %d",&n,&m); 55 for(int i = 1;i <= n;i++) scanf("%s",g[i]+1); 56 bfs(); 57 for(int i = 1;i <= n;i++) 58 { 59 for(int j = 1;j < m;j++) printf("%d ",d[i][j]); 60 printf("%d\n",d[i][m]); 61 } 62 return 0; 63 }
C.三等分
过这么久才补..
内嵌2/3,分别1/3
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 typedef long long LL; 8 const int maxn = 1e5+5; 9 int n,m; 10 vector<int> g[maxn]; 11 int a[maxn],s[maxn],ave[maxn],p[maxn],part,root; 12 LL ans; 13 14 void dfs(int u,int fa) 15 { 16 s[u] = a[u]; 17 ave[u] = 0; 18 for(int i = 0;i < g[u].size();i++) 19 { 20 int v = g[u][i]; 21 if(v == fa) continue; 22 dfs(v,u); 23 s[u] += s[v]; 24 ave[u] += ave[v]; 25 } 26 if(u == root) return; 27 if(s[u] == 2 * part) 28 { 29 ans += ave[u]; 30 } 31 if(s[u] == part) 32 { 33 ans -= ave[u]; 34 ave[u]++; 35 } 36 } 37 38 int main() 39 { 40 int T; 41 scanf("%d",&T); 42 while(T--) 43 { 44 scanf("%d",&n); 45 for(int i = 1;i <= n;i++) g[i].clear(); 46 int sum = 0; 47 for(int i = 1;i <= n;i++) 48 { 49 scanf("%d %d",&a[i],&p[i]); 50 if(p[i] == 0) root = i; 51 else g[i].push_back(p[i]),g[p[i]].push_back(i); 52 sum += a[i]; 53 } 54 if(sum%3) {puts("0");continue;} 55 ans = 0LL;part = sum/3; 56 dfs(root,-1); 57 ans += 1LL * ave[root] * (ave[root] - 1) / 2; 58 printf("%lld\n",ans); 59 } 60 return 0; 61 }
D.矩阵填数
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 const int maxn = 1e4 + 10; 6 const LL mod = 1e9 + 7; 7 LL fac[maxn]; 8 9 LL qpow(LL a, LL b) 10 { 11 LL ret = 1LL; 12 while(b) 13 { 14 if(b & 1) ret = ret * a % mod; 15 a = a * a % mod; 16 b >>= 1; 17 } 18 return ret; 19 } 20 21 LL inv(LL x) 22 { 23 return qpow(x, mod - 2); 24 } 25 26 27 int main() 28 { 29 int n,m; 30 scanf("%d %d",&n,&m); 31 LL ans = 1LL; 32 for(int i = 1;i <= n*m;i++) ans = ans * i % mod; 33 for(int i = 1;i <= n;i++) 34 { 35 for(int j = 1;j <= m;j++) 36 { 37 ans = ans * inv(i+j-1) % mod; 38 } 39 } 40 printf("%lld\n",ans); 41 return 0; 42 }
3.16
cf 404 not find !
于是先看c二分wa整场,主要是check的时候没有再去转化一下,然后直接求和爆long long 还有边界二分完出来再判一下,还是不会二分边界TAT
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 typedef long long LL; 8 const int maxn = 1e6+5; 9 10 LL n,m; 11 LL pre; 12 13 LL ok(LL x) 14 { 15 LL res = 0LL; 16 if(x%2 == 0) res = x/2LL * (x+1); 17 else res = (x+1)/2 * x; 18 return res; 19 } 20 21 int main() 22 { 23 scanf("%I64d %I64d",&n,&m); 24 if(n <= m) {printf("%I64d\n",n);return 0;} 25 LL lb = 0,ub = 2e9,mid,ans = 0; 26 while(ub-lb > 1) 27 { 28 mid = lb + (ub - lb) / 2; 29 if(ok(mid) + m <= n) lb = mid,ans = max(ans,mid); 30 else ub = mid; 31 //printf("lb = %I64d ub = %I64d mid = %I64d ans = %I64d\n",lb,ub,mid,ans); 32 } 33 if(ok(lb) + m == n) ans = m + lb; 34 else ans = m + lb + 1; 35 printf("%I64d\n",ans); 36 return 0; 37 }
3.18
cf 405div2