1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include <iostream> #include <cstdio> #include <fstream> #define INF 100000000 #define ll long long using namespace std; int m,n,MX; int main() { int tt; ll dp; int MN_Rem,MXR; while(scanf("%d%d%d",&m,&n,&tt)!=EOF) { if(m>tt && n>tt) { cout<<0<<" "<<tt<<"\n"; continue; } MX =0; bool f =false; MN_Rem = INF; MXR = 0; for(int i=0;i<=10005;i++) { for(int j=0;j<=10005;j++) { dp =tt-(i*m+j*n); if(i==0 && j==0) continue; if(dp<0)//dp[i][j] break; if(dp==0)//dp[i][j] { if(MX<(i+j)) MX = i+j; f=1; } if(dp>0)//dp[i][j] { if(dp<MN_Rem)//dp[i][j] { MN_Rem = dp;//dp[i][j] MXR = i+j; } else if(dp==MN_Rem)//dp[i][j] { if(MXR<(i+j)) MXR = i+j; } } }//2nd for }//1st for if(f) cout<<MX<<"\n"; else cout<<MXR<<" "<<MN_Rem<<"\n"; } //out.close(); } |
Category: DP
10313
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include <iostream> #include <string> #include <sstream> #define ll long long using namespace std; ll ways[400][400]; ll findways(int a,int b) { ll r=0; if(ways[a][b]!=-1) return ways[a][b]; if(a==0) return 1; else { r = 0; for(int i=b;i>=1;i--) if(a-i>=0) { r+=findways(a-i,i); //ways[a-i][i]=r; } ways[a][b]=r; } return r; } int main() { int a[3],b,c,n; string s; stringstream ss; for(int i=0;i<400;i++) for(int j=0;j<400;j++) ways[i][j]=-1; while(getline(cin,s)) { int k=0; //getline(cin,s); ss.clear(); ss<<s; while(ss>>n) { if(n>300) n=300; a[k++]=n; } if(k==1) { cout<<findways(a[0],a[0])<<"\n"; } if(k==2) { cout<<findways(a[0],a[1])<<"\n"; } else if(k==3) { cout<<findways(a[0],a[2])-findways(a[0],a[1]-1)<<"\n"; } } return 0; } |
11151
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <stdio.h> #include <string.h> #include <string> #include <iostream> using namespace std; int table[1001][1001]; int main() { int n,len; string s; cin>>n; getchar(); while(n--) { getline(cin,s); if(s=="") { cout<<0<<"\n"; continue; } memset(table,0,sizeof(table)); len = s.size(); for(int i=0;i<=len;i++) { table[i][i]=1; } for(int length=2;length<=len;length++) { for(int i=0;i<=len-length+1;i++) { int end=i+length-1; if(end<0 || end>=len) continue; if(s[i]==s[end] && length==2) { table[i][end]=2; } else if(s[i]==s[end]) { table[i][end]=table[i+1][end-1]+2; } else { table[i][end] = max(table[i+1][end],table[i][end-1]); } } } cout<<table[0][len-1]<<"\n"; } return 0; } |
11137
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> #define ll long long using namespace std; int arr[23]={0,1,8,27,64,125,216,343,512,729, 1000,1331,1728,2197,2744,3375,4096,4913,5832, 6859,8000,9261 }; ll ways[10005]; int main() { for(int j=0;j<10005;j++) ways[j]= 0; ways[0]=1; for(int i=1;i<=21;i++) { for(int j=arr[i];j<10005;j++) ways[j]+=ways[j-arr[i]]; } int n; while(cin>>n) { //cout<<calc(1,n)<<"\n"; cout<<ways[n]<<"\n"; } return 0; } |
725
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#include <iostream> #include <stdio.h> #include <map> #include <vector> using namespace std; map<int,int> m,n; vector<int> den[80]; vector<int> numer[80]; int main() { int cnt,org; for(int i=0;i<80;i++) { den[i].clear(); numer[i].clear(); } for(int i=1234;i<=98765;i++) { if(i%10==0 && i<10000) continue; org=i; m.clear(); if(i<10000) m[0]=1; while(org) { if(m[org%10]) break; m[org%10]=1; org/=10; } if(org) continue; cnt=1; for(int j=i*2;j<100000;j+=i) { cnt++; if(i<10000 && j<10000) continue; org=j; n.clear(); while(org) { if(m[org%10]==1 || n[org%10]==1) break; n[org%10]=1; org/=10; } if(org) continue; if(i<10000 && n[0]==1) continue; numer[cnt].push_back(j); den[cnt].push_back(i); } } int N,c=0; while(cin>>N) { if(N==0) break; if(c) cout<<"\n"; c=1; if(den[N].size()==0) cout<<"There are no solutions for "<<N<<".\n"; else { //cout<<den[N].size()<<"\n"; for(int i=0;i<den[N].size();i++) { if(numer[N][i]<10000) cout<<0; cout<<numer[N][i]; cout<<" / "; if(den[N][i]<10000) cout<<0; cout<<den[N][i]<<" = "<<N<<"\n"; } } //cout<<"\n"; } return 0; } |
1213
Hint: Start from 1120 not to count same prime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <iostream> using namespace std; int dp[1121][15]; int main() { int n,k; int prme[189]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127, 131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349, 353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673, 677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809, 811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117, 1123, 1129}; for(int i=0;i<1121;i++) { for(int j=0;j<15;j++) { dp[i][j]=0; } } dp[0][0]=1; for(int i=0;i<187;i++) { for(int j=1120;j>=prme[i];j--) { for(int k=1;k<=14;k++) { dp[j][k]+=dp[j-prme[i]][k-1]; } } } /*for(int i=0;i<189;i++) for(int j=1120;j>=prme[i];j--) for(int k=1;k<15;k++) dp[j][k]+=dp[j-prme[i]][k-1]; */ while(cin>>n) { cin>>k; if(n==0 && k==0) break; cout<<dp[n][k]<<"\n"; } return 0; } |
11762
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <iostream> #include <vector> #include <map> #include <stdio.h> using namespace std; int prime[1000001]; int primes[1000001]; double dp[1000001]; vector<int> vb; map<int,int> mp; int main() { int n,t; primes[0]=primes[1]=0; for(int i=2;i*i<1000001;i++) { if(prime[i]==0) { for(int j=i*i;j<1000001;j+=i) { prime[j]=1; primes[j]=i; } //primes[i]=i; } } prime[0]=prime[1]=0; for(int i=2;i<1000001;i++) { if(primes[i]==0) primes[i]=i; if(prime[i]) prime[i]=0; else prime[i]=1; prime[i]+=prime[i-1]; } dp[1]=0; for(int i=2;i<1000001;i++) { int k=i; mp.clear(); vb.clear(); while(primes[k]!=0) { if(mp[primes[k]]==0) { mp[primes[k]]=1; vb.push_back(primes[k]); } k/=primes[k]; } double g=0; for(int j=0;j<vb.size();j++) g+=dp[i/vb[j]]; dp[i]=(g+prime[i])/vb.size(); } cin>>t; int c=0; while(t--) { cin>>n; //cout<<prime[n]<<"\n"; c++; cout<<"Case "<<c<<": "; printf("%.10lf\n",dp[n]); } return 0; } |
11780
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <iostream> #include <math.h> #include <map> #include <stdio.h> #include <string> #include <vector> using namespace std; map<int,int> mp; int fib[50]; double dp[1010]; int main() { fib[0]=0; fib[1]=1; fib[2]=2; mp[1]=1; mp[2]=2; for(int i=3;i<=20;i++) { fib[i]=fib[i-1]+fib[i-2]; mp[fib[i]]=i; } dp[1]=.4; for(int i=1;i<=1000;i++) { double p=i*1.6; double d=1000000; for(int j=1;j<=18;j++) { if(i-fib[j]>=0) { int a=mp[fib[j]]; double x; if((fib[a+1]+dp[i-fib[j]]-p)<0) { x=-1*(fib[a+1]+dp[i-fib[j]]-p); } else x=(fib[a+1]+dp[i-fib[j]]-p); if(x<d) { d=x; dp[i]=fib[a+1]+dp[i-fib[j]]; } } } } int n; while(cin>>n) { if(n==0) break; printf("%.2lf\n",fabs(dp[n]-n*1.6));//"\n"; } return 0; } |
991
Theory: For larger n, imagine choosing an arbitrary pair of people to shake hands. You’ve now divided the circle into two smaller circles (one of which may have 0 people in it). The number of ways you can arrange the rest of the people is the product of the answers for the two smaller circles.