Codeforces 963A Alternating Sum ( 思维 && 数论 )
题意 : 题目链接
分析 :
Tutorial 讲的很清楚
至于为什么这样去考虑
算是一个经验问题吧
如果一个问题要你给出模意义下的答案
就多考虑一下答案是要用逆元构造出来
也就说明有除法的存在
那么可以去考虑等比数列或者等差数列求和公式等
#include<bits/stdc++.h> #define LL long long using namespace std; const LL mod = 1e9 + 9; LL pow_mod(LL a, LL b) { LL ret = 1; while(b){ if(b & 1) ret = (ret * a) % mod; a = (a * a) % mod; b >>= 1; }return ret; } LL inv(LL a) { return pow_mod(a, mod-2); } LL n, a, b, k, tmp; int main(void) { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>a>>b>>k; string str; cin>>str; LL a0 = 0; for(int i=0; i<k; i++){ tmp = ( pow_mod(a, n-i) * pow_mod(b, i) ) % mod; if(str[i] == '-') a0 = ((a0 - tmp) + mod) % mod; else a0 = (a0 + tmp) % mod; } LL inv_a = inv(a); tmp = (b * inv_a)%mod; LL q = pow_mod(tmp, k); LL res; if(q == 1){ res = (a0 * (n+1)/k)%mod; cout<<res<<endl; return 0; } LL qq = pow_mod(tmp, n+1); LL inv_q_1 = inv((q-1+mod)%mod); res = (a0 * (qq - 1 + mod)%mod )%mod; res = (res * inv_q_1) % mod; cout<<res<<endl; return 0; }