【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.5
public class Solution { public static void main(String[] args) { final double POUNDS_PER_KILOGRAM = ...
- APNs消息推送完整讲解
在项目的AppDelegate中的didFinishLaunchingWithOptions方法中加入下面的代码: [[UIApplication sharedApplication] registe ...
- Sublime Text3使用及常用插件
1.安装packages组件: 参考一: https://sublime.wbond.net/installation 参考二: http://blog.csdn.net/superskk6/arti ...
- Harris角点算法
特征点检测广泛应用到目标匹配.目标跟踪.三维重建等应用中,在进行目标建模时会对图像进行目标特征的提取,常用的有颜色.角点.特征点.轮廓.纹理等特征.现在开始讲解常用的特征点检测,其中Harris角点检 ...
- 【面试虐菜】—— Oracle知识整理《DBA的思想天空》
Inventory Oracle安装工具OUI用来管理Oracle安装目录的 Oracle的参数文件,启动后按照下面的顺序读取参数文件,如果读取失败,启动数据库 失败: 1 $ORACLE_HO ...
- Hyper-V网络虚拟化--VM之间拷贝速度慢
Hyper-V网络虚拟化后,两台VM使用的是同一个VM网卡,相同IP地址池,但是互相拷贝文件速度很慢,只有2M左右,拷贝同时ping延迟在2000ms,解决方法: 主机型号:HP ProLiant D ...
- ThinkPHP3.1快速入门(2)数据CURD
上一篇中,我们了解了ThinkPHP的基础部分,以及如何创建一个控制器和模板,并知道了M方法的用法,本篇将会讲解下数据的CURD操作,探索下更多的数据操作. CURD CURD是一个数据库技术中的缩写 ...
- 编译openjdk源码
http://www.cnblogs.com/ACFLOOD/p/5528035.html
- .net core 1.1.0 MVC 控制器接收Json字串 (JObject对象) (一)
.net core 1.1.0 MVC 控制器接收Json字串 (JObject对象) (二) Json是WEB交互常见的数据,.net core 处理方式是转为强类型,没有对应的强类型会被抛弃,有时 ...
- 【转】Cocos2d-x 2.x CCSprite 灰白图的生成(利用shader设置)——2013-08-27 21
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=325 游戏中人物死掉后要把人物头 ...