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

Posted basasuya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #475 (Div. 2) C - Alternating Sum相关的知识,希望对你有一定的参考价值。

首先0-k项可以直接求出
其次后面(n+1)/k组项其实构成了等比数列(比值是a^{-k}b^{k})
那么此时还需考虑这个比值为1的特殊情况就行了(不仅a==b的时候这个比值会为1,因为取余,一些乱七八糟的情况也会造成比值为1)

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
#define MS(x, y) memset(x, y, sizeof(x))
#define MP(x, y) make_pair(x, y)
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 9;
char S[N];

ll Pow(ll x, ll y) {
    ll result = 1;
    while (y) {
        if (y & 1)
            result = result * x % MOD;
        x = x * x % MOD;
        y >>= 1;
    }
    return result;
}
int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out1.txt", "w", stdout);
    ll n, a, b, k;
    while (~scanf("%lld %lld %lld %lld %s", &n, &a, &b, &k, S)) {
        ll origin = Pow(a, n);
        ll ans = 0;
        ll aDivide = Pow(a, MOD - 2);
        for (int i = 0; i < k; ++i) {
            if (S[i] == ‘+‘)
                ans = (ans + origin) % MOD;
            else
                ans = (ans - origin + MOD) % MOD;
            //      printf("%lld\n", origin);
            origin = origin * aDivide % MOD * b % MOD;
        }
        //  printf("%lld\n", ans);
        ll mulUnit = Pow(Pow(a, k), MOD - 2) * Pow(b, k) % MOD;
        ll times = (n + 1) / k;
        ll ansUnit;
        if (a == b || mulUnit == 1) {
            ansUnit = times;
        } else {
            ansUnit = (Pow(mulUnit, times) - 1 + MOD) % MOD;
            ansUnit = ansUnit * Pow((mulUnit - 1 + MOD) % MOD, MOD - 2) % MOD;
        }
        printf("%lld\n", ans * ansUnit % MOD);
    }
    return 0;
}
/*

5 2 3 3
+-+

5 1 1 3
+-+
*/

以上是关于Codeforces Round #475 (Div. 2) C - Alternating Sum的主要内容,如果未能解决你的问题,请参考以下文章

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

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

Codeforces Round #475 Div. 2 A B C D

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

Codeforces Round #436 E. Fire(背包dp+输出路径)

[ACM]Codeforces Round #534 (Div. 2)