【九度OJ】题目1442:A sequence of numbers 解题报告

91 篇文章 0 订阅
90 篇文章 33 订阅

【九度OJ】题目1442:A sequence of numbers 解题报告

标签(空格分隔): 九度OJ


原题地址:http://ac.jobdu.com/problem.php?pid=1442

题目描述:

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

解题方法

这个题的意思就是这个数列可能是等差数列也是等比数列,自己判断,然后找出第k个数。

等差比较好计算,等比数列求解要用到二分求幂。

#include<stdio.h>

#define M  200907

long long fun(long long a, long long d, long long k) {
    k--;//公式
    long long ans = a;//等比初项
    while (k != 0) {
        if (k % 2 == 1) {
            ans *= d;
            ans %= M;//不超过M
        }
        k /= 2;
        d *= d;
        d %= M;//
    }
    return ans;
}

int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        while (n-- != 0) {
            long long answer = 0;
            long long a, b, c;
            int k;
            scanf("%lld%lld%lld%d", &a, &b, &c, &k);
            if (2 * b == a + c) {
                long long dis = b - a;
                answer = (a % M) + (((k - 1) % M) * (dis % M) % M) % M;//等差

            } else {
                long long dis = b / a;
                answer = fun(a, dis, k);//等比,二分求幂
            }
            printf("%lld\n", answer);
        }
    }
    return 0;
}

Date

2017 年 3 月 8 日

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值