CF815B:Karen and Test(数学)
来源:互联网 发布:淘宝考试系统答案 编辑:程序博客网 时间:2024/06/03 17:02
Karen has just arrived at school, and she has a math test today!
The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points.
There are n integers written on a row. Karen must alternately add and subtract each pair of adjacent integers, and write down the sums or differences on the next row. She must repeat this process on the values on the next row, and so on, until only one integer remains. The first operation should be addition.
Note that, if she ended the previous row by adding the integers, she should start the next row by subtracting, and vice versa.
The teachers will simply look at the last integer, and then if it is correct, Karen gets a perfect score, otherwise, she gets a zero for the test.
Karen has studied well for this test, but she is scared that she might make a mistake somewhere and it will cause her final answer to be wrong. If the process is followed, what number can she expect to be written on the last row?
Since this number can be quite large, output only the non-negative remainder after dividing it by 109 + 7.
The first line of input contains a single integer n (1 ≤ n ≤ 200000), the number of numbers written on the first row.
The next line contains n integers. Specifically, the i-th one among these is ai (1 ≤ ai ≤ 109), the i-th number on the first row.
Output a single integer on a line by itself, the number on the final row after performing the process above.
Since this number can be quite large, print only the non-negative remainder after dividing it by 109 + 7.
53 6 9 12 15
36
43 7 5 2
1000000006
In the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15.
Karen performs the operations as follows:
The non-negative remainder after dividing the final number by 109 + 7 is still 36, so this is the correct output.
In the second test case, the numbers written on the first row are 3, 7, 5 and 2.
Karen performs the operations as follows:
The non-negative remainder after dividing the final number by 109 + 7 is 109 + 6, so this is the correct output.
题意:给出一段数字,按照图示计算求结果mod 1e9+7。
思路:考虑n为偶数的情况,观察到倒数第二行两部分的系数都满足组合数C(n/2-1, (i-1)/2),这两部分相加还是相减取决于n能否被4整除,n为奇数的情况先计算一遍使n变为偶数。
# include <bits/stdc++.h>using namespace std;typedef long long LL;const LL mod = 1e9+7;const int maxn = 2e5;LL a[maxn+3], fac[maxn/2+3]={1,1}, inv[maxn/2+3]={1,1}, fi[maxn/2+3]={1,1};void init(){ for(int i=2; i<=maxn/2; ++i) { fac[i] = fac[i-1]*i%mod; inv[i] = (mod-mod/i)*inv[mod%i]%mod; fi[i] = fi[i-1]*inv[i]%mod; }}LL c(LL n, LL m){ return fac[n]*fi[n-m]%mod*fi[m]%mod;}int main(){ init(); int n; scanf("%d",&n); for(int i=1; i<=n; ++i) scanf("%lld",&a[i]); if(n<=2) return 0*printf("%lld\n",(a[1]+a[2])%mod); if(n&1) { int t = 1; for(int i=1; i<n; ++i) { a[i] = a[i]+a[i+1]*t; t *= -1; } --n; } LL sum=0, t=(n%4)?1:-1; for(int i=1; i<=n; i+=2) { LL tmp = c(n/2-1, i/2); sum = (sum + (a[i]+t*a[i+1])*tmp + mod) % mod; } printf("%lld\n",(sum+mod)%mod); return 0;}
- CF815B:Karen and Test(数学)
- codeforces 816D Karen and Test(数学)
- [数学杂题]Codeforces #419(div1) 815B. Karen and Test
- Codeforces Round #419 (Div. 2) (Codeforces 815B) D. Karen and Test 组合数学
- Codeforces 815B Karen and Test
- Codeforces 816D Karen and Test
- Codeforces 815B. Karen and Test 【规律】
- Codeforces Round #419 (Div. 1) B. Karen and Test(组合数)
- CF815A:Karen and Game(思维)
- CF816A-Karen and Morning
- CF816B-Karen and Coffee
- Karen and Morning
- Karen and Coffee codeforces
- CF816B-Karen and Coffee
- C. Karen and Game
- Karen and Game Codeforces
- [codeforces815D] Karen and Cards
- Codeforces816A Karen and Morning
- Bootstrap学习笔记(三)
- Ubuntu配置Shadowsocks qt5
- 动态规划------走楼梯问题
- c++中内存拷贝函数(C++ memcpy)详解
- Hibernate主题4——缓存机制
- CF815B:Karen and Test(数学)
- 【0056】jdk安装教程
- MyBatais
- 解决 git push Failed to connect to 127.0.0.1 port 45463: 拒绝连接
- python安装相关知识
- [6] Linux文件压缩打包
- java发送邮件
- 【OpenCV入门教程之十八】OpenCV仿射变换 & SURF特征点描述合辑
- Swift