DP (optimized) uvalive 6073 Math Magic

Source: Internet
Author: User

/************************************************* author:running_time* Created time:2015/10/28 Wednesday 20:20:09* Fi Le name:h.cpp ************************************************/#include <cstdio> #include <algorithm># Include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e3 + 10;const int M = 1e2 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;con    St Double EPS = 1e-10;const Double PI = ACOs ( -1.0); int dp[2][n][n];int lcm[n][n];int vec[n];int GCD (int a, int b) { Return b? GCD (b, a% B): A;} void init (void) {for (int i=1; i<=1000; ++i) {       for (int j=1; j<=1000; ++j) {Lcm[i][j] = i * J/GCD (i, j);    }}}inline void Add (int &x, int y) {x + = y; if (x > MoD) x-= mod;}    int main (void) {init ();    int n, m, K;        while (scanf ("%d%d%d", &n, &m, &k) = = 3) {int t = 0;        for (int i=1; i<=m; ++i) {if (m% i = = 0) vec[t++] = i;        } int now = 0;            for (int i=0, i<=n; ++i) {for (int j=0; j<t; ++j) {dp[now][i][vec[j]] = 0;        }} Dp[now][0][1] = 1;            for (int. l=1; l<=k; ++l) {now ^= 1;                for (int i=0, i<=n; ++i) {for (int j=0; j<t; ++j) {dp[now][i][vec[j]] = 0;                    }} for (int i=l-1, i<=n; ++i) {for (int j=0; j<t; ++j) {                    if (dp[now^1][i][vec[j]] = = 0) continue; for (int p=0; p<t; ++p) {int x = i + vec[p];                        int y = lcm[vec[j]][vec[p]];                        if (x > N | | m% y! = 0) Continue;                    Dp[now][x][y] = (Dp[now][x][y] + dp[now^1][i][vec[j])% MOD;    }}}} printf ("%d\n", Dp[now][n][m]);    }//cout << "time Elapsed:" << 1.0 * Clock ()/clocks_per_sec << "s.\n"; return 0;}


DP (optimized) uvalive 6073 Math Magic

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.