hdu2817 A sequence of numbers
题目链接: 2817 ( A sequence of numbers )
算术序列即等差数列,几何序列即等比数列。
等差数列 \(a_k=a_1+(k-1)\cdot d\) ( \(d\) 为公差)
等比数列 \(a_k=a_1\cdot q^{k-1}\) ( \(q\) 为公比),由于 \(k\) 可能很大,需用快速幂计算。
/**
* hdu2817 A sequence of numbers
* We will call a sequence an arithmetic sequence if there is a common difference.
* A Geometric Sequence is a sequence which the ratio of the common terms is equal.
*/
#include <iostream>
using namespace std;
typedef long long LL;
const int mod = 200907;
LL fastPow(LL a, LL b, LL mod = LLONG_MAX)
{
a %= mod;
LL t = 1;
while (b) {
if (b&1) t = t*a%mod;
a = a*a%mod;
b >>= 1;
}
return t%mod;
}
int main()
{
int N;
cin >> N;
while (N--) {
LL a, b, c;
int k;
cin >> a >> b >> c >> k;
int ans;
if (b-a == c-b) {
LL d = b-a, a0 = a-d;
ans = (a0%mod+d%mod*k) % mod;
}
else if (b%a == 0 && c%b == 0 && b/a == c/b) {
LL q = c/b, a0 = a;
ans = a0%mod*fastPow(q, k-1, mod)%mod;
}
printf("%d\n", ans);
}
return 0;
}
模板总结
// 用于快速计算 a^b%mod
// 待试炼
LL fastPow(LL a, LL b, LL mod = LLONG_MAX)
{
a %= mod;
LL t = 1;
while (b) {
if (b&1) t = t*a%mod;
a = a*a%mod;
b >>= 1;
}
return t%mod;
}