【CF521C】【排列组合】Pluses everywhere
Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out nnumbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect.
The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him!
The first line contains two integers, n and k (0 ≤ k < n ≤ 105).
The second line contains a string consisting of n digits.
Print the answer to the problem modulo 109 + 7.
108
|
1
2
|
3 1
108
|
|
1
|
27
|
108
|
1
2
|
3 2
108
|
|
1
|
9
|
In the first sample the result equals (1 + 08) + (10 + 8) = 27.
In the second sample the result equals 1 + 0 + 8 = 9.
【分析】
排列组合计算每一位对答案的贡献。
/*
宋代朱敦儒
《西江月·世事短如春梦》
世事短如春梦,人情薄似秋云。不须计较苦劳心。万事原来有命。
幸遇三杯酒好,况逢一朵花新。片时欢笑且相亲。明日阴晴未定。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const int MAXN = + ;
const long long MOD = ;
const double Pi = acos(-1.0);
long long G = ;//原根
const int MAXM = * + ;
using namespace std;
typedef long long ll;
void read(ll &x){//读入优化
char ch;x = ;
ll flag = ;
ch = getchar();
while (ch < '' || ch > '') {if (ch == '') flag = -; ch = getchar();}
while (ch >= '' && ch <= '') {x = x * + (ch - ''); ch = getchar();}
x *= flag;
}
//分别为阶乘和前缀和
ll Ans, fac[MAXN], sum[MAXN];
ll n, m;
char str[MAXN]; ll pow(ll a, ll b){
if (b == ) return 1ll;
if (b == ) return (a % MOD);
ll tmp = pow(a, b / );
if (b % == ) return (tmp * tmp) % MOD;
else return (((tmp * tmp) % MOD) * (a % MOD)) % MOD;
}
//计算组合数
ll C(ll n, ll m){
if(m > n || m < ) return ;
return fac[n] * pow((fac[n - m] * fac[m]) % MOD, MOD - ) % MOD;//逆元
}
void init(){
read(n);
read(m);
fac[] = ;
for (int i = ; i <= ; i++) fac[i] = (fac[i - ] * i) % MOD;
ll k = ;
for (int i = ; i <= n; i++){
sum[i] = (sum[i - ] + k * C(n - i - , m - ) % MOD) % MOD;
k = (k * ) % MOD;
}
}
void work(){
ll k = ;
scanf("%s", str + );
Ans = ;
for (int i = n; i > ; i--){
int t = str[i] - '';
Ans = (Ans + ((t * k) % MOD) * C(i - , m) % MOD) % MOD;
Ans = (Ans + (t * sum[n - i]) % MOD) % MOD;
k = (k * ) % MOD;
}
printf("%lld\n", Ans);
} int main(){
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
work();
return ;
}
【CF521C】【排列组合】Pluses everywhere的更多相关文章
- 学习sql中的排列组合,在园子里搜着看于是。。。
学习sql中的排列组合,在园子里搜着看,看到篇文章,于是自己(新手)用了最最原始的sql去写出来: --需求----B, C, F, M and S住在一座房子的不同楼层.--B 不住顶层.C 不住底 ...
- .NET平台开源项目速览(11)KwCombinatorics排列组合使用案例(1)
今年上半年,我在KwCombinatorics系列文章中,重点介绍了KwCombinatorics组件的使用情况,其实这个组件我5年前就开始用了,非常方便,麻雀虽小五脏俱全.所以一直非常喜欢,才写了几 ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(三)——笛卡尔积组合
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(二)——排列生成
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(一)—组合生成
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- hdu1521 排列组合(指数型母函数)
题意: 有n种物品,并且知道每种物品的数量ki.要求从中选出m件物品的排数. (全题文末) 知识点: 普通母函数 指数型母函数:(用来求解多重集的排列问题) n个元素,其中a1,a2, ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- 排列组合算法(PHP)
用php实现的排列组合算法.使用递归算法,效率低,胜在简单易懂.可对付元素不多的情况. //从$input数组中取$m个数的组合算法 function comb($input, $m) { if($m ...
- iOS多线程中,队列和执行的排列组合结果分析
本文是对以往学习的多线程中知识点的一个整理. 多线程中的队列有:串行队列,并发队列,全局队列,主队列. 执行的方法有:同步执行和异步执行.那么两两一组合会有哪些注意事项呢? 如果不是在董铂然博客园看到 ...
随机推荐
- HW4.10
public class Solution { public static void main(String[] args) { int count = 0; for(int i = 100; i & ...
- HW3.16
public class Solution { public static void main(String[] args) { int randomValue = (int)(Math.random ...
- Asp.Net的应用程序生命周期概述
参考文献: MSDN:Asp.Net应用程序生命周期 博客:选择HttpHandler还是HttpModule? 1.HttpModule 应用程序(HttpApplication)引发的事件可以由实 ...
- MapKit框架使用
MapKit框架使用 一.地图的基本使用 1.简介 MapKit:用于地图展示,例如大头针/路线/覆盖层展示等(着重界面展示) 使用步骤: 导入主头文件 MapKit/MapKit.h MapKit有 ...
- iOS开发中使用静态库 .a 文件
iOS开发中,在使用一些第三方库时,可能是一个静态库(比如GPUImage).这种情况下,需要编译出静态库文件(.a) ,然后配合响应的头文件(.h 文件)使用. 编译静态库,直接在Xcode中编 ...
- 用sharding技术来扩展你的数据库(一)sharding 介绍
数据库的sharding技术作为一个“新瓶装旧酒”的概念,在新的应用环境中被赋予了新的意义.随着云计算的发展,sharding在最近几年是越来越火热,越来越多的产品开始声称自己支持sharding功能 ...
- os4开始CLLocationManager类中不支持locationServicesEnabled属性了
locationServicesEnabled改为类方法 //判断定位服务是否开启 if ([CLLocationManager locationServicesEnabled]) { }
- [Unity] How to stop camera rendering?
http://answers.unity3d.com/questions/147988/how-to-pause-the-main-camera-.html I would simply pause ...
- Nginx【第一篇】安装
一.简介 Nginx("engine x")是一款是由俄罗斯的程序设计师 Igor Sysoev 所开发高性能的 Web 和 反向代理 服务器,也是一个 IMAP/POP3/SMT ...
- 解锁Dagger2使用姿势(二) 之带你理解@Scope
关于Dagger2使用的基础如果你还不了解,可以参考我的上一篇文章解锁Dagger2使用姿势(一),这有助于你理解本篇文章. OK,我们在上篇文章介绍另外Dagger2使用过程中四个基本的注解,分别是 ...