hdu 5104 Primes Problem(素数判定)
来源:互联网 发布:java开发oa系统 编辑:程序博客网 时间:2024/06/11 02:04
Primes Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2838 Accepted Submission(s): 1275
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
39
Sample Output
02
题意:给一个n,问有多少种方式能找到三个非递减的素数之和等于n
思路:用素数筛选法记下所有的素数,然后两重循环枚举即可。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 2010int prime[N],vis[N*10];int cnt;void init(){ cnt=0; memset(vis,0,sizeof(vis)); vis[0]=vis[1]=1; for(int i=2;i<=10000;i++) { if(!vis[i]) { prime[cnt++]=i; for(int j=i*i;j<=10000;j+=i) vis[j]=1; } }}int main(){ int n; init(); while(~scanf("%d",&n)) { long long ans=0; for(int i=0;i<cnt&&prime[i]<=n;i++) { for(int j=i;j<cnt&&prime[i]+prime[j]<=n;j++) { int v=n-prime[i]-prime[j]; if(v>=prime[j]&&!vis[v]) ans++; } } printf("%lld\n",ans); } return 0;}
0 0
- hdu 5104 Primes Problem(素数判定)
- HDU 5104 Primes Problem(素数打表)
- HDU 5104 Primes Problem
- HDU 5104 Primes Problem
- hdu 5104 Primes Problem
- HDU-#5104 Primes Problem
- HDU 5104 Primes Problem
- hdu 5104 Primes Problem
- HDU 5104 Primes Problem
- hdu 5104 Primes Problem
- HDU 5104 Primes Problem
- HDU 5104-Primes Problem
- HDU 5104 Primes Problem(拆分素数和问题)【BestCoder Round #18 1001】
- HDU 5104 Primes Problem(数学)
- HDU 5104 Primes Problem(打表)
- hdoj Primes Problem 5104 (素数打表&&技巧)
- hdoj-5104Primes Problem(素数打表)
- hdu 5104 Primes Problem(数学)
- 关于app的几个核心功能的设计想法
- BZOJ 1115: [POI2009]石子游戏Kam
- oracle 12c:新特性-截断表CASCADE
- 【NOIP2016模拟赛No.1】 牛宫
- 计算时间复杂度
- hdu 5104 Primes Problem(素数判定)
- 冒泡排序的实现
- miller-rabin
- CListCtrl
- 1068. Find More Coins (30)
- SVN安装问题
- perl json模块
- 请求ajax失败或者不执行的原因
- 设置absolute后元素宽度问题