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 ...
随机推荐
- scanf,printf函数细节
今天笔试的时候遇到一个考察C语言scanf函数的题目 int x; float y; scanf("%3d%f",&x,&y); // input 123456 6 ...
- 【java项目实战】ThreadLocal封装Connection,实现同一线程共享资源
线程安全一直是程序员们关注的焦点.多线程也一直是比較让人头疼的话题,想必大家以前也遇到过各种各种的问题.我就不再累述了.当然,解决方案也有非常多,这篇博文给大家提供一种非常好的解决线程安全问题的思路. ...
- Local Response Normalization作用——对局部神经元的活动创建竞争机制,使得其中响应比较大的值变得相对更大,并抑制其他反馈较小的神经元,增强了模型的泛化能力
AlexNet将LeNet的思想发扬光大,把CNN的基本原理应用到了很深很宽的网络中.AlexNet主要使用到的新技术点如下. (1)成功使用ReLU作为CNN的激活函数,并验证其效果在较深的网络超过 ...
- hdoj--1150--Machine Schedule(最小点覆盖)
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- nyoj--973--天下第一(SPFA判断负环)
天下第一 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 AC_Grazy一直对江湖羡慕不已,向往着大碗吃肉大碗喝酒的豪情,但是"人在江湖漂,怎能 不挨刀&qu ...
- C#编译器优化
C#编译器优化 https://www.cnblogs.com/podolski/p/8987595.html 使用C#编写程序,给最终用户的程序,是需要使用release配置的,而release配置 ...
- ubuntu的home目录下,Desktop等目录消失不见
第一步:创建相应的文件夹 首先当然是创建几个相应的英文文件夹喽,比如:Desktop.Downloads. Documents. Music. Pictures. Videos , Template ...
- BZOJ 1443 二分图博弈 网络流
思路: 二分图博弈嘛 找到最大匹配的必须点 跑个网络流 前后DFS一遍 //By SiriusRen #include <queue> #include <cstdio> #i ...
- 利用@factory和@dataProvider实现参数化
利用构造函数将要用的的参数初始化,非常有利于简化脚本.它将外部的数据赋给本地的变量,可以使得初始化后的参数被其他方法使用.直接上代码: private String url; private S ...
- P1304 哥德巴赫猜想
题目描述 输入N(N<=10000),验证4~N所有偶数是否符合哥德巴赫猜想. (N为偶数). 如果一个数,例如10,则输出第一个加数相比其他解法最小的方案.如10=3+7=5+5,则10=5+ ...