-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathncr_primefactor method.cpp
More file actions
138 lines (134 loc) · 2.3 KB
/
Copy pathncr_primefactor method.cpp
File metadata and controls
138 lines (134 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//This code returns the value of ncr in long long int.....
//It uses the technique of expressing n! as a product of primes
//ncr of upto 1000000...where solution is storable....
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
int sieve[1000000+1];
vector <int> v;
vector <int > vn;
vector <int> vm;
void initsieve()
{
int i;
for(i=0;i<=1000000;i++)
{
sieve[i]=0;
}
}
long long int modexp(int a,int b)
{
if(b==0)
{
return 1;
}
else if(b%2==0)
{
long long int c=modexp(a,b/2);
return((c*c));
}
else
{
return(((a)*modexp(a,b-1)));
}
}
void getprime()
{
int i,j;
for(i=2;i*i<=1000000;i++)
{
if(!sieve[i])
{
sieve[i]=1;
v.push_back(i);
for(j=i*i;j<=1000000;j=j+i)
{
sieve[j]=1;
}
}
}
for(i=1000;i<=1000000;i++)
{
if(!sieve[i])
v.push_back(i);
}
/*for(i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}*/
}
long long int getNcr(int n,int m)
{
vn.clear();
vm.clear();
long long int ans=1;
int i,j,ln,powi,ch;
for(i=0;i<v.size();i++)
{
ch=n;
powi=0;
while(ch>0)
{
ch=ch/v[i];
powi=powi+ch;
}
if(powi==0)
break;
vn.push_back(powi);
}
ln=i;
/*for(i=0;i<ln;i++)
{
cout<<vn[i]<<" ";
}cout<<endl;*/
for(i=0;i<ln;i++)
{
ch=m;
powi=0;
while(ch>0)
{
ch=ch/v[i];
powi=powi+ch;
}
vm.push_back(powi);
}
/*for(i=0;i<ln;i++)
{
cout<<vm[i]<<" ";
}cout<<endl;*/
for(i=0;i<ln;i++)
{
ch=n-m;
powi=0;
while(ch>0)
{
ch=ch/v[i];
powi=powi+ch;
}
vm[i]+=powi;
}
/*for(i=0;i<ln;i++)
{
cout<<vm[i]<<" ";
}cout<<endl;*/
for(i=0;i<ln;i++)
{
vn[i]=vn[i]-vm[i];
}
for(i=0;i<ln;i++)
{
ans=ans*modexp(v[i],vn[i]);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,i,j;
long long int ans=1;
int t,q,m,ln,powi,ch;
initsieve();
getprime();
cout<<getNcr(110,100)<<endl;
return 0;
}