Skip to content

Commit 5e0322a

Browse files
authored
Merge pull request #30 from tossy310/kumachan/even2primes
Even2Primes(writer:kumachan)
2 parents 876d15e + f6e864f commit 5e0322a

File tree

20 files changed

+508
-0
lines changed

20 files changed

+508
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.Scanner;
2+
import java.util.stream.IntStream;
3+
4+
public class Even2PrimesNaive {
5+
private static final Scanner sc = new Scanner(System.in);
6+
7+
private static boolean isPrime(final int x) {
8+
for (int i = 2; i < x; i++) {
9+
if (x % i == 0) return false;
10+
}
11+
return true;
12+
}
13+
14+
private static int solve(final int x) {
15+
int answer = 0;
16+
for (int j = 2; j <= x / 2; j++) {
17+
if (isPrime(j) && isPrime(x - j)) {
18+
answer++;
19+
}
20+
}
21+
return answer;
22+
}
23+
24+
public static void main(final String[] args) {
25+
final int T = sc.nextInt();
26+
for (int i = 0; i < T; i++) {
27+
final int x = sc.nextInt();
28+
System.out.println(solve(x));
29+
}
30+
}
31+
}

enen2primes/java-tossy-tle/SOLUTION

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8; mode: python -*-
2+
3+
## Solution
4+
#c_solution(src='main.c') # -lm -O2 as default
5+
#cxx_solution(src='main.cc', flags=[]) # -std=c++11 -O2 as default
6+
#java_solution(src='Main.java', encoding='UTF-8', mainclass='Main')
7+
java_solution(src='Even2PrimesNaive.java', encoding='UTF-8', mainclass='Even2PrimesNaive',
8+
challenge_cases=['large.in'])
9+
#java_solution(src='Main.java', encoding='UTF-8', mainclass='Main',
10+
# challenge_cases=['10_corner*.in'])
11+
#script_solution(src='main.sh') # shebang line is required
12+
#script_solution(src='main.pl') # shebang line is required
13+
#script_solution(src='main.py') # shebang line is required
14+
#script_solution(src='main.rb') # shebang line is required
15+
#js_solution(src='main.js') # javascript (nodejs)
16+
#hs_solution(src='main.hs') # haskell (stack + ghc)
17+
#cs_solution(src='main.cs') # C# (mono)
18+
19+
## Score
20+
#expected_score(100)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class Even2Primes {
5+
private static final Scanner sc = new Scanner(System.in);
6+
private static final int MAX_N = 5000000;
7+
private static boolean isPrime[] = new boolean[MAX_N + 1];
8+
9+
private static void primeList() {
10+
Arrays.fill(isPrime, true);
11+
isPrime[1] = false;
12+
isPrime[2] = true;
13+
for (int i = 4; i <= MAX_N; i += 2) {
14+
isPrime[i] = false;
15+
}
16+
for (int i = 3; i * i <= MAX_N; i += 2) {
17+
if (!isPrime[i]) continue;
18+
// isPrime[i] = true;
19+
for (int j = i * i; j <= MAX_N; j += i) {
20+
isPrime[j] = false;
21+
}
22+
}
23+
}
24+
25+
public static void main(final String[] args) {
26+
primeList();
27+
final int T = sc.nextInt();
28+
for (int i = 0; i < T; i++) {
29+
int x = sc.nextInt();
30+
int answer = 0;
31+
for (int j = 2; j <= x / 2; j++) {
32+
if (isPrime[j] && isPrime[x - j]) {
33+
answer++;
34+
}
35+
}
36+
System.out.println(answer);
37+
}
38+
}
39+
}

enen2primes/java-tossy/SOLUTION

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8; mode: python -*-
2+
3+
## Solution
4+
#c_solution(src='main.c') # -lm -O2 as default
5+
#cxx_solution(src='main.cc', flags=[]) # -std=c++11 -O2 as default
6+
java_solution(src='Even2Primes.java', encoding='UTF-8', mainclass='Even2Primes')
7+
#java_solution(src='Main.java', encoding='UTF-8', mainclass='Main',
8+
# challenge_cases=[])
9+
#java_solution(src='Main.java', encoding='UTF-8', mainclass='Main',
10+
# challenge_cases=['10_corner*.in'])
11+
#script_solution(src='main.sh') # shebang line is required
12+
#script_solution(src='main.pl') # shebang line is required
13+
#script_solution(src='main.py') # shebang line is required
14+
#script_solution(src='main.rb') # shebang line is required
15+
#js_solution(src='main.js') # javascript (nodejs)
16+
#hs_solution(src='main.hs') # haskell (stack + ghc)
17+
#cs_solution(src='main.cs') # C# (mono)
18+
19+
## Score
20+
#expected_score(100)

even2primes/PROBLEM

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8; mode: python -*-
2+
3+
pid='X'
4+
5+
problem(
6+
time_limit=5.0,
7+
id=pid,
8+
title=pid + "Even2Primes",
9+
#wiki_name="Your pukiwiki page name", # for wikify plugin
10+
#assignees=['Assignees', 'for', 'this', 'problem'], # for wikify plugin
11+
#need_custom_judge=True, # for wikify plugin
12+
reference_solution='cpp-correct',
13+
)
14+
15+
atcoder_config(
16+
task_id=None # None means a spare
17+
)

even2primes/c-correct/SOLUTION

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: utf-8; mode: python -*-
2+
3+
## Solution definition.
4+
## Choose and modify one of definitions below.
5+
## Adding a parameter challenge_cases marks this solution as a wrong solution.
6+
c_solution(src='main.c')
7+
#cxx_solution(src='main.cc')
8+
#java_solution(src='Main.java', encoding='UTF-8', mainclass='Main')
9+
#script_solution(src='main.py')

even2primes/c-correct/main.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdio.h>
2+
3+
#define INC(i, a, b) for((i) = (a); (i) < (b); ++(i))
4+
#define REP(i, n) INC(i, 0, n)
5+
#define maxN 5000000
6+
7+
char isPrime[maxN + 1];
8+
9+
void uN_prime(){
10+
int i, k;
11+
isPrime[0] = 0;
12+
isPrime[1] = 0;
13+
isPrime[2] = 1;
14+
isPrime[3] = 1;
15+
16+
INC(i, 4, maxN + 1){
17+
if(i % 6 == 1 || i % 6 == 5){
18+
isPrime[i] = 1;
19+
}else{
20+
isPrime[i] = 0;
21+
}
22+
}
23+
24+
INC(i, 5, maxN + 1){
25+
if(isPrime[i]){
26+
INC(k, 2, maxN/i + 1){
27+
isPrime[i * k] = 0;
28+
}
29+
}
30+
}
31+
}
32+
33+
int main(){
34+
35+
uN_prime();
36+
37+
int T;
38+
scanf("%d", &T);
39+
40+
int E;
41+
int t, p;
42+
int ans = 0;
43+
REP(t, T){
44+
ans = 0;
45+
scanf("%d", &E);
46+
INC(p, 2, E/2 + 1){
47+
if(isPrime[p] == 1 && isPrime[E - p] == 1){
48+
ans++;
49+
}
50+
}
51+
printf("%d\n", ans);
52+
}
53+
return 0;
54+
}

even2primes/cpp-correct/SOLUTION

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: utf-8; mode: python -*-
2+
3+
## Solution definition.
4+
## Choose and modify one of definitions below.
5+
## Adding a parameter challenge_cases marks this solution as a wrong solution.
6+
#c_solution(src='main.c')
7+
cxx_solution(src='main.cpp')
8+
#java_solution(src='Main.java', encoding='UTF-8', mainclass='Main')
9+
#script_solution(src='main.py')

even2primes/cpp-correct/main.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
#define INC(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
3+
#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
4+
#define maxN 5000000
5+
using namespace std;
6+
7+
bool isPrime[maxN + 1];
8+
9+
void uN_prime(){
10+
isPrime[0] = false;
11+
isPrime[1] = false;
12+
isPrime[2] = true;
13+
isPrime[3] = true;
14+
15+
INC(i, 4, maxN + 1){
16+
if(i % 6 == 1 || i % 6 == 5){
17+
isPrime[i] = true;
18+
}else{
19+
isPrime[i] = false;
20+
}
21+
}
22+
23+
INC(i, 5, maxN + 1){
24+
if(isPrime[i]){
25+
INC(k, 2, maxN/i + 1){
26+
isPrime[i * k] = false;
27+
}
28+
}
29+
}
30+
}
31+
32+
int main(){
33+
34+
uN_prime();
35+
36+
int T;
37+
cin >> T;
38+
39+
int E;
40+
int ans = 0;
41+
REP(t, T){
42+
ans = 0;
43+
cin >> E;
44+
INC(p, 2, E/2 + 1){
45+
if(isPrime[p] && isPrime[E - p]){
46+
ans++;
47+
}
48+
}
49+
cout << ans << endl;
50+
}
51+
return 0;
52+
}

even2primes/cpp-ryoissy/SOLUTION

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: utf-8; mode: python -*-
2+
3+
## Solution definition.
4+
## Choose and modify one of definitions below.
5+
## Adding a parameter challenge_cases marks this solution as a wrong solution.
6+
#c_solution(src='main.c')
7+
cxx_solution(src='ryoissy-even2primes.cpp')
8+
#java_solution(src='Main.java', encoding='UTF-8', mainclass='Main')
9+
#script_solution(src='main.py')

0 commit comments

Comments
 (0)