Alternating Sum
不考虑正负的话,每两项之间之间公比为b/a,考虑正负,则把k段作为循环节,循环节育循环节之间公比为(b/a)^k,在把第一个k小节整体看作第一项,等比数列求和。
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <stack> #include <cstdlib> #include <iomanip> #include <cmath> #include <cassert> #include <ctime> #include <map> #include <set> using namespace std; #pragma comment(linker, "/stck:1024000000,1024000000") #define lowbit(x) (x&(-x)) #define max(x,y) (x>=y?x:y) #define min(x,y) (x<=y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.1415926535897932384626433832 #define ios() ios::sync_with_stdio(true) #define INF 0x3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) #define ll long long const int mod=1e9+9; ll a,b,k,n; char s[100005]; ll quick_pow(ll x,ll y) { ll ans=1; while(y) { if(y&1) ans=ans*x%mod; x=x*x%mod; y>>=1; } return ans%mod; } int main() { scanf("%lld%lld%lld%lld",&n,&a,&b,&k); scanf("%s",s); ll ans=0,r=quick_pow(a,mod-2)*b%mod,top=1,pos; for(int i=0;i<k;i++) ans=(ans+(s[i]=='+'?1ll:-1ll)*quick_pow(a,n-i)%mod*quick_pow(b,i)%mod)%mod; n=(n+1)/k; ll q=quick_pow(r,k); if(q==1) pos=ans*n%mod; else pos=ans*(quick_pow(q,n)-1)%mod*quick_pow(q-1,mod-2)%mod; printf("%lld\n",(pos+mod)%mod); return 0; }