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 ...
随机推荐
- android 基于wifi模块通信开发
这篇文章主要是我写完手机与wifi模块通信后所用来总结编写过程的文章,下面,我分几点来说一下编写的大概流程. 一.拉出按钮控件并设置它的点击事件 二.设置wifi权限 三.打开和关闭wifi 四.扫描 ...
- 在Docker中部署Spring Boot项目
想要获取更多文章可以访问我的博客 - 代码无止境. 微服务现在在互联网公司可谓非常流行了,之前找工作的的时候很多HR电话约面试的时候都会问对微服务是否有过接触.而微服务和Docker可以非常完美的结合 ...
- 2019上半年总结——Github上那些Java面试、学习相关仓库
分享一下最近逛Github看到了一些对于Java面试以及学习有帮助的仓库,这些仓库涉及Java核心知识点整理.Java常见面试题.算法.基础知识点比如网络和操作系统等等. 知识点相关 1.JavaGu ...
- Python pip包管理器安装第三方库超时解决方案
一.国内镜像安装 使用方法:pip install --index 镜像网站 第三方库名 二.镜像网站 http://pypi.douban.com/simple/ 豆瓣 http://mirrors ...
- Docker最简单入门之(一)——介绍和配置Docker
0. 前言 最近学完了Dokcer,特别记录一下,算是对自己学习成果的一个总结.以便自己能够更好的理解Docker.粗略估计了一下,我大概会分成4个部分,只记录一下常用的操作,至于一些比较难的操作或者 ...
- Go和Python学习计划
计划虽然不一定能实现,但还是要有的,万一实现了呢. 一.学习Go 1.先看尚雪谷https://www.bilibili.com/video/av48141461/?p=12的go语言全套,把基础的过 ...
- 【C++】string::substr函数
形式:s.substr(p, n) 返回一个string,包含字符串s中从p开始的n个字符的拷贝(p的默认值是0,n的默认值是s.size() - p,即不加参数会默认拷贝整个s) int main( ...
- Appium+python自动化(三十)- 实现代码与数据分离 - 数据配置-yaml(超详解)
简介 本篇文章主要介绍了python中yaml配置文件模块的使用让其完成数据和代码的分离,宏哥觉得挺不错的,于是就义无反顾地分享给大家,也给大家做个参考.一起跟随宏哥过来看看吧. 思考问题 前面我们配 ...
- 洛谷 P2158 [SDOI2008]仪仗队
题意简述 给定一个n,求gcd(x, y) = 1(x, y <= n)的(x, y)个数 题解思路 欧拉函数, 则gcd(x, y) = 1(x <= y <= n)的个数 ans ...
- SQL 分组后,获取每组中的最大值对应的数据
select gr,num,dt,(select bys from test where gr=b.gr and dt=b.dt) bys from ( select gr,count(0) num, ...