codeforces 964C

Alternating Sum

题意:给你n,a,b,k,求式子 模1e9+9的值

思路:很容易得出每一个周期的结果xi是一个等比数列,公比q=(b/a)^k,因为取模,所以必然要求逆元,q=b*(a^(mod-2)),特判q=1的情况,注意,因为q是取了模的,所以q=1是模意义下等于1,并不是单纯的a==b

AC代码:

#include "iostream"
#include "iomanip"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define step(x) fixed<< setprecision(x)<<
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ll long long
#define endl ("\n")
#define ft first
#define sd second
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const ll mod=1e9+9;
const ll INF = 1e18+1LL;
const int inf = 1e9+1e8;
const double PI=acos(-1.0);
const int N=2e5+100;

ll n, a, b , k;
char s[N];

ll PowMod(ll a, ll b) {
    ll ret = 1;
    a = a%mod;
    while(b) {
        if(b&1) ret = (ret*a)%mod;
        a = (a*a)%mod;
        b >>= 1;
    }
    return ret;
}

int main() {
    scanf("%lld %lld %lld %lld", &n, &a, &b, &k);
    scanf("%s", s);
    ll q = PowMod(b*PowMod(a,mod-2), k);
    ll ans = 0;
    for(int i=0; i<k; ++i) {
        int p = 1;
        if(s[i] == '-') p = -1;
        ans = (ans+p*PowMod(a,n-i)*PowMod(b,i))%mod;
    }//cout<<q<<" "<<ans<<endl;
    if(q == 1) {
        ans = ans * ((n+1)/k);
        return 0*printf("%lld\n", ((ans%mod)+mod)%mod);
    }
    ans = (((ans*(PowMod(q,(n+1)/k)-1))%mod)*PowMod(q-1, mod-2))%mod;
    printf("%lld\n", (ans+mod)%mod);
    return 0;
}

 

 

 

posted on 2018-04-18 22:16  lazzzy  阅读(399)  评论(0编辑  收藏  举报

导航