poj 3641 快速幂

来源:互联网 发布:淘宝店铺运营费用 编辑:程序博客网 时间:2024/06/16 11:02
Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear now, and only the first three numbers of each sequence are recognizable. Xinlv wants to know some numbers in these sequences, and he needs your help.InputThe first line contains an integer N, indicting that there are N sequences. Each of the following N lines contain four integers. The first three indicating the first three numbers of the sequence, and the last one is K, indicating that we want to know the K-th numbers of the sequence. You can assume 0 < K <= 10^9, and the other three numbers are in the range [0, 2^63). All the numbers of the sequences are integers. And the sequences are non-decreasing. OutputOutput one line for each test case, that is, the K-th number module (%) 200907.Sample Input21 2 3 51 2 4 5Sample Output516
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef long long ll;const int mod=200907;ll quick_mod(ll a,ll b) {    ll ans=1;    while(b) {        if(b&1) ans=(ans*a)%mod;        a=(a*a)%mod;        b>>=1;    }    return ans;}int main() {    int t;    scanf("%d",&t);    while(t--) {        ll a,b,c,k;        scanf("%lld%lld%lld%lld",&a,&b,&c,&k);        if(2*b==a+c) {            ll d=(b-a);            ll ans=(a%mod+(k-1)*(b-a)%mod)%mod;            printf("%lld\n",ans);        }        else {            ll q=b/a;            ll ans=(a*quick_mod(q,k-1))%mod;            printf("%lld\n",ans);        }    }    return 0;}