obsidian

HDU 2817 A sequence of numbers(快速幂)

18
2017/08

题目

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 \leq 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

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<cstdlib>
#include<functional>
#include<climits>
#include<cctype>
#include<iomanip>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define clr(a,x) memset(a,x,sizeof(a))
const double eps = 1e-6;
const int mod=200907;
ll quick_mod(ll a,ll n,int mod)
{
ll t=1;
for(;n;n>>=1,a=(a*a%mod))
if(n&1)
t=(t*a%mod);
return t;
}
int main()
{
int t;
cin>>t;
double a,b,c;
ll n;
while(t--)
{
cin>>a>>b>>c>>n;
if(a+c==2*b)
{
ll a1=a;
ll d=(b-a);
ll ans=(a1%mod+((n-1)%mod)*(d%mod))%mod;
printf("%lld\n",ans);
}
else
{
ll a1=a;
ll t1=a1%mod;
double q1=b/a;
ll q2=q1;
ll q=q2%mod;
ll tmp=quick_mod(q,n-1,mod);
ll ans=(t1*tmp)%mod;
printf("%lld\n",ans);
}
}
return 0;
}

Last modification：November 2nd, 2019 at 03:01 pm