最讨厌数学题了, 而且还有一阵子没做过了,不过做做也好,数学面前自己就是弱渣,要好好学数学。
题解如下:
Let Z=k−1∑i=0sian−ibiZ=∑i=0k−1sian−ibi and q=(ba)kq=(ba)k.
Let's notice that according equality is true: n∑i=0sian−ibi=(n+1)/k−1∑i=0Z⋅qi∑i=0nsian−ibi=∑i=0(n+1)/k−1Z⋅qi
We can easily get values of ZZ, qq. We only have to count the value of geometric progression.
Remember to handle the situation when q=1q=1. In this case it is not necessarily means that a=ba=b.
下面代码,copy某位大神的求P(N,M),以及求模数逆元的代码
对于经常使用的定理做一下说明,对于 b/a %M 等价于 b*inv(a , M) % M,
下面代码,有一处即使用等比数列求和公式,需要注意分母不能为0,所以分两种情况讨论,本人使用的方式和上面将的方式,大致雷同,只不过上面可以做到o(1),我的代码是O(k);
老莫名奇妙将这里的等比形式误认为应该和是二项式,要注意呀
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll M=1e9+9,n,a,b,k;
char s[1<<17];
inline ll P(ll a,ll b){ll r=1;do{if(b&1)r=(r*a)%M;a=(a*a)%M;}while(b/=2);return r;}
ll inv(ll a){return P(a,M-2);}
int main()
{
cin>>n>>a>>b>>k>>s;
ll t = (n + 1) / k;
ll all = 0;
ll ba = inv(P(a,k))*P(b,k)%M;
//cout<<ba<<endl;
for(int i=0;i<k;i++){
ll ans = P(a,n-i)*P(b,i)*(s[i]=='+' ? 1 : -1)%M;
if(ba != 1) ans = (ans * (P( ba , t ) - 1)%M *inv(ba - 1))%M;
else ans = ans * t %M;
all = (all+ ans) % M;
}
cout<<(all + M) %M<<endl;
return 0;
}