天下一プログラマ 予選A
A:フィボナッチ
#include<stdio.h> #include "stdafx.h" int main() { int num; scanf("%d",&num); long long a=1,b=1; for(int i=0;i<num;i++) { long long c=a+b; a=b; b=c; } printf("%lld\n",a); }
B:やるだけ。なのに2WA
#include<stdio.h> #include "stdafx.h" int main() { char str[10000]; gets(str); int han=0; for(int i=0;;i++) { if(str[i]=='\0') { break; } if(str[i]==' ') { han=1; } else { if(han==0) { printf("%c",str[i]); } else { printf(",%c",str[i]); han=0; } } } }
C:Mがクッソ小さいのはわかったがそこから進まない。詰む。
とりあえず部分点をとる。
#include<stdio.h> #include "stdafx.h" #include<algorithm> #include<vector> #include<string> #include<iostream> #include<stack> using namespace std; bool tek[100][100]; int main() { int cit,way; scanf("%d%d",&cit,&way); for(int i=0;i<way;i++) { int za,zb; scanf("%d%d",&za,&zb); tek[zb-1][za-1]=true; } string str; cin>>str; stack<bool>sta; for(;;) { if(str.substr(str.size()-3)=="\"ww") { sta.push(1); str=str.substr(1,str.size()-4); } else { if(str[str.size()-1]=='\"') { sta.push(0); str=str.substr(1,str.size()-2); } else { break; } } } str=str.substr(5); if(str[str.size()-1]=='w') { str=str.substr(0,str.size()-1); sta.push(1); } else { sta.push(0); } int gro=0; for(int i=0;i<str.size();i++) { gro*=10; gro+=str[i]-'0'; } gro--; bool dp[100]; bool newdp[100]; fill(dp,dp+100,false); fill(newdp,newdp+100,false); dp[gro]=true; for(;;) { if(sta.empty()) { break; } for(int i=0;i<cit;i++) { for(int j=0;j<cit;j++) { if(dp[i]) { if(tek[i][j]==sta.top()) { newdp[j]=true; } } } } sta.pop(); for(int i=0;i<cit;i++) { dp[i]=newdp[i]; newdp[i]=0; } } int ret=0; for(int i=0;i<cit;i++) { if(dp[i]) { ret++; } } printf("%d\n",ret); }
D:満点はあきらめておとなしく部分点取りに行く。メモ化探索。ロリハでメモ化とかいう謎なことをした。
#include<stdio.h> #include "stdafx.h" #include<algorithm> #include<vector> #include<map> using namespace std; int num; typedef long long ll; map<ll,double>ma; ll mod=1000000007; double dfs(vector<vector<int> >map,int nx,int ny,int hou,int dep,int kai) { if(nx==num&&ny==num) { return kai; } if(dep>30) { return 0; } ll hash=0; for(int i=0;i<num+2;i++) { for(int j=0;j<num+2;j++) { hash*=mod; hash+=map[i][j]; } } hash*=mod; hash+=nx; hash*=mod; hash+=ny; hash*=mod; hash+=hou; if(ma.find(hash)!=ma.end()) { return ma[hash]; } double ret=0; if(hou==0) { if(map[nx-1][ny]==0) { ret+=dfs(map,nx-1,ny,hou,dep,kai); } else { if(map[nx][ny-1]==0) { if(map[nx][ny+1]==0) { ret+=dfs(map,nx,ny-1,1,dep+1,kai)/2.0+dfs(map,nx,ny+1,3,dep+1,kai)/2.0; } else { ret+=dfs(map,nx,ny-1,1,dep,kai); } } else { if(map[nx][ny+1]==0) { ret+=dfs(map,nx,ny+1,3,dep,kai); } else { map[nx][ny]=1; ret+=dfs(map,1,1,3,dep,kai+1); } } } } if(hou==2) { if(map[nx+1][ny]==0) { ret+=dfs(map,nx+1,ny,hou,dep,kai); } else { if(map[nx][ny-1]==0) { if(map[nx][ny+1]==0) { ret+=dfs(map,nx,ny-1,1,dep+1,kai)/2.0+dfs(map,nx,ny+1,3,dep+1,kai)/2.0; } else { ret+=dfs(map,nx,ny-1,1,dep,kai); } } else { if(map[nx][ny+1]==0) { ret+=dfs(map,nx,ny+1,3,dep,kai); } else { map[nx][ny]=1; ret+=dfs(map,1,1,3,dep,kai+1); } } } } if(hou==1) { if(map[nx][ny-1]==0) { ret+=dfs(map,nx,ny-1,hou,dep,kai); } else { if(map[nx-1][ny]==0) { if(map[nx+1][ny]==0) { ret+=dfs(map,nx-1,ny,0,dep+1,kai)/2.0+dfs(map,nx+1,ny,2,dep+1,kai)/2.0; } else { ret+=dfs(map,nx-1,ny,0,dep,kai); } } else { if(map[nx+1][ny]==0) { ret+=dfs(map,nx+1,ny,2,dep,kai); } else { map[nx][ny]=1; ret+=dfs(map,1,1,3,dep,kai+1); } } } } if(hou==3) { if(map[nx][ny+1]==0) { ret+=dfs(map,nx,ny+1,hou,dep,kai); } else { if(map[nx-1][ny]==0) { if(map[nx+1][ny]==0) { ret+=dfs(map,nx-1,ny,0,dep+1,kai)/2.0+dfs(map,nx+1,ny,2,dep+1,kai)/2.0; } else { ret+=dfs(map,nx-1,ny,0,dep,kai); } } else { if(map[nx+1][ny]==0) { ret+=dfs(map,nx+1,ny,2,dep,kai); } else { map[nx][ny]=1; ret+=dfs(map,1,1,3,dep,kai+1); } } } } ma.insert(pair<ll,double>(hash,ret)); return ret; } int main() { vector<vector<int> >vec; scanf("%d",&num); for(int i=0;i<num+2;i++) { vector<int>damy; vec.push_back(damy); for(int j=0;j<num+2;j++) { vec[i].push_back(1); } } for(int i=0;i<num;i++) { for(int j=0;j<num;j++) { char zan; scanf(" %c",&zan); if(zan!='#') { vec[i+1][j+1]=0; } } } printf("%.10lf\n",dfs(vec,1,1,3,0,1)); }
Dで正の点数をとったのが自分含め6人だけだった。適当にメモ化探索したら通るのに。
その中で通過を決めなかったのは自分だけです。はい。
でもCの満点解とか答え聴いてもすっきり来ないしまあ妥当なのでは。hogloidさんぱない。