标签:long return def amp bsp 偶数 div lse while
若b为奇数,则
sum(a,b)=1+a+a^2+...+a^b=(1+a+a^2+...+a^((b-1)/2))+(a^((b+1)/2)+...+a^b)=(1+a^((b+1)/2))*sum(a,(b-1)/2)
若b为偶数,则
sum(a,b)=(1+a^(b/2))*sum(a,b/2-1)+a^b
1 typedef long long ll; 2 3 ll qpow(ll a, ll b, ll p) { 4 ll ans = 1, x = a % p; 5 while (b) { 6 if (b & 1) ans = ans * x % p; 7 x = x * x % p, b >>= 1; 8 } 9 return ans; 10 } 11 12 ll sum(ll a, ll b, ll p) { 13 if (!b) return 1; 14 if (b & 1) 15 return (1 + qpow(a, (b + 1) / 2, p)) * sum(a, (b - 1) / 2, p) % p; 16 else 17 return ((1 + qpow(a, b / 2, p)) * sum(a, b / 2 - 1, p) % p + qpow(a, b, p)) % p; 18 }
分治法求sum(a,b)=1+a+a^2+...+a^b=?
标签:long return def amp bsp 偶数 div lse while
原文地址:https://www.cnblogs.com/Mr94Kevin/p/10415955.html