0% found this document useful (0 votes)
26 views2 pages

Prime Subtractorization Aayush Dhankecha Aayush Dhankecha Source Code

Uploaded by

pemacox845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Prime Subtractorization Aayush Dhankecha Aayush Dhankecha Source Code

Uploaded by

pemacox845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<bits/stdc++.

h>
using namespace std;
#define ll long long
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("Ofast")
#define endl '\n'
#define MOD 1000000007
#define all(v) (v).begin(),(v).end()

#ifndef ONLINE_JUDGE
#include "dbg.hpp"
#else
#define debug(...) 18
#endif

#define Gili_gili_chhu ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);


bool isprime(ll number){if (number <= 1)return false;if (number == 2)return true;if
(number % 2 == 0) return false;int boundary = (int)floor(sqrt(number));for (int i =
3; i <= boundary; i += 2)if (number % i == 0)return false;return true;}
ll power (ll a, ll b){ll ans=1;while(b)
{if(b&1)ans=(ans*a)%MOD;a=(a*a)%MOD;b=b/2;}return ans;}
vector<int> Sieve(int n){vector<bool> is_prime(n+1,true);vector<int>
prime;is_prime[0]=is_prime[1]=false;for(int i=2;i<=n;i++){if(is_prime[i])
prime.push_back(i);if(is_prime[i] && (long long)i*i<= n){for(int
j=i*i;j<=n;j+=i)is_prime[j]=false;}}return prime;}

vector<int> pr = Sieve(10000001);
vector<int> ans(10000005,0);

void solve()
{
int n; cin>>n;
if(n<=3){
cout<<0<<endl; return;
}
auto it=upper_bound(all(pr), n);
it--;
cout<<ans[(*it)]<<endl;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("prime_subtractorization_input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ans[2]=ans[3]=1;
for(int i=0; i<(int)pr.size(); i++){
if(pr[i]<=3) continue;
if(pr[i]==5){
ans[pr[i]]=2; continue;
}
int dif=pr[i]-pr[i-1];
if(ans[dif]>0) ans[pr[i]]=ans[pr[i-1]]+1;
else ans[pr[i]]=ans[pr[i-1]];
}
Gili_gili_chhu
int t = 1;
cin >> t;
int i = 1;
while (i <= t){
cout << "Case #" << i << ": ";
solve();
i++;
}

//cout << "\n\n"<<"Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "
seconds.\n";
return 0;
}

You might also like