[Cpp]/* pay attention to the for loop of dp, for repeated calculation between States, dp [I] [j] indicates the sum of the remainder of I and j. */# include <stdio. h> # include <string. h> typedef long lld; const int mod = 1000000007; lld dp [52] [1300]; lld fac [52]; class DistinctRemainders {public: lld Bin (lld, lld B) {lld ret = 1; while (B) {if (B & 1) ret = ret * a % mod; B> = 1; a = a * a % mod;} return ret;} void init (int M) {memset (fac, 0, sizeof (fac); fac [0] = 1; for (int I = 1; I <= M; I ++) fac [I] = fac [I-1] * I % mod;} lld Kao (lld x, lld y) {lld ret = 1; for (lld I = 1; I <= y; I ++) {ret = ret * x % mod; x --;} return ret;} lld Gao (lld x, lld y) {return Kao (y, x) * Bin (Kao (x, x), mod-2) % mod;} int howMany (lld N, int M) {int I, j, k; init (M ); memset (dp, 0, sizeof (dp); dp [0] [0] = 1; for (I = 0; I <M; I ++) {for (j = M; j> 0; j --) {for (k = M * M/2; k> = I; k --) {if (! Dp [J-1] [k-I]) continue; dp [j] [k] = (dp [J-1] [k-I] + dp [j] [k]) % mod; // printf ("dp [% d] [% d] = % lld \ n", I, j, dp [I] [j]);} lld ans = 0; for (I = 1; I <= M; I ++) {for (j = 0; j <= M * M/2; j ++) {if (! Dp [I] [j]) continue; if (j> N | (N-j) % M! = 0) continue; ans = (ans + (dp [I] [j] * fac [I] % mod) * Gao (I-1, (N-j) /M % mod + i-1) % mod; printf ("I = % d j = % d % lld \ n", I, j, ans );}} return (int) ans ;}};