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 ...
随机推荐
- 固定管线shader编写:基本属性
欢迎转载!转载时请注明出处:http://write.blog.csdn.net/postedit/50753008 shader 部分介绍: properties:属性部分 material:材质部 ...
- h5-10 canvas 简易祖玛
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [转载]linux上安装oracle
原文地址:linux上安装oracle作者:天涯恨客 1.创建oinstall组 [root@xieqing ~]# groupadd oinstall 创建dba组 [root@xieqing ~] ...
- php word转pdf
我们知道,PHP语言的合理运用可以帮助我们实现导出Word文档的功能.今天我们将为大家介绍PHP处理Word转PDF的相关实现方法. PHP关键字this指向当前对象指针 PHP self关键字指向类 ...
- ROS-TF-广播
前言:将海龟的坐标系变换广播到TF. URDF文件的描述是在相对坐标上进行的,运动起来就需要考虑机器人各个连杆的相对位置关系.TF的诞生就是为了自动管理这些相对关系下的坐标变换的,而我们需要做的就是给 ...
- 关于CommandTimeOut
指示在终止尝试和产生错误之前执行命令期间需等待的时间. 设置和返回值 设置或返回长整型值,该值指示等待命令执行的时间(单位为秒).默认值为 30. 说明 Connection 对象或Command 上 ...
- main函数解析
原文链接:http://parisliu2008.blog.163.com/blog/static/95070867200951510412959/ main参数 2009-06-15 10:41:2 ...
- vc++元文件的保存,保存图形,重绘图形
1, CMateFileDC 可以用来多次打开自己的画布,这个元文件包含许多接口的命令 当绘制好之后可以用来播放元文件 首先,创建一个CMateFileDC的元文件对象 然后调用Create原函数,创 ...
- android学习路线总结
感谢安辉作者,学习路线 https://www.cnblogs.com/yishaochu/p/5436094.html https://www.cnblogs.com/jycboy/p/60666 ...
- jquery.lazyload滚动不起作用
昨天同事在开发图片懒加载功能时用到了lazyload,使用相当标准,然而结果却不如人意,在滚动时未能起作用.引用https://cdn.bootcss.com/jquery_lazyload/1.9. ...