hihoCoder #1430 : A Boring Problem(一琐繁题)

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

Description - 题目描述

  As a student of the school of electronics engineering and computer science in Peking University, Kyle took the course named Advanced Algebra in his freshman year, which, unluckily, became his nightmare.
  His teacher, Mr. X, has an approximately paranoid requirements in the ability of calculation, from which his students suffer a lot.
  One day, Mr. X got a whim that he wanted to know for a given integer k and a long numeric string S whose length is N, what is the result of   for each i(1 ≤ i ≤ N), where . S[L] means the Lth digit in S, and L starts from 1.
  Mr. X added the problem to the midterm test. Please give a hand to Kyle and tell him the answer mod 1000000007.

作为北大信息科学技术学院的学生,Kyle曾经不知何为噩梦直到大一时候上了一门叫高等代数的课。
他的老师,Mr. X,对计算能力有着丧心病狂的要求。学生们饱受其害。
一天,Mr. X突发奇想,一个整数k与一个长度为N的数字串S,对于每个i( ≤ i ≤ N) 式子Σi j=1F(j, i)的结果是什么?这里F(j, i)=(Σi L=j S[L])^k。S[L]表示S的第L个数,L从1开始。
Mr. X把这道题放到了期中考试里。快帮帮Kyle 并告诉他模1000000007后的答案。

CN

Input - 输入

  There are multiple test cases.
  The first line of the input contains an integer T which means the number of test cases.
  The first line of each test case contains two integers, above mentioned N and k.
  The next line is the above mentioned string S. S consists of only digits('0 - '9').

多组测试用例。
输入的第一行为一个整数T,表示测试用例的数量。
每组测试用例的第一行有两个整数,即上述的N与K。
下一行为一个数字串S。S仅包含数字('0 - '')。

CN

Output - 输出

  For each test case, print a single line representing the result of   for each i(1 ≤ i ≤ N).

对于每个测试用例,输出一行数字表示每个i( ≤ i ≤ N)在式子Σi j=1F(j, i)中的结果。

CN

Sample Input - 样例输入

2
5 1
12345
5 1
54321

Sample Output - 样例输出

1 5 14 30 55
5 13 22 30 35

Note - 注意

  T ≤ 5
  N ≤ 50,000, k ≤ 100

题解

  为了防止和后面的说明混淆,字符串中的S[l]用ai代替

  先按题目意思弄出几项,长得有点像01背包。

  这个时候如果按上面的形式做的话,时间复杂度就是O(N*N*Log2K),会爆炸的。

  如果用看,中间的子串就不好表示……而且似乎没法化简(反正本渣没化简出来)。

然后加法不行,用减法?

  使用前N项和(前缀和)就能把每个子串用减法表示出来。

  此处Si表示前i项和,且S0 = 0。

  式子转变如下:

  再把每一项改写成二项展开式:

  合并二项展开式

  用SSi表示前i项S之和,则可以得到通项:

  这个时候时间复杂度就降到O(NK)了。

  注意取模与最后输出,然后就没什么问题了。

代码 C++

 #include <cstdio>
#define mod 1000000007
#define mx 50005
#define ll long long
ll c[][], s[mx][], ss[mx][];
char rd[mx];
int main(){
int t, n, k, i, j;
ll opt, tmp;
for (i = ; i <= ; ++i){
c[i][] = ;
for (j = ; j <= i; ++j) c[i][j] = (c[i - ][j - ] + c[i - ][j]) % mod;
}
for (i = ; i < mx; ++i) s[i][] = ss[i][] = ; for (scanf("%d", &t); t; --t){
scanf("%d%d ", &n, &k); gets(rd);
for (i = ; i < n; ++i) s[i + ][] = rd[i] - '';
for (i = ; i <= n; ++i){
s[i][] = (s[i][] + s[i - ][]) % mod;
for (j = ; j <= k; ++j) s[i][j] = (s[i][j - ] * s[i][]) % mod;
}
for (i = ; i <= n; ++i){
for (j = ; j <= k; ++j) ss[i][j] = (s[i][j] + ss[i - ][j]) % mod;
} for (i = ; i <= n; ++i){
opt = ;
for (j = ; j <= k; ++j){
tmp = (c[k][j] * s[i][j]) % mod;
if ((k - j) & ) opt -= (tmp*ss[i - ][k - j]) % mod;
else opt += (tmp*ss[i - ][k - j]) % mod;
opt %= mod;
}
printf("%lld%c", (opt + mod) % mod, " \n"[i == n]);
}
}
return ;
}

hihoCoder 1430 : A Boring Problem(一琐繁题)的更多相关文章

  1. A Boring Problem UVALive - 7676 (二项式定理+前缀和)

    题目链接: I - A Boring Problem UVALive - 7676 题目大意:就是求给定的式子. 学习的网址:https://blog.csdn.net/weixin_37517391 ...

  2. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  3. CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】

    CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...

  4. A Boring Problem UVALive - 7676

    16年北京现场赛的题,全场过的队30+. 初看只知道 O(N^2logK)的暴力,以为是什么变换. 仔细发现活用 二项式定理 就行. #include <bits/stdc++.h> us ...

  5. 【XSY1642】Another Boring Problem 树上莫队

    题目大意 给你一棵\(n\)个点的树,每个点有一个颜色\(c_i\),每次给你\(x,y,k\),求从\(x\)到\(y\)的路径上出现次数第\(k\)多的颜色的出现次数 \(n,q\leq 1000 ...

  6. 北京区域赛I题,Uva7676,A Boring Problem,前缀和差分

    转载自https://blog.csdn.net/weixin_37517391/article/details/83821752 题解 其实这题不难,只要想到了前缀和差分就基本OK了. 我们要求的是 ...

  7. hdu 3518 Boring counting 后缀数组基础题

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  8. Train Problem I (HDU 100题纪念)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  9. hdu 5427 A problem of sorting 水题

    A problem of sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contest ...

随机推荐

  1. 【C# 小窍门】WeakEventManager 无法识别!ErrorCS0246The type or namespace name 'WeakEventManager' could not be found

    WeakEventManager 想用一下这个,在学习C# 事件的时候,结果添加了Using System.Windows, WeakEventManager却一直是红色的,无法识别,报这个错哦~ 好 ...

  2. [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. struts2中的jsp值传到后台action接收的三种方法

    struts2中的Action接收表单传递过来的参数有3种方法: 如,登陆表单login.jsp: <form action="login" method="pos ...

  4. CentOS 6.2 SVN搭建 (YUM安装)

    安装说明 安装了一下SVN服务器,本文没有与Apache整合,过程如下: 系统环境:CentOS-6.2安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN ...

  5. 多线程中共享变量——CCF总决赛试题

    题目要求 数据格式 Q 系统的输入为纯文本格式的文件,由若干行组成,每一行由城市编号.年龄.收入组成,相邻两项之间用一个空格分隔.以下是输入的一个片段: 1001 20 12000 1001 50 2 ...

  6. 用自己赚的钱第一次坐飞机 那feel倍儿爽

    马年春晚上,歌手大张伟的神曲<倍儿爽>如今已传遍大街小巷.其实,不管人家到底有没有炒作,能让我们这些观众感觉到放松.乐呵,那就是一个成功的春晚节目.而今年,我也如同这歌中唱的一样,活得倍儿 ...

  7. jqGrid APi 详解

    jqGrid APi 详解 jqGrid皮肤 从3.5版本开始,jqGrid完全支持jquery UI的theme.我们可以从http://jqueryui.com/themeroller/下载我们所 ...

  8. 一个可拖拽的DIV框框

    http://codepen.io/lrelia/pen/bEyLB 使用了JQuery UI库, draggable来自于JQuery UI库

  9. iOS的生命周期

    iOS应用程序一般都是由自己编写的代码和系统框架组成.系统框架提供了一些基本的infrastructure给APP来运行,而开发者则自己编写代码定制APP的外观和行为,了解iOS infrastruc ...

  10. XPlane android 2D手游开发实战

    android 飞行射击游戏类 采用 xamarin 跨平台开发技术  纯C#语言编写 操作简单 1.手指拖动飞机 躲避敌机 2.通过吃敌机爆炸后遗落的物品增加各种属性 3.双击战机放大技能 4.目前 ...