博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sgu116
阅读量:4977 次
发布时间:2019-06-12

本文共 2192 字,大约阅读时间需要 7 分钟。

 Index of super-prime

time limit per test: 0.25 sec. 

memory limit per test: 4096 KB

 

Let P1, P2, … ,PN, … be a sequence of prime numbers. Super-prime number is such a prime number that its current number in prime numbers sequence is a prime number too. For example, 3 is a super-prime number, but 7 is not. Index of super-prime for number is 0 iff it is impossible to present it as a sum of few (maybe one) super-prime numbers, and if such presentation exists, index is equal to minimal number of items in such presentation. Your task is to find index of super-prime for given numbers and find optimal presentation as a sum of super-primes.

 

Input

There is a positive integer number in input. Number is not more than 10000.

 

Output

Write index I for given number as the first number in line. Write I super-primes numbers that are items in optimal presentation for given number. Write these numbers in order of non-increasing.

 

Sample Input

6

Sample Output

23 3
#include
#include
#include
using namespace std;const int maxn = 1000;int Q, Ps[maxn] = {0}, cnt = 0, snt = 0;bool isprime(int p){ if(p == 2) return true; if(p % 2 == 0) return false; for(int i = 3, j= 9; j <= p; i+=2, j = i*i) { if(p %i == 0) return false; } return true;}void init(){ cin >> Q; cnt = 1; for(int i = 3; i <= Q; i++) { if(isprime(i)) { cnt++; if(isprime(cnt)) { Ps[snt++] = i; } } }}bool G[maxn * 10] = {0};int F[maxn * 10] = {0};int P[maxn * 10] = {0};int DP(int x){ if(G[x]) return F[x]; G[x] = true; for(int i = 0; i < snt; i++) { if(x >= Ps[i]) { if(DP(x-Ps[i]) < F[x]) { F[x] = F[x-Ps[i]]; P[x] = Ps[i]; } } } F[x]++; return F[x];}bool cmp(const int & x, const int & y){ return x > y;}void print(int x){ int ans[maxn], cnt = 0; if(P[x] == 0) return; while(x) { ans[cnt++] = P[x]; x -= P[x]; } sort(ans, ans+cnt, cmp); cout << ans[0]; for(int i = 1; i < cnt; i++) cout << " " << ans[i];}int main(){ init(); for(int i = 0; i <= Q; i++) F[i]=~0U >> 3; F[0] = 0; G[0] = true; if(DP(Q) > 10000) cout<< 0 << endl; else { cout << DP(Q) << endl; print(Q); } return 0;}

  

 

转载于:https://www.cnblogs.com/LyningCoder/p/3728085.html

你可能感兴趣的文章
在Visual Studio 2005中调试SQL Server 2005的存储过程
查看>>
浅析C#基于TCP协议的SCOKET通信
查看>>
文件资源使用Texture管理cocosBuilder项目资源:纹理文件使用(TexturePacker)
查看>>
Java Web应用CAS Client端的配置详解
查看>>
MapGIS计算瓦片数据集
查看>>
你最美好的年华
查看>>
中兴MF667S WCDMA猫Linux拨号笔记
查看>>
jQuery
查看>>
探究绑定事件的this指向以及event传参的小问题
查看>>
BOM window对象 localtion navigator
查看>>
Linux的.pid文件
查看>>
unity性能优化-CPU
查看>>
使用ssh正向连接、反向连接、做socks代理的方法
查看>>
IOS AppStore介绍图的尺寸大小(还有一些自己被拒的分享...)
查看>>
Android 实现在线程中联网
查看>>
Akka(30): Http:High-Level-Api,Routing DSL
查看>>
第八章:FTP publisher plugin插件下载(支持绝对路径)
查看>>
HDU5779 Tower Defence (BestCoder Round #85 D) 计数dp
查看>>
storm学习笔记
查看>>
进程与线程杂谈
查看>>