Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added GONE, LUCIFER, RAONE cpp solutions #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions GONE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Created by Pranav Gupta on 5/8/18

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000007
typedef long long ll;

ll dp[20][180][2];
bool isPrime[200];

void init(){
memset(isPrime,true,sizeof(isPrime));
isPrime[0] = isPrime[1] = false;
for(ll i=2;i*i<200;i++){
if(isPrime[i])
for(ll j=i*i;j<200;j+=i)
isPrime[j] = false;
}
}
void getDigits(ll a,vector<ll> &v){
while(a){
v.pb(a%10);
a /= 10;
}
}

ll helper(ll idx,ll sum,ll tight,vector<ll> &digit){
if(idx==-1){
return isPrime[sum];
}
if(dp[idx][sum][tight]!=-1)
return dp[idx][sum][tight];

ll k = tight ? digit[idx] : 9;
ll ans = 0;
for(ll i=0;i<=k;i++){
ll newTight = (digit[idx]==i) ? tight : 0;
ans += helper(idx-1,sum+i,newTight,digit);
}
return dp[idx][sum][tight] = ans;
}
ll solve(ll a,ll b){
memset(dp,-1,sizeof(dp));
vector<ll> da,db;

getDigits(a-1,da);
getDigits(b,db);

ll x = helper(db.size()-1,0,1,db);
memset(dp,-1,sizeof(dp));
ll y = helper(da.size()-1,0,1,da);
return x-y;
}
int main(){
ll t;
cin >> t;
init();
while(t--){
ll a,b;
cin >> a >> b;
cout << solve(a,b) << endl;
}
}
68 changes: 68 additions & 0 deletions LUCIFER.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Created by Pranav Gupta on 5/8/18.

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000007
typedef long long ll;

ll dp[20][180][180][2];
bool isPrime[200];

void init(){
memset(isPrime,true,sizeof(isPrime));
isPrime[0] = isPrime[1] = false;
for(ll i=2;i*i<200;i++){
if(isPrime[i])
for(ll j=i*i;j<200;j+=i)
isPrime[j] = false;
}
}
void getDigits(ll a,vector<int> &v){
while(a){
v.pb(a%10);
a /= 10;
}
}
ll helper(int idx,int sume,int sumo,int tight,vector<int> &digit){
if(idx==-1){
if(sume-sumo<0)
return 0;
return isPrime[sume-sumo];
}
if(dp[idx][sume][sumo][tight]!=-1)
return dp[idx][sume][sumo][tight];
int currIdx = idx+1;
int k = tight ? digit[idx] : 9;
ll ans = 0;
for(int i=0;i<=k;i++){
int newTight = (digit[idx]==i) ? tight : 0;
if(currIdx&1)
ans += helper(idx-1,sume,sumo+i,newTight,digit);
else
ans += helper(idx-1,sume+i,sumo,newTight,digit);
}
return dp[idx][sume][sumo][tight] = ans;
}
ll solve(ll a,ll b){
memset(dp,-1,sizeof(dp));
vector<int> da,db;

getDigits(a-1,da);
getDigits(b,db);

ll x = helper(db.size()-1,0,0,1,db);
memset(dp,-1,sizeof(dp));
ll y = helper(da.size()-1,0,0,1,da);
return x-y;
}
int main(){
int t;
cin >> t;
init();
while(t--){
int a,b;
cin >> a >> b;
cout << solve(a,b) << endl;
}
}
55 changes: 55 additions & 0 deletions RAONE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Created by Pranav Gupta on 5/8/18

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000007
typedef long long ll;

ll dp[20][180][180][2];

void getDigits(ll a,vector<int> &v){
while(a){
v.pb(a%10);
a /= 10;
}
}
ll helper(int idx,int sume,int sumo,int tight,vector<int> &digit){
if(idx==-1){
return (sume-sumo)==1;
}
if(dp[idx][sume][sumo][tight]!=-1)
return dp[idx][sume][sumo][tight];
int currIdx = idx+1;
int k = tight ? digit[idx] : 9;
ll ans = 0;
for(int i=0;i<=k;i++){
int newTight = (digit[idx]==i) ? tight : 0;
if(currIdx&1)
ans += helper(idx-1,sume,sumo+i,newTight,digit);
else
ans += helper(idx-1,sume+i,sumo,newTight,digit);
}
return dp[idx][sume][sumo][tight] = ans;
}
ll solve(ll a,ll b){
memset(dp,-1,sizeof(dp));
vector<int> da,db;

getDigits(a-1,da);
getDigits(b,db);

ll x = helper(db.size()-1,0,0,1,db);
memset(dp,-1,sizeof(dp));
ll y = helper(da.size()-1,0,0,1,da);
return x-y;
}
int main(){
int t;
cin >> t;
while(t--){
int a,b;
cin >> a >> b;
cout << solve(a,b) << endl;
}
}