1040:count time limit: 1 Sec memory limit: MB
Submitted by: $ 23
Submitted State [Discussion Version] Title Description
Many ACM team name may be very funny,such as "Complier_error", "VVVVV". Oh,wait for a minute here.
Is it "W w" + "V", or "w" + "V v V", or something others we can treat as? There is several ways we can treat this name "VVVVV" (5 ' V ' s), as V-V can be treat as a W.
For 5 ' V ' S,our has 8 ways. They is:
-
v v v v v
-
v w w
-
w W V
-
v W v
-
w V W
-
w v v v
-
v v W v
-
v v v W
The problem here is, for n ' V ' s,how Many ways does we have to treat it? Because The answer is too large, you should output the answer module by P . (If n is 0,then we have just one.)
Input
There is multiple test cases. the first line of the input contains an integerM, meaning the number of the test cases.
for each test cases, there is Bothintegers nand PIn a single line .
can assume that0<=n<=2100000000, 0<p<=2009.
Output
For each test case, the output of the answer.
Sample input
25 54 7
Sample output
35
Main topic:
That gives you 5 v words "VVVVV", there may be a part of V connected together as W, ask how many kinds of sequence, the answer to P to take the remainder.
Input:
The first line, one m, indicates that there are m test data,
The second line to the m+1 line two integers per line n,p, representing N V, the P remainder.
Output:
An integer that requires title.
Fibonacci that cut the sequence?? You should.
Well, that's Fibonacci, the number of n is inherited by N-1 and N-2, and n is larger, so the matrix multiplication is fast-power-optimized recursion
1#include <cstdio>2#include <cstring>3 intp;4 structMatrix5 {6 inta[2][2];7Matrix (Matrix &p)8 {9 for(intI=0;i<2; i++)Ten for(intj=0;j<2; j + +) One This->a[i][j]=P.a[i][j]; A } -Matrixintx) - { the for(intI=0;i<2; i++) - for(intj=0;j<2; j + +) - This->a[i][j]=x; - } + Matrix () - { +Memset (A,0,sizeof(a)); A for(intj=0;j<2; j + +) at This->a[j][j]=1; - } -Matrixoperator* (Matrix &b) - { - Matrix C; - for(intI=0;i<2; i++) in for(intj=0;j<2; j + +) - { toc.a[i][j]=0; + for(intk=0;k<2; k++) - { thec.a[i][j]+= This->a[i][k]*B.a[k][j]; * } $c.a[i][j]%=p;Panax Notoginseng } - returnC; the } + }; AMatrix Quickmult (Matrix &a,intk) the { + Matrix Ans,temp (a); - while(k) $ { $ if(k%2) ans=ans*temp; -temp=temp*temp; -K/=2; the } - returnans;Wuyi } the intMain () - { Wu intM,n; -scanf"%d",&m); About while(m--) $ { -scanf"%d%d",&n,&p); -Matrix Ini,tra;//ini means initial matrix, tra means transform matrix -ini.a[0][0]=1; ini.a[0][1]=1; ini.a[1][0]=0; ini.a[1][1]=0; Atra.a[0][0]=0; tra.a[0][1]=1; tra.a[1][0]=1; tra.a[1][1]=1; +Tra=Quickmult (tra,n); theini=ini*tra; -printf"%d\n", ini.a[0][0]); $ } the return 0; the}
NEU 1040 Count