CodeForces 414B Mashmokh and ACM (DP)

本文介绍了一个编程问题,任务是计算所有长度为k且满足特定整除条件的整数序列的数量。通过动态规划的方法解决了这个问题,并提供了优化的实现方案。
B. Mashmokh and ACM
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.

A sequence of l integers b1, b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is called good if each number divides (without a remainder) by the next number in the sequence. More formally  for all i (1 ≤ i ≤ l - 1).

Given n and k find the number of good sequences of length k. As the answer can be rather large print it modulo 1000000007 (109 + 7).

Input

The first line of input contains two space-separated integers n, k (1 ≤ n, k ≤ 2000).

Output

Output a single integer — the number of good sequences of length k modulo 1000000007 (109 + 7).

Sample test(s)
input
3 2
output
5
input
6 4
output
39
input
2 1
output
2
Note

In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3].


大致题意:
给出一个n和k,让你从 1 -- n 这n个数中,选出 k个(可重复)组成一个序列,使这个序列满足任意一个数都能够整除该序列中它前面的那个数。求这样的序列的个数。


分析:

由于每一个数都与其前面的一个数直接相关,每个数必须是其前面一个数的倍数。那么我们可以定义一个跟末尾数字有关的状态,我们用dp[ i ] [ j ] 表示长度为 i 并且最后一位是 j 的序列 ,那么状态转移方程:dp [ i ] [ j ] = sum(dp [ i-1 ] [ x ] ) x 是 j 前面满足( j % x == 0)的数。

因为是多组测试,在开始的时候直接打表可以节省时间。



比较雷的是,刚开始写的循环是

for(int i=1;i<2010;i++)
{
	for(int j=1;j<2010;j++)
	{
		for(int q=1;q<=j;q++)
		{
			if(j%q==0)
			{
				dp[i][j] += dp[i-1][q];
				dp[i][j] %= MOD;
			}
		}
	}
}



显然会T的,不能这么暴力那么无知的做。。

换成这样会好一点。至少就不会T了。。对于每一个 i ,都有从1开始的 j 去控制的 q ,来增加dp [ i ] [ ] ,这样一圈循环下来,可以完全更新~


for(int i=1;i<2010;i++)
{
	for(int j=1;j<2010;j++)
	{
		for(int q=j;q<2010;q=q+j)
		{
			dp[i][q] += dp[i-1][j];
			dp[i][q] %= MOD; 
		}
	}
}


#include 
#include 
#define MOD 1000000007

int dp[2010][2010];

int main()
{
	int n,k,sum;
	for(int i=1;i<2010;i++)
    {
        dp[1][i]=1;
    }
	for(int i=1;i<2010;i++)
	{
		for(int j=1;j<2010;j++)
		{
			for(int q=j;q<2010;q=q+j)
			{
				dp[i][q] += dp[i-1][j];
				dp[i][q] %= MOD; 
			}
		}
	}
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		sum=0;
		for(int i=1;i<=n;i++)
		{
			sum=sum+dp[k][i];
			sum %= MOD;
		}
		printf("%d\n",sum);
	}
}

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值