【HDU】2817-A sequence of numbers(快速幂)

对于两种序列一种等比,一种等差

如果是等比 ak = a1 + (k - 1) * d 直接用((a1 * mod) + ((k - 1) % mod) * (d % mod)) % mod求就可以了

如果是等差 ak = a1 * q ^(k - 1) 利用快速幂求出q ^(k - 1)可以得到结果

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
int n;
int k;
LL a[5];
LL mod = 200907;
LL pow_mod(LL a,int m){
    //printf("%d\n",m);
    if(m == 1)
        return a % mod;
    if(m == 0)
        return 1;
    LL   d = pow_mod(a,m / 2);
    LL ans = d * d % mod;
    if(m & 1)
        ans = ans * a % mod;
    return ans;
}
int main(){
    scanf("%d",&n);
    while(n--){
        for(int i = 0; i < 3; i++)
            scanf("%I64d",&a[i]);
        scanf("%d",&k);
        LL ans;
        if(a[1] - a[0] == a[2] - a[1]){
            ans = ((a[0] % mod) + ((k - 1) % mod) * ((a[1] - a[0]) % mod)) % mod;
            printf("%I64d\n",ans);
        }
        else{
            LL d = a[1] / a[0];
            ans = ((a[0] % mod) * pow_mod(d,k - 1)) % mod;
            printf("%I64d\n",ans);
        }
    }
    return 0;
}
/*
1
1 10 100 1000000000
*/
时间: 2024-08-30 13:19:14

【HDU】2817-A sequence of numbers(快速幂)的相关文章

hdu 2817 A sequence of numbers(快速幂)

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

hdu 2817 A sequence of numbers(快速幂取余)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 题目大意:给出三个数,来判断是等差还是等比数列,再输入一个n,来计算第n个数的值. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #define m 200907 5 6 using namespace std; 7 8 __int64 fun(__int64 j,__int64 k) 9

HDU - 2817 - A sequence of numbers (快速幂取模!)

A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3494    Accepted Submission(s): 1073 Problem Description Xinlv wrote some sequences on the paper a long time ago, they migh

HDU 2817 A sequence of numbers 整数快速幂

A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4550    Accepted Submission(s): 1444 Problem Description Xinlv wrote some sequences on the paper a long time ago, they might

hdu 2817 A sequence of numbers

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2817 题意分析: 给数列前3项数字,要么是等差数列,要么是等比数列,然后计算出第K项.通过前3项套入等差数列的中项公式然后判断数列的类型,然后对应计算第K项.等差没什么好说的,等比这里需要用快速幂. 代码: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #i

HDu 5950 Recursive sequence(矩阵快速幂)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1323    Accepted Submission(s): 589 Problem Description Farmer John likes to play mathematics games with his N cows. Recently,

HDU - 1005 Number Sequence(简单矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 就是这道题目,然而找了一晚上的错误 \("▔□▔)/\("▔□▔)/\("▔□▔)/. 1 #include <iostream> 2 #include <cstring> 3 using namespace std;

HDOJ 2817 A sequence of numbers【快速幂取模】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3754    Accepted Submission(s): 1152 Problem Description Xinlv wrote so

HDU - 1588 Gauss Fibonacci (矩阵快速幂+二分求等比数列和)

Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. " How good an opportunity that Gardon can not give up! The "Prob

hdu 1588 Gauss Fibonacci(矩阵快速幂)

Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2090    Accepted Submission(s): 903 Problem Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r