UVa 10689 - Yet another Number Sequence Solution

#include <iostream>
#include <cmath>
using namespace std;
int pw(int x){
    int ans=1;
    while(x--)
        ans*=10;
    return ans;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int a,b,n,m;
        cin>>a>>b>>n>>m;
        if(m==1)
            n%=60;
        else if(m==2)
            n%=300;
        else if(m==3)
            n%=1500;
        else
            n%=15000;
        if(n==0)
            cout<<a%pw(m)<<endl;
        else if(n==1)
            cout<<b%pw(m)<<endl;
        else{
            int x=0,y=1,z=1;
            int N=n-2,mod=pw(m);
            while(N--){
                x=y;
                y=z;
                z+=x;
                z%=mod;
            }
            int ans=a*(y)+b*(z);
            ans%=mod;
            cout<<ans<<endl;
        }
    }
}

Comments