题目

给出两个正整数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. Oracle插入时间

    现象:Oracle 插入时间时 ,报错:ORA-01861: 文字与格式字符串不匹配 解决方法: 这个错误一般出现在时间字段上,即你插入的时间格式和数据库现有的时间格式不一致,解决的方法是格式化你 插 ...

  2. Firefox浏览器设置字符编码格式

    按照网上说的:工具 -> 选项 -> 内容 -> 字体&颜色 -> 高级 -> 字体编码,根本没有找到utf-8,还是把浏览器定制一下吧,看源文件的时候也可以用, ...

  3. /etc/ld.so.conf 介绍

    /etc/ld.so.conf 这个文件记录了编译时使用的动态链接库的路径,告诉链接器去哪个路径下寻找链接时需要用到的库,如果找不到,就会提示链接错误. 如果我们安装了第三方的库,而没有将它放在链接器 ...

  4. 大型网站技术架构介绍--squid

    一.大型网站技术架构介绍 1.pv高  ip高 并发量 2.大型网站架构重点    1. 高性能:响应时间,TPS,系统性能计数器.缓存,消息队列等.     高可用性High Availabilit ...

  5. 在 Server 端存取 Excel 檔案的利器:NPOI Library

    转处 http://msdn.microsoft.com/zh-tw/ee818993.aspx Codeplex 軟體套件(Package)資訊 套件名稱 NPOI 作者 tonyqus, huse ...

  6. hibernate.properties

    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect #hibernate.dialect=org.hibernate.dialect.Oracl ...

  7. Struts2的配置

    Struts2的配置 Struts2可以通过Convention插件管理Action和结果映射,也可以通过使用XML文件进行管理,这两种方式各有好处:使用Convention插件管理减少了XML文件的 ...

  8. LTMP手动编译安装以及全自动化部署实践(附详细代码)

    大家使用LNMP架构,一般可以理解为Linux Shell为CentOS/RadHat/Fedora/Debian/Ubuntu/等平台安装LNMP(Nginx/MySQL /PHP),LNMPA(N ...

  9. git -C

    https://git-scm.com/docs/git -C <path> Run as if git was started in <path> instead of th ...

  10. Creating HTML table with vertically oriented text as table header 表头文字方向

    AS an old question, this is more like info or reminder about vertical margin or padding in % that ta ...