Cards
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 470 Accepted Submission(s): 72
Problem Description
Given some cards each assigned a number, you're required to select EXACTLY K cards among them.
While you select a card, I will check the number assigned to it and see if it satisfies some of the following conditions:
1. the number is a prime number;
2. the amount of its divisors is a prime number;
3. the sum of its divisors is a prime number;
4. the product of all its divisors is a perfect square number. A perfect square number is such a kind of number that it can be written as a square of an integer.
The score you get from this card is equal to the amount of conditions that its number satisfies. The total score you get from the selection of K cards is equal to the sum of scores of each card you select.
After you have selected K cards, I will check if there's any condition that has never been satisfied by any card you select. If there is, I will add some extra scores to you for each unsatisfied condition. To make the game more interesting, this score may be negative.
After this, you will get your final score. Your task is to figure out the score of each card and find some way to maximize your final score.
Note that 1 is not a prime number. In this problem, we consider a number to be a divisor of itself. For example, considering the number 16, it is not a prime. All its divisors are respectively 1, 2, 4, 8 and 16, and thus, it has 5 divisors with a sum of 31 and a product of 1024. Therefore, it satisfies the condition 2, 3 and 4, which deserves 3 points.
While you select a card, I will check the number assigned to it and see if it satisfies some of the following conditions:
1. the number is a prime number;
2. the amount of its divisors is a prime number;
3. the sum of its divisors is a prime number;
4. the product of all its divisors is a perfect square number. A perfect square number is such a kind of number that it can be written as a square of an integer.
The score you get from this card is equal to the amount of conditions that its number satisfies. The total score you get from the selection of K cards is equal to the sum of scores of each card you select.
After you have selected K cards, I will check if there's any condition that has never been satisfied by any card you select. If there is, I will add some extra scores to you for each unsatisfied condition. To make the game more interesting, this score may be negative.
After this, you will get your final score. Your task is to figure out the score of each card and find some way to maximize your final score.
Note that 1 is not a prime number. In this problem, we consider a number to be a divisor of itself. For example, considering the number 16, it is not a prime. All its divisors are respectively 1, 2, 4, 8 and 16, and thus, it has 5 divisors with a sum of 31 and a product of 1024. Therefore, it satisfies the condition 2, 3 and 4, which deserves 3 points.
Input
The first line of the input contains the number of test cases T.
Each test case begins with two integers N and K, indicating there are N kinds of cards, and you're required to select K cards among them.
The next N lines describes all the cards. Each of the N lines consists of two integers A and B, which denote that the number written on this kind of card is A, and you can select at most B cards of this kind.
The last line contains 4 integers, where the ith integer indicates the extra score that will be added to the result if the ith condition is not satisfied. The ABSOLUTE value of these four integers will not exceed 40000.
You may assume 0<N≤10 3,0<K≤10 4,1≤A≤10 6,1≤B≤10 4,T≤40 and the total N of all cases is no more than 20000. In each case there are always enough cards that you're able to select exact K cards among them.
Each test case begins with two integers N and K, indicating there are N kinds of cards, and you're required to select K cards among them.
The next N lines describes all the cards. Each of the N lines consists of two integers A and B, which denote that the number written on this kind of card is A, and you can select at most B cards of this kind.
The last line contains 4 integers, where the ith integer indicates the extra score that will be added to the result if the ith condition is not satisfied. The ABSOLUTE value of these four integers will not exceed 40000.
You may assume 0<N≤10 3,0<K≤10 4,1≤A≤10 6,1≤B≤10 4,T≤40 and the total N of all cases is no more than 20000. In each case there are always enough cards that you're able to select exact K cards among them.
Output
Output two lines for each test case.
The first line consists of N integers separated by blanks, where the ith integer is the score of the ith card.
The second line contains a single integer, the maximum final scores you can get.
The first line consists of N integers separated by blanks, where the ith integer is the score of the ith card.
The second line contains a single integer, the maximum final scores you can get.
Sample Input
1 5 3 1 1 2 1 3 1 4 1 5 1 1 2 3 4
Sample Output
1 3 2 2 2 11
Source
Recommend
liuyiding
题目意思很长。
需要解决,判断一个数是不是素数,一个数约数的个数是不是素数,一个数约数的和是不是素数,一个数约数的乘积是不是素数。
一个数是不是素数直接判断的。
约数个数是素数的话,肯定这个数只能有一个素因子,判断这个素因子的指数+1是不是素数就可以了。
约数的和为素数,也必须只含一个素因子p^k.然后求1+p^1+p^2+..+p^k .判断是不是素数。
比较麻烦的是约数的乘积是不是素数的判断。
其实就是每一个素因子的指数为偶数。
之后我是枚举的。貌似正确的枚举方法是把所有点分成16种,2^16枚举的。
我做的时候是枚举2^4,就是判断每一种能不能取,然后从大到小选择。
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <math.h> using namespace std; const int MAXN = 1000010; //素数筛选部分 bool notprime[MAXN];//值为false表示素数,值为true表示非素数 int prime[MAXN+1]; void getPrime() { memset(notprime,false,sizeof(notprime)); notprime[0]=notprime[1]=true; memset(prime,0,sizeof(prime)); for(int i=2;i<=MAXN;i++) { if(!notprime[i])prime[++prime[0]]=i; for(int j=1;j<=prime[0]&&prime[j]<=MAXN/i;j++) { notprime[prime[j]*i]=true; if(i%prime[j]==0) break; } } } //合数分解 long long factor[100][2]; int fatCnt; int getFactors(long long x) { fatCnt=0; long long tmp=x; for(int i=1;prime[i]<=tmp/prime[i];i++) { factor[fatCnt][1]=0; if(tmp%prime[i]==0) { factor[fatCnt][0]=prime[i]; while(tmp%prime[i]==0) { factor[fatCnt][1]++; tmp/=prime[i]; } fatCnt++; } } if(tmp!=1) { factor[fatCnt][0]=tmp; factor[fatCnt++][1]=1; } return fatCnt; } struct Node { int A,B; int score; int s; }node[1010]; bool cmp(Node a,Node b) { return a.score > b.score; } long long pow_m(long long a,long long n) { long long ret = 1; long long tmp = a; while(n) { if(n&1)ret*=tmp; tmp*=tmp; n>>=1; } return ret; } long long sum(long long p,long long n)//求1+p+p^2+p^3+..p^n { if(p==0)return 0; if(n == 0)return 1; if(n&1) return (1+pow_m(p,n/2+1))*sum(p,n/2); else return (1+pow_m(p,n/2+1))*sum(p,n/2-1)+pow_m(p,n/2); } void check(int index) { if(node[index].A == 1) { node[index].score = 1; node[index].s = (1<<3); return; } getFactors(node[index].A); node[index].s = 0; //第一个条件(是素数) if(fatCnt == 1 && factor[0][1] == 1) node[index].s |= (1<<0); //第二个条件 if(fatCnt == 1 && notprime[factor[0][1]+1]==false) node[index].s |= (1<<1); //第三个条件 if(fatCnt == 1 && notprime[sum(factor[0][0],factor[0][1])]==false) node[index].s |= (1<<2); //第四个条件 bool flag = true; for(int i = 0;i < fatCnt;i++) { long long tmp = (factor[i][1]+1)*factor[i][1]/2; for(int j = 0;j < fatCnt;j++) if(i != j) tmp *= (factor[j][1]+1); if(tmp%2!=0) { flag = false; break; } } if(flag)node[index].s |= (1<<3); node[index].score = 0; for(int i = 0;i < 4;i++) if(node[index].s &(1<<i)) node[index].score++; } int b[10]; int main() { //freopen("1011.in","r",stdin); //freopen("out.txt","w",stdout); getPrime(); int T; int N,K; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&K); for(int i = 0;i < N;i++) { scanf("%d%d",&node[i].A,&node[i].B); check(i); } for(int i = 0;i < N;i++) { printf("%d",node[i].score); if(i < N-1)printf(" "); else printf("\n"); } for(int i = 0;i < 4;i++) scanf("%d",&b[i]); int ans = -10000000; sort(node,node+N,cmp); for(int k = 0;k <(1<<4);k++) { int tmp = 0; int temps = 0; int cc = K; for(int i = 0;i < N;i++) if((node[i].s & k)==0) { if(cc == 0)break; temps |= node[i].s; tmp += node[i].score*min(cc,node[i].B); cc -= min(cc,node[i].B); if(cc == 0)break; } for(int i = 0;i < 4;i++) if((temps&(1<<i))==0) tmp += b[i]; if(cc!=0)continue; else ans = max(ans,tmp); } printf("%d\n",ans); } return 0; }