2019 GDUT Spring Training IV (Div.2) (dp,)
D. Mashmokh and ACM
题目链接:http://codeforces.com/group/NVaJtLaLjS/contest/240952/problem/D
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).
Examples
input3 2
output5
input6 4
output39
input2 1
output2
NoteIn the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3].
题目大意:
从1到n中找出能被ai+1能被ai整除,且长度为k的方案数。
题目思路:
dp:
dp[i][j]表示长为i,以j结尾的方案数
求dp[i+1][j]=∑dp[i][k],k为j的因数;
初始化dp[1][ j ] =1;
i→k;
j→n;
o→o*j<=n;
dp[i+1][o*j]+=dp[i][j];
最后统计dp[k][i]和即可。
代码:
#include <bits/stdc++.h> using namespace std; #define maxn 100005 #define ll long long const ll mod=1e9+7; long long dp[2005][2005]; int n,k; int main(){ cin>>n>>k; for(int i=1;i<=n;i++){ d'p[1][i]=1; } for(int i=1;i<=k;i++){ for(int j=1;j<=n;j++){ for(int o=1;o*j<=2000;o++){ dp[i+1][o*j]+=dp[i][j]; dp[i][o*j]%=mod; } } } ll ans=0; for(int i=1;i<=n;i++){ ans+=dp[k][i]; ans%=mod; } cout<<ans<<"\n"; return 0; }
设dp[i]为前i项已经确定第i项的方案数,
先初始化i(1→n):dp[i]=1;
t=j的倍数
t=j+j;
dp[j] = (dp[j]+dp[t])%MOD;
当k=1时,递推一遍;
当k=2时,递推一遍;
…
每次递推都加上后面可以接上的dp。
递推k-1次,后所有dp[i]加起来即是答案。
代码:
#include<cstdio> #include <cstring> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #define MOD 1000000007 using namespace std; int main(){ int i, j, t; int n, k; int dp[2005]; scanf("%d%d", &n, &k); for (i=1;i<=n;i++){ dp[i] = 1; } for (i=0;i<k-1;i++){ for (j=1;j<=n;j++) { t=j+j; while (t<=n){ dp[j]=(dp[j]+dp[t])%MOD; t+=j; } } } for (dp[0]=0,i=1;i<=n;i++) { dp[0]=(dp[0]+dp[i])%MOD; } printf("%d\n", dp[0]); return 0; }
之后的再补
- Spring Training IV D (dp)
- SDKD 2017 Spring Team Training B G题 -The Debut Album or URAL - 2018 (dp 三维)
- SDKD 2017 Spring Team Training B H题Pair: normal and paranormal or URAL - 2019( 栈应用)
- 2019最新升级 微服务时代Spring Boot 企业微信点餐系统
- bnu 51640 Training Plan(类似区间dp)(北师16校赛)
- F. Training Plan--类似区间dp
- 2015 Multi-University Training Contest 2 1004 Delicious Apples(DP)
- hdu5763Another Meaning 2016 Multi-University Training Contest 4(kmp+dp)
- 2017 Multi-University Training Contest 10 && HDU 6178 Monkeys 【贪心||树形DP】
- [kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV
- hdu 4317 Unfair Nim(状态压缩DP)——2012 Multi-University Training Contest 2
- LightOJ 1018 Brush (IV)(状态压缩DP)
- POJ3735 Training little cats DP,矩阵快速幂,稀疏矩阵优化
- Spring Training I D (并查集+最短路)
- Codeforces #369(Div.2) C.Coloring Trees【Dp】
- HDU-2870 Largest Submatrix (线性dp 最大01矩阵)(2009 Multi-University Training Contest 7 )
- dp - 2015 Multi-University Training Contest 2 1004 Delicious Apples
- 【HDU5743 2016 Multi-University Training Contest 2J】【dfs展开式DP 前缀和思想】Join The Future 40个数已知区间和为奇或偶输出方案
- 2019最新Spring Boot 2.0深度实践之核心技术篇
- 2019最新Spring Boot相关13套教程合辑