17国庆day4
题目:链接
题解:链接
Candle Box
整场就写了这一道题还手残交错题WA两发...
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() { 5 int d, r, t; 6 scanf("%d %d %d", &d, &r, &t); 7 for(int i = 4; i < 100; i++){ 8 int a = (i+4)*(i-3)/2; 9 int b = (i+3-d)*(i-d-2)/2; 10 if(a+b == r+t) { 11 printf("%d\n", r-a); 12 return 0; 13 } 14 } 15 }
Balls and Needles
睡前补道题过得还比较干脆~
场上没有想到用set处理重边......而且把坐标映射成整数写起来也方便很多~
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 50010; 4 struct Edge{ 5 int u,v; 6 Edge(int a=0, int b=0) { 7 u = min(a, b); 8 v = max(a, b); 9 } 10 }e; 11 bool operator < (const Edge &a, const Edge &b){ 12 if(a.u!=b.u) return a.u < b.u; 13 return a.v < b.v; 14 } 15 set<Edge> s; 16 int p2[maxn][2], p3[maxn][2]; 17 int f[maxn<<1]; 18 int n; 19 map<int,int> mp3, mp2; 20 int cnt3, cnt2; 21 int gf(int x) { 22 return x==f[x] ? x:f[x] = gf(f[x]); 23 } 24 int ck1(){ 25 for(int i = 0; i < n*2; i++) f[i] = i; 26 for(int i = 0; i < n; i++){ 27 int a = mp3[p3[i][0]]; 28 int b = mp3[p3[i][1]]; 29 int pa = gf(a); 30 int pb = gf(b); 31 if(pa == pb) { 32 return 0; 33 } else{ 34 f[pa] = pb; 35 } 36 } 37 return 1; 38 } 39 int ck2(){ 40 s.clear(); 41 for(int i = 0; i < n*2; i++) f[i] = i; 42 for(int i = 0; i < n; i++){ 43 int a = mp2[p2[i][0]]; 44 int b = mp2[p2[i][1]]; 45 if(!s.count(Edge(a,b)) && a!=b) { 46 s.insert(Edge(a,b)); 47 int pa = gf(a); 48 int pb = gf(b); 49 if(pa == pb) { 50 return 0; 51 } else{ 52 f[pa] = pb; 53 } 54 } 55 } 56 return 1; 57 } 58 int main() { 59 // freopen("in.txt", "r", stdin); 60 while(scanf("%d", &n)!=EOF){ 61 cnt3 = cnt2 = 0; 62 for(int i = 0; i < n; i++){ 63 for(int j = 0; j < 2; j++){ 64 int a, b, c; 65 scanf("%d %d %d", &a, &b, &c); 66 p3[i][j] = a*1000000 + b*1000 + c; 67 if(!mp3.count(p3[i][j])) mp3[p3[i][j]] = cnt3++; 68 p2[i][j] = a*1000 + b; 69 if(!mp2.count(p2[i][j])) mp2[p2[i][j]] = cnt2++; 70 } 71 } 72 if(ck1()) puts("No true closed chains"); 73 else puts("True closed chains"); 74 if(ck2()) puts("No floor closed chains"); 75 else puts("Floor closed chains"); 76 } 77 return 0; 78 }
The White Rabbit Pocket Watch
高斯消元 + 最短路
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 struct Edge{ 5 int u, v; 6 Edge(int u=0, int v=0) : u(u), v(v){} 7 }; 8 const int inf=0x3f3f3f3f; 9 const int mod=13; 10 const int maxn=510; 11 int a[maxn][maxn]; //系数 12 int x[maxn],free_x[maxn]; 13 14 int gcd(int a,int b){ 15 return b==0?a:gcd(b,a%b); 16 } 17 int lcm(int a,int b){ 18 return a/gcd(a,b)*b; 19 } 20 void exgcd(int a,int b,int &d,int &x,int &y){ 21 if(!b){d=a;x=1;y=0;} 22 else {exgcd(b,a%b,d,y,x); y-=x*(a/b);} 23 } 24 25 //n个方程m个未知量 26 int gauss(int n,int m){ 27 int r,c; 28 int num=0; //自由变元 29 for(r=0,c=0;r<n&&c<m;c++){ 30 int max_r=r; 31 for(int i=r+1;i<n;i++) if(abs(a[i][c]) > abs(a[max_r][c])) max_r=i; 32 if(max_r!=r) for(int j=c;j<=m;j++) swap(a[r][j],a[max_r][j]); 33 if(!a[r][c]) {free_x[num++]=c;continue;} // 34 for(int i=r+1;i<n;i++) if(a[i][c]){ 35 int d=lcm(abs(a[i][c]),abs(a[r][c])); 36 int t1=d/a[i][c],t2=d/a[r][c]; 37 for(int j=c;j<=m;j++) a[i][j]=((a[i][j]*t1-a[r][j]*t2)%mod+mod)%mod; 38 39 } 40 r++; 41 } 42 for(int i=r-1;i>=0;i--){ 43 x[i]=a[i][m]; 44 for(int j=i+1;j<m;j++){ 45 x[i]=((x[i]-a[i][j]*x[j])%mod+mod)%mod; 46 } 47 int x1,y1,d; 48 exgcd(a[i][i],mod,d,x1,y1); 49 x1=((x1%mod)+mod)%mod; 50 x[i]=x[i]*x1%mod; 51 } 52 } 53 int d[maxn][maxn]; 54 map<int, Edge> mp; 55 int n, S, T, m; 56 57 int dis[maxn]; 58 int vis[maxn]; 59 int dijkstra(int s, int t){ 60 memset(dis, inf, sizeof(dis)); 61 memset(vis, 0, sizeof(vis)); 62 dis[s] = 0; 63 for(int i = 0; i < n; i++){ 64 int u, md = inf; 65 for(int v = 1; v <= n; v++){ 66 if(!vis[v] && dis[v] <= md) md = dis[u=v]; 67 } 68 vis[u] = 1; 69 for(int v = 1; v <= n; v++) if(u != v){ 70 dis[v] = min(dis[v], dis[u]+d[u][v]); 71 } 72 } 73 return dis[t]; 74 } 75 76 int main(){ 77 // freopen("in.txt", "r", stdin); 78 mp.clear(); 79 int cnt = 0; 80 memset(d, -1, sizeof(d)); 81 scanf("%d %d %d %d", &n, &S, &T, &m); 82 for(int i = 0; i < m; i++){ 83 scanf("%d", &a[i][m]); 84 int k, u, v; 85 scanf("%d", &k); 86 scanf("%d", &u); 87 for(int j = 1; j < k; j++){ 88 scanf("%d", &v); 89 if(u==v) continue; 90 int ea = min(u, v); 91 int eb = max(u, v); 92 if(d[ea][eb] == -1){ 93 mp[cnt] = Edge(ea,eb); 94 d[ea][eb] = cnt++; 95 } 96 a[i][d[ea][eb]]++; 97 if(a[i][d[ea][eb]] >= mod)a[i][d[ea][eb]]%=mod; 98 u = v; 99 } 100 } 101 for(int i = 0; i < m; i++) { 102 a[i][cnt] = a[i][m]; 103 } 104 105 gauss(m, cnt); 106 /* 107 for(int i = 0; i < cnt; i++){ 108 cout<<x[i]<<endl; 109 // cout<<mp[i].u<<" "<<mp[i].v<<endl; 110 } 111 */ 112 memset(d, inf, sizeof(d)); 113 for(int i = 0; i < cnt; i++){ 114 int u = mp[i].u; 115 int v = mp[i].v; 116 if(x[i]) d[u][v] = d[v][u] = x[i]; 117 } 118 int ans = dijkstra(S, T); 119 printf("%d\n", ans); 120 }
Passwords
ac自动机 + DP
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int sigma = 26; 4 const int maxnode = 1010; 5 const int mod = 1000003; 6 int dp[21][maxnode][8]; 7 struct AC{ 8 int ch[maxnode][sigma]; 9 int f[maxnode]; 10 int mc[maxnode]; 11 int sz; 12 int yy[11]; 13 14 void init(){ 15 memset(ch[0], 0, sizeof(ch[0])); 16 memset(mc, 0, sizeof(mc)); 17 memset(yy, -1, sizeof(yy)); 18 sz = 1; 19 yy[0] = 'o' - 'a'; 20 yy[1] = 'i' - 'a'; 21 yy[3] = 'e' - 'a'; 22 yy[5] = 's' - 'a'; 23 yy[7] = 't' - 'a'; 24 } 25 26 int idx(char c) { 27 return c-'a'; 28 } 29 30 void insert(char *s) { 31 int n = strlen(s), u = 0; 32 for(int i = 0; i < n; i++){ 33 int c = idx(s[i]); 34 if(!ch[u][c]){ 35 memset(ch[sz], 0, sizeof(ch[sz])); 36 ch[u][c] = sz++; 37 } 38 u = ch[u][c]; 39 } 40 mc[u] = 1; 41 } 42 43 void getfail(){ 44 queue<int> q; 45 f[0] = 0; 46 for(int c = 0; c < sigma; c++){ 47 int u = ch[0][c]; 48 if(u) { 49 q.push(u); 50 f[u] = 0; 51 } 52 } 53 while(!q.empty()){ 54 int r = q.front(); 55 q.pop(); 56 for(int c = 0; c < sigma; c++){ 57 int u = ch[r][c]; 58 if(!u){ 59 ch[r][c] = ch[f[r]][c]; 60 continue; 61 } 62 q.push(u); 63 int v = f[r]; 64 while(v && !ch[v][c]) v = f[v]; 65 f[u] = ch[v][c]; 66 mc[u] |= mc[f[u]]; 67 } 68 } 69 } 70 }ac; 71 72 void add(int &a, int b){ 73 a = a+b; 74 if(a < 0) a += mod; 75 if(a >= mod) a -= mod; 76 } 77 78 void solve(int a, int b) { 79 memset(dp, 0, sizeof(dp)); 80 dp[0][0][0] = 1; 81 for(int i = 1; i <= b; i++){ 82 for(int j = 0; j < ac.sz; j++){ 83 if(ac.mc[j]) continue; 84 for(int k = 0; k < 8; k++){ 85 // if(dp[i-1][j][k]==0) continue; 86 for(int c = 0; c < sigma; c++){ 87 int u = ac.ch[j][c]; 88 if(ac.mc[u]) continue; 89 add(dp[i][u][k|(1<<0)], dp[i-1][j][k]); 90 add(dp[i][u][k|(1<<1)], dp[i-1][j][k]); 91 } 92 for(int c = 0; c < 10; c++){ 93 if(ac.yy[c] == -1){ 94 add(dp[i][0][k|(1<<2)], dp[i-1][j][k]); 95 } else { 96 int u = ac.ch[j][ac.yy[c]]; 97 if(ac.mc[u]) continue; 98 add(dp[i][u][k|(1<<2)], dp[i-1][j][k]); 99 } 100 } 101 } 102 } 103 } 104 int ans = 0; 105 for(int i = a; i <= b; i++){ 106 for(int j = 0; j < ac.sz; j++){ 107 add(ans, dp[i][j][7]); 108 } 109 } 110 printf("%d\n", ans); 111 } 112 113 int main(){ 114 int a, b, n; 115 // freopen("in.txt", "r", stdin); 116 scanf("%d %d %d", &a, &b, &n); 117 ac.init(); 118 char s[21]; 119 for(int i = 0; i < n; i++){ 120 scanf("%s", s); 121 ac.insert(s); 122 } 123 ac.getfail(); 124 solve(a, b); 125 return 0; 126 }