Codeforces 963A(Math , 统计)

最讨厌数学题了, 而且还有一阵子没做过了,不过做做也好,数学面前自己就是弱渣,要好好学数学。

题解如下:

Let Z=k1i=0sianibiZ=∑i=0k−1sian−ibi and q=(ba)kq=(ba)k.

Let's notice that according equality is true: ni=0sianibi=(n+1)/k1i=0Zqi∑i=0nsian−ibi=∑i=0(n+1)/k−1Z⋅qi

We can easily get values of ZZqq. 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值