2020牛客寒假算法基础集训营3
A |
牛牛的DRB迷宫I |
思路:dp求方案数
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define MOD 1000000007 5 const int maxn=1e5+100; 6 ll dp[55][55]; 7 char s[55][55]; 8 int main() 9 { 10 int n,m; 11 scanf("%d%d",&n,&m); 12 dp[1][1]=1; 13 for(int i=1;i<=n;i++) 14 { 15 for(int j=1;j<=m;j++) 16 { 17 cin>>s[i][j]; 18 if(s[i][j]=='R') 19 dp[i][j+1]=(dp[i][j]+dp[i][j+1])%MOD; 20 else if(s[i][j]=='D') 21 dp[i+1][j]=(dp[i][j]+dp[i+1][j])%MOD; 22 else if(s[i][j]=='B') 23 { 24 dp[i][j+1]=(dp[i][j]+dp[i][j+1])%MOD; 25 dp[i+1][j]=(dp[i][j]+dp[i+1][j])%MOD; 26 } 27 } 28 } 29 cout<<dp[n][m]%MOD<<endl; 30 return 0; 31 }
C |
牛牛的数组越位 |
思路:模拟,不仅考虑<0还有>n,>m的情况,还有n*m与m*x+y的关系
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[1000010]; 4 5 6 int main() 7 { 8 int t; 9 int n,m,p; 10 int x,y,val; 11 cin >>t; 12 while(t--) 13 { 14 memset(a,0,sizeof(a)); 15 cin>>n>>m>>p; 16 int len=n*m; 17 int flag1=0; 18 int flag2=0; 19 while(p--) 20 { 21 22 cin>>x>>y>>val; 23 int num=x*m+y; 24 if(x<0||x>=n||y<0||y>=m)flag1=1; 25 26 if(num<0||num>=len) 27 { 28 flag2=1; 29 30 } 31 else 32 { 33 a[num]=val; 34 } 35 } 36 37 if(flag2) 38 { 39 cout<<"Runtime error"<<endl; 40 } 41 else 42 { 43 for(int i=0;i<len;i++) 44 { 45 cout<<a[i]<<' '; 46 if((i+1)%m==0)cout<<endl; 47 } 48 if(flag1) 49 { 50 cout<<"Undefined Behaviour"<<endl; 51 } 52 else 53 { 54 cout<<"Accepted"<<endl; 55 } 56 57 } 58 59 } 60 return 0; 61 }
D |
牛牛与二叉树的数组存储 |
思路:存位置即可,除去-1的情况
1 #include<iostream> 2 #include<cstdio> 3 #include<map> 4 using namespace std; 5 int a[100005]; 6 int main() 7 { 8 int n; 9 scanf("%d",&n); 10 map<int,int> m; 11 int cnt=0; 12 for(int i=1;i<=n;i++) 13 { 14 scanf("%d",&a[i]); 15 if(a[i]>0) 16 { 17 cnt++; 18 m[a[i]]=i; 19 } 20 } 21 printf("The size of the tree is %d\n",cnt); 22 printf("Node %d is the root node of the tree\n",a[1]); 23 for(int i=1;i<=cnt;i++) 24 { 25 printf("The father of node %d is %d,",i,m[i]/2==0?-1:a[m[i]/2]); 26 printf(" the left child is %d, and the right child is %d\n",m[i]*2>n?-1:a[m[i]*2],m[i]*2+1>n?-1:a[m[i]*2+1]); 27 } 28 return 0; 29 }
F |
牛牛的Link Power I |
思路:刚开始想前缀和,算了一遍,没想到要两遍前缀和。。。注意模
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e5+105; 4 const int MOD=1e9+7; 5 long long a[maxn]; 6 int main() 7 { 8 int n; 9 scanf("%d",&n); 10 string s; 11 cin>>s; 12 memset(a,0,sizeof(a)); 13 for(int i=0;i<n;i++) 14 { 15 if(s[i]=='1') 16 a[i+1]++; 17 if(i!=0) 18 { 19 a[i]+=a[i-1]; 20 a[i]%=MOD; 21 } 22 } 23 for(int i=0;i<n;i++) 24 if(i!=0) 25 { 26 a[i]+=a[i-1]; 27 a[i]%=MOD; 28 } 29 long long sum=0; 30 for(int i=0;i<n;i++) 31 { 32 if(s[i]=='1') 33 { 34 sum+=a[i]; 35 sum%=MOD; 36 } 37 } 38 printf("%lld\n",sum); 39 return 0; 40 }
H |
牛牛的k合因子数 |
思路:筛法求素数后应当就把1~n的合因子数存起来,否则会超时
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e5+10; 4 int pre[maxn]; 5 const int MAX_N = 1e5+10; 6 int prime[MAX_N]; 7 bool is_prime[MAX_N+1]; 8 void Ai(){ 9 int p = 0; 10 for(int i = 0; i <= maxn; i++) is_prime[i] = true; 11 is_prime[0] = is_prime[1] =true; 12 for(int i = 2; i <= maxn; i++){ 13 if(is_prime[i]){ 14 prime[p++] = i; 15 for(int j = 2*i; j <= maxn; j+=i) is_prime[j] = false; 16 } 17 } 18 } 19 int n; 20 int res[maxn]; 21 void he() 22 { 23 for(int j=1;j<=n;j++) 24 { 25 int cnt=0; 26 for(int i=1;i*i<=j;i++){ 27 if(j%i==0&&is_prime[i]==0) 28 cnt++; 29 if(j%i==0&&is_prime[j/i]==0&&i!=j/i) 30 cnt++; 31 } 32 res[cnt]++; 33 } 34 } 35 int main() 36 { 37 Ai(); 38 int m; 39 scanf("%d%d",&n,&m); 40 he(); 41 int k; 42 while(m--) 43 { 44 scanf("%d",&k); 45 printf("%d\n",res[k]); 46 } 47 return 0; 48 }
I |
牛牛的汉诺塔 |
思路:只会打表找规律。。。
1 #include<iostream> 2 using namespace std; 3 const int maxn = 7; 4 typedef long long ll; 5 ll a[maxn]; 6 int main(){ 7 int n; 8 cin>>n; 9 for(int i=1;i<=n;i++){ 10 if(i&1){ 11 a[2]=a[1]+a[4]+1; 12 a[3]=a[4]+a[5]; 13 a[6]=a[3]; 14 } 15 else{ 16 a[1] = a[2]+a[3]; 17 a[4] = a[1]; 18 a[5] = a[3]+a[6]; 19 } 20 } 21 cout<<"A->B:"<<a[1]<<endl; 22 cout<<"A->C:"<<a[2]<<endl; 23 cout<<"B->A:"<<a[3]<<endl; 24 cout<<"B->C:"<<a[4]<<endl; 25 cout<<"C->A:"<<a[5]<<endl; 26 cout<<"C->B:"<<a[6]<<endl; 27 cout<<"SUM:"<<(1ll<<n)-1<<endl; 28 return 0; 29 }