codeforces 816 D. Karen and Test(逆元+思维+组合数)
题目链接:http://codeforces.com/contest/816/problem/D
题解:显然一看到这题应该会想到是有什么规律的于是多写几项就会发现偶数列之间是有关系的。
满足a[i][j]=a[i-2][j]+a[i-2][j+2],于是递推到最后第2列a[2][0],a[2][1]就可以用最早出现的偶数列来求的,最后是加还是减只要看n就行了。
由于a[2][0]是有几个最早出现的偶数列的奇数项求的,而且这些奇数项选择的次数符合二项式分布(这个可以通过花一下杨辉三角理解一下)。
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#define mod 1000000007
using namespace std;
const int M = 2e5 + 10;
typedef long long ll;
ll anum[M] , bnum[M] , up[M] , down[M];
ll inv(ll a) {
return a == 1 ? 1 : (ll)(mod - mod / a) * inv(mod % a) % mod;
}
//ll C(ll n , ll m)
//{
// if(m < 0)return 0;
// if(n < m)return 0;
// if(m > n-m) m = n-m;
// ll up = 1, down = 1;
// for(ll i = 0 ; i < m ; i++){
// up = up * (n-i) % mod;
// down = down * (i+1) % mod;
// }
// return up * inv(down) % mod;
//}//原先利用逆元的算法,但是这里直接求会超时与处理一下up和down就行。
int main() {
int n;
scanf("%d" , &n);
for(int i = 0 ; i < n ; i++) scanf("%lld" , &anum[i]) , bnum[i] = anum[i];
if(n == 1) {printf("%lld\n" , anum[0]); return 0;}
if(n & 1) {
n--;
int flag = 1;
for(int i = 0 ; i < n ; i++) {
if(flag) anum[i] = (bnum[i] + bnum[i + 1]) % mod;
else anum[i] = (bnum[i] - bnum[i + 1] + mod) % mod;
flag ^= 1;
}
}
n = n / 2 - 1;
ll ans1 = 0 , ans2 = 0;
up[0] = 1 , down[0] = 1;
for(int i = 1 ; i <= n / 2 ; i++) up[i] = up[i - 1] * (n - i + 1) % mod , down[i] = down[i - 1] * i % mod;
for(int i = n / 2 + 1 ; i <= n ; i++) up[i] = up[n - i] , down[i] = down[n - i];
for(int i = 0 ; i <= n ; i++) {
ans1 += anum[2 * i] * (up[i] * inv(down[i]) % mod) % mod;
ans1 %= mod;
ans2 += anum[2 * i + 1] * (up[i] * inv(down[i]) % mod) % mod;
ans2 %= mod;
}
if(n % 2) ans1 = (ans1 - ans2 + mod) % mod;
else ans1 = (ans1 + ans2) % mod;
printf("%lld\n" , ans1);
return 0;
}
codeforces 816 D. Karen and Test(逆元+思维+组合数)的更多相关文章
- codeforces 816 B. Karen and Coffee(思维)
题目链接:http://codeforces.com/contest/816/problem/B 题意:给出n个范围,q个查询问查询区间出现多少点在给出的n个范围中至少占了k次 题解:很显然的一道题目 ...
- codeforces 816 C. Karen and Game(模拟+思维)
题目链接:http://codeforces.com/contest/816/problem/C 题意:给出一个矩阵,问能否从都是0的情况下只将一整行+1或者一整列+1变形过来,如果可以输出需要步数最 ...
- codeforces 816 E. Karen and Supermarket(树形dp)
题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- Educational Codeforces Round 88 (Rated for Div. 2) E、Modular Stability 逆元+思维
题目链接:E.Modular Stability 题意: 给你一个n数,一个k,在1,2,3...n里挑选k个数,使得对于任意非负整数x,对于这k个数的任何排列顺序,然后用x对这个排列一次取模,如果最 ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces 816C/815A - Karen and Game
传送门:http://codeforces.com/contest/816/problem/C 本题是一个模拟问题. 有一个n×m的矩阵.最初,这个矩阵为零矩阵O.现有以下操作: a.行操作“row ...
- 【codeforces 816C】Karen and Game
[题目链接]:http://codeforces.com/contest/816/problem/C [题意] 给你一个n*m的矩阵; 一开始所有数字都是0; 每次操作,你能把某一行,或某一列的数字全 ...
- 【codeforces 816B】Karen and Coffee
[题目链接]:http://codeforces.com/contest/816/problem/B [题意] 给你很多个区间[l,r]; 1<=l<=r<=2e5 一个数字如果被k ...
随机推荐
- tab选项卡代码
$('.case_header ul li').click(function(){ $(this).addClass('active').siblings().removeClass('active' ...
- Java VisualVM监控远程JVM
我们经常需要对我们的开发的软件做各种测试, 软件对系统资源的使用情况更是不可少, 目前有多个监控工具, 相比JProfiler对系统资源尤其是内存的消耗是非常庞大,JDK1.6开始自带的VisualV ...
- Python pip包管理器安装第三方库超时解决方案
一.国内镜像安装 使用方法:pip install --index 镜像网站 第三方库名 二.镜像网站 http://pypi.douban.com/simple/ 豆瓣 http://mirrors ...
- [Inno Setup]写入注册表时32位系统和64位系统的路由
昨天下午组内一位同事跟说,他想在Inno Setup的安装包中写入一个注册表.目标位置是HKLM:\Software\下面创建自己的注册表项.然后说尝试了好几次都不行, 但是往HKCU下面写入却是OK ...
- 邻域保持嵌入(NPE)
传统的线性降维方法,如主成分分析(PCA).因子分析(FA)等,关注的是样本的方差,能学习线性流形的结构,却无法学习非线性流形.而经典的流形学习方法虽然能够学习非线性流形结构,但由于本身属于直推学习, ...
- egg 自学入门demo分享
目录 安装 项目 连接数据库 编写model 编写controller 添加路由 2018-08,本文适用于对egg有兴趣想要了解的同学 完整项目代码:https://github.com/NameH ...
- 01、VM安装教程
1.运行下载完成的Vmware Workstation虚拟机软件包,将会看到如图所示,然后点击“下一步”按钮, 2.在最终用户许可协议界面选中“我接受许可协议中的条款”复选框,然后点击“下一步”按钮 ...
- 《深入理解Java虚拟机》-Java代码是如何运行的
问题一:Java与C++区别 1.Java需要运行时环境,包括Java虚拟机以及Java核心类库等. 2.C++无需额外的运行时,通常编译后的代码可以让机器直接读取,即机器码 问题一:Java为什么要 ...
- input默认值设置
在input框里我们可以设置 一些默认值,在点击之后input之后就消失了 <input id="_le_name" type="text" onFocu ...
- lumen错误 NotFoundHttpException in RoutesRequests.php line 442:
解决:进入 public/index.PHP 将 $app->run(); 修改成下面的: $request = Illuminate\Http\Request::capture(); $app ...