From 129f68f48fa6ee7de5a967440fa0c1d07f8ac988 Mon Sep 17 00:00:00 2001 From: Shrutiruchi <46797012+Shrutiruchi@users.noreply.github.com> Date: Sat, 19 Oct 2019 07:41:56 +0530 Subject: [PATCH 1/5] Update sieve.cpp --- Sieve of Eratosthenes/sieve.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sieve of Eratosthenes/sieve.cpp b/Sieve of Eratosthenes/sieve.cpp index a3a8362..0f2a10b 100644 --- a/Sieve of Eratosthenes/sieve.cpp +++ b/Sieve of Eratosthenes/sieve.cpp @@ -10,8 +10,8 @@ void sieveOptimized(int N) { if (isPrime[i]) { // For further optimization, You can do instead of j += i, j += (2 * i). // Proof is left to reader :) - for (int j = i * i; j <= N; j += i) - isPrime[j] = 0; + for (int j = 2; (j*i) <= N; j += 1) + isPrime[j*i] = 0; } } } From 1a08d0c6863108c824cc5c8a7b7114811a116186 Mon Sep 17 00:00:00 2001 From: Shrutiruchi <46797012+Shrutiruchi@users.noreply.github.com> Date: Sat, 19 Oct 2019 07:54:21 +0530 Subject: [PATCH 2/5] Update LIS.cpp --- Longest Increasing Subsequence/LIS.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Longest Increasing Subsequence/LIS.cpp b/Longest Increasing Subsequence/LIS.cpp index 9714c10..5563c21 100644 --- a/Longest Increasing Subsequence/LIS.cpp +++ b/Longest Increasing Subsequence/LIS.cpp @@ -12,10 +12,24 @@ int LIS() for(int i=0;i>elem; - //vector::iterator it = upper_bound(d.begin(),d.end(),elem); // non-decreasing - vector::iterator it = lower_bound(d.begin(),d.end(),elem); // strictly increasing - if(it == d.end()) d.push_back(elem); - else *it=elem; + d.push_back(elem); } - cout<=0;j--){ + if(d[i]>=d[j]){ + dp[i]=max(dp[i],dp[j]+1); + } + } + } + int ans=INT_MIN; + for(int i=0;i Date: Sat, 19 Oct 2019 08:03:56 +0530 Subject: [PATCH 3/5] Update etf.cpp --- Euler Totient Function/etf.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Euler Totient Function/etf.cpp b/Euler Totient Function/etf.cpp index 5c99e1c..02891b1 100644 --- a/Euler Totient Function/etf.cpp +++ b/Euler Totient Function/etf.cpp @@ -8,15 +8,15 @@ void cal_etf() { for(int i = 1 ;i <= MAXN ; i++ ) etf[i]=i; - for(int i=2 ; i<= MAXN ; i++ ) - if ( etf[i] == i) - for(int j = 2*i ; j <= MAXN ; j += i) - etf[j] -= etf[j]/ i ; - - etf[1]=1; - for(int i=2 ; i<= MAXN ; i++) { - if ( etf[i]==i) //if that number is prime - etf[i] = i-1; + } + + for(int i=2;i<=MAXN;i++){ + if(etf[i]==i){ + etf[i]=i-1; + for(int j=2;j*i<=MAXN;j++){ + etf[j*i]=(1-(1/i)); + } + } } } From bb8a41ebbc05bdc4e0e547fbdba17024e95f9dbe Mon Sep 17 00:00:00 2001 From: Shrutiruchi <46797012+Shrutiruchi@users.noreply.github.com> Date: Sat, 19 Oct 2019 08:09:21 +0530 Subject: [PATCH 4/5] Update fast_doubling.cpp --- Fibonacci/fast_doubling.cpp | 51 ++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/Fibonacci/fast_doubling.cpp b/Fibonacci/fast_doubling.cpp index a157574..6d9713a 100644 --- a/Fibonacci/fast_doubling.cpp +++ b/Fibonacci/fast_doubling.cpp @@ -3,26 +3,41 @@ #define REP(i,n) for (int i = 1; i <= n; i++) using namespace std; -typedef long long ll; - -map F; - -ll m=1000000007; - -long long f(long long n) { - if (F.count(n)) - return F[n]; - long long k = n / 2; - if (n % 2 == 0) { // n=2*k - return F[n] = (f(k) * f(k) + f(k - 1) * f(k - 1)) % m; - } else { // n=2*k+1 - return F[n] = (f(k) * f(k + 1) + f(k - 1) * f(k)) % m; - } +void fast_fib(long long int n,long long int ans[]) +{ + if(n == 0) + { + ans[0] = 0; + ans[1] = 1; + return; + } + fast_fib((n/2),ans); + a = ans[0]; /* F(n) */ + b = ans[1]; /* F(n+1) */ + c = 2*b - a; + if(c < 0) + c += MOD; + c = (a * c) % MOD; /* F(2n) */ + d = (a*a + b*b) % MOD; /* F(2n + 1) */ + if(n%2 == 0) + { + ans[0] = c; + ans[1] = d; + } + else + { + ans[0] = d; + ans[1] = c+d; + } } int main() { - F[0] = F[1] = 1; - ll n;cin >> n; // This answers the term n - cout << f(n-1); + + long long int n; /* nth value to be found */ + cin>>n; + long long int ans[2]={0}; + fast_fib(n,ans); + cout< Date: Sat, 19 Oct 2019 08:10:10 +0530 Subject: [PATCH 5/5] Update fast_doubling.cpp --- Fibonacci/fast_doubling.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Fibonacci/fast_doubling.cpp b/Fibonacci/fast_doubling.cpp index 6d9713a..9724040 100644 --- a/Fibonacci/fast_doubling.cpp +++ b/Fibonacci/fast_doubling.cpp @@ -1,8 +1,10 @@ /* Fast Doubling Method - Fibonacci */ #include -#define REP(i,n) for (int i = 1; i <= n; i++) using namespace std; +#define MOD 1000000007; +long long int a,b,c,d; + void fast_fib(long long int n,long long int ans[]) { if(n == 0)