-
Notifications
You must be signed in to change notification settings - Fork 0
/
goldbachs_conjecture.cpp
372 lines (237 loc) · 5.29 KB
/
goldbachs_conjecture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define tt(t) read(t); while(t--)
#define endl '\n'
#define vll vector<ll>
#define vvll vector< vll >
#define pll pair<ll ,ll >
#define pss pair < string , string >
#define vpll vector< pll >
#define vpss vector< pss >
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define inf 1e18;
#define find(v,x) ((v).find(x) != (v).end())
#define vfind(v,x) (find(all(v),x) != (v).end())
#define clr(c) (c).clear()
#define cres(c,n) (c).clear(),(c).resize(n)
#define ios ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define fst first
#define scd second
#define fr(i,n) for(ll (i) = 0 ; (i) < (n) ; ++(i))
#define fr1(i,n) for(ll (i) = 1 ; (i) <= (n) ; ++(i))
#define frr(i,n) for(ll (i) = (n)-1 ; (i)>=0 ; --(i))
#define frab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c))
#define mst(A) memset( (A) , 0 , sizeof(A) );
template< class T > void setmax(T &a, T b) { if(a < b) a = b; }
template< class T > void setmin(T &a, T b) { if(b < a) a = b; }
using namespace std;
void read(ll &x){
scanf("%lld",&x);
}
void read(ll &x,ll &y){
scanf("%lld %lld",&x,&y);
}
void read(ll &x,ll &y,ll &z){
scanf("%lld %lld %lld",&x,&y,&z);
}
void read(ll &x,ll &y,ll &z,ll &w){
scanf("%lld %lld %lld %lld",&x,&y,&z,&w);
}
void read(vll &oneD){
for(ll i=0;i<oneD.size();i++){
read(oneD[i]);
}
}
void read(vvll &twoD){
for(ll i=0;i<twoD.size();i++){
read(twoD[i]);
}
}
void write(vll &oneD){
for(ll i=0;i<oneD.size();i++){
printf("%lld ",oneD[i]);
}
printf("\n");
}
void write(vvll &twoD){
for(ll i=0;i<twoD.size();i++){
write(twoD[i]);
}
}
void write(vpll &oneDP){
fr(i,oneDP.size()){
printf("%lld %lld\n" , oneDP[i].fst , oneDP[i].scd);
}
cout<<"\n";
}
void write(map< ll , ll > &mpp){
for(map<ll , ll >::iterator it = mpp.begin() ; it != mpp.end() ; it++){
cout<<it->fst<<" : "<<it->scd<<endl;
}
cout<<endl;
}
bool cmp(const pair<ll,ll> &a,const pair<ll,ll> &b)
{
if(a.first == b.first)
return (a.scd >= b.scd);
return (a.first < b.first);
}
ll A[100005];
vll seive;
vll primes;
void Seive(){
const ll maxn = 100005;
seive.resize(maxn);
fr(i,maxn) seive[i] = 1;
//seive[1] = 0;
//seive[0] = 0;
for(ll i=2;i*i<maxn;i++)
{
if(seive[i]==1 )
{
for(ll j = i*i ; j <maxn ; j+=i)
{
seive[j] = 0;
}
}
}
primes.pb(2);
for(ll i=3;i<maxn;i+=2)
{
if(seive[i])
primes.pb(i);
}
}
void printprime(ll l,ll r)
{
if(l<2)
l=2;
bool isprime[r-l+1];
fr(i,r-l+1)
isprime[i]=true;
for( int i =0; primes[i]*primes[i] <= r; i++)
{
// Smaller or equal value to l
//cout<<primes[i]<<"primes[i] ";
ll base = (l/primes[i])*primes[i];
if(base < l)
{
base =base + primes[i];
}
//Mark all multiples of primes [i] as false
for( ll j= base ;j<=r; j+=primes[i])
{
isprime[j-l]= false;
}
// There may be a case where base itsef a prime
if(base == primes[i])
isprime[base-l]=true;
}
for( int i=0; i<r-l+1; i++)
{
if(isprime[i]==true)
cout<<i+l<<endl;
}
}
ll mulmod(ll a, ll b, ll mod)
{
ll res = 0; // Initialize result
a = a % mod;
while (b > 0)
{
// If b is odd, add 'a' to result
if (b % 2 == 1)
res = (res + a) % mod;
// Multiply 'a' with 2
a = (a * 2) % mod;
// Divide b by 2
b /= 2;
}
// Return result
return res % mod;
}
ll power(ll x, ll y , ll m){
long long int res = 1;
x = x % m;
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = mulmod(res,x,m);
// y must be even now
y = y>>1; // y = y/2
x = mulmod(x,x, m);
}
return res;
}
ll modinv(ll x , ll mod = MOD){
return power(x , mod - 2 , mod);
}
ll isprime(ll N){
if(N<2 || (!(N&1) && N!=2))
return 0;
for(ll i=3; i*i<=N; i+=2){
if(!(N%i))
return 0;
}
return 1;
}
//////////////////////////////////////////////////////////////////////////////////////
/*
1012325652652652
1000000000000000012
100000000000
100000000000000
*/
/*
193 1012325652652459
3 1000000000000000009
23 99999999977
29 99999999999971
*/
ll solve(ll n)
{
ll d,sum,m=n;
if (n<=2 || n%2 != 0)
{
cout << "No primes possible \n";
return 0;
}
for(int i=0; primes[i]<=n/2;i++)
{
sum=n-primes[i];
if(isprime(sum))
//if(binary_search(all(primes),sum))
{
d=primes[i];
cout<<d<<" "<<sum<<endl;
break;
}
}
return d;
}
int main()
{
ll a,b,c,d;
ll k,l,m,n,p,q,r,s,t;
ll A[100001],B[100001];
vpll v;
v.clear();
Seive();
tt(t)
{
read(n);
if(n<=2){
cout<<"NO\n";continue;
}
if(n==3){
cout<<"1 2"<<endl; continue;}
m=solve(n);
//cout<<m<<" "<<n-m<<endl;
}
}