sgu 181. X-Sequence 答案出现循环

本文介绍了一个整数序列生成算法,并提供了完整的C++实现代码。该算法通过特定公式递推生成序列,解决了寻找序列中第k项的问题。输入包括初始值及各项参数,输出为所求项的值。

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

181. X-Sequence
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard
output: standard



Let {xi} be the infinite sequence of integers: 
1) x0 = A; 
2) xi = (alpha * xi-1^2 + beta * xi-1 + gamma) mod M, for i >= 1. 
Your task is to find xk if you know A, alpha, beta, gamma, M and k.

Input
Given A (1 <= A <= 10000), alpha (0 <= alpha <= 100), beta (0 <= beta <= 100), gamma (0 <= gamma <= 100), M (1 <= M <= 1000), k (0 <= k <= 10^9). All numbers are integer.

Output
Write xk.

Sample test(s)

Input
1 1 1 1 10 1
Output
3


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll n[10000];

int check(int x){
    for(int i=x-1;i>=1;i--){
        if(n[x]==n[i])return i;
    }
    return -1;
}

int main(){
    ll A,a,b,c,M,k;
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&A,&a,&b,&c,&M,&k);
    if(k==0){
        printf("%I64d",A);
        return 0;
    }
    n[0]=A;
    for(int i=1;;i++){
        n[i]=(a*n[i-1]*n[i-1]%M+b*n[i-1]%M+c)%M;
        if(k==i){
            printf("%I64d",n[k]);
            return 0;
        }
        int ans=check(i);
        if(ans==-1)continue;
        printf("%I64d",n[ans+(k-ans)%(i-ans)]);
        return 0;
    }
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值