您的位置:首页 > 其它

hdu 5895 Mathematician QSC(快速幂+指数循环节)

2016-09-21 16:50 239 查看


Mathematician QSC

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

Total Submission(s): 326    Accepted Submission(s): 172


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

2016 ACM/ICPC Asia Regional Shenyang Online

 

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5901 5900 5899 5898 5897 

大意就是给你一个斐波那契数列的平方加为指数的一个级数让你求这个级数%(s+1)后的值。

一开始是想用欧拉定理,先对x进行因数分解之后用欧拉定理降幂,但是没有考虑到(s+1)%因子==0的情况,

之后从学长那里知道指数循环节这个东西。。A^bmodc==A^(b%phi(c)+phi(c))%c

之后问题便迎刃而解了。

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define F(x,a,b) for (ll x=a;x<=b;x++)
#define ll long long
#define me(x) memset(x,0,sizeof(x))
#define _fast(x) F(i,1,4)F(j,1,4)F(k,1,4) b[i][j]=(b[i][j]+a[i][k]*a[k][j])%x;
#define _reset F(i,1,4)F(j,1,4) {a[i][j]=b[i][j];b[i][j]=0;}
#define _orz(x) F(i,1,4)F(j,1,4)F(k,1,4) b[i][j]=(b[i][j]+a[i][k]*c[k][j])%x;
ll a[10][10],b[10][10],c[10][10],p[100005],isp[100005];
ll cnt;
void fastmat(ll k,ll MOD)
{
if (k==1)return;
if (k&1){fastmat(k-1,MOD);_orz(MOD) _reset}
else {fastmat(k/2,MOD);_fast(MOD) _reset}
}
void _pr(ll x)
{
F(i,2,x)
{
if (!p[i]) isp[++p[0]]=i;
F(j,1,p[0])
{
if (isp[j]*i>x) break;
p[isp[j]*i]=1;
if (i%isp[j]==0) break;
}
}
}
ll phi(ll x)
{
ll t=x;
ll res=x;
F(i,1,p[0])
{
if (isp[i]*isp[i]>x) break;
if (t%isp[i]==0)
{
res-=res/isp[i];
while (t%isp[i]==0) t/=isp[i];
}
}
if (t>1) res-=res/t;
return res;
}
ll _q(ll a,ll b,ll MOD)
{
if (!b) return 1;
if (b&1) return a*_q(a,b-1,MOD)%MOD;
else
{
ll tt=_q(a,b/2,MOD)%MOD;
return tt*tt%MOD;
}
}
int main()
{
ll N;
cin>>N;
_pr(100000);
while (N--)
{
ll n,y,x,s;
cin>>n>>y>>x>>s;
if (n*y==0) {cout<<"1"<<endl;continue;}
if (x==0) {cout<<"0"<<endl;continue;}
me(a);me(b);me(c);
a[1][1]=1;a[1][3]=1;a[2][3]=1;a[3][2]=9;a[3][3]=4;a[3][4]=4;
a[4][2]=2;a[4][4]=1;
F(i,1,4)F(j,1,4)c[i][j]=a[i][j];
ll ph=phi(s+1);
ll t=n*y;
fastmat(t,ph);
cout<<_q(x,a[1][3]+ph,s+1)%(s+1)<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: