Thursday, June 16, 2016

Euler phy function as sieve

#include<bits/stdc++.h>
using namespace std;

#define M 10000050

int phi[M];

int main()
{

  for (int i = 1; i < M; i++) {
    phi[i] = i;
  }
int t,w;
  for (int p = 2; p < M; p++) {
         w=phi[p];
    if (phi[p] == p) { // p is a prime
      for (int k = p; k < M; k += p) {
        phi[k] -= phi[k] / p;
   t=phi[k];

      }
    }
  }

}

1014 - Ifter Party

Problem link : http://lightoj.com/volume_showproblem.php?problem=1014

Solution idea : very simple. Just need to find all the divsors of (p-l) by iterating a for loop from 1 to square root of (p-l) and we will select the number if the number is >l and has not been selected before.

Solution :

#include<bits/stdc++.h>
using namespace std;

vector<int>ans;

void nod(long long int  n,long long int  l)
{
    int sq=sqrt(n);

    if(n==0)
    {
        ans.push_back(1);
        return;
    }
    for(int i=1; i<=sq; i++)
    {

        long long int a;
        a=n/i;
        if(n%i==0)
        {
            if(i>l) ans.push_back(i);
            if(a>l and a!=i)ans.push_back(a);
        }
    }
}

int main()
{
    int t;
    cin>>t;
    long long int p,l;

    for(int ca=1; ca<=t; ca++)
    {
        cin>>p>>l;
        nod(p-l,l);
        sort(ans.begin(),ans.end());
        int q=ans.size();
        if(q==0)
        {
            cout<<"Case "<<ca<<": "<<"impossible"<<endl;
        }
        else
        {
            cout<<"Case "<<ca<<":";
            for(int i=0; i<q; i++) cout<<" "<<ans[i];
            cout<<endl;
        }
        ans.clear();
    }

}

Bitwise sieve source code

#include <bits/stdc++.h>
using namespace std;

const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)

int _c[(MAX>>6)+1];

vector<int> primes;

#define IsComp(n)  (_c[n>>6]&(1<<((n>>1)&31)))

#define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))

void prime_sieve() {
    for (int i = 3; i <= LMT; i += 2)
        if (!IsComp(i))
            for (int j = i*i; j <= MAX; j += i+i)
                SetComp(j);

    primes.push_back(2);
    for (int i=3; i <= MAX; i += 2)
        if (!IsComp(i))
            primes.push_back(i);
}

int main()
{

    prime_sieve();
    for(int i=0;i<primes.size();i++) cout<<primes[i]<<endl;
}

Friday, May 20, 2016

674 - Coin Change

Problem linkhttps://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=615

Solution :

#include<bits/stdc++.h>

using namespace std;

int dp[6][7492];

int amnt[]= {50,25,10,5,1};

int coin(int i,int ammount)
{
    if(i>=5)
    {
        if(ammount==0) return 1;
        else return 0;

    }

    int ret1=0,ret2=0;

    if(dp[i][ammount]!=-1) return dp[i][ammount];

    if(ammount-amnt[i]>=0) ret1=coin(i,ammount-amnt[i]);
    ret2=coin(i+1,ammount);

    return dp[i][ammount]=ret1+ret2;

}

int main()
{
    int make;

    memset(dp,-1,sizeof(dp));

    while(scanf("%d",&make)==1)
    {
        cout<<coin(0,make)<<endl;
    }

}

10130 - SuperSale

Problem link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1071

Solution :

#include<bits/stdc++.h>

using namespace std;

long long int dp[1005][35];

long long int weight[1005];
long long int cost[1005];
long long int cap;
long long int node;
long long int ans=0;

long long int knapsack(long long int idx,long long int w)
{
    if(idx==node+1) return 0;

    if(dp[idx][w]!=-1) return dp[idx][w];

    int profit1=0,profit2=0;

    if(w+weight[idx]<=cap) profit1=cost[idx]+knapsack(idx+1,w+weight[idx]);

    profit2=knapsack(idx+1,w);

    dp[idx][w]=max(profit1,profit2);

    return dp[idx][w];
}


int main()
{


    long long int t,i,g,p;
    cin>>t;


    while(t--)
    {
        cin>>node;
        p=node;

        i=1;
        while(p--)
        {
            cin>>cost[i]>>weight[i];
            i++;
        }
        cin>>g;
        while(g--)
        {
            memset(dp,-1,sizeof(dp));
            cin>>cap;
            ans=ans+knapsack(1,0);
        }
        cout<<ans<<endl;
        ans=0;
    }

}

Wednesday, May 18, 2016

1006 - Hex-a-bonacci

Problem link : http://www.lightoj.com/volume_showproblem.php?problem=1006


Solution :

#include<bits/stdc++.h>

using namespace std;

long long int dp[10050];

long long int a, b, c, d, e, f;
long long int fn( long long int n )
{
    if(dp[n]!=-1) return dp[n];
    if( n == 0 ) return a;
    if( n == 1 ) return b;
    if( n == 2 ) return c;
    if( n == 3 ) return d;
    if( n == 4 ) return e;
    if( n == 5 ) return f;
    return dp[n]=(( fn(n-1) + fn(n-2) + fn(n-3) + fn(n-4) + fn(n-5) + fn(n-6) )%10000007);
}
int main()
{

    long long int n, caseno = 0, cases;


    scanf("%lld", &cases);
    while( cases-- )
    {
        memset(dp,-1,sizeof(dp));
        scanf("%lld %lld %lld %lld %lld %lld %lld", &a, &b, &c, &d, &e, &f, &n);

        printf("Case %lld: %lld\n", ++caseno, fn(n)%10000007);
    }
    return 0;

}

Thursday, April 28, 2016

1294 - Positive Negative Sign

Problem Linkhttp://www.lightoj.com/volume_showproblem.php?problem=1294

Solve :

#include<bits/stdc++.h>

using namespace std;

int main()
{

    long long int n,m,j=1,t;
    scanf("%lld",&t);

    while(t--)
    {
        scanf("%lld%lld",&n,&m);

        n=n/2;
        n=n*m;
        printf("Case %lld: %lld\n",j,n);
        j++;

    }

}