hdu 5104 Primes Problem (素数+递推)

Problem Description

Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.

Input

Multiple test cases(less than 100), for each test case, the only line indicates the positive integer n(n≤10000).

Output

For each test case, print the number of ways.

Sample Input

3

9

Sample Output

0

2

先找出所有素数,再递推。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int N=10005;
 5 int zj[N],ssb[N],pn,ans,n;
 6 void f()
 7 {
 8     int i,j;
 9     zj[0]=zj[1]=1;
10     pn=0;
11     for (i=2;i<N;i++)
12     {
13         if (!zj[i]) ssb[pn++]=i;
14         for (j=0;j<pn;j++)
15         {
16             if (i*ssb[j]>N) break;
17             zj[i*ssb[j]]=1;
18             if (i%ssb[j]==0) break;
19         }
20     }
21 }
22 void dfs(int sum,int x,int p)
23 {
24     if (p==2)
25     {
26         if (zj[n-sum]==0&&n-sum>=ssb[x]) ans++;
27         return ;
28     }
29     int i;
30     for (i=x;i<pn;i++)
31     {
32         if (sum+ssb[i]>=n) return ;
33         dfs(sum+ssb[i],i,p+1);
34     }
35 }
36 int main()
37 {
38     f();
39     while (~scanf("%d",&n))
40     {
41         ans=0;
42         dfs(0,0,0);
43         printf("%d\n",ans);
44     }
45 }
时间: 2024-11-05 19:40:32

hdu 5104 Primes Problem (素数+递推)的相关文章

hdu 5104 Primes Problem (素数 打表 水)

http://acm.hdu.edu.cn/showproblem.php?pid=5104 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; int isp[10000+100]; int prime[10000]; int coun; bool isprime(int x)

HDU 5104 Primes Problem(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5104 Problem Description Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n. Input Multiple test cases(less than 100), for each

hdu 5104 Primes Problem(prime 将三重循环化两重)

//宁用大量的二维不用量小的三维 #include <iostream> #include<cstdio> #include<cstring> using namespace std; int prime[1000],num[10005]; int Cout; void initPrime() { for(int i=2;i<=10000;i++) num[i]=1; for(int i=2;i<=10000;i++) { if(num[i]) { for(

hdu 4869 Turn the pokers(递推&amp;组合数学&amp;逆元)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1279    Accepted Submission(s): 466 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t

hdu 1284 钱币兑换问题 (递推 || DP || 母函数)

钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5069    Accepted Submission(s): 2868 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Outpu

HDU Tickets(简单的dp递推)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 972    Accepted Submission(s): 495 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

hdu 2050 折线分割平面 (递推)

折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15709    Accepted Submission(s): 10836 Problem Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面

uva10401Injured Queen Problem(递推)

题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现状,???3?4:在一个6*6的棋盘上,由于皇后是能够列向攻击的,所以一列仅仅能放一个皇后,所以第一个?代表第一列的皇后放的行未知,这样3的意思就是第4列皇后在第三行.也就是确定了第4列皇后的位置. 要求棋盘上的皇后不互相攻击.问这种棋盘能够有多少不同的放法. 解题思路:一開始想状态.想把不同的棋盘

HDU 3899 树形DP||树上递推

JLUCPC 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3899 Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1375    Accepted Submission(s): 413 Problem Description Dr. Skywind and Dr. Walkonclo