hdu 5895 Mathematician QSC(快速冪+指數循環節)

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(n2)+2f(n1)(n2)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(ny)%(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
 

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;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章