hdoj 2817 A sequence of numbers

http://acm.hdu.edu.cn/showproblem.php?pid=2817
题目意思是给一个数列的前三项,这个数列要么是等差数列要么是等比数列,输出数列的第K项;
数据范围要求求等比数列的第K项的时候要用快速幂取模,然后就很水了;

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define ll long long
#define MOD 200907
using namespace std;
ll qpow(ll a,ll b)
{
    ll res=1;
    a%=MOD;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%MOD;
        b>>=1;
        a=(a*a)%MOD;
    }
    return res;
}
int main()
{
    ll a[10];
    int k,T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=1;i<=3;i++)
            scanf("%lld",&a[i]);
        scanf("%d",&k);
        if(a[2]-a[1]==a[3]-a[2])
        {
            printf("%lld\n",(a[1]+(k-1)*(a[2]-a[1]))%MOD);
        }
        else
        {
            printf("%lld\n",(a[1]*qpow(a[2]/a[1],k-1))%MOD);
        }
    }
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/sdxtcqs/article/details/78739390