POJ 1777
一道好题。
由算术基本定理,知:

那么,对于上式的每个因子值只能是2^M的形式。取第一个式子为例,通过分解因式出(1+p^2)=2^k知,a只能为1.
于是对于p只能是梅森素数。而且每个梅森素数只能出现一次,利用这个就可以求解了,
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int Max=1<<8; int Mec[10]={(1<<2)-1,(1<<3)-1,(1<<5)-1,(1<<7)-1,(1<<13)-1,(1<<17)-1,(1<<19)-1,(1<<31)-1,0,0};
int vp[10]={2,3,5,7,13,17,19,31,0,0};
int v[150],tans[150],tp;
bool p[Max]; int judge(int t){
int ans=0;
for(int i=0;i<8;i++){
if(t%Mec[i]==0){
ans|=(1<<i);
t/=Mec[i];
}
}
if(t==1)
return ans;
else return 0;
} int main(){
int t;
while(scanf("%d",&t)!=EOF){
tp=0;
int ans=0;
for(int i=0;i<t;i++)
scanf("%d",&v[i]);
memset(p,false,sizeof(p));
p[0]=true;
for(int i=0;i<t;i++){
int tmp=judge(v[i]);
if(tmp){
p[tmp]=true;
tans[tp++]=tmp;
}
}
for(int i=0;i<tp;i++){
for(int k=0;k<Max;k++){
if(p[k]){
if(!(k&tans[i]))
p[k|tans[i]]=true;
}
}
}
int e;
for(int i=Max-1;i>=0;i--)
if(p[i]){
int c=0;
for(int k=0;k<8;k++){
e=(1<<k);
if(e&i)
c+=vp[k];
}
ans=max(ans,c);
}
if(!ans){
printf("NO\n");
continue;
}
printf("%d\n",ans);
}
return 0;
}
POJ 1777的更多相关文章
- POJ 1777 mason素数
题目大意: 给定数列 a1 , a2 , ... , an 希望找到一个 N = sigma(ai^ki) , (0<=ki<10) ,ki可随自己定为什么 只要保证N的因子和可以表示 ...
- [ACM] POJ 3686 The Windy's (二分图最小权匹配,KM算法,特殊建图)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4158 Accepted: 1777 Descr ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- POJ中和质数相关的三个例题(POJ 2262、POJ 2739、POJ 3006)
质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能被其他自然数整除,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数. 最小的质数 ...
- POJ 3090 Visible Lattice Points (ZOJ 2777)
http://poj.org/problem?id=3090 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1777 题目大意: ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
随机推荐
- 通过top 5等待事件查看sql语句
设计的动态性能视图有:v$session_event,v$session,v$sqlarea,首先在v$session_event中可以找到event,然后通过其动态性能视图找到sid,可以在v$se ...
- 杂项-事务:OLTP(联机事务处理过程)
ylbtech-杂项-事务:OLTP(联机事务处理过程) On-Line Transaction Processing联机事务处理过程(OLTP) 也称为面向交易的处理过程,其基本特征是前台接收的用户 ...
- 部署微信定位精灵APK到Genymotion
- redis集群部署及常用的操作命令_01
简单说下自己测试搭建简单的redis集群的大体步骤: 1.首先你的有6个redis(官方说最少6个,3master,3slave),可以先在一台机器上搭建,搭建到多台上应该只需要改变启动命令即可(可能 ...
- 使用C语言扩展Python3
使用C语言扩展Python3.在Python3中正确调用C函数. 1. 文件demo.c #include <Python.h> // c function static PyObject ...
- All Metro Apps on Windows 8.1 Do Not Work
所有的Metro Apps不能够正常打开,表现为打开后自动最小化到任务栏,并且不能恢复正常状态.在Event Viewer\Application中相应的错误信息为: Activation of ap ...
- WEBGL学习笔记(七):实践练手1-飞行类小游戏之游戏控制
接上一节,游戏控制首先要解决的就是碰撞检测了 这里用到了学习笔记(三)射线检测的内容了 以鸟为射线原点,向前.上.下分别发射3个射线,射线的长度较短大概为10~30. 根据上一节场景的建设,我把y轴设 ...
- 【C】一些字符串处理函数
1.复制函数 我更愿意称之为”字符串覆盖函数” a. strcpy(str1,str2); 将字符串str2 覆盖到str1上 b. strncpy(str1,str2,n); 2.拼接函数 a. s ...
- mwArray与C++接口
1.Matlab调用C++:http://blog.csdn.net/zouxy09/article/details/20553007 摘录下效果图: 2.mwArray类操作:http://blog ...
- WinAPI使用: 时间,线程,中断
(1):C/C++获取当前系统时间:http://www.cnblogs.com/mfryf/archive/2012/02/13/2349360.html 不过当计算算法耗时的时候,不要忘记seco ...