6 Feb 2020 : 1 practice problem video Added
L11 : Modular GCD(Codechef)
Bro I am really looking forward for your remaining number theory videos.
bro please upload the complete solutions of the live contest… i am sure it is something that everyone would like to see …and thanks for your efforts bro…hopefully your channel grows much big asap
Thanx Bro:clap:
you’re welcome man.
bruh I got to know about it last month and completed number theory
and today in April cookoff I am able to reach and think about the solution of decomposing matrix thanks @waqar_ahmad224
That’s really nice to hear.
you’re welcome buddy
working on it.
uploaded a video today
25 April 2020 : 1 new lecture added
L17 : Calculating Euler’s Totient Function in O(sqrt(N)) Time
Brother i tried to calculate Eule totient with the help of modulo multiplicative inverse but it gives me WA , can you please check where i m doing mistake??
Please help!!
#include<bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
#define ull unsigned long long int // ranges from (0 - twice of long long int)
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i–)
#define pb push_back
#define mp make_pair
#define vll vector
#define mod 1000000007LL
ll power(ll a, ll n, ll m) // using Binary Exponential in TC O(logn)
{
ll res=1;
while(n)
{
if(n%2)
res=(resa)%m ,n–;
else
a=(aa)%m ,n/=2;
}
return res;
}
ll Euler_totient(ll n)
{
// firstly calculate the Number of primes
vectorprimes;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
primes.pb(i);
while(n%i==0)
n=n/i;
}
}
if(n>1) // it means it still a prime number
primes.pb(n);
// now calulate the inverse modulo of the each primes and stores in the another array
vector<ll>inverse_of_primes;
for(auto x: primes)
{
inverse_of_primes.pb(power(x,mod-2,mod));
}
ll res=n;
for(int i=0;i<(int)primes.size();i++)
{
res=(res*(primes[i]-1)*inverse_of_primes[i])%mod;
}
return res;
}
int main() {
auto start = chrono::high_resolution_clock::now();
fio;
ll t=1;
//cin>>t;
while(t–)
{
ll n; cin>>n;
cout<<Euler_totient(n)<<"\n";
}
auto finish = chrono::high_resolution_clock::now();
cerr << "Time elapsed: " << (chrono::duration<long double>(finish-start)).count() << "s\n";
return 0;
}
you need to know you are not calculating phi(n) but phi(n) % mod.
second thing you are initializing res = n ,which is wrong for now because n is not n which was sent to this function because it has changed where you were calculating prime factorization (in first for loop you are dividing n).
so set res = n before first for loop
#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
#define ull unsigned long long int // ranges from (0 - twice of long long int)
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i–)
#define pb push_back
#define mp make_pair
#define vll vector
#define mod 1000000007LL
ll power(ll a, ll n, ll m) // using Binary Exponential in TC O(logn)
{
ll res=1;
while(n)
{
if(n%2)
res=(res * a)%m ,n--;
else
a=(a * a)%m ,n/=2;
}
return res;
}
ll Euler_totient(ll n)
{
// firstly calculate the Number of primes
vector<int> primes;
ll res=n;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
primes.pb(i);
while(n%i==0)
n=n/i;
}
}
if(n>1) // it means it still a prime number
primes.pb(n);
// now calulate the inverse modulo of the each primes and stores in the another array
vector<ll>inverse_of_primes;
for(auto x: primes)
{
inverse_of_primes.pb(power(x,mod-2,mod));
}
for(int i=0;i<(int)primes.size();i++)
{
res=(res*(primes[i]-1))%mod;
res = (res * inverse_of_primes[i]) % mod;
}
return res;
}
int main() {
auto start = chrono::high_resolution_clock::now();
fio;
ll t=1;
//cin>>t;
while(t--)
{
ll n; cin>>n;
cout<<Euler_totient(n)<<"\n";
}
auto finish = chrono::high_resolution_clock::now();
cerr << "Time elapsed: " << (chrono::duration<long double>(finish-start)).count() << "s\n";
return 0;
}
here is the correct code (it is still calculating phi(n) % mod)