hdu_5104 Primes Problem()
2024-10-19 01:25:30
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5104
rimes Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2844 Accepted Submission(s): 1277
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
9
Sample Output
0
2
2
题意:输入一个数字n,找出三个数字p1,p2,p3,满足p1<=p2<=p3并且p1+p2+p3=n
//筛素数
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = ;
bool pri[N];
int prime[N];
int cnt;
void init()
{
cnt = ;
pri[] = pri[] = ;
for(int i = ; i < N; i++)
{
if(!pri[i]){
prime[cnt++] = i;
for(int j = i+i; j < N; j+=i)
{
pri[j] = ;
}
}
}
return;
}
int main()
{
int n;
init();
while(~scanf("%d",&n))
{
int ans = ; //printf("%d\n",cnt);
for(int i = ; i < cnt; i++)
{
if(*prime[i]>n) break;
for(int j = i; j < cnt; j++)
{
if(prime[i]+*prime[j]>n) break;
//for(int k = j; k < cnt; k++)
//{
// if(prime[i]+prime[j]+prime[k]==n) ans++;// printf("%d %d %d\n",prime[i],prime[j],prime[k]);printf("%d %d %d\n",i,j,k);}
// }
if(!pri[n-prime[i]-prime[j]]) ans++;
}
}
printf("%d\n",ans);
}
return ;
}
最新文章
- ORA-12520:TNS:监听程序无法为请求的服务器类型找到可用的处理程序
- [转]Spring3 MVC + jQuery easyUI 做的ajax版本用户管理
- 八款你不得不知的开源前端JS框架
- SharePoint 2010中使用SPListItemCollectionPosition更快的结果
- discuz X3.1的门户文章实现伪静态,利于搜索引擎收录url的地址修改
- python apschedule安装使用与源码分析
- git使用过程中遇到的问题及处理方法
- linux shell 基本规范
- parted分区和挂载及非交互式操作
- spring javaconfig druidsource
- (转)用JS获取地址栏参数的方法(超级简单)
- build.gradle
- 一些安全相关的HTTP header
- Codeforces Round #493 (Div. 2)
- webstorm我用到的快捷键【不断更新】
- 涨知识:equals 和 == 你真的了解吗?
- 使用CSS进行定位
- 生成activiti需要的25张系统表
- 「Python」pandas入门教程
- Linux汇编教程02:编写第一个汇编程序