题目http://codeforces.com/contest/1189/problem/C

题意:给定n个数,每次查询一个区间$[l,r]$。对这个区间内的数,相邻两个数之和超过10,则得到一个candy,然后将他们之和取模10的结果作为新的数。

一共操作$l-r$次,问这个区间得到的candy数。

思路:一种思路是可以发现,实际上candy数是和/10,而新得到的数实际上是/10之后的小数部分。

每次操作的过程其实可以看成是两两求和然后取/10的整数部分,然后把小数部分再两两求和取整数部分,剩下的再留给下一次操作。

所以$ans[l,r] = \lfloor \frac{a_l+a_{l+1}+...+a_r}{10} \rfloor$

另一种思路是用倍增的思想进行dp。

$dp[i][k]$表示以$i$为开头,$2^k$个数所得到的candy数。因为转移的时候还需要知道当前操作之后的数是什么,所以用$dig[i][k]$记录对应操作后的数。

$dp[i][k] = dp[i][k-1]+dp[i + 2^{k-1}][k-1] +(dig[i][k-1] + dig[i + 2^{k-1}][k-1] \ge 10)$

DP解法

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; const int maxn = 1e5 + ;
int n, q;
int num[maxn];
int dp[maxn][], dig[maxn][]; int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &num[i]);
dp[i][] = ;
dig[i][] = num[i];
}
int pow = ;
for(int k = ; k < ; k++, pow *= ){
for(int i = ; i + pow <= n + ; i++){
dp[i][k] = dp[i][k - ] + dp[i + pow / ][k - ];
if(dig[i][k - ] + dig[i + pow / ][k - ] >= )dp[i][k]++;
dig[i][k] = (dig[i][k - ] + dig[i + pow / ][k - ]) % ;
}
}
scanf("%d", &q);
while(q--){
int l, r;
scanf("%d%d", &l, &r);
int k = , seg = r - l + ;
while(seg % == ){
k++;
seg >>= ;
}
printf("%d\n", dp[l][k]);
}
return ;
}

公式解法:

#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; const int maxn = 1e5 + ;
int n, q;
int num[maxn], sum[maxn]; int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &num[i]);
sum[i] = sum[i - ] + num[i];
}
scanf("%d", &q);
while(q--){
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", (sum[r] - sum[l - ]) / );
} return ;
}

Codeforces#572 Div2 C---Candies!【倍增】【DP】【思维】的更多相关文章

  1. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  2. codeforces 985E Pencils and Boxes(dp+思维)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  4. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

  5. Codeforces 1140G Double Tree 倍增 + dp

    刚开始, 我以为两个点肯定是通过树上最短路径过去的, 无非是在两棵树之间来回切换, 这个可以用倍增 + dp 去维护它. 但是后来又发现, 它可以不通过树上最短路径过去, 我们考虑这样一种情况, 起点 ...

  6. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  7. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  8. codeforces #321 DIV2

    A题: 链接:http://codeforces.com/contest/580/problem/A dp,最长连续不上升子序列 #include<iostream> #include&l ...

  9. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

随机推荐

  1. grpc proto文件生成java、.net实体类以及客户端代码

    背景 工作中对接对方服务是GRPC,对方只提供了proto契约文件,需要自己生成对应的实体类以及客户端代码,故记录下操作流程. Java 代码生成 实体类: 通过protoc插件生成实体类 在 htt ...

  2. django使用pyecharts(5)----django加入echarts_增量更新_定长

    五.Django 前后端分离_定时增量更新图表定长数据 1.安装 djangorestframework linux pip3 install djangorestframework windows ...

  3. 05 IO流(三)——IO流标准流程

    流程 选择源 选择合适的流 操作 关闭流:先打开的后关闭 演示 import java.io.File; import java.io.InputStream; import java.io.File ...

  4. RT-Flash imxrt 系列rt1052 rt1060量产神器宣传

    转载: 恩智浦半导体2017年10月正式发布了业内首款跨界处理器—i.MX RT系列,超强的性能.超高的性价比使得i.MX RT系列火遍大江南北,一度成为基于MCU的产品主控首选,尤其是那些对于性能有 ...

  5. Spring Cloud Alibaba学习笔记(7) - Sentinel规则持久化及生产环境使用

    Sentinel 控制台 需要具备下面几个特性: 规则管理及推送,集中管理和推送规则.sentinel-core 提供 API 和扩展接口来接收信息.开发者需要根据自己的环境,选取一个可靠的推送规则方 ...

  6. VS.NET(C#)--2.2_事件

    事件 事件 由用戶行为生成系统触发(事件处理程序).在ASP.NET中,对象可以触发事件.用户在浏览器对服务器控件的任何行为都可能触发事件,但是在服务器端处理. 页面和控件事件 它们继承自contro ...

  7. VSCode-设置webstorm的主题和快捷键

    前提:VScode很火啊,理由:轻量,免费,不用找各种破解qi.... 好吧那我也从webstorm转过来试试,但是webstorm已经用了4年多了,对于一个有洁癖的人,必须把VScode打扮的和we ...

  8. 关于Vue中,使用watch同时监听两个值的实现方法

    1. 先在computed中,用需要监听的两个值(start.end)定义一个对象(dateRange) computed: { dateRange () { const { start, end } ...

  9. body onload()事件和table insertRow()、tr insertCell()

    onload事件: 定义和用法: onload 事件会在页面或图像加载完成后立即发生. onload 通常用于 <body> 元素,在页面完全载入后(包括图片.css文件等等.)执行脚本代 ...

  10. py-1 语言介绍

    一.编程与编程语言 1.编程的目的 计算机的发明,是为了用机器取代并解放人力.而编程的目的则是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而达到让计算机能够像人脑.电脑一样自动执 ...