lightoj 1259
来源:互联网 发布:河南民生频道网络直播 编辑:程序博客网 时间:2024/06/09 13:55
Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:
Every even integer, greater than 2, can be expressed as the sum of two primes [1].
Now your task is to check whether this conjecture holds for integers up to 107.
Input
Input starts with an integer T (≤ 300), denoting the number of test cases.
Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).
Output
For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b) where
1) Both a and b are prime
2) a + b = n
3) a ≤ b
Sample Input
Output for Sample Input
2
6
4
Case 1: 1
Case 2: 1
Note
1. An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13, ...
#include <stdio.h>const int MAXN = 1e7 + 10;int prime[700005];bool isprime[MAXN];void init() { int p = 0; for (int i = 0; i < MAXN; i++) isprime[i] = true; isprime[0] = isprime[1] = false; for (int i = 2; i < MAXN; i++) { if (isprime[i]) { prime[p++] = i; for (int j = 2*i; j < MAXN; j += i) isprime[j] = false; } }}int main(int argc, char const *argv[]){ int T; init(); scanf("%d", &T); int Kcase = 0; while (T--) { int N; scanf("%d", &N); int ans = 0; for (int i = 0; 2*prime[i] <= N; i++) { if (isprime[N - prime[i]]) ans++; } printf("%s%d: %d\n", "Case ", ++Kcase, ans); } return 0;}
- LightOJ 1259
- LightOJ 1259
- LightOJ 1259
- lightoj 1259
- LightOJ-1259
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- [LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- Java集合(二):List列表
- 2017微众银行校招实习
- 一个只有99行代码的JS流程框架
- 实例介绍利用valgrind定位内存泄漏问题
- Rsync文件同步
- lightoj 1259
- 设计模式之工厂模式
- 今日头条血槽已空
- Hdu 3966 . Aragorn's Story
- ORA-01950: no privileges on tablespace 'USERS'--解决办法
- 字母变成下一字母
- Java集合(三):Queue队列
- 腾讯云上PhantomJS用法示例
- AngularJS入门