九度OJ 1442 A sequence of numbers

本文介绍了一种算法,用于解决已知序列前三项的情况下,求取该序列第K项的问题。具体包括如何判断序列类型(等差或等比),并提供对应的计算方法。输入包含多个测试案例,每个案例由序列的前三项及欲求项数构成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址: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
#include <stdio.h>
 
#define M 200907
 
long long fun1(long long data[], int k){
    long long p = data[1] - data[0];
    return ((data[0] % M) + (((k-1) % M) * (p % M)) % M) % M;
}
 
long long fun2(long long data[], int k){
    long long p = data[1] / data[0];
    long long ans = data[0];
    --k;
    while (k != 0){
        if (k % 2 == 1){
            ans = (ans * p) % M;
        }
        k /= 2;
        p = (p * p) % M;
    }
    return ans;
}
 
long long KthNumber(long long data[], int k){
    int flag;
 
    if ((data[1] - data[0]) == (data[2] - data[1]))
        flag = 0;
    else
        flag = 1;
    if (flag == 0){
        return fun1(data, k);
    }
    else{
        return fun2(data, k);
    }
}
 
int main(void){
    int n;
    long long data[3];
    int k;
    int i;
 
    while (scanf ("%d", &n) != EOF){
        while (n-- != 0){
            for (i=0; i<3; ++i){
                scanf ("%lld", &data[i]);
            }
            scanf ("%d", &k);
            printf ("%d\n", KthNumber (data, k));
        }
    }
 
    return 0;
}


 

转载于:https://www.cnblogs.com/liushaobo/p/4373784.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值