Happy Sequence ZOJ - 4011 (dp)

本文探讨了ZJO-4011 HappySequence问题,即寻找满足每个数整除序列中下一个数的快乐数列。通过记录每个数的因子并使用动态规划方法,实现了高效求解特定长度快乐数列数量的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Happy Sequence

 ZOJ - 4011 

A sequence of  integers  () is called a happy sequence if each number divides (without a remainder) the next number in the sequence. More formally, we can say  for all , or we can say  for all .

Given  and , find the number of happy sequences of length . Two sequences  and  are different, if and only if there exists an  such that  and .

As the answer can be rather large print it modulo  ().

Input

There are multiple test cases. The first line of the input contains an integer  (about 50), indicating the number of test cases. For each test case:

The first and only line contains two integers  and  (), indicating the upper limit of the elements in the sequence and the length of the sequence.

Output

For each case output a single integer, indicating the number of happy sequences of length  modulo 

Sample Input

1
3 2

Sample Output

5
#include <iostream>
#include <cstring>
#include <algorithm>
#include <list>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstdio>
using namespace std; 

#define ll long long
#define CL(a, b) memset(a, b, sizeof(a))
#define mp make_pair

const int N = 1e6 + 7;
const int M = 1e4 + 7;
const int D = 1e9 + 7;

ll dp[2002][2002];
vector<int> a[2010];


int main() {
	
	for (int i = 1; i <= 2000; ++i) {
		for (int j = i; j <= 2000; ++j) {
			if (j%i == 0) a[j].push_back(i); //把j的因子都记录下来 
		}
	}
	for (int i = 1; i <= 2000; ++i) dp[1][i] = 1;  //长度为1无论什么数字结尾都只有一个 
	for (int i = 2; i <= 2000; ++i) {
		for (int j = 1; j <= 2000; ++j) {
			for (int k = 0; k < a[j].size(); ++k) {
				dp[i][j] = (dp[i][j] + dp[i-1][a[j][k]])%D;
			}
		}
	}
	int t;
	scanf ("%d", &t);
	while (t--) {
		
		int n, m;
		scanf ("%d %d", &n, &m);
		ll sum = 0;
		//由于我记录的是长度为i,以j结尾的个数
		//所以得把同长度的以不同数字结尾的个数加起来才是答案 
		for (int i = 1; i <= n; ++i) { 
			sum = (sum + dp[m][i])%D;
		}
		printf ("%lld\n", sum);
	}
	
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值