Description

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 Input

Input
 
 
1 1 1 1 10 1
 
 
Output
 
 
3
 

/**********************
打表观察,一个周期问题,暴力找周期 ,巧用map找周期
********************************/
#include"iostream"
#include"map"
#include"cstdio"
using namespace std;
const int ms=;
int T,begin,A,a,b,c,m,k;
void find()
{
map<int, int> mp;
//map[0]=A;
mp[]=A;
int tmp=A;
for(int i=;i<=ms;i++)
{
tmp=(a*tmp*tmp+b*tmp+c)%m;
if(mp[tmp])
{
begin=mp[tmp];
T=i-mp[tmp];
break;
}
else
{
mp[tmp]=i;
}
}
}
int main()
{
scanf("%d%d%d%d%d%d",&A,&a,&b,&c,&m,&k);
find();
int ans;
if(k<begin)
{
ans=A;
for(int i=;i<=k;i++)
ans=((long long)a*ans*ans+b*ans+c)%m;
cout<<ans<<endl;
}
else
{
k-=begin;k%=T;ans=A;
for(int i=;i<=begin+k;i++)
{
ans=(a*ans*ans+b*ans+c)%m;
}
cout<<ans<<endl;
}
return ;
}

X-Sequence的更多相关文章

  1. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  2. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  3. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  10. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. Codeforces Round #363

    http://codeforces.com/contest/699 ALaunch of Collider 题意:n个球,每个球向左或右,速度都为1米每秒,问第一次碰撞的时间,否则输出-1 贪心最短时 ...

  2. [HIve - LanguageManual] Subqueries

    Subqueries in the FROM Clause Subqueries in the WHERE Clause Subqueries in the FROM Clause SELECT .. ...

  3. av_interleaved_write_frame 网络不好的情况下返回较慢

    用libvlc做直播推流引擎在网络较差的情况下,需要关闭直播,并且重新开播.这个过程中,推流引擎重启,需要的是快速响应.实际上测试结果发现,经常会发生引擎关闭接口卡住.后来跟踪代码,定位到s_rtmp ...

  4. 第二百八十天 how can I 坚持

    今天发现一只大bug,目前还没有解决掉... 晚上和徐斌还有他同学一块吃了个饭.还有.没了. 今天想早睡觉. 今天股市暴跌,二度熔断,好精彩,哈哈,不说啥了,还有苹果股票和谷歌市值越来越接近了,要走下 ...

  5. 【转】Maven实战(六)--- dependencies与dependencyManagement的区别

    原博文出自于:http://blog.csdn.net/liutengteng130/article/details/46991829   感谢! 在上一个项目中遇到一些jar包冲突的问题,之后还有很 ...

  6. Python多线程学习资料1

    一.Python中的线程使用: Python中使用线程有两种方式:函数或者用类来包装线程对象. 1.  函数式:调用thread模块中的start_new_thread()函数来产生新线程.如下例: ...

  7. 强连通分量(tarjan求强连通分量)

    双DFS方法就是正dfs扫一遍,然后将边反向dfs扫一遍.<挑战程序设计>上有说明. 双dfs代码: #include <iostream> #include <cstd ...

  8. C#一些知识点:委托和事件的区别

    在C#中,委托和事件是比较容易混淆的两个知识点,本篇博客就记录一下委托和事件之间的区别. 定义上的区别 委托:委托实际上是一个类,用来表示一个函数,可以理解为C++中的函数指针. 事件:事件是一个修饰 ...

  9. 在MVC项目中使用RDLC报表

    原文地址:http://www.cnblogs.com/wuhuacong/p/4109833.html RDLC是一个不错的报表,有着比较不错的设计模式和展现效果,在我的Winform开发里面,使用 ...

  10. [c++]堆和栈的区别

    堆和栈的区别一.预备知识—程序的内存分配一个由c/C++编译的程序占用的内存分为以下几个部分1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构 ...