please help me to find why i am getting Wrong Answer for this Bytelandian gold coins question.
here is my solution COINS
#include <iostream>
#include <cstdio>
#include <map>
#define max2(a, b) ((a) > (b) ? (a) : (b))
using namespace std;
map <long long , long long > C;
long long f(long long n)
{
if (n == 0) return 0;
long long r = C[n];
if (r == 0)
{
r = max2( n , f(n/2)+f(n/3)+f(n/4) );
C[n] = r;
}
return r;
}
int main()
{
int t;
long long n;
scanf("%d",&t);
while(t--){
scanf("%lld",&n);
printf("%lld\n",f(n));
}
return 0;
}
I am new to dynamic programming,so I google for this question and i am trying to implement
from SPOJ_Coins,
but still getting wrong answer on both codechef.com and spoj.
For this problem, you have to take input until EOF. But you are taking input using test case.
Try this code:
#include <iostream>
#include <cstdio>
#include <map>
#define max2(a, b) ((a) > (b) ? (a) : (b))
using namespace std;
map <long long , long long > C;
long long f(long long n)
{
if (n == 0) return 0;
long long r = C[n];
if (r == 0)
{
r = max2( n , f(n/2)+f(n/3)+f(n/4) );
C[n] = r;
}
return r;
}
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
long long n;
{
while(scanf("%lld",&n)==1)
{
printf("%lld\n",f(n));
}
}
return 0;
}
Related
#include <bits/stdc++.h>
#define mx 1000005
#define mod 1000003
using namespace std;
long long arr[mx];
int fact()
{
arr[0]=1;
for(int i=1; i<mx; i++)
{
arr[i]=((i%mod)*(arr[i-1]%mod))%mod;
}
}
int main()
{
int t;
long long a,b,C,E;
fact();
cin>>t;
while(t--)
{
cin>>a>>b;
C=(arr[a]%mod)%mod;
E=((arr[b])%mod)*((arr[a-b])%mod)%mod;
}
}
In this problem i have to calculate (C/E)%1000003. How could i do this using modular multiplicative inverse technique ? Is there other way to calculate this ?
Since 1000003 is prime.
long long mod = 1000003;
inline long long mpow(long long b, long long ex){
if (b==1)return 1;
long long r = 1;
while (ex ){
if (ex&1)r=(r * b)%mod;
ex = ex >> 1;
b = (b * b)%mod;}
return r;
}
Then do
inverse of E % mod is = mpow(E,mod-2)
Fermats's little theorem
geekforgeeks
You can find the problem statement here.
My solution can be found here. I am getting correct answers for the test cases but the judge is indicating all answers wrong. What is wrong with my code?
#include <iostream>
#include <vector>
#include <map>
#include <math.h>
using namespace std;
map<long long,long long> arr;
void preProcess();
long long powerOfThree(long long num);
long long powerOfTwo(long long num);
long long theta(long long num);
int main()
{
preProcess();
long long t,k;
cin>>t;
while(t--)
{
cin>>k;
cout<<theta(k)<<endl;
}
}
void preProcess()
{
arr.insert(pair<long long,long long>(0,0));//arr[0]=0;
arr.insert(pair<long long,long long>(1,0));//arr[1]=0;
arr.insert(pair<long long,long long>(2,1));//arr[2]=1;
arr.insert(pair<long long,long long>(3,1));//arr[3]=1;
//arr.insert(pair<long long,long long>(4,2));//arr[4]=2;
//arr.insert(pair<long long,long long>(5,3));//arr[5]=3;
}
long long powerOfTwo(long long num)
{
long long ret=0;
while(num%2==0)
{
ret++;
num = num/2;
}
return ret;
}
long long theta(long long num)
{
long long ret;
map<long long,long long>::iterator it = arr.find(num);
if(it != arr.end())
{
return arr[num];
}
else
{
if(num%2==0)
{
long long a = powerOfTwo(num);
long long q = (num/(long long)pow(2,a));
ret = a + theta(q);
}
else if(num%3==0)
{
long long a = powerOfThree(num);
long long q = (num/(long long)pow(3,a));
ret = a + theta(q);
}
else
{
ret = 1 + theta(num-1);
}
}
arr.insert(pair<long long,long long>(num,ret));//arr[num]=ret;
return ret;
}
long long powerOfThree(long long num)
{
long long ret=0;
while(num%3==0)
{
ret++;
num = num/3;
}
return ret;
}
My approach is to first count all the powers of 2 and 3 in number and then reduce the result by 1. Again repeat the process till it is 1.
your method will fail in a number say for eg (2^8 + 1) *3^2. There would infact be many such numbers.
I would give a solution in Python(bcoz thats what I am comfortable in):
step = [0,0,1,1]
def find(a):
global step
t = len(step)
for n in range(t,a+1):
if ((n%3 == 0) and (n%2 == 0)):
step.append( 1 + min(step[n-1],step[n/2],step[n/3]))
elif (n%2==0):
step.append( 1 + min(step[n-1],step[n/2]))
elif (n%3 == 0) :
step.append( 1 + min(step[n-1],step[n/3]))
else :
step.append( 1 + step[n-1])
return step[a]
n = int(raw_input())
arr = []
for a in range(0,n):
arr.append(raw_input())
for st in arr:
n = int(st)
print find(n)
Hi i have problem in implementing nCr MODm in code sprint5 problem.
Link to the problem is ......
https://www.hackerrank.com/contests/codesprint5/challenges/matrix-tracing.
what I learned yet is I can apply rules of mudular arithmatic to factorial computation and inverse factorial computation and also to computing pow(a,b) MODm.But I don't know what I am missing which is leading to wrong answer.
Here is my current code .
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include<math.h>
using namespace std;
const int md = 1000000007;
const int co = 2000020;
unsigned long long int ft[co];
long long int fact(unsigned long long int n)
{
return ft[n];
}
void fct(){
ft[1]=1;
for(unsigned long long int i = 2;i<=2000020;i++){
ft[i]=(i*ft[i-1]) % md;
}
}
long long int pow(long long int x, long long int n, long long int mod){
long long int result=1;
while(n>0){
if(n%2 ==1){
result = (result*x) % mod;
}
n= n>>1;
x= (x*x)% mod;
}
return result;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
unsigned long long int m , n;
long long result;
int T;
fct();
cin>>T;
while(T--){
cin>>m>>n;
unsigned long long int mod = md-2;
result = (fact(m+n-2) * pow( ( fact(m-1) * fact(n-1) ) , mod, md )) % md ;
cout<<result<<endl;
}
return 0;
}
Finally I got the errors in my code.
errors....
I should use constant variables md and co as unsigned long long
int instead of only int
Second error is in algorithm for computing pow(a,b) % md ..... in pow()
function, I should first do x % md before processing further
because there is probability that x can be passed greater than md .
current working code is.....
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include<math.h>
using namespace std;
const unsigned long long int md = 1000000007;
const unsigned long long int co = 2000020;
unsigned long long int ft[co];
unsigned long long int fact(unsigned long long int n)
{
return ft[n];
}
void fct(){
ft[0]=1;
for(unsigned long long int i = 1;i<=2000020;i++){
ft[i]=(i*ft[i-1]) % md;
}
}
unsigned long long int pow(unsigned long long int x, unsigned long long int n, unsigned long long int mod){
unsigned long long int result=1;
x = x % md;
while(n>0){
if(n%2 ==1){
result = (result*x) % md;
}
n= n>>1;
x= (x*x)% md;
}
return result;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
unsigned long long int m , n;
unsigned long long int result;
int T;
fct();
cin>>T;
while(T--){
cin>>m>>n;
unsigned long long int mod = md-2;
result = (fact(m+n-2) * pow( ( fact(m-1) * fact(n-1) ) , mod, md )) % md ;
cout<<result<<endl;
}
return 0;
}
I wrote the following code to calculate n!modulo p...Given that n and p are close...but its running in a rather funny way, cant figure out the bug..There is some overflow somewhere..The constraints are 1 < P <= 2*10^9
1 <= N <= 2*10^9
though it runs fine for few cases...what could be the error.I have used
(a/b)mod p = ((a mod p)*(b^(p-2))mod p)mod p
as p is prime....and wilsons theorem that (p-1)! mod p = p-1
#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
unsigned int pow(unsigned int a, unsigned n,unsigned int p) {
unsigned int ret = 1;
while(n) {
if((n&1)== 1) ret=(ret*a)%p;
a=a%p;
a=(a*a)%p;
n=n>>1;
}
return ret;
}
int main(){_
int t;
cin>>t;
while(t--){
unsigned int n,p;
long long int r;
cin>>n>>p;
r=p-1;
if(n>=p){
cout<<"0\n";
}
else{
for(unsigned int i=p-1;i>n;i--){
r=((long long)r*pow(i,p-2,p))%p;
}
cout<<r<<"\n";
}
}
return 0;
}
21! is 51090942171709440000, while 2^64 is only 1844674407370955161: so if unsigned long long is a 64-bit quantity (as is likely), it doesn't fit.
I am solving this problem -> http://www.spoj.com/problems/COINS/ . A very simple DP problem with a very straight forward DP approach.I found enough hints in the problem statement to use DP. All the test cases are running perfectly in my compiler but I am getting a WA in SPOJ. My code is as follows :
My code
#include <cstdio>
#include <map>
#include <cstring>
#include<algorithm>
using namespace std;
map< long long,long long > data;
map < long long,long long> :: iterator p;
int max(int a,int b)
{
if(a>b)return a;
return b;
}
long long calc(int n)
{
long long c;
if(n==0 || n==1 || n==2)
return n;
p = data.find(n);
if(p==data.end())
{
c = max(n, calc(n/2) + calc(n/3) + calc(n/4));
data.insert(p, pair < long long, long long > (n, c));
return c;
}
else return (*p).second;
}
int main()
{
int t;
long long n;
scanf("%d",&t);
if(t>10)return 0;
while(t--)
{
scanf("%lld",&n);
if(n<0 || n>1000000000)
break;
data.clear();
printf("%lld",calc(n));
}
return 0;
}
I am finding it really difficult for me to figure out where I am going wrong!
A test case which contradicts my code would also do.
Perhaps a stack overflow in calculate. The recursion is killing your program :-)
Or simply the fact that calculate(1000000000) is too much slow.
Use Dynamic programming , store your result in an array . Since the value can go upto 10^9, and you cann't take array of that size , just take array of size till 10^6 and store their result and rest value calculate using simple recursion .
Here is a solution in python
import sys
mydict = {}
def count(n):
if n <= 5:
return n
elif n in mydict.keys():
return mydict[n]
else:
k=max(n,count(n / 2) + count(n / 3) + count(n / 4))
mydict[n]=k
return mydict[n]
for line in sys.stdin:
res = count(int(line))
print(int(res))
Here's my solution, uses dp :
#include<bits/stdc++.h>
using namespace std;
map<long long int,long long int> m;
long long int dp(long long int k){
long long int a;
if(k==0){
return 0;
}
a=m[k];
/* if(k<12){
return k;
} */
if(k<12){
return k;
}
else if(a==0){
a=max(k,dp(k/2)+dp(k/4)+dp(k/3));
m[k]=a;
}
return a;
}
int main(){
long long int n,t;
while(scanf("%lld",&n)>0){
t=dp(n);
cout << t << endl;
}
return 0;
}