cf 414B Mashmokh and ACM 动态规划

题目链接:http://codeforces.com/problemset/problem/414/B

dp[i][j]表示长度为i、最后一个数字为j的合法序列总数

dp[1][1...2000]都是1

后面用dp[i-1][j] 去更新 dp[i][j*t] (1 <= j*t <= 2000) 即用因子去更新它的倍数

表面上看是2000^3的复杂度会爆 其实不用算那么多次

最外层循环是2000

分析第二层和第三层 需要算 2000/1 + 2000/2 + 2000/3 + 2000/4 + ... + 2000/2000 次

即2000 * (1/1 + 1/2 + 1/3 + ... + 1/2000)

后面的括号是一个【调和级数】 利用高等数学知识可知 它是有上限的 为自然对数e

所以本算法的时间复杂度实际上仅为 e*2000*2000

绰绰有余

因为下标从1开始计数

然后一开始fill的时候没注意看wa了几炮T^T

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

const int maxn = 2010;
const int M = 1000000007;

int dp[maxn][maxn];

int main()
{
    //freopen("in.txt", "r", stdin);

    int n, k;
    scanf("%d%d", &n, &k);

    fill(dp[1] + 1, dp[1] + 2001, 1);

    for(int i = 2; i <= k; i++)
    {
        for(int j = 1; j <= 2000; j++)
        {
            for(int t = 1; j*t <= 2000; t++)
            {
                dp[i][j*t] = (dp[i][j*t] + dp[i-1][j]) % M;
            }
        }

    }

    int ans = 0;
    for(int i = 1; i <= n; i++)
        ans = (ans + dp[k][i]) % M;

    printf("%d\n", ans);

    return 0;
}
时间: 2024-11-20 19:19:41

cf 414B Mashmokh and ACM 动态规划的相关文章

Codeforces 414B Mashmokh and ACM(DP)

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 tas

CodeForces 414B - Mashmokh and ACM

给你长度为 l 的整数数列b1, b2, ..., bl(1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) 如果这个数列被称为好的,那么每个元素都可以整除下一个元素 给你n和k,去找到长度为k的好数列的个数 dp[任意i][1] = 1: dp[i的倍数][长度k] = dp[i][长度k-1] + 1; 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int MOD = 100

【DP专辑】ACM动态规划总结

转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ******************

【codeforces 415D】Mashmokh and ACM(普通dp)

[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=2000),问满足[数列长度是k && 数列中每一个元素arr[i]在1~n之间 && 数列中元素可以重复]的数列有多少个?结果对10^9+7取余 解题思路:dp[i][j]表示长度是j,最后一位是i的种数 if(kk%i==0) dp[kk][j+1]+=dp[i][j] 1 #i

codeforces 414B B. Mashmokh and ACM(dp)

题目链接: codeforces 414B 题目大意: 定义一个序列,前一项能够整除后一项,给定这个序列中数的取值范围和序列的长度,问有多少种构造方法. 题目分析: 我们定义状态dp[i][j]为前i项已经确定且第i项为j的方案数. 转移方程 dp[i][j]=∑k|jdp[i?1][k] 复杂度O(n?k) AC代码: #include <iostream> #include <cstring> #include <algorithm> #include <cs

Codeforces Round #240 (Div. 1)---B.Mashmokh and ACM(dp)

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 tas

CodeForces 415D Mashmokh and ACM

$dp$. 记$dp[i][j]$表示已经放了$i$个数字,并且第$i$个数字放了$j$的方案数.那么$dp[i][j] = \sum\limits_{k|j}^{}  {dp[i - 1][k]}$ #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorit

codeforces D.Mashmokh and ACM

题意:给你n和k,然后找出b1, b2, ..., bl(1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n),并且对所有的bi+1%bi==0,问有多少这样的序列? 思路:dp[i][j] 表示长度为i,以j为结尾有多少.dp[i][j]+=dp[i-1][s],j%s==0; 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const

ACM 动态规划 D

Problem D Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 13   Accepted Submission(s) : 5 Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The seque