Friday, January 20, 2017

Solution of Light OJ 1067-Combinations

See the problem Light OJ 1067

#include<bits/stdc++.h>
#define ll long long 
using namespace std;
ll n, r, t, i, ans, cs=1, up, dwn, fac[1000009];
const ll mod=1000003;

ll bigmod(ll b, ll p){
    if(p==0) return 1;
    ll x=bigmod(b, p/2);
    x=(x*x)%mod;
    if(p%2==1)x=(x*b)%mod;
    return x;
}

int main(){
    fac[0]=1;
    for(i=1; i<=1000000; i++){
        fac[i]=(fac[i-1]*i)%mod;
    }

    cin>>t;
    while(t--){
        cin>>n>>r;
        up=fac[n];
        dwn=(fac[n-r]*fac[r])%mod;
        ans=up*bigmod(dwn, mod-2);
        cout<<"Case "<<cs++<<": "<<ans%mod<<endl;
    }
    return 0;
}

Solution of Light OJ 1028 -Trailing Zeroes (I)

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

ll t, n, m, i, j, ans, prime[1000009], num[1000009]={0}, cnt, cs=1;

void seive(){
    for(i=3; i*i<=1000000; i+=2){
        if(num[i]==0){
            for(j=i*i; j<=1000000; j=j+(i*2)) num[j]=1;
        }
    }
    prime[0]=2;
    j=1;
    for(i=3; i<=1000000; i+=2){
        if(num[i]==0) prime[j++]=i;
    }
}

int main(){
    seive();
    scanf("%lld", &t);

    while(t--){
        cin>>n;
        ans=1;
        for(i=0; i<j&&prime[i]*prime[i]<=n; i++){
            cnt=0;
            while(n%prime[i]==0){
                cnt++;
                n=n/prime[i];
            }
            ans=ans*(cnt+1);
        }

        if(n!=1)ans=ans*2;
        cout<<"Case "<<cs++<<": "<<ans-1<<endl;

    }
    return 0;
}

Thursday, January 19, 2017

Solution of UVa-11029 Leading and Trailing


#include<bits/stdc++.h>
#define ll long long

using namespace std;

ll n, k, t_case;

ll bigmod(ll b, ll p, ll m){

    if(p==0)return 1;

    ll xx=bigmod(b, p/2, 1000);
    xx=(xx*xx)%1000;

    if(p%2==1)xx=(xx*b)%1000;

    return xx;
}

int main(){

    cin>>t_case;

    while(t_case){

        cin>>n>>k;

        /* executing first 3 digits */

        double x=k*(log10(n));

        x=x-(int)x; // taking fraction value only
        
        double ans=pow(10, x);

        ans=ans*100;

        cout<<(int)ans<<"...";

        /* executing last 3 digits */

        printf("%03d\n", bigmod(n, k, 1000));

        t_case--;

    }

    return 0;
}