博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
牛客网暑期ACM多校训练营(第二场)A run (dp)
阅读量:2125 次
发布时间:2019-04-30

本文共 731 字,大约阅读时间需要 2 分钟。

题意

白云一次可以走1米或者跑k米,但是不能连续跑,问白云到达区间 [L, R]一共有几种方法

AC

  • dp
    dp的意义是记录当前的方案数
    0表示走到当前位置的方案数
    1表示跑到当前位置的方案数
    这里写图片描述
using namespace std;int inf = 0x3f3f3f3f;int mod = 1e9 + 7;ll dp[N][2], ans[N];int main(){#ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);#endif    int q, k;    scanf("%d %d", &q, &k);    dp[0][0] = 1;    dp[0][1] = 0;    for (int i = 1; i < N; ++i) {        dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % mod;        if (i - k >= 0) {            dp[i][1] = dp[i - k][0] % mod;        }    }    for (int i = 1; i < N; ++i) {        ans[i] = ans[i - 1] + dp[i][1] + dp[i][0];    }    for (int i = 0; i < q; ++i) {        int l, r;        scanf("%d%d", &l, &r);        printf("%lld\n", (ans[r] - ans[l - 1]) % mod);    }    return 0;}
你可能感兴趣的文章
剑指offer 38.丑数
查看>>
剑指offer 39.构建乘积数组
查看>>
剑指offer 57. 删除链表中重复的结点
查看>>
剑指offer 58. 链表中环的入口结点
查看>>
剑指offer 59. 把字符串转换成整数
查看>>
剑指offer 60. 不用加减乘除做加法
查看>>
leetcode 热题 Hot 100-3. 合并两个有序链表
查看>>
leetcode 热题 Hot 100-4. 对称二叉树
查看>>
Leetcode C++《热题 Hot 100-12》226.翻转二叉树
查看>>
Leetcode C++《热题 Hot 100-13》234.回文链表
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>