Codeforces Round #174 (Div. 2)
A题 模拟题 比赛是 sb 了
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 int main( ) 8 { 9 int N,res,k; 10 while( scanf("%d",&N) != EOF ) 11 { 12 res = 0; 13 for( int i = 1; i < N; i++ ) 14 { 15 bool fell = false; k = 1; 16 for( int j = 1; j < N-1; j++ ) 17 { 18 k *= i; k %= N; 19 if( k == 1 ) 20 { 21 fell = true; 22 break; 23 } 24 } 25 if( !fell && k*i%N == 1 ) 26 res++; 27 } 28 printf("%d\n",res); 29 } 30 return 0; 31 }
B题 模拟题 关键是读懂题目
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 7 8 char str[212345]; 9 10 int main( ) 11 { 12 int N; 13 while( scanf("%d",&N) != EOF ) 14 { 15 scanf("%s",&str); 16 int len = strlen(str); 17 int a = 0,b = 0,c = 0; 18 for( int i = 0; i < len; i++ ) 19 if( str[i] == 'A') a++ ; 20 else if( str[i] == 'F')b++; 21 else if( str[i] == 'I')c++; 22 if( b == len || c >= 2 ) 23 { 24 cout<<0<<endl; 25 } 26 else 27 if( c != 0 ) 28 cout<<1<<endl; 29 else cout<<a<<endl; 30 } 31 return 0; 32 }
C 题 比赛后才想出来 sb了
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 long long arr[212345],cnt[212345]; 8 9 int main( ) 10 { 11 int N,k,t; 12 long long a,b,ans,res; 13 scanf("%d",&N); 14 memset(cnt,0,sizeof(cnt)); 15 res = 0; ans = 1;k = 1; arr[1] = 0; 16 while( N-- ) 17 { 18 scanf("%d",&t); 19 if( t == 1 ) 20 { 21 scanf("%I64d%I64d",&a,&b); 22 res += (a*b); 23 cnt[a] += b; 24 printf("%.6lf\n",double(res*1.0/ans*1.0)); 25 continue; 26 } 27 if( t == 2 ) 28 { 29 scanf("%I64d",&arr[++k]); 30 res += arr[k]; 31 ans++; 32 printf("%.6lf\n",double(res*1.0/ans*1.0)); 33 continue; 34 } 35 if( t == 3 && k >= 2 ) 36 { 37 res = res - arr[k] - cnt[k]; 38 cnt[k-1] += cnt[k]; cnt[k] = 0; 39 ans--; 40 k--; 41 printf("%.6lf\n",double(res*1.0/ans*1.0)); 42 } 43 } 44 return 0; 45 }
D题 记忆化搜索 关键是 记住三个状态
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 long long arr[212345]; 8 long long num[212345][3],N,ans,res; 9 10 bool stu[212345][3]; 11 bool vis[212345][3]; 12 13 long long DFS( long long x,long long y,int tab ) 14 { 15 if( x <= 0 || x > N ) { res = y; return ans = 0; } 16 if( vis[x][tab] ) 17 { 18 if( num[x][tab] == -1 ) return res = -1; 19 res = num[x][tab] + y; 20 return ans = num[x][tab]; 21 } 22 if( stu[x][tab] ){ res = -1;vis[x][tab] = true; num[x][tab] = -1; return -1;} 23 stu[x][tab] = true; 24 if( !tab ) 25 { 26 long long t = DFS( x-arr[x],y+arr[x],1 ); 27 vis[x][0] = true; 28 if( t != -1 ) ans += arr[x]; 29 return num[x][0] = ans; 30 } 31 else 32 { 33 long long t = DFS( x+arr[x],y+arr[x],0 ); 34 vis[x][1] = true; 35 if( t != -1 ) ans += arr[x]; 36 return num[x][1] = ans; 37 } 38 return 0; 39 } 40 41 int main( ) 42 { 43 int i,x,y; 44 scanf("%I64d",&N); 45 for( i = 2; i <= N; i++ ) 46 scanf("%I64d",&arr[i]); 47 memset( vis,false,sizeof(vis) ); 48 memset( stu,false,sizeof(stu) ); 49 memset( num,0,sizeof(num) ); 50 for( i = 1; i < N; i++ ) 51 { 52 arr[1] = i; x = 1 + i; y = i;ans = -1; DFS( x,y,0 ); 53 printf("%I64d\n",res); 54 } 55 return 0; 56 }
E题 好题,,,看来N 篇解题报告才出来 还有 一个就是 题目真的很坑啊! 可能是没有读通题目,后来把自己代码换了一个地方就过了;原理没有改变;真是 坑爹的;
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int inf = 1000000007; 8 int next[330],pre[330],arr[330],stu[440],h[330],va,wa,k; 9 int dp[212345]; 10 int vis[330],vic[330]; 11 bool fell; 12 13 void dfs(int u) 14 { 15 if( pre[u] == 0) 16 { 17 vis[u] = 2; 18 h[u] = 1; 19 return; 20 } 21 if( vis[pre[u]] == 1 ) 22 { 23 fell = false; 24 return; 25 } 26 vis[u] = 1; 27 if( vis[pre[u]] == 0 ) 28 dfs( pre[u] ); 29 if ( !fell ) return; 30 h[u] = h[pre[u]] + 1; 31 vis[u] = 2; 32 } 33 34 void DFS( int sta ) 35 { 36 if( vic[sta] ) return ; 37 va += arr[sta]; 38 stu[sta] = va; 39 vic[sta] = true; 40 if( pre[sta] && next[pre[sta]] == sta ) DFS( pre[sta] ); 41 return ; 42 } 43 44 int main( ) 45 { 46 int N,M,x,y,T; 47 while( scanf("%d%d%d",&N,&M,&T) != EOF ) 48 { 49 for( int i = 1; i <= N; i++ ) 50 scanf("%d",&arr[i]); 51 memset( pre,0,sizeof(pre) ); 52 memset( next,0,sizeof(next) ); 53 for( int i = 1; i <= M; i++ ) 54 { 55 scanf("%d%d",&x,&y); 56 next[y] = x;pre[x] = y; 57 } 58 fell = true; 59 memset( vis,0,sizeof(vis) ); 60 memset( h,0,sizeof(h)); 61 for (int i = 1; i <= N; i++) 62 if( vis[i] == 0 && fell ) dfs(i); 63 if (fell) 64 { 65 for ( int i = 1; i <= N; i++) 66 { 67 T -= ( h[i] - 1) * arr[i]; 68 if (T < 0) break; 69 } 70 if (T < 0) fell = false; 71 } 72 73 if (!fell) 74 { 75 printf("0\n"); 76 continue; 77 } 78 memset( vic,false,sizeof(vic) ); 79 for( int i = 1; i <= N; i++ ) 80 if( next[i] == 0 ) 81 { 82 va = 0; DFS( i ); 83 } 84 85 memset( dp,0,sizeof(dp) ); 86 dp[0] = 1; 87 for( int i = 1; i <= N; i++ ) 88 { 89 for( int j = stu[i]; j <= T; j++ ) 90 { 91 long long t = (dp[j] + dp[j-stu[i]]); 92 if( t >= inf ) t -= inf; 93 dp[j] = t; 94 } 95 } 96 printf("%d\n",dp[T]); 97 } 98 return 0; 99 }