Mathematician QSC
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 326 Accepted Submission(s): 172
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
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;
}
}