Hdu 5895 mathematician QSC exponential cyclic section + Matrix fast Power

Source: Internet
Author: User

Mathematician QSC

Time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others)


Problem DESCRIPTIONQSC dream of becoming a mathematician, he believes that everything in this world have a mathematical law .

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

This sequence was such like that, first of all,F(0)=0,F(1)=1,F(N)=f (n− 2) +2∗ f ( N−1 ) (n ≥2 ) /span> Then the definition of the QSC sequence is  g ( N) =∑n i =0 F (i) 2 . If We know the birthday of the student is N, the year at the beginning of the semester are Y, the course number x and the C Ourse 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 are very AC Curate, the shortcoming is the complex calculation. As clever as is, can you write a program to predict the mark?

Inputfirst line was 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 is not negetive.

1≤s≤100000000

Outputfor Each test case the output was only one integer number ans in a line.

Sample Input220160830 2016 12345678 66620101010 2014 03030303 333

Sample Output1317

Source2016 ACM/ICPC Asia Regional Shenyang Online

Idea: First of all, the a^b%c=a^ (B%phi (c) +phi (c))%c B>=phi (c) Index follow-up link;

Then, the G function, f (n) can obviously be used to quickly write a matrix, g (n) =f (n) *f (n+1)/2; because/2, modulo division, the first thought of the inverse, but the mold is not necessarily odd, even the case 2 without inverse element;

Now how to deal with the 2,F (n) and F (n+1) must have an even number, found that the recurrence after 2 is changed to f (n) =6*f (n-1)-F (n-2);

PS: A small trick to deal with 2,MDZZ, modulo the number of * *, answer/2;

See Code;

#include <bits/stdc++.h>using namespacestd;#definell Long Long#definePi (4*atan (1.0))Const intn=1e5+Ten, m=1e6+1010, inf=1e9+Ten, mod=1e9+7;Constll inf=1e18+Ten; ll N,x,y,s;ll m;struct  is{ll a[Ten][Ten];}; isJuzhenmul ( isA isB,ll Hang, ll lie,ll MoD) {    inti,t,j;  isans; memset (ANS.A,0,sizeof(ANS.A));  for(i=1; i<=hang;i++)     for(t=1; t<=lie;t++)     for(j=1; j<=lie;j++) {Ans.a[i][t]+ = (a.a[i][j]*b.a[j][t]); Ans.a[i][t]%=MoD; }    returnans;} isQuickpow ( isAns isa,ll x,ll MoD) {     while(x) {if(x&1) Ans=juzhenmul (Ans,a,2,2, MoD); A=juzhenmul (A,a,2,2, MoD); X>>=1; }    returnans;}voidExtend_euclid (ll A, ll B, ll &x, LL &y) {    if(b = =0) {x=1; Y=0; 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=2; i*i<=n;i++)    {        if(n%i==0) {rea=rea-rea/i;  while(n%i==0) n/=i; }    }    if(n>1) Rea=rea-rea/N; returnRea;} ll Pow (ll a,ll n,ll MoD) {ll ans=1;  while(n) {if(n&1) {ans=ans*a%MoD; } A=a*a%MoD; N>>=1; }    if(ans==0) ans+=MoD; returnans;} ll Getans (ll x,ll MoD) {if(x==0)    return 0; if(x==1)    return 1;  isAnsBase; memset (ANS.A,0,sizeof(ANS.A)); ans.a[1][1]=1; ans.a[2][2]=1; Base. a[1][1]=0; Base. a[1][2]=1; Base. a[2][1]=1; Base. a[2][2]=2; Ans=quickpow (ans,Base, X-2, MoD); return(ans.a[2][1]+ans.a[2][2]*2)%MoD;} ll Getans2 (ll x,ll MoD) {if(x==0)    return 0; if(x==1)    return 1;  isAnsBase; memset (ANS.A,0,sizeof(ANS.A)); ans.a[1][1]=1; ans.a[2][2]=1; Base. a[1][1]=6; Base. a[1][2]=-1; Base. a[2][1]=1; Base. a[2][2]=0; Ans=quickpow (ans,Base, X-2, MoD); return((((ans.a[1][1]*6-ans.a[2][1])%mod) +mod)%mod);}intMain () {intT; scanf ("%d",&T);  while(t--) {scanf ("%lld%lld%lld%lld",&n,&y,&x,&s); ll Zhi=n*y; M=phi (s+1);        ll K; if(zhi%2==0) K= (Getans (zhi+1, m)%m) * (Getans2 (zhi/2, m)%m)%m; Elsek= (Getans (zhi,m)%m) * (Getans2 (zhi+1)/2, m)%m)%m; ll out=pow (x,k,s+1); printf ("%lld\n", out); }    return 0;}

Hdu 5895 mathematician QSC exponential cyclic section + Matrix fast Power

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.