Fork me on GitHub

题目1442:A sequence of numbers(数列计算以及二分求幂运用)

题目链接:http://ac.jobdu.com/problem.php?pid=1442

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

 

参考代码:

//
//  1442 A sequence of numbers.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 15/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define ret 200907
using namespace std;
 
int n;
long long a,b,c,k;
 
long long cal(long long a, long long q, long long  k){
    long long ans = a;
    k--;
    while(k!=0){
        if(k%2==1){
            ans *= q;
            ans%=ret;
        }
        k/=2;
        q*=q;
        q%=ret;
    }
    return ans;
}
 
int main(){
    scanf("%d",&n);
    while(n--){
        scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
        long long ans = 0;
        if(b-a==c-b){
            ans=( a % ret )+( ( (k-1)%ret )*( (b-a)%ret )%ret )%ret;
        }
        else{
            long long  q = b/a;
            ans = cal(a,q,k);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
/**************************************************************
    Problem: 1442
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/

 

posted @ 2017-04-15 20:42  伊甸一点  阅读(276)  评论(0编辑  收藏  举报