数据不是很大,直接枚举约数,判断4个条件是否满足!
这样就得到4种卡片,总共2^4种情况,枚举各种情况即可!!!
1 #include<iostream> 2 #include<cmath> 3 #include<algorithm> 4 #define MAX 5000005 5 #define ll long long 6 using namespace std; 7 bool ispri[MAX]; 8 int extra[4]; 9 struct card 10 { 11 int score,num,s; 12 }p[1002]; 13 bool cmp(const card &aa,const card &bb){ 14 return aa.score>bb.score?1:0; 15 } 16 void init(){ 17 ll i,j; 18 ispri[1]=1; 19 for (i=2;i<MAX;i++){ 20 if (ispri[i]==0){ 21 for (j=i*i;j<MAX;j+=i) 22 ispri[j] = 1; 23 } 24 } 25 } 26 void factor(int n,int k){ 27 ll i,j,num=0,sum=0,mul=1,t; 28 for (i=1;i*i<=n;i++){ 29 if (n%i==0){ 30 num ++; 31 sum += i; 32 mul = (ll)mul*i; 33 if (i*i != n){ 34 num ++; 35 sum += n/i; 36 mul = (ll)mul*(n/i); 37 } 38 if (mul == (ll)n*n) 39 mul = 1; 40 } 41 } 42 p[k].s = 0;j = 0; 43 if (ispri[n]==0){ 44 p[k].s |= (1<<0); 45 j ++; 46 } 47 if (ispri[num]==0){ 48 p[k].s |= (1<<1); 49 j ++; 50 } 51 if (ispri[sum]==0){ 52 p[k].s |= (1<<2); 53 j ++; 54 } 55 t = (ll)sqrt(mul+0.0); 56 if (t*t==mul||(t+1)*(t+1)==mul||(t-1)*(t-1)==mul){ 57 p[k].s |= (1<<3); 58 j++; 59 } 60 p[k].score = j; 61 } 62 int main(){ 63 init(); 64 int t,i,j,k,n,aa; 65 cin>>t; 66 while (t--){ 67 cin>>n>>k; 68 for (i=0;i<n;i++){ 69 cin>>aa>>p[i].num; 70 if (aa==1){ 71 p[i].score = 1; 72 p[i].s = (1<<3); 73 } 74 else factor(aa,i); 75 } 76 for (i=0;i<4;i++){ 77 cin>>extra[i]; 78 } 79 cout<<p[0].score; 80 for (i=1;i<n;i++){ 81 cout<<' '<<p[i].score; 82 } 83 cout<<endl; 84 sort(p,p+n,cmp); 85 ll ans = -(1<<20); 86 for (i=0;i<(1<<4);i++){ 87 ll temp=0,an=k,flag=0; 88 for (j=0;j<n;j++){ 89 if ((i&p[j].s)==0){ 90 if (p[j].num < an){ 91 temp += p[j].score*p[j].num; 92 an -= p[j].num; 93 flag |= p[j].s; 94 } 95 else{ 96 temp += p[j].score*an; 97 an = 0; 98 flag |= p[j].s; 99 break; 100 } 101 } 102 } 103 for (j=0;j<4;j++){ 104 if ((flag&(1<<j))==0) 105 temp += extra[j]; 106 } 107 if (an != 0) continue; 108 else ans = max(ans,temp); 109 } 110 cout<<ans<<endl; 111 } 112 return 0; 113 }