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.
 
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≤103,0<K≤104,1≤A≤106,1≤B≤104,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.
 
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 = ;
//素数筛选部分
bool notprime[MAXN];//值为false表示素数,值为true表示非素数
int prime[MAXN+];
void getPrime()
{
memset(notprime,false,sizeof(notprime));
notprime[]=notprime[]=true;
memset(prime,,sizeof(prime));
for(int i=;i<=MAXN;i++)
{
if(!notprime[i])prime[++prime[]]=i;
for(int j=;j<=prime[]&&prime[j]<=MAXN/i;j++)
{
notprime[prime[j]*i]=true;
if(i%prime[j]==) break;
}
}
}
//合数分解
long long factor[][];
int fatCnt;
int getFactors(long long x)
{
fatCnt=;
long long tmp=x;
for(int i=;prime[i]<=tmp/prime[i];i++)
{
factor[fatCnt][]=;
if(tmp%prime[i]==)
{
factor[fatCnt][]=prime[i];
while(tmp%prime[i]==)
{
factor[fatCnt][]++;
tmp/=prime[i];
}
fatCnt++;
}
}
if(tmp!=)
{
factor[fatCnt][]=tmp;
factor[fatCnt++][]=;
}
return fatCnt;
}
struct Node
{
int A,B;
int score;
int s;
}node[];
bool cmp(Node a,Node b)
{
return a.score > b.score;
}
long long pow_m(long long a,long long n)
{
long long ret = ;
long long tmp = a;
while(n)
{
if(n&)ret*=tmp;
tmp*=tmp;
n>>=;
}
return ret;
}
long long sum(long long p,long long n)//求1+p+p^2+p^3+..p^n
{
if(p==)return ;
if(n == )return ;
if(n&)
return (+pow_m(p,n/+))*sum(p,n/);
else return (+pow_m(p,n/+))*sum(p,n/-)+pow_m(p,n/);
}
void check(int index)
{
if(node[index].A == )
{
node[index].score = ;
node[index].s = (<<);
return;
}
getFactors(node[index].A);
node[index].s = ;
//第一个条件(是素数)
if(fatCnt == && factor[][] == )
node[index].s |= (<<);
//第二个条件
if(fatCnt == && notprime[factor[][]+]==false)
node[index].s |= (<<);
//第三个条件
if(fatCnt == && notprime[sum(factor[][],factor[][])]==false)
node[index].s |= (<<);
//第四个条件
bool flag = true;
for(int i = ;i < fatCnt;i++)
{
long long tmp = (factor[i][]+)*factor[i][]/;
for(int j = ;j < fatCnt;j++)
if(i != j)
tmp *= (factor[j][]+);
if(tmp%!=)
{
flag = false;
break;
}
}
if(flag)node[index].s |= (<<);
node[index].score = ;
for(int i = ;i < ;i++)
if(node[index].s &(<<i))
node[index].score++;
} int b[];
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 = ;i < N;i++)
{
scanf("%d%d",&node[i].A,&node[i].B);
check(i);
}
for(int i = ;i < N;i++)
{
printf("%d",node[i].score);
if(i < N-)printf(" ");
else printf("\n");
}
for(int i = ;i < ;i++)
scanf("%d",&b[i]);
int ans = -;
sort(node,node+N,cmp);
for(int k = ;k <(<<);k++)
{
int tmp = ;
int temps = ;
int cc = K;
for(int i = ;i < N;i++)
if((node[i].s & k)==)
{
if(cc == )break;
temps |= node[i].s;
tmp += node[i].score*min(cc,node[i].B);
cc -= min(cc,node[i].B);
if(cc == )break;
}
for(int i = ;i < ;i++)
if((temps&(<<i))==)
tmp += b[i];
if(cc!=)continue;
else ans = max(ans,tmp);
}
printf("%d\n",ans);
}
return ;
}

HDU 4610 Cards (合数分解,枚举)的更多相关文章

  1. hdu 4610 Cards

    http://acm.hdu.edu.cn/showproblem.php?pid=4610 先求出每个数的得分情况,分数和得分状态,(1<<4)种状态 按分数从大到小排序 然后每种状态取 ...

  2. hdu 5317 合数分解+预处理

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  3. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. HDU 4497 GCD and LCM (合数分解)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  5. hdu_4497GCD and LCM(合数分解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 GCD and LCM Time Limit: 2000/1000 MS (Java/Other ...

  6. Perfect Pth Powers pku-1730(筛+合数分解)

    题意:x可以表示为bp, 求这个p的最大值,比如 25=52, 64=26,  然后输入x 输出 p 就是一个质因子分解.算法.(表示数据上卡了2个小时.) 合数质因子分解模板. ]; ]; ; ;n ...

  7. pku1365 Prime Land (数论,合数分解模板)

    题意:给你一个个数对a, b 表示ab这样的每个数相乘的一个数n,求n-1的质数因子并且每个指数因子k所对应的次数 h. 先把合数分解模板乖乖放上: ; ans != ; ++i) { ) { num ...

  8. GCD and LCM HDU - 4497(质因数分解)

    Problem Description Given two positive integers G and L, could you tell me how many solutions of (x, ...

  9. hdu 5428 The Factor 分解质因数

    The Factor  Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest ...

随机推荐

  1. 深入理解Java对象的序列化与反序列化的应用

    当两个进程在进行远程通信时,彼此可以发送各种类型的数据.无论是何种类型的数据,都会以二进制序列的形式在网络上传送.发送方需要把这个Java对象转换为字节序列,才能在网络上传送:接收方则需要把字节序列再 ...

  2. android下身份验证方式调用webservice

    在企业开发领域,webservice还是经常被用到的服务体系,因为他对安全事务支持都比较好. 有时候,我们就需要在android下调用后端的webservice服务,因为在内部网络环境下,所有需要ba ...

  3. [反汇编练习] 160个CrackMe之026

    [反汇编练习] 160个CrackMe之026. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  4. Linux Shell编程(4): 逻辑运算符、逻辑表达式详解

    shell的逻辑运算符 涉及有以下几种类型,因此只要适当选择,可以解决我们很多复杂的判断,达到事半功倍效果. 一.逻辑运算符 逻辑卷标 表示意思 1. 关于档案与目录的侦测逻辑卷标! -f 常用!侦测 ...

  5. marginCollapse之兄弟关系的DIV

    废话不说,直接上图 基本代码如下: 效果图如下: 给两个div分别加marginBottom和marginTop看一下效果 实际效果如下: 我们可以看出两个div之间的距离并不是50+50,而是只显示 ...

  6. Be quiet

    Be quiet */--> UP | HOME Be quiet Table of Contents 1 Be quiet 1 Be quiet 最近心情有点不太好,各方面原因.主要是25岁是 ...

  7. Linux VPS 基本命令

    我们Linux VPS用命令才能管理他,我们来罗列一些基本和简单的Linux的命令 1.lsls / 查看根目录ls -a / 查看根目录下所要文件,包括隐藏文件ls -l / 详细列出目录下文件的权 ...

  8. 定时组件quartz系列<一>模拟定时组件小程序

    一.核心概念 Quartz的原理不是很复杂,只要搞明白几个概念,然后知道如何去启动和关闭一个调度程序即可. 1.Job表示一个工作,要执行的具体内容.此接口中只有一个方法void execute(Jo ...

  9. 【转】iOS开发UI篇—iPad和iPhone开发的比较

    原文网址:http://www.cnblogs.com/wendingding/p/3918007.html iOS开发UI篇—iPad和iPhone开发的比较 一.iPad简介 1.什么是iPad ...

  10. js match regex

    需要返回成数组元素的要放在括号里头 var arr = /input-([0-9]*)-([0-9]*)/.exec(id); var all = arr[0]; var row = arr[1]; ...