Happy Sequence
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;
}

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





