Simple pass game (matrix) and pass game Matrix

Source: Internet
Author: User

Simple pass game (matrix) and pass game Matrix
Simple pass gameTime Limit:1000 msMemory Limit:65536KB 64-bit integer IO format:% LldJava class name:Main 

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 Authorsqy question link: http://www.bnuoj.com/bnuoj/problem_show.php? Pid = 49104 reprinted, please specify the Source: accept. (Note: you cannot pass the ball to yourself.) analysis and answer: After the nth pass is passed, there are a [n] ways to pass the ball back to the armor plate, can imagine n-1-1 pass, if each pass is any other one of the K-1 passing, that is, each pass has a K-1, by multiplication principle, total (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 because a is the serving person. The recursive relationship can be used to obtain the following idea: an (n indicates the number of times the ball is passed back to a); 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.
1 # include <cstdio> 2 # include <cstring> 3 # include <iostream> 4 # include <algorithm> 5 using namespace std; 6 # define LL long 7 # define mod 1000000007 8 struct matrix 9 {10 LL mat [2] [2]; 11}; 12 13 matrix multiply (matrix a, matrix B) 14 {15 matrix c; 16 memset (c. mat, 0, sizeof (c. mat); 17 for (int I = 0; I <2; I ++) 18 {19 for (int j = 0; j <2; j ++) 20 {21 if (. mat [I] [j] = 0) continue; 22 for (int k = 0; k <2; k ++) 23 {24 if (B. Mat [j] [k] = 0) continue; 25 c. mat [I] [k] + =. mat [I] [j] * B. mat [j] [k] % mod; 26 // c. mat [I] [k] % = mod; 27 if (c. mat [I] [k]> mod) c. mat [I] [k]-= mod; // sure enough, this is an overhead... 28 else if (c. mat [I] [k] <0) c. mat [I] [k] + = mod; 29} 30} 31} 32 return c; 33} 34 35 matrix quicklymod (matrix a, LL n) 36 {37 matrix res; 38 memset (res. mat, 0, sizeof (res. mat); 39 for (int I = 0; I <2; I ++) res. mat [I] [I] = 1; 40 while (n) 41 {42 if (n & 1) 43 res = multiply (a, res ); 44 a = multiply (a, a); 45 n >>= 1; 46} 47 return res; 48} 49 50 int main () 51 {52 ll n, K; 53 int T; 54 scanf ("% d", & T); 55 while (T --) 56 {57 scanf ("% lld", & N, & K); 58 if (N = 1) {printf ("0 \ n"); continue;} 59 // if (N = 2) {printf ("% lld \ n", K-1); continue;} 60 61 matrix ans; 62 ans. mat [0] [0] = K-1; 63 ans. mat [0] [1] = 0; 64 ans. mat [1] [0] = K-1; 65 ans. mat [1] [1] =-1; 66 67 // ans = quicklymod (ans, N-2); 68 // LL res = (K-1) % mod) * (ans. mat [1] [0] + ans. mat [1] [1]) % mod; 69 // printf ("% lld \ n", res); 70 ans = quicklymod (ans, N-1 ); 71 printf ("% lld \ n", ans. mat [1] [0]); 72} 73 return 0; 74}

 

Other code 1 # include <cstdio> 2 # include <cstring> 3 # include <iostream> 4 # include <algorithm> 5 using namespace std; 6 # define LL long 7 # define mod 1000000007 8 struct matrix 9 {10 LL mat [2] [2]; 11}; 12 13 matrix multiply (matrix a, matrix B) 14 {15 matrix c; 16 memset (c. mat, 0, sizeof (c. mat); 17 for (int I = 0; I <2; I ++) 18 {19 for (int j = 0; j <2; j ++) 20 {21 if (. mat [I] [j] = 0) continue; 22 for (int k = 0; k <2; k ++) 23 {24 if (B. mat [j] [k] = 0) continue; 25 c. mat [I] [k] = (c. mat [I] [k] +. mat [I] [j] * B. mat [j] [k]) % mod; 26} 27} 28} 29 return c; 30} 31 32 matrix quicklymod (matrix a, LL n) 33 {34 matrix res; 35 memset (res. mat, 0, sizeof (res. mat); 36 for (int I = 0; I <2; I ++) res. mat [I] [I] = 1; 37 while (n) 38 {39 if (n & 1) 40 res = multiply (a, res ); 41 a = multiply (a, a); 42 n >>= 1; 43} 44 return res; 45} 46 47 int main () 48 {49 ll n, K; 50 int T; 51 scanf ("% d", & T); 52 while (T --) 53 {54 scanf ("% lld", & N, & K); 55 if (N = 1) {printf ("0 \ n"); continue;} 56 // if (N = 2) {printf ("% lld \ n", K-1); continue;} 57 58 matrix ans; 59 ans. mat [0] [0] = 0; 60 ans. mat [0] [1] = K-1; 61 ans. mat [1] [0] = 1; 62 ans. mat [1] [1] = K-2; 63 64 ans = quicklymod (ans, N-1); 65 // LL res = (K-1) * (ans. mat [1] [0] + ans. mat [1] [1]) % mod; 66 // printf ("% lld \ n", res); 67 printf ("% lld \ n ", ans. mat [0] [1]); 68} 69 return 0; 70}View Code 1 # include <stdio. h> 2 # include <algorithm> 3 using namespace std; 4 long pow (long n, long k) 5 {6 long res = 1; 7 while (k) 8 {9 if (k & 1) res = res * n % 1000000007; 10 n = n * n % 1000000007; 11 k >>=1; 12} 13 return res; 14} 15 long cal (long n, long k) 16 {17 long res = pow (K-1, n); 18 if (res & n & 1) 19 res = 1000000007-res; 20 res + = (k-1); 21 if (res> = 1000000007) res-= 1000000007; 22 res = res * pow (k, 1000000005) % 1000000007; 23 if (res & n & 1) 24 res = 1000000007-res; 25 return res; 26} 27 int main () 28 {29 int _; 30 long N, K; 31 scanf ("% d", & _); 32 while (_ --) 33 {34 scanf ("% lld", & N, & K); 35 printf ("% lld \ n", cal (N, K); 36} 37 return 0; 38}View Code

 

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.