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 ...
随机推荐
- 在IIS6,7中部署ASP.NET网站
查看web.config文件 ASP.NET网站与一般的桌面程序不同,不是拷贝过来就能运行的(数据库连接除外). 要想运行它,通常需要一些配置过程.但是,我们到底需要配置什么呢?答案是:查看web.c ...
- element-UI中table表格的row-click事件怎么获取一行数据的id
<el-table :data="tableData" style="width: 100%" @row-click="openDetails( ...
- Android开机动画、logo、字样的定制过程【转】
本文转载自:http://blog.csdn.net/yinhaide/article/details/43668401 Android开机画面总共有三屏 一.第一屏:开机logo 1.选张png格式 ...
- 【POJ 3696】 The Luckiest number
[题目链接] http://poj.org/problem?id=3696 [算法] 设需要x个8 那么,这个数可以表示为 : 8(10^x - 1) / 9, 由题, L | 8(10^x - 1) ...
- php pdo操作
PDO(PHP Data Object) 是PHP 5 中加入的东西,是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接和处理,什么 p ...
- Npgsql使用入门(二)【实用助手类】
数据库映射的实体类: public class Test { public long TestID { get; set; } public string Name { get; set; } pub ...
- DataTable和List相互转换的类
DataTable与List相互转换 .NET后台数据处理,从数据库中的捞出的数据格式一般是List和DataTable的格式.现在将两种格式相互转换的心得记录下来以便以后查找(直接上代码). pub ...
- Socket server
Socket server的使用方法(精华部分),仅供自用. class MyServer(socketserver.BaseRequestHandler): def handle(self): wh ...
- [hihocoder][Offer收割]编程练习赛44
扫雷游戏 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #includ ...
- 在APP开发设计过程中:如何设计启动页面?
心理学上有一个“7秒理论”,说的是,一个人对另一个人的印象,在初次见面的七秒内就会形成,最近更有研究表明,这个时间可能更短——不到1秒.所以初次见面所展示的形象真的很重要.同理,用户在使用APP时,每 ...