【arc071f】Infinite Sequence(动态规划)

题面

atcoder

洛谷

题解

不难发现如果两个不为\(1\)的数连在一起,那么后面所有数都必须相等。

设\(f[i]\)表示\([i,n]\)的填法数,初值\(f[n]=n,f[n-1]=n*n\)

考虑转移,

首先可以这里填上一个大于\(1\)的数然后后面连续若干个\(1\),这一部分的方案数是\(\sum_{j=i+3}^n f[j]\),这个后缀和记录一下就好了,然而还漏了一部分,即如果\(i+a_i>n\),那么这里的贡献的方案数就是\(1\),所以还需要补上\(i+1\)。

第二种是在这一位填上连续两个大于\(1\)的数,方案数为\((n-1)^2\)。

第三种是在这一位填上一个\(1\),方案数是\(f[i+1]\)。

#include<cstdio>
#define MOD 1000000007
int n,f[1000010];
int main()
{
scanf("%d",&n);f[n]=n;f[n-1]=1ll*n*n%MOD;
for(int i=n-2,s=0;i>0;--i)s=(s+f[i+3])%MOD,f[i]=(f[i+1]+1ll*(n-1)*(n-1)+s+i+1)%MOD;
printf("%d\n",f[1]);return 0;
}

【arc071f】Infinite Sequence(动态规划)的更多相关文章

  1. CodeForces 622 A.Infinite Sequence

    A.Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Educational Codeforces Round 7 A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...

  3. codeforces 675A A. Infinite Sequence(水题)

    题目链接: A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input st ...

  4. codeforces 622A Infinite Sequence

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...

  6. codeforces 622A A. Infinite Sequence (二分)

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. A - Infinite Sequence

    Problem description Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2,  ...

  8. Codeforces Round #353 (Div. 2) A. Infinite Sequence

    Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its fi ...

  9. UVa 1626 Brackets sequence (动态规划)

    题意:用最少的括号将给定的字符串匹配,输出最优解.可能有空行. 思路:dp. dp[i][j]表示将区间i,j之间的字符串匹配需要的最少括号数,那么 如果区间左边是(或[,表示可以和右边的字符串匹配, ...

随机推荐

  1. 安装使用swoole

    swoole首页:https://www.swoole.com/ 方法1:使用pecl安装 pecl install swoole 注意,php版本必须是7.0以及7.0以上的版本. 方法2:编译源码 ...

  2. [转][mysql]创建函数失败(1418错误)mysql双主模式导致的问题

    https://blog.csdn.net/qq523786283/article/details/75102170

  3. [转帖]oracle改版sql server问题点汇总

    https://www.cnblogs.com/zhangdk/p/oracle_sqlserver.html 只记得 最开始的时候看过 没有具体的了解里面的特点 原作者总结的很好 留下来 以后说不定 ...

  4. hihoCoder1033 交错和 数位DP

    题目:交错和 链接:http://hihocoder.com/problemset/problem/1033# 题意:对于一个十进制整数x,令a0.a1.a2.....an是x从高位到低位的数位,定义 ...

  5. linux apache tomcat 安装和升级

    一,安装tomcat 注意!安装tomcat前需安装配置JDK,安装方式请参照这篇文章: http://www.cnblogs.com/blog4matto/p/5582054.html 1 tomc ...

  6. CentOS7安装使用ab压力测试工具

    执行安装命令:yum -y install httpd-tools 安装完毕,执行:ab -help,显示命令参数 命令模板:ab -c 100 -n 10000 待测试网站(建议完整路径) -c 即 ...

  7. elasticsearch介绍,安装,安装错误解决及相应插件安装

    一.elasticsearch介绍 1.简介(使用的是nosql,更新比mongodb慢): ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎, ...

  8. Python实现百度贴吧自动顶贴机

    开发这款小工具,我们需要做一些准备: url.txt:多个需要顶起的帖子地址. reply:多条随机回复的内容. selenium:浏览器自动化测试框架 首先,我们先使用pip完成selenium的安 ...

  9. centOS 开机自启动自己的脚本

    centOS 开机自启动自己的脚本 1. 自己脚本 myservice 如下: #!/bin/bash # chkconfig: # description: myservice .... echo ...

  10. vue之综合Demo:打沙袋

    demo7.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1 ...