薛先生想改变后代的IQ,为此他发明了一种药,这种药有三种属性:A, B,
P。他父亲的智商为X,薛先生的智商为Y,用了这种药之后,薛先生的孩子的智商就可以变为(AX+BY) mod P。后代的智商以此类推。

现在给定X和Y,还有药的属性A、B和P,现在他想知道他的N代子孙的IQ(儿子是第一代,孙子是第二代)。

Input
第一行包含一个整数T(T<=100),表示数据组数 每组数据只有一行,包含六个整数X,Y,A,B,P,N(1 ≤ X, Y ≤ 300,1 ≤ A, B ≤ 30, 1≤ P ≤ 300 , 1 ≤ N < 1000000000),含义如题目所述
Output
针对每组数据,输出第N代子孙的智商。

Sample Input

4
180 80 1 1 190 1 
189 83 2 2 190 1 
189 83 1 1 190 2 
172 73 23 19 273 9999

Sample Output

70 
164 
165 
233

【思路】
本题有两种解法:

  1. 循环节
    由题显然可以看出,P在1-300之间,则结果一定会在1-90000之间出现循环节,注意是连续的两组数据要同时和之前出现的一组数据相同,才被确认为出现了循环节。另外,循环节不一定是从0,1开始,可能呈6字形,也即从数据的中间开始出现循环,八戒影院这种情况一定要考虑。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 90009;
int ans[maxn+2];
int vis[303][303]; int main()
{
int t;
scanf("%d", &t);
int x, y, a, b, p, n;
while(t--){
memset(vis, -1, sizeof(vis));
scanf("%d%d%d%d%d%d", &x, &y, &a, &b, &p, &n);
ans[0] = y;
ans[1] = (a * x + b * y) % p;
vis[ans[0]][ans[1]] = 0;
int start;
int circle;
for(int i = 2; i < maxn; i++){
ans[i] = (a * ans[i-2] + b * ans[i-1]) % p;
if(vis[ans[i-1]][ans[i]] != -1){
start = vis[ans[i-1]][ans[i]];
circle = i - 1 - start;
break;
}
else
vis[ans[i-1]][ans[i]] = i-1;
}
cout << ans[start + (n - start) % circle] << endl;
}
return 0;
}
  1. 快速幂
    这种类似斐波那契的数据类型,可以用快速幂来解决。要注意的是初始矩阵的初始化不能出错。然后再写一个矩乘函数就行了。

代码如下:

 
#include<cstdio>
#include<iostream>
#include<string>
#include<set>
using namespace std;
typedef long long LL;
int mod;
int x, y, p, q;
struct Node{LL m[3][3];}; void Init(Node &t, Node &a){
t.m[1][1] = y;
t.m[2][2] = 0;
t.m[1][2] = x;
t.m[2][1] = 0;
a.m[1][1] = q;
a.m[1][2] = 1;
a.m[2][1] = p;
a.m[2][2] = 0;
} Node mul(Node a, Node b){
Node tem;
tem.m[1][1] = (a.m[1][1] * b.m[1][1] + a.m[1][2] * b.m[2][1]) % mod;
tem.m[1][2] = (a.m[1][1] * b.m[1][2] + a.m[1][2] * b.m[2][2]) % mod;
tem.m[2][1] = (a.m[2][1] * b.m[1][1] + a.m[2][2] * b.m[2][1]) % mod;
tem.m[2][2] = (a.m[2][1] * b.m[1][2] + a.m[2][2] * b.m[2][2]) % mod;
return tem;
} int main (){
int n;
int tim;
scanf("%d", &tim);
Node t, a;
while(tim--){
scanf("%d%d%d%d%d%d",www.rcsx.org &x, &y, &p, &q, &mod, &n);
Init(t, a);
while(n){
if(n&1) t = mul(t,a);
a = mul(a, a);
n >>= 1;
}
cout << t.m[1][1] % mod << endl;
} return 0;
}

薛XX后代的IQ CSU1597【循环节】或【快速幂】的更多相关文章

  1. CSU 1597 薛XX后代的IQ

    Description 薛XX的低IQ是个令人头疼的问题,他的队友深受其害.幸运的是,薛XX非常有钱,所以他买了一些可以提高他的后代的IQ的药.这种药有三个属性,A,B和P.当薛XX使用这种药的时候, ...

  2. HDU - 5451 Best Solver(循环节+矩阵快速幂)

    Best Solver The so-called best problem solver can easily solve this problem, with his/her childhood ...

  3. 2019牛客多校第五场 generator 1——广义斐波那契循环节&&矩阵快速幂

    理论部分 二次剩余 在数论中,整数 $X$ 对整数 $p$ 的二次剩余是指 $X^2$ 除以 $p$ 的余数. 当存在某个 $X$,使得式子 $X^2 \equiv d(mod \ p)$ 成立时,称 ...

  4. hdu4291 暴力循环节+矩阵快速幂

    题意:       给你一个关系式,x[n] = 3*x[n-1] + x[n-2],求x(x(x[n]))%1000000007. 思路:       做这个题目要明确一点,就是对于取余操作大多数时 ...

  5. hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. 循环节 + 矩阵快速幂 - HDU 4291 A Short problem

    A Short problem Problem's Link Mean: 给定一个n,求:g(g(g(n))) % 1000000007 其中:g(n) = 3g(n - 1) + g(n - 2), ...

  7. UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)

    题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...

  8. LA 3704细胞自动机——循环矩阵&&矩阵快速幂

    题目 一个细胞自动机包含 $n$ 个格子,每个格子的取值为 $0 \sim m-1$.给定距离 $d$,则每次操作是将每个格子的值变为到它的距离不超过 $d$ 的所有格子的在操作之前的值的和除以 $m ...

  9. UVA 11582 Colossal Fibonacci Numbers!(循环节打表+幂取模)

    题目链接:https://cn.vjudge.net/problem/UVA-11582 /* 问题 输入a,b,n(0<a,b<2^64(a and bwill not both be ...

随机推荐

  1. 从零开始的Lua宅[1]:编译Lua解释器

    Lua是一门神奇的脚本语言,游戏宅必备,懒人必备.Lua差多不是学起来用起来最简单的语言了,以至于简单到自身就是文档,自身就是配置文件.但是Lua的运行效率却是众多脚本中非常高的,据说仅次于V8爹下的 ...

  2. Jenkins上svn更新策略说明

  3. OTOH

    OTOH n 网络用语 On the Other Hand 另一方面 [例句]OTOH, pressure on the keys of a digital AFTER bottoming can b ...

  4. [LUOGU] 4149 [IOI2011]Race

    点分治裸题 #include<iostream> #include<cstring> #include<cstdio> using namespace std; i ...

  5. MYSQL不能显示中文字,显示错误“ERROR 1366 (HY000): Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89'”

    或者建表时带上编码utf8 CREATE TABLE `students`( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR( ...

  6. Vim如何显示和关闭行号

    显示行号: set nu 去除行号: set nonu

  7. LeetCode之螺旋矩阵

    问题 螺旋矩阵 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ ...

  8. phpExcel使用方法二

    require_once './phpexcel/PHPExcel.php'; // 首先创建一个新的对象 PHPExcel object $objPHPExcel = new PHPExcel(); ...

  9. 重写laravel 异常抛出处理

    所有异常错误都由类App\Exceptions\Handler处理,该类包含两个方法:report和render. 这里我们只看render方法,该方法会将异常渲染到HTTP响应中,就是说上面的错误信 ...

  10. 【js】window.onscroll 无效问题

    body 设置为height:100% 导致window.onscroll 无效