CodeForces-520E Pluses everywhere
题目描述
给出一个长度为 \(n\) 的字符串,给出一个非负整数 \(k\),要求给这个字符串中间添加 \(k\) 个$$+$’号,变成一个表
达式,比如”\(1000101\)”,添加两个\(+\)号,可以变成”\(10+001+01\)”,或者”\(1000+1+01\)”,表达式的值分别是\(12\) 和 \(1002\)。
问所有的添加加号的方案的表达式的值的和是多少。
Input
两个整数 \(n,k\),一个字符串$ s$ \((0<=k<n<=1e5).\)
Output
一个整数,模$ 1000000007$
Sample Input
3 1
108
3 2
108
Sample Output
27
9
计数\(DP\)
首先,看到那么大的数据范围肯定是对于每个数计算贡献。
那么我们该如何计算贡献呢?
对于题目给出的那个字符串\(S\),从左往右每个数字依次为\(a_{n-1},a_{n-2}....a_{1},a_0\).
现在,我们对于\(a_t\),讨论其贡献。
\(a_t\)右边有\(t\)个位置可以放置加号,同时一共有\(n-1\)个位置放置加号。
若\(a_t\)右边第一个位置放置了加号,则\(a_t\)被当成个位,还有\(n-2\)个空位,\(k-1\)个加号,一共有\(C(k-1,n-2)\)种方案。
其贡献为\(C(k-1,n-1)*10^{0}*a_t\)。
若\(a_t\)右边第一个位置为空,第二个位置放置加号,则\(a_t\)为十位,还有\(n-3\)个空位,\(k-1\)个加号,共有\(C(k-1,n-3)\)种方案。
贡献为\(C(k-1,n-2)*10^{1}*a_t\).
依次类推,
若\(a_t\)右边为空,则贡献为\(C(k,n-t-2)*10^{t}*a_t\)。
所以,\(a_t\)这个数的贡献为\((10^{t}*C(k,n-t-2)+\sum^{t}_{j=0}10^{j}*C(k-1,n-j-2))*a_t\)。
于是我们可以得到第\(i\)个数的贡献计算公式\(A_i=10^{i}*C(k,n-i-2)+\sum^{i}_{j=0}10^{j}*C(k-1,n-j-2)\)。
最后,\(Ans=\sum^{n-1}_{i=0}A_i*a_i\)。
代码如下
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
#define reg register
#define Raed Read
#define clr(a,b) memset(a,b,sizeof a)
#define Mod(x) (x>=mod)&&(x-=mod)
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a))
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(int i=(G).Head[x]; i; i=(G).Nxt[i])
#pragma GCC target("avx,avx2,sse4.2")
#pragma GCC optimize(3)
inline int Read(void) {
int res=0,f=1;
char c;
while(c=getchar(),c<48||c>57)if(c=='-')f=0;
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>=48&&c<=57);
return f?res:-res;
}
template<class T>inline bool Min(T &a, T const&b) {
return a>b?a=b,1:0;
}
template<class T>inline bool Max(T &a, T const&b) {
return a<b?a=b,1:0;
}
const int N=1e5+5,M=1e6+5,mod=1e9+7;
bool MOP1;
int Fac[N],Inv[N];
inline int Pow(int x) {
int res=1,y=mod-2;
while(y) {
if(y&1)res=(res*x)%mod;
x=(x*x)%mod,y>>=1;
}
return res;
}
inline int C(int x,int y) {
if(!x)return 1;
return (Fac[y]*((Inv[x]*Inv[y-x])%mod))%mod;
}
int A[N],Pow_10[N];
char S[N];
bool MOP2;
inline void _main() {
int n=Read(),k=Read(),Ans=0,tot=0;
scanf("%s",S),Fac[0]=Pow_10[0]=Inv[0]=1;
if(!k) {
int Ans=0;
ret(i,0,n)Ans=(Ans*10+(S[i]^48))%mod;
printf("%lld\n",Ans);
return;
}
rep(i,1,n) {
Fac[i]=(Fac[i-1]*i)%mod;
Pow_10[i]=(Pow_10[i-1]*10)%mod;
Inv[i]=Pow(Fac[i]);
}
A[0]=C(k-1,n-2);
int P=n-k-1;
rep(i,0,P)A[i]=(A[i-1]+Pow_10[i]*C(k-1,n-i-2))%mod;
rep(i,P+1,n)A[i]=A[i-1];
rep(i,0,P)A[i]=(A[i]+Pow_10[i]*C(k,n-i-2));
ret(i,0,n)Ans=(Ans+(S[i]^48)*A[n-i-1])%mod;
printf("%lld\n",Ans);
}
signed main() {
_main();
return 0;
}
CodeForces-520E Pluses everywhere的更多相关文章
- 【CodeForces 520E】Pluses everywhere
题意 n个数里插入k个+号,所有式子的和是多少(取模1000000007) (0 ≤ k < n ≤ 105). 分析 1.求答案,考虑每个数作为i位数(可为答案贡献10的i-1次方,个位i=1 ...
- Educational Codeforces Round 90 (Rated for Div. 2) C. Pluses and Minuses(差分)
题目链接:https://codeforces.com/contest/1373/problem/C 题意 给出一个只含有 $+$ 或 $-$ 的字符串 $s$,按如下伪代码进行操作: res = 0 ...
- Codeforces Round #295 (Div. 1) C. Pluses everywhere
昨天ZZD大神邀请我做一道题,说这题很有趣啊. 哇,然后我被虐了. Orz ZZD 题目大意: 你有一个长度为n的'0-9'串,你要在其中加入k个'+'号,每种方案就会形成一个算式,算式算出来的值记做 ...
- CodeForces - 589A
题目链接:http://codeforces.com/problemset/problem/589/A Polycarp has quite recently learned about email ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- Eclipse 开发环境修改及MAVEN配置
Eclipse集成Maven配置 默认为 修改为所用版本 选择maven软件所在目录 勾选 默认连接仓库为 修改为
- 报错:没有与参数列表匹配的构造函数 "CFileDialog::CFileDialog" 实例
如果是在解决方案管理器窗口内,右击你的项目“项目”,然后选“属性”(最后一项),再点“配置属性”,是个“+”号,把它展开,然后选“常规”选项卡,倒数第三项“字符集”,选择“使用多字节字符集”.再编译应 ...
- centos 中 redis 的安装
安装流程 Wget http://download.redis.io/releases/redis-5.0.4.tar.gz tar xzf redis-5.0.4.tar.gz mv redis-5 ...
- HDU6579 Operation
题目链接 问题分析 区间求异或和最大,比较自然的想到了线性基.而每次求一个区间的线性基显然是行不通的.我们考虑在每个位置求出首位置到当前位置的线性基.同时我们要使线性基中高位的位置所选的数尽量靠后.这 ...
- Splay教程
目录 前言 引入 教程 Rotate Splay 一些其他操作: 区间翻转 结语 前言 Splay是名副其实的区间小能手.它会经常出现在一些有关区间的题上.而本蒟蒻只会Treap,感到分外难受,于是就 ...
- httpd如何卸载以及安装
卸载 首先,要确认下是否有安装过,或者是系统自带了httpd服务,通过以下命令: # rpm -qa | grep httpd 或者: # yum list | grep httpd 我已经安装过一次 ...
- 关于MySQL GROUP BY 语句
GROUP BY 语句根据一个或多个列对结果集进行分组.在分组的列上我们可以使用 COUNT, SUM, AVG,等函数. 例如: CREATE TABLE `employee_tbl` ( `id` ...
- 第十四周学习总结&课程实验报告
课程总结 一.相关概念 1.什么是JDBC JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统 ...
- 最长不重复子串长度,时间复杂度O(n),空间复杂度O(n),Python实现
def lengthOfLongestSubstring(s): res = 0 d = {} tmp = 0 start = 0 for i in range(len(s)): if s[i] in ...
- ffmpeg剪切视频
测试的时候需要用到视频,原片太大了,就剪切几分钟来测试 ffmpeg -i input.mp4 -ss 0 -t 300 -acodec copy -vcodec copy -scodec copy ...