SRM 572 div2 1000

Source: Internet
Author: User

[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 ;}};

Related Article

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.