Mathematician QSC

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem Description
QSC dream of becoming a mathematician, he believes that everything in this world has a mathematical law.

Through unremitting efforts, one day he finally found the QSC sequence, it is a very magical sequence, can be calculated by a series of calculations to predict the results of a course of a semester of a student.

This sequence is such like that, first of all,f(0)=0,f(1)=1,f(n)=f(n−2)+2∗f(n−1)(n≥2)Then the definition of the QSC sequence is g(n)=∑ni=0f(i)2. If we know the birthday of the student is n, the year at the beginning of the semester is y, the course number x and the course total score s, then the forecast mark is xg(n∗y)%(s+1).
QSC sequence published caused a sensation, after a number of students to find out the results of the prediction is very accurate, the shortcoming is the complex calculation. As clever as you are, can you write a program to predict the mark?

 
Input
First line is an integer T(1≤T≤1000).

The next T lines were given n, y, x, s, respectively.

n、x is 8 bits decimal integer, for example, 00001234.

y is 4 bits decimal integer, for example, 1234.
n、x、y are not negetive.

1≤s≤100000000

 
Output
For each test case the output is only one integer number ans in a line.
 
Sample Input
2
20160830 2016 12345678 666
20101010 2014 03030303 333
 
Sample Output
1
317
 
Source

思路:首先求A^B%C=A^(B%phi(C)+phi(C))%C  B>=phi(C)指数循环节;

     然后,求g函数,f(n)显然可以用矩阵快速幂写,g(n)=f(n)*f(n+1)/2;因为/2,模除法,首先想到逆元,然而模不一定是奇数,偶数的情况2无逆元;

   现在怎么处理2,f(n)与f(n+1)必然有一个是偶数,发现除2后的递推式更改为f(n)=6*f(n-1)-f(n-2);

   ps:一个小技巧处理2,mdzz,模的数*2,答案/2;

   详见代码;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=1e5+,M=1e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
ll n,x,y,s;
ll m;
struct is
{
ll a[][];
};
is juzhenmul(is a,is b,ll hang ,ll lie,ll mod)
{
int i,t,j;
is ans;
memset(ans.a,,sizeof(ans.a));
for(i=;i<=hang;i++)
for(t=;t<=lie;t++)
for(j=;j<=lie;j++)
{
ans.a[i][t]+=(a.a[i][j]*b.a[j][t]);
ans.a[i][t]%=mod;
}
return ans;
}
is quickpow(is ans,is a,ll x,ll mod)
{
while(x)
{
if(x&) ans=juzhenmul(ans,a,,,mod);
a=juzhenmul(a,a,,,mod);
x>>=;
}
return ans;
}
void extend_Euclid(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
extend_Euclid(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
ll phi(ll n)
{
ll i,rea=n;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
rea=rea-rea/i;
while(n%i==) n/=i;
}
}
if(n>)
rea=rea-rea/n;
return rea;
}
ll Pow(ll a,ll n,ll mod)
{
ll ans=;
while(n)
{
if(n&)
{
ans=ans*a%mod;
}
a=a*a%mod;
n>>=;
}
if(ans==) ans+=mod;
return ans;
}
ll getans(ll x,ll mod)
{
if(x==)
return ;
if(x==)
return ;
is ans,base;
memset(ans.a,,sizeof(ans.a));
ans.a[][]=;
ans.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
ans=quickpow(ans,base,x-,mod);
return (ans.a[][]+ans.a[][]*)%mod;
}
ll getans2(ll x,ll mod)
{
if(x==)
return ;
if(x==)
return ;
is ans,base;
memset(ans.a,,sizeof(ans.a));
ans.a[][]=;
ans.a[][]=;
base.a[][]=;
base.a[][]=-;
base.a[][]=;
base.a[][]=;
ans=quickpow(ans,base,x-,mod);
return ((((ans.a[][]*-ans.a[][])%mod)+mod)%mod);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld%lld",&n,&y,&x,&s);
ll zhi=n*y;
m=phi(s+);
ll k;
if(zhi%==)
k=(getans(zhi+,m)%m)*(getans2(zhi/,m)%m)%m;
else
k=(getans(zhi,m)%m)*(getans2((zhi+)/,m)%m)%m;
ll out=Pow(x,k,s+);
printf("%lld\n",out);
}
return ;
}

hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂的更多相关文章

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

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

  2. 循环节 + 矩阵快速幂 - 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), ...

  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(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  6. HDU 5895 Mathematician QSC

    矩阵快速幂,欧拉定理. $g(n)$递推式:$g(n)=5g(n-1)+5g(n-2)-g(n-3)$,可以构造矩阵快速求递$n$项,指数很大,可以利用欧拉定理降幂. #pragma comment( ...

  7. HDU 2276 Kiki & Little Kiki 2( 矩阵快速幂 + 循环同构矩阵 )

    蒟蒻的我还需深入学习 链接:传送门 题意:给出一个长度为 n,n 不超过100的 01 串 s ,每当一个数字左侧为 1 时( 0的左侧是 n-1 ),这个数字就会发生改变,整个串改变一次需要 1s ...

  8. hdu 4291 2012成都赛区网络赛 矩阵快速幂 ***

    分析:假设g(g(g(n)))=g(x),x可能非常大,但是由于mod 10^9+7,所以可以求出x的循环节 求出x的循环节后,假设g(g(g(n)))=g(x)=g(g(y)),即x=g(y),y也 ...

  9. hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...

随机推荐

  1. 【BZOJ4974】字符串大师 KMP

    [BZOJ4974]字符串大师 Description 一个串T是S的循环节,当且仅当存在正整数k,使得S是T^k(即T重复k次)的前缀,比如abcd是abcdabcdab的循环节.给定一个长度为n的 ...

  2. shell脚本中格式化日期

    date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] ...

  3. webpack安装和简单配置

    1.webpack是一个基于node的项目,所以先装好node和npm       参考我的随笔:https://www.cnblogs.com/jtnote/p/6230384.html 2.先创建 ...

  4. ASP-Server.Transfer-Response.Redirect

    Server.Transfer Transfer 方法把一个 ASP 文件中创建的所有状态信息(所有 application/session 变量以及所有 request 集合中的项目)发送(传输)到 ...

  5. 22个所见即所得在线Web编辑器

    这些 Web 编辑器可以在线编辑和处理富 Web 内容,包括格式文本,表格,图片,媒体,链接等等,非常适合集成到 CMS网站内容管理系统中使用.本文又搜集了 22 个 Web 在线编辑器,它们基本代表 ...

  6. Python3.6全栈开发实例[014]

    14.好声音选秀大赛评委在打分的时,可以进行输入. 假设,有10个评委.让10个评委进行打分, 要求, 分数必须大于5分, 小于10分. count = 1 while count <= 10: ...

  7. python面试题(八)

    1 Python中如何使用线程池和进程池? 需要注意一下 不能无限的开进程,不能无限的开线程 最常用的就是开进程池,开线程池.其中回调函数非常重要 回调函数其实可以作为一种编程思想,谁好了谁就去掉 只 ...

  8. Linux下简单的多线程编程--线程池的实现

    /* 写在前面的话: 今天刚“开原”,选择了一篇关于线程池的文件与大家分享,希望能对您学习有所帮助,也希望能与大家共同学习! 选择在这个特殊的时候注册并发文章也是有一些我个人特殊的意义的,看我的id( ...

  9. unknown encoder libvpx

    brew install ffmpeg --with-libvpx or brew reinstall ffmpeg --with-libvpx

  10. Python的模块与函数以及与自动化的结合

    3 模块与函数 3.1程序结构 python的程序由package,module,function组成,分别是包,模块,函数.模块是函数和类的集合,包,模块,函数之间的关系如下: 3.2模块 pyth ...