#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
Related
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;
}
How can we store and print factorial(2^n) / (2^n -1))mod1000000009 in C++.Here n can be as large as 20. When I try to print this using the following code, it shows segmentation fault for n=20
#include
#include
using namespace std;
long int factorial(int n)
{
if(n<=1){return 1;}
else
return (n%1000000009)*(factorial(n-1))%1000000009;
}
int main()
{
int K;
long long int numofmatches=0;
long long int denominator=0;
long long int factor=0;
long long int times=0;
long long int players=0;
cin>>K;
if(K==1)
{
cout<<2<<endl<<2<<endl;
return 0;
}
else
{
denominator=pow(2,K);
cout<<"Denominator="<<denominator<<endl;
numofmatches=factorial(denominator)%1000000009;
denominator-=1;
cout<<"numberofmatches="<<numofmatches<<endl;
cout<<"Denominator="<<denominator<<endl;
factor=numofmatches/denominator;
cout<<"Factor="<<factor<<endl;
while(times<=denominator)
{
cout<<(times*factor)<<endl;
++times;
}
}
return 0;
}
First of all, note that (2^n)! / (2^n-1) is equal to (2^n-2)! x 2^n.
Now, (2^20-2)! by itself is already an extremely large number to calculate.
What you can do instead, is to modulo the intermediate result with 1000000009 after every multiplication:
#define MAX ((1<<20)-2)
unsigned long long res = 1;
for (unsigned int i=1; i<=MAX; i++)
res = (res*i)%1000000009;
res = (res*(MAX+2))%1000000009;
If you want to iterate all values of n between 1 and 20, then you can use:
#define MAX_N 20
unsigned int arr[MAX_N+1] = {0};
void Func()
{
unsigned int i = 1;
unsigned long long res = 1;
for (int n=1; n<=MAX_N; n++)
{
unsigned int max = (1<<n)-2;
for (; i<=max; i++)
res = (res*i)%1000000009;
arr[n] = (unsigned int)((res*(max+2))%1000000009);
}
}
BTW, for any n larger than 29 the result will simply be 0, as (2^30-2) is larger than 1000000009.
So (2^30-2)! is divisible by 1000000009, and therefore, (2^30-2)! mod 1000000009 equals 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;
}