From fc3fbee24cd2295441ffbfe42650bd3dcac43efd Mon Sep 17 00:00:00 2001 From: Vicky-Jha <53377467+Vicky-Jha@users.noreply.github.com> Date: Tue, 6 Oct 2020 17:33:11 +0530 Subject: [PATCH] Add files via upload --- %inverse.cpp | 32 ++++++ Binary_exponentioation.cpp | 34 +++++++ Binomial_CoefficientUsingModularInverse.cpp | 48 +++++++++ EuclidGCD.cpp | 19 ++++ GCDQueries.cpp | 70 +++++++++++++ GcdQuerie.cpp | 88 ++++++++++++++++ KthPrime.cpp | 35 +++++++ MatrixExponentiation.cpp | 71 +++++++++++++ PowerOfMatrix.cpp | 82 +++++++++++++++ PrimeFactorization.cpp | 44 ++++++++ PrimeGenerator.cpp | 64 ++++++++++++ PrimeGenerator2.cpp | 72 +++++++++++++ ProductOfLargeNumbers.cpp | 36 +++++++ Seive+PrimalityTest.cpp | 37 +++++++ arpas.cpp | 17 ++++ binomial_coefficient.cpp | 33 ++++++ eulerTotientFun.cpp | 31 ++++++ gcd_largeNos.cpp | 50 +++++++++ microPrimePrime.cpp | 50 +++++++++ modularGcd.cpp | 70 +++++++++++++ noOfDivisors.cpp | 30 ++++++ nthseries.cpp | 59 +++++++++++ phi_nloglogn_Seive.cpp | 31 ++++++ primePrime.cpp | 56 ++++++++++ qnumber.cpp | 78 ++++++++++++++ qnumberPerfect.cpp | 107 ++++++++++++++++++++ recurrenceSeries.cpp | 78 ++++++++++++++ segmentedSeivePrimeGenerator.cpp | 64 ++++++++++++ sumOfGcdsWithNBelow.cpp | 57 +++++++++++ 29 files changed, 1543 insertions(+) create mode 100644 %inverse.cpp create mode 100644 Binary_exponentioation.cpp create mode 100644 Binomial_CoefficientUsingModularInverse.cpp create mode 100644 EuclidGCD.cpp create mode 100644 GCDQueries.cpp create mode 100644 GcdQuerie.cpp create mode 100644 KthPrime.cpp create mode 100644 MatrixExponentiation.cpp create mode 100644 PowerOfMatrix.cpp create mode 100644 PrimeFactorization.cpp create mode 100644 PrimeGenerator.cpp create mode 100644 PrimeGenerator2.cpp create mode 100644 ProductOfLargeNumbers.cpp create mode 100644 Seive+PrimalityTest.cpp create mode 100644 arpas.cpp create mode 100644 binomial_coefficient.cpp create mode 100644 eulerTotientFun.cpp create mode 100644 gcd_largeNos.cpp create mode 100644 microPrimePrime.cpp create mode 100644 modularGcd.cpp create mode 100644 noOfDivisors.cpp create mode 100644 nthseries.cpp create mode 100644 phi_nloglogn_Seive.cpp create mode 100644 primePrime.cpp create mode 100644 qnumber.cpp create mode 100644 qnumberPerfect.cpp create mode 100644 recurrenceSeries.cpp create mode 100644 segmentedSeivePrimeGenerator.cpp create mode 100644 sumOfGcdsWithNBelow.cpp diff --git a/%inverse.cpp b/%inverse.cpp new file mode 100644 index 0000000..9daf96b --- /dev/null +++ b/%inverse.cpp @@ -0,0 +1,32 @@ +#include +#define lli long long int +using namespace std; +lli Power(lli a, lli n , lli mod) +{ + lli res = 1; + while(n) + if(!(n & 1)) + n /= 2, a = (a*a)%mod; + else + n--, res = (res*a)%mod; + return res; + +} +lli ModularMultiplicativeInverse(lli a, lli n) +{ + return Power(a,n-2,n); +} + +int main() +{ + lli a, n; + scanf("%lld%lld",&a,&n); + + cout< +#define mod 1000000007 +using namespace std; +int powerFunc(int a , int n) +{ + + int res = 1; + while(n) + { + if(!(n & 1)) + { + n = n / 2; + a = ((a%mod)*(a%mod))%mod; + } + else + { + n--; + res = ((res%mod) *( a %mod))%mod; + } + + } + return res%mod; + +} + +int main() +{ + //freopen("input.txt" , "r", stdin); + //freopen("outpux.txt","w" , stdout); + int a,n; + cin>>a>>n; + cout< +#define MAXN 1000 +using namespace std; +int factorial[MAXN+1]; +// this code can be used when finding combination of large numbers +unsigned long long power(unsigned long long a, + int n, int mod) +{ + unsigned long long int res = 1; + while(n) + if(!(n & 1)) + n /= 2, a = (a*a)%mod; + else + n--, res = (res*a)%mod; + return res; +} + +unsigned long long modInverse(unsigned long long n, int p) +{ + return power(n, p - 2, p); +} + +unsigned long long nCrModPFermat(unsigned long long n, + int r, int p) +{ + + if (r == 0) + return 1; + + return (factorial[n] * modInverse(factorial[r], p) % p * modInverse(factorial[n - r], p) % p) % p; +} + +//nCr % p = (factorial[n]* modIverse(factorial[r]) % p * + //modIverse(factorial[n-r]) % p) % p; +int main() +{ + + int n = 10, r = 2, p = 13; + factorial[0] = 1; + for (int i = 1; i <= MAXN; i++) { + factorial[i] = factorial[i - 1] * i % p; + } + cout << "Value of nCr % p is " + << nCrModPFermat(n, r, p)<<"\n"; + return 0; +} +//Here p must always be prime number +//coz modular inverse exits if 2 numbers are coprime diff --git a/EuclidGCD.cpp b/EuclidGCD.cpp new file mode 100644 index 0000000..7a13f63 --- /dev/null +++ b/EuclidGCD.cpp @@ -0,0 +1,19 @@ + +#include +#define lli long long int +using namespace std; + +lli gcd (lli A , lli B) +{ + if(B == 0) return A; + else return gcd ( B , A%B); +} + + +int main() +{ + lli a , b; + cin>>a>>b; + cout< +#define lli long long int +using namespace std; +lli pre[100005]; +lli post[100005]; +lli arr[100005]; +lli gcd(lli A , lli B) +{ + if(A == B || B == 0) + return A; + if(A == 0) + return B; + lli MIN,MAX; + if(A < B){ + MIN = A; + MAX = B; + } + else {MIN = B;MAX=A;} + if(!(MAX % MIN)) + return MIN; + + lli check = 0; + for(lli i = 1 ; i*i<=MIN ; ++i) + if(!(MIN % i)) + { + lli tmp = (MAX)%i; + if(!tmp) + check = max(check , i); + tmp = MAX%(MIN/i); + if(!tmp) + check = max(check , MIN/i); + } + return check; +} +void Pre(lli n) +{ + pre[0] = 0; + for(lli i = 1 ; i<=n ; ++i) + pre[i] = gcd(pre[i-1],arr[i]); + +} +void Post(lli n) +{ + post[n+1] = 0; + for(lli i = n ; i>=1; --i) + post[i] = gcd(post[i+1],arr[i]); + +} +int main() +{ + lli t; + scanf("%lld",&t); + while(t--) + { + lli n,q; + scanf("%lld %lld",&n,&q); + for(lli i = 1 ; i<=n ; ++i) + scanf("%lld",&arr[i]); + Pre(n); + Post(n); + while(q--) + { + lli l ,r ; + scanf("%lld %lld", &l,&r); + printf("%lld\n",gcd(pre[l-1],post[r+1])); + } + } + + return 0; +} diff --git a/GcdQuerie.cpp b/GcdQuerie.cpp new file mode 100644 index 0000000..3b8d4d0 --- /dev/null +++ b/GcdQuerie.cpp @@ -0,0 +1,88 @@ +#include +#define lli long long int +using namespace std; + +//c++ 14 +#include +#define lli long long int +using namespace std; + + + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + int t,n,q,l,r; + cin>>t; + + while( t --) + { + cin>>n>>q; + + vector arr(n); + for ( int i =0 ; i < n ; ++ i) + cin>>arr[i]; + int pre[n+1] {0},suf[n+2] {0}; + + for ( int i = 0 ; i < n ; ++ i) + pre[i+1] = __gcd(arr[i],pre[i]); + + int j = n+1; + for ( int i = n-1 ; i >= 0 ; -- i, --j) + suf[i+1] = __gcd(arr[i],suf[j]); + + while(q --) + { + cin>>l>>r; + cout<<__gcd(pre[l-1], suf[r+1])<<"\n"; + } + } + + return 0; +} + + + + + + + + + +//c++ 17 +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + int t,n,q,l,r; + cin>>t; + + while( t --) + { + cin>>n>>q; + + vector arr(n); + for ( int i =0 ; i < n ; ++ i) + cin>>arr[i]; + vector pre(n+1,0); + vector suf(n+2,0); + + for ( int i = 0 ; i < n ; ++ i) + pre[i+1] = gcd(arr[i],pre[i]); + + int j = n+1; + for ( int i = n-1 ; i >= 0 ; -- i, --j) + suf[i+1] = gcd(arr[i],suf[j]); + + while(q --) + { + cin>>l>>r; + cout< +using namespace std; +vector primes; +void SieveOfEratosthenes(int n) +{ + + vector prime(n+1,1); + + for (int p=2; p*p<=n; p++) + { + + if (prime[p] == true) + { + for (int i=p*2; i<=n; i += p) + prime[i] = false; + } + } + + for (int p=2; p<=n; p++) + if (prime[p]) + primes.emplace_back(p); + +} +int main() { + SieveOfEratosthenes(90000001); + int q,n; + cin>>q; + while(q--) + { + cin>>n; + cout< +#define MAX 101 +#define mod 1e9+7 +using namespace std; +int arr[MAX][MAX] , I[MAX][MAX]; +void mul(int A[][MAX] , int B[][MAX] , int dim) +{ + int res[dim+1][dim+1]; + for(int i=1 ; i<=dim ; ++i){ + for(int j=1 ; j<=dim ; ++j){ + res[i][j] = 0; + for(int k=1 ; k<=dim ; ++k) + { + res[i][j] += (A[i][k] * B[k][j] ) % mod; + } + } + } + + for(int i=1 ; i<=dim ; ++i) + for(int j=1 ; j<= dim ; ++j) + A[i][j] = res[i][j]; + +} +void power(int A[][MAX] , int dim , int n) +{ + for(int i=1 ; i<=dim ; ++i) + for(int j=1 ; j<=dim ; ++j) + if(i == j) I[i][j] = 1; else I[i][j]=0; + + //for(int i=1 ; i<=n ; ++i) + //mul(I,A,dim); + + while(n) + { + if(!(n &1 )) + mul(A,A,dim),n /= 2; + else + mul(I,A,dim),n--; + } + + for(int i=1 ; i<=dim ; ++i) + for(int j=1 ; j<=dim ; ++j) + A[i][j] = I[i][j]; +} +void printMatrix(int A[][MAX] , int dim) +{ + for(int i=1 ; i<=dim; ++i){ + for(int j=1 ; j<=dim ; ++j) + cout<>t; + while(t--) + { + cin >>dim >>n; + for(int i= 1 ; i<=dim ; ++i) + for(int j=1 ; j<=dim ; ++j) + cin>>arr[i][j]; + power(arr , dim , n); + printMatrix(arr , dim); + } + + return 0; +} diff --git a/PowerOfMatrix.cpp b/PowerOfMatrix.cpp new file mode 100644 index 0000000..3b9a20f --- /dev/null +++ b/PowerOfMatrix.cpp @@ -0,0 +1,82 @@ +#include +#define lli long long int +#define mod 1000000007 +using namespace std; +//VJ's code +vector < vector < int > > mul(vector < vector < int > >& A, vector < vector < int > >& B) +{ + vector< vector < int > > C(A.size(), vector(A.size())); + for ( int i = 0 ; i < A.size() ; ++ i) + { + for ( int j = 0 ; j < A.size() ; ++ j) + { + C[i][j] = 0; + for ( int k = 0 ; k < A.size() ; ++ k) + { + C[i][j] += ((A[i][k] % mod) * (B[k][j] % mod) % mod); + } + } + } + return C; +} + +void power(vector< vector< int > > &matrix, int n, int d) +{ + vector < vector < int > > res(d, vector (d)); + + for ( int i = 0 ; i < d ; ++ i) + res[i][i] = 1; + + while ( n ) + { + if(!(n & 1)) + { + matrix = mul(matrix,matrix); + n /= 2; + } + else + { + res = mul(res,matrix); + n --; + } + + } + matrix = res; + +} +void printMatrix(vector < vector < int > >& matrix, int d) +{ + for ( int i = 0 ; i < d ; ++ i){ + for ( int j = 0 ; j < d ; ++ j){ + cout<>t; + + while(t --) + { + cin>>d>>n; + vector < vector < int > > matrix(d, vector(d)); + + for ( int i = 0 ; i < d ; ++ i) + for( int j = 0 ; j < d ; ++ j) + cin>>matrix[i][j]; + power(matrix,n,d); + printMatrix(matrix,d); + } + + + return 0; +} diff --git a/PrimeFactorization.cpp b/PrimeFactorization.cpp new file mode 100644 index 0000000..a6d5f75 --- /dev/null +++ b/PrimeFactorization.cpp @@ -0,0 +1,44 @@ +//Prime Factors and Number of Divisors + +#include + +using namespace std; + +void primeFactors(int n) +{ + int cnt =0 ,res=1; + int i = 2; + while(n && i*i <= n) + { + cnt = 0; + if(!(n % i)) + { + + while(!(n % i)) + { + + cnt++; + n /= i; + } + cout<1 ){ + cout<>n; + primeFactors(n); +} diff --git a/PrimeGenerator.cpp b/PrimeGenerator.cpp new file mode 100644 index 0000000..d81776f --- /dev/null +++ b/PrimeGenerator.cpp @@ -0,0 +1,64 @@ +#include +#define lli long long int +#define vi vector + + +using namespace std; +vi primes; +int prime[100001]; + +void sieve(int maxN) +{ + vi ar(maxN + 1 , 0); + ar[1] = 1; + + for(int i=2;i<=maxN;i++) + if(ar[i] == 0) + { + for(int j=2*i;j<=maxN;j+=i) + ar[j] = 1; + } + + for(int i =1 ; i<= maxN ; i++) + if(ar[i] == 0) + primes.push_back(i); +} + +void init(int L , int R) +{ + if(L == 1) L++; + + int maxN = R - L + 1; + vi ar(maxN , 0); + + for(lli p : primes) + if(p*p <= R) + { + int i = (L / p) * p; + if(i < L) i += p; + + for(;i<=R;i+=p) + { + if(i != p) + ar[i-L] = 1; + } + } + + for(int i=0;i>t; + + while(t--) + { + cin>>L>>R; + init(L , R); + cout< +#define lli long long int +#define mod 1000000007 +using namespace std; + +//VJ's Code +void init(int L, int R) +{ + vector primes; + int r = floor(sqrt(R)); + vector isPrime(floor(sqrt(R)) + 1); + isPrime[1] = 1; + for ( int i = 2 ; i*i <= r; ++ i) + if(!isPrime[i]) + for ( int j = i*i ; j <= r ; j += i ) + isPrime[j] = 1; + + for ( int i = 1 ; i <= r ; ++ i ) + if(!isPrime[i]) + primes.push_back(i); + + + bool f = 1; + if ( L <= r) + { + f = 0; + for ( int i = 0; i < primes.size(); ++ i ) + if( primes[i] >= L ) + cout< isPrime2(R - l + 1); + + for ( int i = 0 ; i < primes.size() ; ++ i ) + for ( int j = l ; j <= R ; ++ j) + if(!isPrime2[j - l]) + if(!(j % primes[i])) + isPrime2[j - l] = 1; + + for ( int i = 0 ; i < isPrime2.size() ; ++ i ) + if ( !(isPrime2[i]) ) + cout<>t; + + long long int n,m; + while ( t -- ) + { + cin>>m>>n; + init(m,n); + cout<<"\n"; + } + + + return 0; +} + diff --git a/ProductOfLargeNumbers.cpp b/ProductOfLargeNumbers.cpp new file mode 100644 index 0000000..dc481b6 --- /dev/null +++ b/ProductOfLargeNumbers.cpp @@ -0,0 +1,36 @@ +#include +#define lli long long int +const int MOD = 1e9 + 7; +using namespace std; + +long long prod(long long a, long long b, long long mod = MOD) +{ + long long res = 0; + + while(b) + { + if(b & 1) + { + res = (res + a) % mod; + b --; + } + else + { + a = (a + a) % mod; + b /= 2; + } + } + + return res; +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + + cout< +#define MAX 100001 +using namespace std; +vector isPrime(MAX+1 , 0); +//int isprime(int n) +//{ +// if(n == 1) +// return false; +// for(int i=2 ; i*i<=n ; i++) +// if(n % i == 0) +// return false; +// return true; +//} +void seive() +{ + isPrime[1] = 1; + for(int i=2 ; i*i<=MAX ; ++i) + if(!isPrime[i]) + for(int j = i*i ; j<=MAX ; j += i) + isPrime[j] = 1; + +} +int main() { + + seive(); + int t,n; + cin>>t; + while(t--) + { + cin>>n; + if(!isPrime[n]) + printf("yes\n"); + else + printf("no\n"); + } + return 0; +} diff --git a/arpas.cpp b/arpas.cpp new file mode 100644 index 0000000..9c55ef0 --- /dev/null +++ b/arpas.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; +int main() +{ + int n; + cin>>n; +if (n == 0) + cout << "1\n"; +else { + int d[] = {8, 4, 2, 6}; + n--; + n %= 4; + cout << d[n] << "\n"; +} +return 0; +} + diff --git a/binomial_coefficient.cpp b/binomial_coefficient.cpp new file mode 100644 index 0000000..9b8a3b4 --- /dev/null +++ b/binomial_coefficient.cpp @@ -0,0 +1,33 @@ +#include +#define lli long long int +using namespace std; +//int nChoosek(int n, int k) // recursive approach +//{ + //if(n == k ||k == 0) + //return 1; + //else + //return nChoosek(n-1,k) +nChoosek(n-1,k-1); +//} +//int nChoosek(int n, int k) // iterative appproach +//{ + //int res = 1; + //for (int i = n - k + 1; i <= n; ++i) + //res *= i; + //for (int i = 2; i <= k; ++i) + //res /= i; + //return res; +//} +int nChoosek(int n , int k) // more improvised +{ + double res = 1; + for (int i = 1; i <= k; ++i) + res = res * (n - k + i) / i; + return floor(res); +} +int main() +{ + int n,k; + cin>>n>>k; + cout< +using namespace std; +//Complexity O(sqrt(n)) +//counts total number of positive numbers which are coprime to n upto n from 2 +int phi(int n) { + int result = n; + for (int i = 2; i * i <= n; i++) { + if (n % i == 0) { + while (n % i == 0) + n /= i; + result -= result / i; + } + } + cout< 1)//if n is prime + result -= result/n; + return result; +} +int main() +{ + int n; + cin>>n; + cout< +#define mod 1000000007 +#define lli long long int +using namespace std; + +//lli gcd (lli A , lli B) +//{ +// if(B == 0) return A; +// else return gcd ( B , A%B); +//} + +lli gcd(lli A , lli B) +{ + if(A == B || B == 0) + return A % mod; + if(A == 0) + return B % mod; + lli MIN,MAX; + if(A < B){ + MIN = A; + MAX = B; + } + else {MIN = B;MAX=A;} + if(!(MAX % MIN)) + return MIN; + + lli check = 0; + for(lli i = 1 ; i*i<=MIN ; ++i) + if(!(MIN % i)) + { + lli tmp = (MAX)%i; + if(!tmp) + check = max(check , i); + tmp = MAX%(MIN/i); + if(!tmp) + check = max(check , MIN/i); + } + return check%mod; +} + + +int main() +{ + lli a , b; + cin>>a>>b; + cout< +#define MAX 1000000 +using namespace std; +vector countPrime(MAX+1,0); +vector isPrime(MAX+1,1); + +void seive() +{ + isPrime[0] = 0; + isPrime[1] = 0; + for(int i=2 ; i*i<=MAX ; ++i) + if(isPrime[i]) + for(int j=i*i ; j<=MAX ; j += i) + isPrime[j] = 0; + for(int i = 1 ; i<=MAX ; ++i) + countPrime[i] = + countPrime[i-1] + isPrime[i]; + +} +int main() +{ + seive(); + int t , l ,r ; + cin>>t; + while(t--) + { + cin>>l>>r; + int c=0; + for(int i=l ; i<=r ; ++i) + if(isPrime[countPrime[i]]){ + c++; +// cout< + +#define mod 1000000007 + +#define lli size_t + + +using namespace std; + +lli power(lli a, lli n , lli d) +{ + lli res = 1; + + while(n) + { + if(n & 1) + { + res = ((res % d) * (a % d)) % d; + n--; + } + + else + { + a = ((a % d) * (a % d)) % d; + n /= 2; + } + } + + return res; +} + +lli GCD(lli A , lli B , lli n) +{ + if(A == B) + { + return (power(A , n , mod) + power(B , n , mod)) % mod; + } + else + { + + lli candidate = 1; + lli num = abs(A - B); + + for(lli i=1;i*i<=num;i++) + if(num % i == 0) + { + lli tmp = (power(A , n , i) + power(B , n , i)) % i; + + if(!tmp) candidate = max(candidate , i); + + tmp = (power(A , n , num/i) + power(B , n , num/i)) % (num/i); + if(!tmp) candidate = max(candidate , num / i); + } + return candidate % mod; + } + + +} + +int main() +{ + lli A , B , n , t; + cin>>t; + + while(t--) + { + cin>>A>>B>>n; + cout< +#define lli long long int +using namespace std; +lli nofDivisors(lli n) +{ + lli cnt=0,res=1; + for(lli i=2 ; i*i<=n ; ++i) + { + cnt = 0; + if(!(n % i)) + { + while(!(n % i)) + cnt++,n /= i; + + res *= (cnt+1); + } + } + + if(n > 1) + res *= 2; + return res; +} +int main() +{ + lli n; + scanf("%lld",&n); + printf("%lld\n",nofDivisors(n)); + + return 0; +} diff --git a/nthseries.cpp b/nthseries.cpp new file mode 100644 index 0000000..892e3ea --- /dev/null +++ b/nthseries.cpp @@ -0,0 +1,59 @@ +#include +#define MOD 10000000007 //if used 1e9+7 then invalid operands to binary exp error +#define lli long long int +using namespace std; +lli T[2][2] , I[2][2]; + +void mul(lli A[][2] , lli B[][2] ) +{ + lli res[2][2]; + for(lli i=0 ; i<2 ; ++i){ + for(lli j=0 ; j<2 ; ++j){ + res[i][j] = 0; + for(lli k=0 ; k<2 ; ++k) + { + lli x = (A[i][k] * B[k][j]) %MOD; + res[i][j] = (res[i][j] + x ) % MOD; + } + } + } + + for(lli i=0 ; i<=1 ; ++i) + for(lli j=0 ; j<= 1 ; ++j) + A[i][j] = res[i][j]; + +} +lli ntheSeries(lli n,lli a, lli b) +{ + lli res=0; + T[0][0] = 0; + T[0][1] = T[1][1] = T[1][0] = 1; + I[0][0] = I[1][1] = 1; + I[0][1] = I[1][0] = 0; + if(n == 1) + return a; + if(n == 2) + return b; + + while(n) + { + if(!(n &1 )) + mul(T,T),n /= 2; + else + mul(I,T),n--; + } + + res = (a*I[0][0] +b*I[1][0])%MOD; + return res; +} +int main() +{ + lli t,a,b,n; + cin>>t; + while(t--) + { + cin >> a>>b>>n; + cout< +#define lli long long int +#define MAXN 1000 +using namespace std; +vector phi(MAXN + 1); +void phi_1_to_n(int n) { + + phi[0] = 0; + int c = 2; + phi[1] = 1; + for (int i = 2; i <= n; i++) + phi[i] = i; + + for (int i = 2; i <= n; i++) { // compulsory to iterate till n + if (phi[i] == i) { // basically checks if phi[i] is prime or not + for (int j = i; j <= n; j += i) + phi[j] -= phi[j] / i; + } + cout<>n; + phi_1_to_n(n); + + cout< +#define lli long long int +#define MAX 1000001 +using namespace std; +//VJ's Code +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + + int t,l,r; + cin>>t; + + bitset IsPrime; + IsPrime[1] = 1; //0 then prime // 1 then non prime + + for ( int i = 2 ; i*i <= (MAX) ; ++ i) + if(!IsPrime[i]) + for ( int j = i*i ; j <= MAX ; j += i) + IsPrime[j] = 1; + + vector primePrime; + + int cummulativeSum = 0; + + for ( int i = 1 ; i < MAX ; ++ i) + { + if(!IsPrime[i]) + cummulativeSum += 1; + if(!IsPrime[cummulativeSum]) + primePrime.push_back(i); + } + while(t --) + { + cin>>l>>r; + int pos ; + + auto it = lower_bound(primePrime.begin(), primePrime.end(), l); + if(it != primePrime.end()) + pos = it - primePrime.begin(); + else + continue; + + auto itr = lower_bound(primePrime.begin(), primePrime.end(), r); + if(*itr != r) + itr --; + if(itr != primePrime.end()) + cout<<(itr - primePrime.begin()) - pos + 1<<"\n"; + else + continue; + } + + + return 0; +} diff --git a/qnumber.cpp b/qnumber.cpp new file mode 100644 index 0000000..f1ad646 --- /dev/null +++ b/qnumber.cpp @@ -0,0 +1,78 @@ + +// 1<=N<=10^12 +// 1<=Q<=5*10^5 +// 1<=T<=3 +// 1<=K<=10^12 + + +#include +#define lli unsigned long long int +using namespace std; +vector divisors; +void Divisors(lli n) +{ + for(lli i = 1 ; i*i <= n ; ++i) + if(!(n % i)) + { + divisors.push_back(i); + divisors.push_back(n/i); + } + sort(divisors.begin(),divisors.end()); +} +int main() { + lli N,Q,T,K; + scanf("%llu %llu ",&N,&Q); + Divisors(N); + map mp; + for(lli i = 2 ; i*i <= N ; ++i) + if(!(N % i)) + { + lli cnt = 0; + while(!(N % i)) + cnt++,N /= i; + mp.insert({i,cnt}); + } + if(N > 1) + mp.insert({N,1}); + + while(Q--) + { + scanf("%llu %llu",&T,&K); + if(T == 1) + { + lli cnt=0,res=1; + for(lli i=2 ; i*i<=K ; ++i) + { + cnt = 0; + if(!(K % i)) + { + while(!(K % i)) + cnt++,K /= i; + if(mp[i]) + res *= (min(cnt,mp[i])+1); + } + } + + if(K > 1 && mp[K]) + res *= 2; + cout< +#define REP(i,n) for (int i = 1; i <= n; i++) +#define mod 1000000007 +#define pb push_back +#define ff first +#define ss second +#define ii pair +#define vi vector +#define vii vector +#define lli long long int +#define INF 1000000000 +#define endl '\n' +const double PI = 3.141592653589793238460; +typedef std::complex Complex; +typedef std::valarray CArray; + +using namespace std; +map PF; +lli total; + +void factorize(lli n) +{ + total = 1; + for(lli i=2;i*i<=n;i++) + if(n % i == 0) + { + int cnt = 0; + while(n % i == 0) + cnt++ , n /= i; + PF[i] = cnt; + total *= (cnt + 1); + } + + if(n > 1) PF[n] = 1 , total *= 2; +} + + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + + lli t , n , q , k; + cin>>n>>q; + + factorize(n); + + while(q--) + { + cin>>t>>k; + if(t == 1) + { + lli res = 1; + for(ii p : PF) + { + lli cnt = 0; + while(k % p.ff == 0) + cnt++ , k /= p.ff; + res *= min(cnt , p.ss) + 1; + } + + cout< p.ss) + { + res = 0; + break; + } + res *= (p.ss - cnt + 1); + } + if(k > 1) res = 0; + cout< p.ss) + { + res = 0; + break; + } + + res *= (p.ss - cnt + 1); + } + + if(k > 1) res = 0; + cout< +#define lli long long int +#define mod 1000000007 +using namespace std; +//VJ's Code + +vector split_string(string); + +vector < vector < lli > > mul(vector < vector < lli > >& A, vector < vector < lli > >& B) +{ + vector< vector < lli > > C(A.size(), vector(A.size())); + for ( int i = 0 ; i < A.size() ; ++ i) + { + for ( int j = 0 ; j < A.size() ; ++ j) + { + C[i][j] = 0; + for ( int k = 0 ; k < A.size() ; ++ k) + { + lli x = ((A[i][k] % mod) * (B[k][j] % mod) % mod); + C[i][j] += x; + } + } + } + return C; +} + +void power(vector< vector< lli > > &matrix, int n, int d) +{ + vector < vector < lli > > res(d, vector (d)); + + for ( lli i = 0 ; i < d ; ++ i) + res[i][i] = 1; + + while ( n ) + { + if(!(n & 1)) + { + matrix = mul(matrix,matrix); + n /= 2; + } + else + { + res = mul(res,matrix); + n --; + } + + } + matrix = res; + +} + +// Complete the solve function below. +int solve(int a, int b, int n) { + if ( n == 0) + return a; + if( n == 1) + return b; + + vector < vector < lli > > matrix ( 2 , vector < lli > (2)); + matrix[0][0] = matrix[0][1] = matrix[1][0] = 1; + matrix[1][1] = 0; + power(matrix, n, 2); + + return (a*matrix[1][1] + b*matrix[1][0] ) % mod; + +} + +int main() +{ + int t,a,b,n; + cin>>t; + + while(t --) + { + cin>>a>>b>>n; + cout< +#define lli long long int +using namespace std; + +void simpleSieve(lli limit, vector& prime) +{ + bool mark[limit + 1]; + memset(mark, false, sizeof(mark)); + + for (lli i = 2; i <= limit; ++i) { + if (mark[i] == false) { + prime.push_back(i); + for (lli j = i; j <= limit; j += i) + mark[j] = true; + } + } +} + +void primesInRange(lli low, lli high) +{ + lli limit = floor(sqrt(high)) + 1; + vector prime; + simpleSieve(limit, prime); + + + lli n = high - low + 1; + + + bool mark[n + 1]; + memset(mark, false, sizeof(mark)); + + for (lli i = 0; i < prime.size(); i++) { + lli loLim = floor(low / prime[i]) * prime[i]; + if (loLim < low) + loLim += prime[i]; + if(loLim==prime[i]) + loLim += prime[i]; + + for (lli j = loLim; j <= high; j += prime[i]) + mark[j - low] = true; + } + for (lli i = low; i <= high; i++) + if (!mark[i - low]) + cout << i << "\n"; +} + + +int main() +{ + lli low,t , high ; + cin>>t; + while(t--){ + cin>>low>>high; + if(low == 1) + low ++; + primesInRange(low, high); + cout<<"\n"; + } + return 0; +} + diff --git a/sumOfGcdsWithNBelow.cpp b/sumOfGcdsWithNBelow.cpp new file mode 100644 index 0000000..9dbc313 --- /dev/null +++ b/sumOfGcdsWithNBelow.cpp @@ -0,0 +1,57 @@ +//Find sum of all numbers with Gcd N from 1 ex N=10, gcd(1,10)+gcd(2,10)+...+gcd(n,n) +//1 <= n <= 2000 +//how Euler's Totient Function can be used to evaluate GCD sum in an efficient way. +#include +#define lli long long int +#define MAXN 1000000 +using namespace std; +int phi[MAXN + 1]; + +void phi_1_to_n(int n) { + + phi[0] = 0; + phi[1] = 1; + for (int i = 2; i <= n; i++) + phi[i] = i; + + for(int i=2 ; i<=n ; ++i) + if(phi[i] == i) + { + for(int j=i ; j <= n ; j += i) + phi[j] -= phi[j]/i; + } + + + +} +lli sumOfGcds(lli n) +{ + lli res =0; + for(int i= 1 ; i*i<=n ; ++i) + if(!(n % i)) + { + lli d1 = i; + lli d2 = n / i; + res += d1*phi[n/d1]; + res += d2*phi[n/d2]; + } + return res; + +} +int main() +{ + phi_1_to_n(MAXN); + lli n,t; + scanf("%lld",&t); + while(t--) + { + scanf("%lld",&n); + printf("%lld\n",sumOfGcds(n)); + } + + return 0; +} + +//Explanation + +//1. GCD(A,B) = C // GCD(A/C,B/C) = 1