Simple pass game
K (3 <= k <= 10 ^ 9) A person passes the ball to each other and passes the ball to another person immediately after receiving the ball. Assume that the initial state ball is in the hands of Party A, and take Party A's serve as the first pass. After n (n <= 10 ^ 9) passes the ball, the ball returns to the number of pass schemes in the armor plate and outputs the results after 10 ^ 9 + 7.
Input
The first line is an integer T (t <= 20000), indicating the number of groups of test data.
In the next t row, enter two numbers N and K (3 <= k <= 10 ^ 9,1 <= n <= 10 ^ 9) in each row ).
Output
Output t rows. Each row outputs N groups. K corresponds to the result of the 10 ^ 9 + 7 solution.
Sample Input
23 33 4
Sample output
26
Hint
In the first example, n = 3, K = 3, and three people pass the ball three times:
1. A-> B-> C->
2. A-> C-> B->
Source 13th Beijing Normal University Program Design Competition finals author
Sqy
Question link: http://www.bnuoj.com/bnuoj/problem_show.php? PID = 1, 49104
Reprinted please indicate the source: http://blog.csdn.net/u010579068
Q: k people pass each other, from the beginning to the end of a, pass n times. (Note: you cannot pass it to yourself)
Analysis and answer: After the n-Pass is set, the ball is returned to the pass method of the first n-1-1 pass, if one of the other K-1 is chosen for each pass, that is, each pass has a K-1, by multiplication principle, a total of (K-1) ^ (n-1. These pass methods do not fully conform to the conditions. They are divided into two types: the first n-1 is passed to a, and a [n-1] is not met, because the n times cannot be passed to a. The other type is that the n-1 times are not in the hands of, the nth time the ball was held and then passed to Jia had a [n] method, according to the addition principle a [n-1] + A [n] = (K-1) ^ (n-1) A [1] = 0 is used because a is the serving operator.
Idea: An (N indicates the number of times that the ball is passed n times and the number of times that the ball is returned to the armor plate );
A1 = 0;
A2 = (K-1) ^ 1-a1;
A3 = (K-1) ^ 2-a2;
A4 = (K-1) ^ 3-a3;
......
Note that when the remainder is obtained, I have crossed the border several times. t ^ t.
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; # define ll long # define mod 1000000007 struct matrix {ll mat [2] [2];}; matrix multiply (matrix A, matrix B) {matrix C; memset (C. mat, 0, sizeof (C. mat); For (INT I = 0; I <2; I ++) {for (Int J = 0; j <2; j ++) {if (. mat [I] [J] = 0) continue; For (int K = 0; k <2; k ++) {If (B. mat [J] [k] = 0) continue; C. mat [I] [k] + =. mat [I] [J] * B. mat [J] [k] % MOD; // C. mat [I] [k] % = MOD; If (C. mat [I] [k]> mod) C. mat [I] [k]-= MOD; // sure enough, this is an overhead... Else if (C. mat [I] [k] <0) C. mat [I] [k] + = mod ;}} return C;} matrix quicklymod (matrix A, ll N) {matrix res; memset (res. mat, 0, sizeof (res. mat); For (INT I = 0; I <2; I ++) res. mat [I] [I] = 1; while (n) {If (N & 1) RES = multiply (A, Res); A = multiply (A, ); N >>= 1 ;}return res ;}int main () {ll N, K; int t; scanf ("% d", & T); While (t --) {scanf ("% LLD", & N, & K); If (n = 1) {printf ("0 \ n"); continue ;} // If (n = 2) {printf ("% LLD \ n", K-1); continue;} matrix ans; ans. mat [0] [0] = K-1; ans. mat [0] [1] = 0; ans. mat [1] [0] = K-1; ans. mat [1] [1] =-1; // ans = quicklymod (ANS, N-2); // ll res = (K-1) % mod) * (ans. mat [1] [0] + ans. mat [1] [1]) % mod) % MOD; // printf ("% LLD \ n", Res); ans = quicklymod (ANS, N-1 ); printf ("% LLD \ n", ans. mat [1] [0]);} return 0 ;}
There are two other methods:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define LL long long#define mod 1000000007struct matrix{ LL mat[2][2];};matrix multiply(matrix a,matrix b){ matrix c; memset(c.mat,0,sizeof(c.mat)); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { if(a.mat[i][j]==0)continue; for(int k=0;k<2;k++) { if(b.mat[j][k]==0)continue; c.mat[i][k]=(c.mat[i][k]+a.mat[i][j]*b.mat[j][k])%mod; } } } return c;}matrix quicklymod(matrix a,LL n){ matrix res; memset(res.mat,0,sizeof(res.mat)); for(int i=0;i<2;i++) res.mat[i][i]=1; while(n) { if(n&1) res=multiply(a,res); a=multiply(a,a); n>>=1; } return res;}int main(){ LL N,K; int T; scanf("%d",&T); while(T--) { scanf("%lld%lld",&N,&K); if(N==1){printf("0\n");continue;}// if(N==2){printf("%lld\n",K-1);continue;} matrix ans; ans.mat[0][0]=0; ans.mat[0][1]=K-1; ans.mat[1][0]=1; ans.mat[1][1]=K-2; ans=quicklymod(ans,N-1); // LL res=((K-1)*(ans.mat[1][0]+ans.mat[1][1])%mod)%mod; // printf("%lld\n",res); printf("%lld\n",ans.mat[0][1]); } return 0;}
#include<stdio.h>#include<algorithm>using namespace std;long long pow(long long n,long long k){ long long res = 1; while (k) { if (k&1) res = res*n%1000000007; n = n*n%1000000007; k >>= 1; } return res;}long long cal(long long n,long long k){ long long res = pow(k-1,n); if(res && n & 1) res = 1000000007 - res; res += (k-1); if (res >= 1000000007) res -= 1000000007; res = res * pow(k,1000000005)%1000000007; if(res && n & 1) res = 1000000007 - res; return res;}int main(){ int _; long long N,K; scanf("%d",&_); while (_--) { scanf("%lld %lld",&N,&K); printf("%lld\n",cal(N,K)); } return 0;}
Simple pass game (matrix)