2019国赛
B
思想:dfs
答案:
55965365465060
#include<iostream> #include<stdio.h> #include<string.h> #include<vector> using namespace std; typedef long long ll; const int maxn = 3000; ll f[maxn][maxn];//用long long vector<int>prime; bool vis[10000]; void init(){//素数筛 for(int i = 2;i<= 3000;i++) { if(!vis[i]) { for(int j = i*i;j<= 3000;j+= i) { vis[j] = true; } } } for(int i = 2;i<= 2019;i++) { if(!vis[i]) prime.push_back(i); } } ll dfs(int pos,int sum) { if(f[pos][sum]!= -1) return f[pos][sum]; if(sum == 2019) return 1; if(pos>= prime.size()||sum> 2019) return 0; ll ans = 0; ans+= dfs(pos+1,sum);// 不要当前这个素数 ans+= dfs(pos+1,sum+prime[pos]);// 要当前这个素数 return f[pos][sum] = ans; } int main(){ init(); memset(f,-1,sizeof(f)); ll ans = dfs(0,0); cout<<ans<<endl; return 0; }
D
代码:
#include<iostream> #include<stdio.h> #include<vector> using namespace std; typedef long long ll; bool vis[100000]; vector<int>prime; void init(){ for(int i=2;i<=10000;i++){ if(!vis[i]){ for(int j=i*i;j<=10000;j+=i){ vis[j]=true; } } } for(int i=2;i<=10000;i++){ if(!vis[i]){ prime.push_back(i); } } return ; } ll cal(int n){ ll ans=1; for(int i=0;i<prime.size();i++){ int cnt=0; while(n%prime[i]==0){ cnt++; n = n/prime[i]; } ans = ans*(cnt+1); } return ans; } int main(){ init(); for(int i=99;i<=1000000;i++){ if(cal(i)==100){ cout<<i<<endl; break; } } return 0; }
E
代码:
#include<iostream> #include<stdio.h> using namespace std; int vis[8][8]; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int ans; void dfs(int x,int y,int len){ if(len>2&&len<=12&&x==0&&y==0){ ans++; return ; } for(int i=0;i<4;i++){ int nx =x+dx[i]; int ny =y+dy[i]; if(nx<0||nx>7||ny<0||ny>7) continue; if(!vis[nx][ny]&&len<=12){ vis[nx][ny] =1; dfs(nx,ny,len+1); vis[nx][ny] = 0; } } } int main(){ dfs(0,0,0); cout<<ans<<endl; return 0; }