快速幂和约数和为什么不对

P1593 因子和

hope_echo @ 2024-03-17 12:31:22

#include <iostream>
using namespace std;
typedef long long ll;
ll qmi(ll m, ll k)
{
    ll res = 1 , t = m;
    while (k)
    {
        if (k&1) res = res * t ;
        t = t * t ;
        k >>= 1;
    }

    return res;
}
int main()
{   
    ll sums = 0;
    ll a, b;
    cin >> a >> b;
    ll res = qmi(a,b);

    for(int j = 1; j <= res / j; j++)
        {
            if(res % j == 0)
            {
                sums += j;
            if(res != j * j) sums += res /j;
            }
        }
    cout << sums % 9901;
}

by Ew_Cors @ 2024-03-17 12:35:16

你为什么不取模?


by hope_echo @ 2024-03-18 16:21:03

@Ew_Cors ```cpp

include <iostream>

using namespace std;

define mod 9901

typedef long long ll; int qmi(int m, int k) { int res = 1 % mod, t = m; while (k) { if (k&1) res = res t % mod; t = t t % mod; k >>= 1; } return res % mod; }

int main() {
ll sums = 0; ll a, b; cin >> a >> b; ll res = qmi(a,b);

for(int j = 1; j <= res / j; j++)
    {
        if(res % j == 0)
        {
            sums += j;
        if(res != j * j) sums += res /j;
        }
    }
cout << sums % 9901;

}


by hope_echo @ 2024-03-18 16:21:56

@Ew_Cors

#include <iostream>
using namespace std;
#define mod 9901
typedef long long ll;
int qmi(int m, int k)
{
    int res = 1 % mod, t = m;
    while (k)
    {
        if (k&1) res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res % mod;
}

int main()
{   
    ll sums = 0;
    ll a, b;
    cin >> a >> b;
    ll res = qmi(a,b);

    for(int j = 1; j <= res / j; j++)
        {
            if(res % j == 0)
            {
                sums += j;
            if(res != j * j) sums += res /j;
            }
        }
    cout << sums % 9901;
}

by hope_echo @ 2024-03-18 16:22:31

@Ew_Cors 取了还是错


by _TCR_ @ 2024-03-24 10:11:14

@lxk20020629 加9901再取模排除ans<0


|