hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂
Mathematician QSC
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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?
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
20160830 2016 12345678 666
20101010 2014 03030303 333
317

思路:首先求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 指数循环节+矩阵快速幂的更多相关文章
- HDU - 5451 Best Solver(循环节+矩阵快速幂)
Best Solver The so-called best problem solver can easily solve this problem, with his/her childhood ...
- 循环节 + 矩阵快速幂 - 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), ...
- 2019牛客多校第五场 generator 1——广义斐波那契循环节&&矩阵快速幂
理论部分 二次剩余 在数论中,整数 $X$ 对整数 $p$ 的二次剩余是指 $X^2$ 除以 $p$ 的余数. 当存在某个 $X$,使得式子 $X^2 \equiv d(mod \ p)$ 成立时,称 ...
- hdu4291 暴力循环节+矩阵快速幂
题意: 给你一个关系式,x[n] = 3*x[n-1] + x[n-2],求x(x(x[n]))%1000000007. 思路: 做这个题目要明确一点,就是对于取余操作大多数时 ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- HDU 5895 Mathematician QSC
矩阵快速幂,欧拉定理. $g(n)$递推式:$g(n)=5g(n-1)+5g(n-2)-g(n-3)$,可以构造矩阵快速求递$n$项,指数很大,可以利用欧拉定理降幂. #pragma comment( ...
- HDU 2276 Kiki & Little Kiki 2( 矩阵快速幂 + 循环同构矩阵 )
蒟蒻的我还需深入学习 链接:传送门 题意:给出一个长度为 n,n 不超过100的 01 串 s ,每当一个数字左侧为 1 时( 0的左侧是 n-1 ),这个数字就会发生改变,整个串改变一次需要 1s ...
- 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也 ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
随机推荐
- Java基础 - 获取随机数
使用方法 package com.demo5; import java.util.Random; /* * 使用步骤: * A:导包 * import java.util.Random; * B:创建 ...
- Delphi重定义的消息结构
// 除去DDE和MDI消息,一共159个消息,其中部分消息仅仅的转定义 // 普通消息,有两个参数和结果 PMessage = ^TMessage; TMessage = packed record ...
- 数字雨(Javascript使用canvas绘制Matrix,效果很赞哦)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Mysql常用优化方案
摘自:http://www.jb51.net/article/18934.htm 1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也 ...
- ThinkPHP官网瀑布流实现分享
很多人都想做瀑布流的效果,这里告诉大家官网使用的方法. 首先要下载瀑布流的插件jquery.masonry.min.js 地址:http://masonry.desandro.com/index.ht ...
- python下多线程的限制以及多进程中传递参数的方式
python多线程有个全局解释器锁(global interpreter lock),这个锁的意思是任一时间只能有一个线程使用解释器,跟单cpu跑多个程序一个意思,大家都是轮着用的,这叫“并发”,不是 ...
- nodejs中全栈开发框架meteor的文档
http://wiki.jikexueyuan.com/project/discover-meteor/routing.html, 这本书的源码地址: https://github.com/Dis ...
- LVC函数重要参数 EDT_CLL_CB:退出可编辑单元格时回调
6. I_GRID_SETTINGS 参数属性该参数用于设置Grid相关参数(打印.单元格回调):类型为:LVC_S_GLAY,该结构包括:01) COLL_TOP_P:最小化 TOP_OF_PAGE ...
- Get Region Information from IP Address with Taobao API
通过淘宝的API "http://ip.taobao.com/service/getIpInfo.php?ip=*.*.*.*" 来获得你要查询的IP地址的国家,地区,城市,ISP ...
- Python之模块和包(Day21)
一.Python模块 Python模块(module),是一个Python文件,以.py结尾,包含了Python对象定义和Python语句. 模块让你能够有逻辑的组织你的Python代码段 把相关的代 ...