一道好题。

由算术基本定理,知:

那么,对于上式的每个因子值只能是2^M的形式。取第一个式子为例,通过分解因式出(1+p^2)=2^k知,a只能为1.

于是对于p只能是梅森素数。而且每个梅森素数只能出现一次,利用这个就可以求解了,

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. const int Max=1<<8;
  8.  
  9. 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};
  10. int vp[10]={2,3,5,7,13,17,19,31,0,0};
  11. int v[150],tans[150],tp;
  12. bool p[Max];
  13.  
  14. int judge(int t){
  15. int ans=0;
  16. for(int i=0;i<8;i++){
  17. if(t%Mec[i]==0){
  18. ans|=(1<<i);
  19. t/=Mec[i];
  20. }
  21. }
  22. if(t==1)
  23. return ans;
  24. else return 0;
  25. }
  26.  
  27. int main(){
  28. int t;
  29. while(scanf("%d",&t)!=EOF){
  30. tp=0;
  31. int ans=0;
  32. for(int i=0;i<t;i++)
  33. scanf("%d",&v[i]);
  34. memset(p,false,sizeof(p));
  35. p[0]=true;
  36. for(int i=0;i<t;i++){
  37. int tmp=judge(v[i]);
  38. if(tmp){
  39. p[tmp]=true;
  40. tans[tp++]=tmp;
  41. }
  42. }
  43. for(int i=0;i<tp;i++){
  44. for(int k=0;k<Max;k++){
  45. if(p[k]){
  46. if(!(k&tans[i]))
  47. p[k|tans[i]]=true;
  48. }
  49. }
  50. }
  51. int e;
  52. for(int i=Max-1;i>=0;i--)
  53. if(p[i]){
  54. int c=0;
  55. for(int k=0;k<8;k++){
  56. e=(1<<k);
  57. if(e&i)
  58. c+=vp[k];
  59. }
  60. ans=max(ans,c);
  61. }
  62. if(!ans){
  63. printf("NO\n");
  64. continue;
  65. }
  66. printf("%d\n",ans);
  67. }
  68. return 0;
  69. }

  

POJ 1777的更多相关文章

  1. POJ 1777 mason素数

    题目大意: 给定数列 a1 , a2 , ... , an 希望找到一个  N = sigma(ai^ki)  , (0<=ki<10) ,ki可随自己定为什么 只要保证N的因子和可以表示 ...

  2. [ACM] POJ 3686 The Windy&#39;s (二分图最小权匹配,KM算法,特殊建图)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4158   Accepted: 1777 Descr ...

  3. poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】

    题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...

  4. POJ中和质数相关的三个例题(POJ 2262、POJ 2739、POJ 3006)

    质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能被其他自然数整除,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.      最小的质数 ...

  5. POJ 3090 Visible Lattice Points (ZOJ 2777)

    http://poj.org/problem?id=3090 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1777 题目大意: ...

  6. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  7. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  8. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  9. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

随机推荐

  1. 坚向的ViewPager,上下滑动的组件,android上下滑动 VerticalPager

    package com.zhulin.android.atools; import android.content.Context; import android.util.AttributeSet; ...

  2. [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  3. beisen

    #include <stdio.h> #include <pthread.h> #include <windows.h> #define N 100 #define ...

  4. ubuntu下如何查看和设置分辨率

    ubuntu下如何查看和设置分辨率 在ubuntu下可以使用xrandr来设置自己需要的分辨率.大致步骤如下:  www.2cto.com   1.使用xrandr命令查看屏幕分辨率,这里我使用的是显 ...

  5. 【转】git rebase简介(基本篇)

    原文网址:http://blog.csdn.net/hudashi/article/details/7664631/ 原文: http://gitbook.liuhui998.com/4_2.html ...

  6. weixin js接口

    <!DOCTYPE HTML><html><head> <meta http-equiv="content-type" content=& ...

  7. ubuntu-设置分辨率

    xrandr -s 1440x900 -r 60 前提是,分辨率选项中有对应的设置选项.

  8. (转)Java进阶java int与Integer的区别

    Java进阶java int与Integer的区别 前言 int与Integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而Integer是对象,用一个引 ...

  9. 二.Windows I/O模型之异步选择(WSAAsyncSelect)模型

    1.基于windows消息为基础的网络事件io模型.因此我们必须要在窗口程序中使用该模型.该模型中的核心是调用WSAAsyncSelect函数实现异步I/O. 2.WSAAsyncSelect函数:注 ...

  10. 极光推送设置标签和别名无效的解决办法:JPush设置别名不走成功回调

    极光推送设置标签和别名无效的解决办法 JPush设置别名不走成功回调的解决办法 http://www.cnblogs.com/chenqitao/p/5506023.html 主要是网络加载过快导致的 ...