hdu 2817 A sequence of numbers(快速幂)

时间:2022-09-01 12:36:59
Problem Description
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.
 
Input
The 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.
 
Output
Output one line for each test case, that is, the K-th number module (%) 200907.
 
Sample Input
2
1 2 3 5
1 2 4 5
 
Sample Output
5
16
 
Source
 

等比数列或等差数列。。。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define MOD 200907
#define ll long long
ll pow_mod(ll a,ll n)
{
if(n==)
return %MOD;
ll tt=pow_mod(a,n>>);
ll ans=tt*tt%MOD;
if(n&)
ans=ans*a%MOD;
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ll a,b,c,k;
scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);
if(a==b && b==c)
{
printf("%I64d\n",a%MOD);
continue;
}
if(k==)
{
printf("%I64d\n",a%MOD);
continue;
}
if(k==)
{
printf("%I64d\n",b%MOD);
continue;
}
if(k==)
{
printf("%I64d\n",c%MOD);
continue;
}
ll cnt=b-a;
if(c-b==cnt)
{
ll ans=a+(k-)*cnt;
printf("%I64d\n",ans%MOD);
}
else
{
ll q=b/a;
printf("%I64d\n",a*pow_mod(q,k-)%MOD);
}
}
return ;
}