describe
There are n children playing the game of losing sandbags. The rule is that after receiving the sandbags, each child will immediately throw them to another child. At the beginning, the sand bag was in the hands of the first child. After being thrown out m times, it returned to the hands of the first child. It can be assumed that the sand bag will not fall to the ground. How many different possibilities are there in the process of throwing the sand bag.
input
Enter the number of first behavior test sample groups t (T < = 100)
Each set of data contains two positive integers n, M. (2<=n,m<=10^5)
output
For each set of data, enter an integer to represent how many different possibilities there are. Results 10 ^ 9 + 7 was modeled.
sample input
2
4 2
5 5
sample output
3
204
Tips
The first group of data has three cases: 1 - > 2 - > 1, 1 - > 3 - > 1, 1 - > 4 - > 1.
When I saw the label, I thought it was a water problem until I was disgusted to vomit...
So today, I'm going to talk about this problem...
I read the title first, knew it was a dp at a glance, and then began to draft
Take 4 2 as an example and start brushing the watch
m
0 1 0 0 0
1 0 1 1 1
2 3 2 2 2
ok, very clear, f [i] [J] + = {f [k] [J] | K! = I}, yo, so eazy! Open source!!!!!
Then there is the following:
//f[i][j] indicates the possibility that the sandbag is now in the hands of the i-th child and has been thrown j times //Original question: find f[1][m] //Sub question 1: find f[2][m - 1] //Sub problem 2: find f[3][m - 1] // ... // Recurse again and again //Can easily be merged into // F [i] [J] = {f [k] [J - 1] sum | K! = I} #include<bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int f[10005][10005]; int main(){ int a; scanf("%d", &a); while(a--){ int n, m; scanf("%d%d", &n, &m); //input for(int i = 1; i <= n; i++) for(int j = 0; j <= m; j++) f[i][j] = 0; f[1][0] = 1; //initialization for(int j = 1; j <= m - 1; j++) for(int i = 1; i <= n; i++) for(int k = 1; k <= n; k++) if(k != i) { f[i][j] = (f[i][j] % mod + f[k][j - 1] % mod) % mod; printf("f[%d][%d] = f[%d][%d] + f[%d][%d]\n", i, j, i, j, k, j - 1); //It's a little crude } //Classic dp for(int i = 2; i <= n; i++) f[1][m] = (f[1][m] % mod + f[i][m - 1] % mod) % mod; //accumulation //for(int j = 0; j <= m; j++){ // for(int i = 1; i <= n; i++) // printf("%d ", f[i][j]); // printf("\n"); //}/ / small debugging~~~ printf("%d\n", f[1][m]); } return 0; }
After an hour of repeated polyphony, I finally passed...
So there are the following
Very happy...
So I went back to the table...
m
0 1 0 0 0
1 0 1 1 1
2 3 2 2 2
It's f[1][m]...
It's the same except the first column??? There seems to be a breakthrough, so I tried and simplified it into:
m
0 1 0
1 0 3
2 3 6
At first glance, huh? Isn't this the state compression of dp? So the time complexity becomes O (2m)
My heart: 2m? I feel very uncomfortable. The teacher's time card is tight and unstable? Very empty? Recompress!!!
So I didn't write any algorithms, so I thought O (m)...
Linear, huh? f[i][2] is equal to f[i + 1][1] every time???
Then came linearity:
#include<bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int a, n, m, f[100005]; bool t[100005]; int main(){ for(int i = 0; i <= 100005; i++) { if(i % 2) t[i] = false; else t[i] = true; } scanf("%d", &a); while(a--){ memset(f, 0, sizeof(f)); scanf("%d%d", &n, &m); f[0] = 1; for(int i = 1; i <= m; i++){ if(t[i - 1]) f[i] = ((f[i - 1] - 1) % mod) * ((n - 1) % mod); else f[i] = ((f[i - 1] + 1) % mod) * ((n - 1) % mod); } printf("%d\n", f[m] % mod); } return 0; }
So there was another tune..........
Bother...
So after the sample, hand it in!!!!!
therefore:
It should be right (whisper bb)
pretty
So I came to 2m:
Dong Dong Dong, Diao Diao, alas... From 6:00 to 9:00
Finally:
#include<bits/stdc++.h> using namespace std; const int d = 1e9 + 7; int s, n, m; long long a, b, c; int main(){ scanf("%d", &s); while(s--){ a = 1, b = 0; scanf("%d%d", &n, &m); for(int i = 1; i <= m; i++) c = b, b = (b * (n - 2) + a) % d, a = c * (n - 1) % d; printf("%d\n",a); } return 0; }
From line 25 to line 16,
Finally, finally, finally....
Ah... Bashi ~~~~~~~~~~
Distinctive brain circuits^_^