P1707 Brush Problem Contest

Source: Internet
Author: User

P1707 Brush Problem Contest
      • through
      • Submit
    • Topic provider NODGD
    • Tag multiplier recursive matrix luo gu original
    • Difficulty Increase +/province selection-

Submit a discussion of the problem record

Recent discussions
    • Not scientific
Topic Background NODGD is a like to write program classmate, before long Rokua OJ turned out, NODGD classmate of course the first time to Rokua OJ brush problem. So a series of interesting things happened, and he was going to use these things to make an issue of disgusting everyone ... Title Description

Rokua OJ Of course is a good place, NODGD students intend to share with friends. So he pulled up his friend Ciocio and Nicole two students to brush the problem together. Like the game they certainly do not miss the opportunity to brush the title of the game!

On the 1th day, Nodgd,coicoi,nicole only did 1 questions.

On the 2nd day, Nodgd,coicoi,nicole only did 3 questions.

They all have strict brush-up rules and will follow the rules every day to brush a certain amount of questions.

(1) NODGD classmate k+2 days brush questions number a[k+2]=p*a[k+1]+q*a[k]+b[k+1]+c[k+1]+r*k^2+t*k+1;

(2) Ciocio classmate K+2 Days brush problem number b[k+2]=u*b[k+1]+v*b[k]+a[k+1]+c[k+1]+w^k;

(3) Nicole classmate K+2 Days brush problem number c[k+2]=x*c[k+1]+y*c[k]+a[k+1]+b[k+1]+z^k+k+2;

(The above letter p,q,r,t,u,v,w,x,y,z are given constants and are guaranteed to be positive integers)

So they started a long-time brush game! Altogether N days (4<=n<=10^16)

But time is valuable, NODGD want to quickly know the number of each person's brush question in the nth day. But NODGD students still have a lot of math contest questions, Physics contest questions, English contest questions, art competition problems, sports competition problems ... If you want to do it, please help him.

Since the result is large, the output mod K value can be.

Input output Format input format:

The first row of two positive integers n,k. (4<=n<=10^16,2<=k<=10^16)

The second line is four positive integer p,q,r,t.

The third row is three positive integer u,v,w.

Three positive integers x, y, z in line four.

(Ensure that the p,q,r,t,u,v,w,x,y,z are not more than 100 positive integers)

Output format:

A total of three lines, one name per line + one space + one integer. In turn are the values of nodgd,ciocio,nicole and the number of mod k that they brush on the nth day.

Input and Output Sample input example # #:
4 100072 1 1 12 2 31 1 2
Sample # # of output:
NODGD 74Ciocio 80Nicole 59
Description

Matrix multiplication.

Note that the intermediate multiplication process may be larger than the data range of the 64-bit long integer.

See the data range is very large, after the whimsical, thought will be the answer loop, plus the fast power, up to just 50 points.

#include <cstdio>#include<iostream>#definell Long Longusing namespacestd;Const intn=2e5+ -; ll N,mod;ll A[n]={0,1,3};ll B[n]={0,1,3};ll C[n]={0,1,3};ll Kpow (ll A,ll p) {ll ans=1;  for(;p; p>>=1, A= (a*a)%mod)if(p&1) ans= (ans*a)%MoD; returnans;}intMain () {//freopen ("Sh.txt", "R", stdin);    intP,q,r,t,u,v,w,x,y,z;ll MA (0), MB (0), MC (0);BOOLFa0), FB (0), FC (0); CIN>>n>>MoD; CIN>>p>>q>>r>>t>>u>>v>>w>>x>>y>>Z; if(mod==1) {printf ("nodgd 0\nciocio 0\nnicole 0\n");return 0;} if(n==1) {printf ("nodgd 1\nciocio 1\nnicole 1\n");return 0;} if(n==2) {printf ("nodgd%d\nciocio%d\nnicole%d\n",3%mod,3%mod,3%MOD);return 0;}  for(LL i=3; i<=n;i++) {A[i]= (p*a[i-1]%mod+q*a[i-2]%mod+b[i-1]+c[i-1]+r%mod*kpow ((I-2)%mod,2)%mod+t* (I-2)%mod+1)%MoD; B[i]= (u*b[i-1]%mod+v*b[i-2]%mod+a[i-1]+c[i-1]+kpow (W%mod, (i-2))%mod)%MoD; C[i]= (x*c[i-1]%mod+y*c[i-2]%mod+a[i-1]+b[i-1]+kpow (Z%mod, (i-2))%mod+i)%MoD; if(a[i]==a[2]&&a[i-1]==a[1]) {ma=i-2; fa=1;} if(b[i]==b[2]&&b[i-1]==b[1]) {mb=i-2; fb=1;} if(c[i]==c[2]&&c[i-1]==c[1]) {mc=i-2; fc=1;} if(FA&AMP;&AMP;FB&AMP;&AMP;FC) Break; }    if(fa&&fb&&FC) {cout<<"NODGD"<<a[n%ma]<<Endl; cout<<"Ciocio"<<b[n%mb]<<Endl; cout<<"Nicole"<<c[n%mc]<<Endl; }    Else{cout<<"NODGD"<<a[n]<<Endl; cout<<"Ciocio"<<b[n]<<Endl; cout<<"Nicole"<<c[n]<<Endl; }    return 0;}

See tags, " matrix multiplication ", go online look at matrix multiplication

Attach the AC code:

#include <iostream>#include<cstdio>#include<cstring>#definell Long Longusing namespacestd;ll n,mod,p,q,r,t,u,v,w,x,y,z;ll f[ A][ A],a[ A][ A];ll Slow_mul (ll A,ll b) {ll ans=0;  while(b) {if(b&1) {b--;ans+=a;ans%=MoD; } A<<=1; a%=mod;b>>=1; }    returnans;}voidMul (LL a[ A][ A],ll b[ A][ A]) {ll c[ A][ A];memset (c,0,sizeof(c));  for(intI=1; i<= One; i++)         for(intj=1; j<= One; j + +)             for(intk=1; k<= One; k++) C[i][j]= (C[i][j]+slow_mul (a[i][k],b[k][j))%MoD;  for(intI=1; i<= One; i++)         for(intj=1; j<= One; j + +) A[i][j]=c[i][j];}intMain () {CIN>>n>>mod>>p>>q>>r>>t>>u>>v>>w>>x>>y>>z ; n-=2; f[1][1]=f[1][3]=f[1][5]=3; f[1][2]=f[1][4]=f[1][6]=1; f[1][7]=f[1][8]=f[1][9]=1; f[1][Ten]=w;f[1][ One]=Z; a[1][1]=p;a[2][1]=q;a[7][1]=r;a[8][1]=t;a[3][3]=u; a[4][3]=v;a[5][5]=x;a[6][5]=y;a[9][5]=a[8][7]=2; a[Ten][Ten]=w;a[ One][ One]=Z; a[1][2]=a[1][3]=a[1][5]=a[3][1]=a[3][4]=a[3][5]=1; a[5][1]=a[5][3]=a[7][7]=a[8][5]=a[8][8]=a[9][1]=1; a[9][7]=a[9][8]=a[9][9]=a[Ten][3]=a[ One][5]=a[5][6]=1;  while(n) {if(n&1) Mul (f,a); Mul (a,a); n>>=1; } cout<<"NODGD"<<" "<<f[1][1]<<Endl; cout<<"Ciocio"<<" "<<f[1][3]<<Endl; cout<<"Nicole"<<" "<<f[1][5]<<Endl; return 0;}

P1707 Brush Problem Contest

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.