You are given two integers aa and bb. Moreover, you are given a sequence s0,s1,…,sns0,s1,…,sn. All values in ss are integers 11 or −1−1. It's known that sequence is kk-periodic and kk divides n+1n+1. In other words, for each k≤i≤nk≤i≤n it's satisfied that si=si−ksi=si−k.
Find out the non-negative remainder of division of n∑i=0sian−ibi∑i=0nsian−ibi by 109+9109+9.
Note that the modulo is unusual!
The first line contains four integers n,a,bn,a,b and kk (1≤n≤109,1≤a,b≤109,1≤k≤105)(1≤n≤109,1≤a,b≤109,1≤k≤105).
The second line contains a sequence of length kk consisting of characters '+' and '-'.
If the ii-th character (0-indexed) is '+', then si=1si=1, otherwise si=−1si=−1.
Note that only the first kk members of the sequence are given, the rest can be obtained using the periodicity property.
Output a single integer — value of given expression modulo 109+9109+9.
2 2 3 3 +-+
7
4 1 5 1 -
999999228
In the first example:
(n∑i=0sian−ibi)(∑i=0nsian−ibi) = 2230−2131+20322230−2131+2032 = 7
In the second example:
以下公式提出一个a^n后就有这个等比数列
b/a可以用乘法逆元c=b*a^(-1)来表示;
则有(c==1单独讨论);
对于以k为周期的数列,
可以考虑求出每一个i+k,i+2k......i+m*k(i+m*k<=n);
则变形为X=;
令C=,则有X=
则每一个k的表达式为(sign)
快速幂加逆元,可以在O(logn)内求出每一个i;
总时间复杂度O(k*logn)
#include
using namespace std;
#define ll long long
const ll p=1e9+9;
ll Inv,C;
ll pow1(ll a,ll n)
{
ll ans=1;
a=a%p;
while(n)
{
if(n&1)ans=ans*a%p;
a=a*a%p;
n>>=1;
}
return ans;
}
ll f(ll n)
{
if(C==1)return n+1;
return (pow1(C,n+1)-1+p)%p*Inv%p;
}
int main()
{
ll n,A,B,k,c,x;
cin>>n>>A>>B>>k;
// if(A!=B)
c=B*pow1(A,p-2)%p;
C=pow1(c,k);
if(C!=1)
Inv=pow1(C-1,p-2);
ll s=pow1(A,n);
ll ans=0;
string ss;
cin>>ss;
for(int i=0; i
#include
using namespace std;
#define ll long long
const ll p=1e9+9;
ll Inv,C;
ll pow1(ll a,ll n)
{
ll ans=1;
a=a%p;
while(n)
{
if(n&1)ans=ans*a%p;
a=a*a%p;
n>>=1;
}
return ans;
}
ll f(ll n)
{
if(C==1)return n+1;
return (pow1(C,n+1)-1+p)%p*Inv%p;
}
int main()
{
ll n,A,B,k,c,x;
cin>>n>>A>>B>>k;
// if(A!=B)
c=B*pow1(A,p-2)%p;
C=pow1(c,k);
if(C!=1)
Inv=pow1(C-1,p-2);
ll s=pow1(A,n);
ll ans=0;
string ss;
cin>>ss;
for(int i=0; i
#include
using namespace std;
string str;
#define ll long long
ll mi(ll a,ll b)
{
ll ans=1;
a%=1000000009;
while (b)
{
if (b%2==1) ans=ans*a%1000000009;
b>>=1;
a=a*a%1000000009;
}
return ans;
}
int main()
{
ll n,a,b,c,t,ans=0,sum=0;
cin >> n >> a>> b >> c ;
cin >> str;
for(int i=0; i<=n; i++)
{
if(str[i%c]=='-')
{
ans-=mi(a,n-i)*(mi(b,i));
//cout << 32<< endl;
}
else if(str[i%c]=='+')
{
ans+=mi(a,n-i)*(mi(b,i));
//cout << 33<< endl;
}
ans=ans%1000000009;
}
//cout << 3 << endl;
if(ans<0)
{
ans=ans+1000000009;
}
cout << ans << endl;
return 0;
}