九度OJ 1442 A sequence of numbers

这题的题目一开始没有看懂,因为没有理解arithmetic sequences 和 geometric sequences的意思,百度了一下后明白了是等差和等比数列的意思。
本题有几个点:
第一,数据非常大,在计算幂次的时候要使用二分求幂防止溢出;
第二,答案要求 mod 200907,因此要注意mod公式的使用:
①(a*b)%c=((a%c)*(b%c))%c
   ②(a+b)%c=((a%c)+(b%c))%c

然而,还是WA了一次。。。发现是因为用pow函数(不能用自带的函数,要自己写)的时候,两个参数mod了200907,这样计算出来的答案就不对了,改后AC。

题目描述:
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.

输入:
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 one line for each test case, that is, the K-th number module (%) 200907.

样例输入:
2
1 2 3 5
1 2 4 5

样例输出:
5
16

代码如下:

#include <cstdio>
#define M 200907

long long pow(long long x,long long y)
{
    long long ans = 1;
    while(y)
    {
        if(y % 2 == 1)
        {
            ans *= x;
            ans = ans % M;
        }
        x *= x;
        x = x % M;
        y /= 2;

    }
    return ans;
}

int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        while(n--)
        {
            long long buf[3];
            for(int i = 0; i < 3; i++)
                scanf("%lld",&buf[i]);
            long long k;
            scanf("%lld",&k);
            long long ans;
            if(buf[2] - buf[1] == buf[1] - buf[0])
                ans = (buf[0]%M + ((k-1)%M)*((buf[1]-buf[0])%M))%M;
            else 
                ans  = (buf[0]%M)*((pow((buf[1]/buf[0]),(k-1)))%M)%M;
            printf("%lld\n",ans);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值