Skip to content

Update sieve.cpp #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Euler Totient Function/etf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
}

Expand Down
53 changes: 35 additions & 18 deletions Fibonacci/fast_doubling.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
/* Fast Doubling Method - Fibonacci */
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 1; i <= n; i++)
using namespace std;

typedef long long ll;
#define MOD 1000000007;
long long int a,b,c,d;

map<long long, long long> 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<<ans[0]<<endl
return 0;
}
24 changes: 19 additions & 5 deletions Longest Increasing Subsequence/LIS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,24 @@ int LIS()
for(int i=0;i<n;i++)
{
cin>>elem;
//vector<int>::iterator it = upper_bound(d.begin(),d.end(),elem); // non-decreasing
vector<int>::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<<d.size();
int dp[n];
for(int i=0;i<n;i++){
dp[i]=1;
}
for(int i=2;i<n;i++){
for(int j=i-1;j>=0;j--){
if(d[i]>=d[j]){
dp[i]=max(dp[i],dp[j]+1);
}
}
}
int ans=INT_MIN;
for(int i=0;i<n;i++){
if(ans<dp[i]){
ans=dp[i];
}
}
cout<<ans<<endl;
}
4 changes: 2 additions & 2 deletions Sieve of Eratosthenes/sieve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down