E题求等比数列的前n项和,为什么用逆元过不了啊
求大佬康康
不能过:
ll sum(ll a, ll b) {
ll fenzi = poww(a, b + 1) - 1;ll niyuan = poww(a - 1, mod - 2);
return fenzi * niyuan % mod;
}
能过:
int sum(int p, int n)
{
if (n == 0) return 1;
if (n & 1) return sum(p, (n - 1) / 2) * (1 + poww(p, (n + 1) / 2)) % mod;
else return (sum(p, n / 2 - 1) * (1 + poww(p, n / 2)) + poww(p, n)) % mod;
}
{
if (n == 0) return 1;
if (n & 1) return sum(p, (n - 1) / 2) * (1 + poww(p, (n + 1) / 2)) % mod;
else return (sum(p, n / 2 - 1) * (1 + poww(p, n / 2)) + poww(p, n)) % mod;
}
总的代码:
#include<iostream>
#include <stack>
using namespace std;
stack <int>s;
#define ll long long
const int mod = 9901;
ll poww(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) {
ans = (ans * a) % mod;
}
b >>= 1;
a = (a * a) % mod;
}
return ans % mod;
}
/*ll sum(ll a, ll b) {
ll fenzi = poww(a, b + 1) - 1;
ll niyuan = poww(a - 1, mod - 2);
return fenzi * niyuan % mod;
}*/
int sum(int p, int n)
{
if (n == 0) return 1;
if (n & 1) return sum(p, (n - 1) / 2) * (1 + poww(p, (n + 1) / 2)) % mod;
else return (sum(p, n / 2 - 1) * (1 + poww(p, n / 2)) + poww(p, n)) % mod;
}
int main() {
ll a, b;
cin >> a >> b;
if (a == 0) {
cout << 0 << endl; return 0;
}
ll ans = 1;
for (int i = 2; i <= a; i++) {
if (a % i == 0) {
int s = 0;
while (a % i == 0) {
a /= i;
s++;
}
ans = ans * sum(i, b * s) % mod;
}
}
if(a>1) ans = ans * sum(a, b) %mod;
cout << ans << endl;
}
#include <stack>
using namespace std;
stack <int>s;
#define ll long long
const int mod = 9901;
ll poww(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) {
ans = (ans * a) % mod;
}
b >>= 1;
a = (a * a) % mod;
}
return ans % mod;
}
/*ll sum(ll a, ll b) {
ll fenzi = poww(a, b + 1) - 1;
ll niyuan = poww(a - 1, mod - 2);
return fenzi * niyuan % mod;
}*/
int sum(int p, int n)
{
if (n == 0) return 1;
if (n & 1) return sum(p, (n - 1) / 2) * (1 + poww(p, (n + 1) / 2)) % mod;
else return (sum(p, n / 2 - 1) * (1 + poww(p, n / 2)) + poww(p, n)) % mod;
}
int main() {
ll a, b;
cin >> a >> b;
if (a == 0) {
cout << 0 << endl; return 0;
}
ll ans = 1;
for (int i = 2; i <= a; i++) {
if (a % i == 0) {
int s = 0;
while (a % i == 0) {
a /= i;
s++;
}
ans = ans * sum(i, b * s) % mod;
}
}
if(a>1) ans = ans * sum(a, b) %mod;
cout << ans << endl;
}