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;
}
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 have N different balls numbered from 1 to N, and M different boxes numbered from 1 to M.
Input:
First line of input contains the number of test cases T. After that, next T lines contain the value of N and M.
Output:
For each test case, print the answer. As it can be very large, you should print it modulo 10^9 + 7.
I tried the below code, but it gives an error:
#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
unsigned short int T;
unsigned long int N,M;
cin>>T;
for (int i = 0; i < T; i++)
{
cin>>N>>M;
long int res;
res= pow(M,N);
int c=0;
c=pow(10,9);
res=res%(c + 7);
cout<<res<<endl;
}
return 0;
}
You must be facing integer overflow problem, that's why you must have been getting wrong answer.
Do the following steps to fix this problem.
change the unsigned long to long long or unsigned long long. (Why? Think).
Use the logarithmic user-defined function to calculate the value of the res = pow(M,N) along with the modulo consideration side-by-side. This will boost up your program.
See my code snippet to check what changes to be made:
#include<iostream>
#define MOD 1000000007
int main() {
unsigned short int T;
unsigned long long N , M , result;
unsigned long long power(unsigned long long, unsigned long long); /*prototype of power*/
std::cin>>T;
for (int i = 0; i < T; i++) {
std::cin >> N >> M;
result = power(M , N);
std::cout << result << std::endl;
}
return 0;
}
unsigned long long power(unsigned long long M, unsigned long long N) {
if(N == 0) {
return 1;
}
unsigned long long result = power(M , N/2);
result = (result * result) % MOD;
if(N%2 == 1) {
result = (result * M) % MOD;
}
return result;
}
Find the smallest number which is divisible by all numbers from 1 to N, without leaving any remainder. As number can be very large we take the answer modulo 1000000007.
I think the smallest number that would be divisible by all the number from 1 to N,would be LCM(1..N).
Example: for N = 5, that smallest number would be 60.
As 60 is the smallest number divisible by all the number form (1-5).
But for some strange reason its giving me wrong answer for large N(1000), etc.
What can cause the possible error here, is my login correct here?
Here's what i tried to Implement.
#include <iostream>
#include <vector>
using namespace std;
vector<long long> lcmArr;
const long long mod = 1000000007;
long long gcd(long long a, long long b){
if(b == 0)
{
return a;
}
return gcd(b, a%b);
}
long long lcmFumction(long long a, long long b)
{
return (a*b)/gcd(a,b);
}
int main() {
lcmArr.clear();lcmArr.resize(1002);
lcmArr[0] =0; lcmArr[1] = 1;
for(int i =2; i <=1000; i++){
lcmArr[i] = lcmFumction(lcmArr[i-1], i)%mod;
//cout<<lcmArr[i-1]<<" ";
}
int T;
cin >> T;
while(T--) {
int N;
cin>>N;
cout<<lcmArr[N]<<"\n";
}
return 0;
}
The problem is when you calculate LCM, you use division,
And
((A/B)/C) mod M != (((A/B) mod M)/C)mod M
For example (10/5/2) % 2 != ((10/5)%2)/2)%2
You should use modular inverse to calculate that.
Some explanation about modular inverse.
If we have:
(a*b) % m = 1, then b is modular inverse of a, as b % m = (1/a) % m.
Thus, if we need to calculate (x/a) % m, we can make it become (x * b ) %m.
And we know that (A*B*C)% m = ((A * B) % m)*C)% m, so, in your case, modular inverse will come in handy.
I know the answer above has already been accepted, but I think that won't be enough to solve your problem. The problem lies in the fact that the first modular LCM will take away all divisors you need to check in subsequent GCD calls, so the answer will still be wrong.
One possible solution is to keep an array of factors for the answer. Each factor will be each number from 1..N, divided by GCD(number, [all previous numbers]). For this to work, you have to code a special GCD that computes the result between a single number and an array of factors. This C++ code shows how this would work:
#include <iostream>
#include <vector>
#define lli long long int
using namespace std;
vector<lli> T;
lli gcd(lli a, lli b) {
if(b == 0)
return a;
return gcd(b, a%b);
}
lli gcd_vector(vector<lli>& a, lli b) {
lli ma = 1;
for(int i=0; i<T.size(); i++)
ma = ma*T[i]%b;
return gcd(b, ma);
}
int main() {
lli answer = 1;
for(int i=1; i<=1000; i++) {
lli factor = i/gcd_vector(T, i);
T.push_back(factor);
answer = (answer*factor)%1000000007;
}
cout << answer << endl;
}
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;
}
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.