题目

给出两个正整数N和M, N <= 100, M <= 50, 可以将N分解成若干个不相等的正整数A1, A2... Ak的和,且A1, A2 ... Ak的乘积为M的倍数。即 
N = A1 + A2 + ... + Ak; 
A1*A2*...Ak % M = 0; 
求可以有多少种分解方式? 
题目链接: divided product

分析

直接DFS搜索,DFS(cur_sum, cur_max_num, cur_product) 枚举出总和为N的,且各个数字不断增加的方案,然后判断他们的乘积是否等于M的倍数。实现复杂度为:(2^t, t 为几十的量级),显然不行; 
考虑动态规划来解决: 
    维护状态 dp[i][j][t] 表示 A1,A2...Ak的总和为i,且最大的数字Ak等于j,A1*A2..A*k的结果模M为t的分解方案总个数。 
则可以有递推公式:

  1. int tt = k*t%M;
  2. dp[i + k][k][tt] += dp[i][j][t];
  3. dp[i + k][k][tt] %= mod;

其中需要注意 边界条件 dp[i][i][i%M] = 1.

实现

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
using namespace std;
int dp[105][105][55];
int main(){
const int mod = 1000000007;
int n, m;
scanf("%d %d", &n, &m);
memset(dp, 0, sizeof(dp));
for (int i = 0; i <= n; i++){
for (int j = 0; j <= i; j++){
for (int k = j + 1; (k + i) <= n; k++){
for (int t = 0; t < m; t++){
if (i == 0)
dp[k][k][k%m] = 1;
else{
int tt = k*t%m;
dp[i + k][k][tt] += dp[i][j][t];
dp[i + k][k][tt] %= mod;
}
}
}
}
}
int result = 0;
for (int i = 1; i <= n; i++)
result = (result + dp[n][i][0]) % mod;
printf("%d\n", result);
return 0;
}

hiho1096_divided_product的更多相关文章

随机推荐

  1. UVa(12821),MCMF

    题目链接:https://uva.onlinejudge.org/external/128/12821.pdf 比赛的时候,准备用最短路来做,存两张图,做两次最短路,本来还觉得第二张图的设计很好的,很 ...

  2. Java中的的XML文件读写

    XML简介 要理解XML,HTML等格式,先来理解文档对象模型DOM 根据 DOM,HTML 文档中的每个成分都是一个节点,这些节点组成了一棵树.DOM 是这样规定的:整个文档是一个文档节点每个 HT ...

  3. sqlitehelper封装

    appsettings <configuration>    <appSettings>        <add key="ConnectionString&q ...

  4. C# for和 foreach 的数组遍历 比较

    刚学习程序,感觉写代码 很有意思,所以把自己的感悟写下来啦,第一次写博客,可能是菜鸟中的菜鸟  时间久了,相信就会写的很好哦! for和 foreach 的数组遍历 比较 很简单的程序,不解释啦! u ...

  5. 5-JS函数

    函数 定义函数 JS中有3种定义函数的方法: 函数声明 用函数声明定义函数的方式如下: function abs(x) { if (x >= 0) { return x; } else { re ...

  6. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  7. extern "C" __declspec(dllexport) __declspec(dllimport) 和 def

    原文:extern "C" __declspec(dllexport) __declspec(dllimport) 和 def 前面的extern "C"  _ ...

  8. js之字面量、对象字面量的访问、关键字in的用法

    一:字面量含义 字面量表示如何表达这个值,一般除去表达式,给变量赋值时,等号右边都可以认为是字面量. 字面量分为字符串字面量(string literal ).数组字面量(array literal) ...

  9. wamp出现could not execute run action问题

    wamp出现could not execute run action问题     原文地址:http://blog.sina.com.cn/s/blog_4a60ba9c0100zzlr.html上午 ...

  10. OpenCV installation on Linux

    Getting the Cutting-edge OpenCV from the Git Repository Launch Git client and clone OpenCV repositor ...