Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) C. Alternating Sum

C. Alternating Sum

write picture description here

portal

This question, how do you say it, this student, let's understand the geometric sequence.
First of all, the first point is to use fast exponentiation in the operation, the basic operation.
Then according to the given conditions, we can find that two consecutive segments have b k a k The number of sub-different
ends is n + 1 k
From the above, we can easily know that the result is a n s = i = 0 k 1 s i a n i b i a n s = i = 0 ( n + 1 k ) k b k a k
or a n s = i = 0 k 1 s i a n i b i b k a k n + 1 k 1 b k a k

#include<cstdio>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<limits.h>
#include<string.h>
#include<map>
#include<list>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define inf int(0x3f3f3f3f)
#define mod int(1e9+9)
#define eps double(1e-6)
#define pi acos(-1.0)
#define lson  root << 1
#define rson  root << 1 | 1

char s[200005];

ll n,a,b,k;


ll cal(ll x,ll n)
{
    if(x<0)
    {
        x+=mod;
        x%=mod;
    }
    ll num=x,ans=1;
    for(int i=0; i<64; i++)
    {
        if(1ll<<i&n)
            ans=ans*num%mod;
        num=num*num%mod;
    }
    return ans;
}

ll numa;
ll numb;
ll aa;
ll bb;
ll a1;

void solve()
{
    ll num=cal(aa,k)*cal(b,k)%mod;
    a1+=mod;
    a1%=mod;
    ll sum=0;
    if(num==1)
        sum=(n+1)/k*a1%mod;
    else
        sum=a1*(1-cal(num,(n+1)/k)+mod)%mod*cal(1-num,mod-2)%mod;
    sum+=mod;
    sum%=mod;
    cout<<sum<<endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie();
    cin>>n>>a>>b>>k;
    cin>>s;
    ll numa=cal(a,n);
    ll numb=1;
    ll aa=cal(a,mod-2);
    ll bb=cal(b,mod-2);
    ll a1=0;
    for(int i=0; i<k; i++)
    {
        if(s[i]=='+')
            a1=(a1+numa*numb%mod)%mod;
        else
            a1=(a1-numa*numb%mod+mod)%mod;
        numa=numa*aa%mod;
        numb=numb*b%mod;
    }
    //solve();
    ll num=cal(aa,k)*cal(b,k)%mod;
    a1+=mod;
    a1%=mod;
    ll sum=0;
    if(num==1)
        sum=(n+1)/k*a1%mod;
    else
        sum=a1*(1-cal(num,(n+1)/k)+mod)%mod*cal(1-num,mod-2)%mod;
    sum+=mod;
    sum%=mod;
    cout<<sum<<endl;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325713861&siteId=291194637