Codeforces Round #475 (Div. 2) C - Alternating Sum

等比数列求和一定要分类讨论!!!!!!!!!!!!

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define fi first
 4 #define se second
 5 #define mk make_pair
 6 #define pii pair<int,int>
 7 #define ull unsigned long long
 8 using namespace std;
 9
10 const int N=1e6+7;
11 const int M=100+7;
12 const int inf=0x3f3f3f3f;
13 const LL INF=0x3f3f3f3f3f3f3f3f;
14 const int mod=1e9 + 9;
15
16 LL n, a, b, k;
17 char s[N];
18
19 LL q_pow(LL a, LL b) {
20     LL ans = 1;
21     while(b) {
22         if(b & 1) ans = ans * a % mod;
23         a = a * a % mod; b >>= 1;
24     }
25     return ans;
26 }
27
28 LL ivn(LL x) {
29     return q_pow(x, mod - 2);
30 }
31
32
33 int main() {
34
35     scanf("%lld%lld%lld%lld", &n, &a, &b, &k);
36     scanf("%s", s);
37     LL w = q_pow(b, k) * ivn(q_pow(a, k)) % mod;
38     w = (w + mod) % mod;
39
40     LL ans = 0;
41     LL cnt = (n + 1) / k;
42
43     for(LL i = 0; i < k; i++) {
44         LL ret1 = q_pow(a, n - i) * q_pow(b, i) % mod;
45         LL ret2 = (1 - q_pow(w, cnt) + mod) % mod;
46         LL ret3 = ivn((1 - w + mod) % mod);
47         LL ret = (((ret1 * ret2) % mod) * ret3) % mod;
48         if(w == 1) ret = (ret1 * cnt) % mod;
49         if(s[i] == ‘-‘) ret = -ret;
50         ans = (ans + ret + mod) % mod;
51     }
52     printf("%lld\n", ans);
53     return 0;
54 }
55 /*
56 */

原文地址:https://www.cnblogs.com/CJLHY/p/8873939.html

时间: 2024-10-08 03:07:19

Codeforces Round #475 (Div. 2) C - Alternating Sum的相关文章

Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Description Input Output Sample Input 51 2 1 2 1 Sample Output 1 1 1 2 2 题解:这个题有做慢了,这种题做慢了和没做出来区别不大... 读题的时候脑子里还意识到素数除了2都是奇数,读完之后就脑子里就只剩欧拉筛了,贪心地构造使得前缀和是连续的素数,那

Codeforces Round #344 (Div. 2) E. Product Sum 二分斜率优化DP

E. Product Sum Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special. You are given an array a of

Codeforces Round #319 (Div. 2) B Modulo Sum

直接O(n*m)的dp也可以直接跑过. 因为上最多跑到m就终止了,因为sum[i]取余数,i = 0,1,2,3...,m,会有m+1种可能,m的余数只有m种必然有两个相同. #include<bits/stdc++.h> using namespace std; const int maxn = 1e3+5; int cnt[maxn]; bool dp[maxn][maxn]; #define Y { puts("YES"); return 0; } int main(

Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)

B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given an array a consisting of n integers a1,a2,-,an. You want to split it into exactly k non-empty non-intersecting

Codeforces Round #475 (Div. 2) D. Destruction of a Tree

1 You are given a tree (a graph with n vertices and n?-?1 edges in which it's possible to reach any vertex from any other vertex using only its edges). 2 3 A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges c

Codeforces Round #556 (Div. 2) C. Prefix Sum Primes

题目大意让你改变数组的排序,使前缀和的素数最多: 这是一道模拟题,让你通过判断1和2的个数来解决, 只要特判一下1的个数是零的时候,2的个数是零的时候,或者1只有一个而2的个数又不是0个的时候,剩下的情况我们只有把1的个数分奇数和偶数来考虑,大致思路是这样,接下来就看代码吧 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除