## codeforces 964 C. Alternating Sum（逆元、数学）

``````#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%I64d",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod int(1e9+7)
#define lc (d<<1)
#define rc (d<<1|1)
#define P pair<int,int>
#define pi acos(-1)
ll n,a,b,k;
string d;
ll gmod(ll a, ll b, ll c)
{
long long res = 1;
while(b>0)
{
if(b&1)
{
res=(res*a)%c;
}
a=(a*a)%c;
b>>=1;
}
return res;
}
int main()
{
cin.tie(0);
cin>>n>>a>>b>>k;
ll m=1e9+9,z=0,x,y,qw;
cin>>d;
FOR(i,0,k-1)
{
x=(gmod(a,n-i,m)%m+m)%m;
y=(gmod(b,i,m)%m+m)%m;
if(d[i]=='+')
{
z=(z%m+(x*y)%m)%m;
}
else
{
z=(z%m-(x*y)%m)%m;
}
}
x=(gmod(gmod(a,m-2,m),k,m)%m+m)%m;
y=(gmod(b,k,m)%m+m)%m;
x*=y;
x%=m;
qw=(n+1)/k;
ll s=0;
if(x==1)  s=(z%m*qw%m)%m;
else  s=(z%m*((gmod(x,qw,m)%m-1)%m*gmod(x-1,m-2,m)%m))%m;
while(s<0) s+=m;
s%=m;
cout<<s<<endl;
return 0;
}
``````