zoukankan      html  css  js  c++  java
  • 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(n10000).
     
    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
    ",ans);
    44     }
    45 }
  • 相关阅读:
    【BJOI2018】求和
    【洛谷P1613】跑路
    【NOI2001】食物链
    【NOI2002】银河英雄传说
    【POJ1456】Supermarket
    【NOIP2013】货车运输
    【CH#56C】异象石
    【POJ3417】Network
    【APIO2010】巡逻
    【CH6201】走廊泼水节
  • 原文地址:https://www.cnblogs.com/pblr/p/4758343.html
Copyright © 2011-2022 走看看