poj 1845 【数论:逆元,二分(乘法),拓展欧几里得,费马小定理】

时间:2023-03-09 07:00:41
poj 1845 【数论:逆元,二分(乘法),拓展欧几里得,费马小定理】

POJ 1845

题意不说了,网上一大堆。此题做了一天,必须要整理一下了。

poj 1845 【数论:逆元,二分(乘法),拓展欧几里得,费马小定理】

刚开始用费马小定理做,WA。(poj敢说我代码WA???)(以下代码其实都不严谨,按照数据要求A是可以等于0的,那么结果自然就是0了,需要特判一下,但是poj好像没有为0的数据,能AC。先不改了。)

后来看了好多人的博客,发现很少用费马小定理写的,或者写的代码我看不下去。。就先用那个什么二分等比数列写了一下。

过程也不说了,很多博客都说了。(【1】【2】):

 #include<iostream>
#include<cmath>
#define mod 9901
using namespace std;
typedef long long ll;
const int maxn=;
ll p[maxn],r[maxn];
int tot; void Factor(ll n)//质因数分解
{
tot=;
int m=sqrt(n+0.5);
for (int i=;i<=m;i++)
if (n%i==){
p[++tot]=i;
r[tot]=;
while(n%i==){
++r[tot];
n/=i;
}
}
if(n>){
p[++tot]=n;
r[tot]=;
}
} ll qpow(ll a,ll b)//快速幂
{
ll r=;
while(b)
{
if(b&) r=r*a%mod;
b>>=;
a=a*a%mod;
}
return r;
} ll sum(int p,int n)//二分等比数列
{
if(n==)
return ;
if(n&)
return (sum(p,n/)%mod*(+qpow(p,n/+))%mod)%mod;
else
return (sum(p,n/-)%mod*(+qpow(p,n/+))%mod+qpow(p,n/))%mod;
} int main()
{
ll A,B;
cin>>A>>B;
Factor(A);
ll ans=;
for (int i=;i<=tot;i++)
{
ans=(ans%mod*sum(p[i],r[i]*B)%mod)%mod;
}
cout<<ans<<endl;
return ;
}

后来,又看见有人用二分乘法写:

 #include<iostream>
#include<cmath>
#define mod 9901
using namespace std;
typedef long long ll;
const int maxn = ;
ll p[maxn], r[maxn];
int tot; void Factor(ll n)
{
tot = ;
int m = sqrt(n + 0.5);
for (int i = ; i <= m; i++)
if (n%i == ) {
p[++tot] = i;
r[tot] = ;
while (n%i == ) {
++r[tot];
n /= i;
}
}
if (n>) {
p[++tot] = n;
r[tot] = ;
}
} ll muti(ll a, ll b, ll m)//二分乘法???求教!!!
{
ll ans = ;
a %= m;
while (b)
{
if (b & )
{
ans = (ans + a) % m;
b--;
}
b >>= ;
a = (a + a) % m;
}
return ans;
} ll qpow(ll a, ll b, ll m)
{
ll ans = ;
a %= m;
while (b)
{
if (b & ) {
ans = muti(ans, a, m);
b--;
}
b >>= ;
a = muti(a, a, m);
}
return ans;
} int main()
{
ll A, B;
cin >> A >> B; Factor(A);
ll ans = ;
for (int i = ; i <= tot; i++)
{
ll m = (p[i] - )*mod;
ans *= (qpow(p[i], r[i] * B + , m) - + m) / (p[i] - );
ans %= mod;
}
cout << ans << endl;
return ;
}

说实话我现在还不懂为什么要二分乘法?啥子是二分乘法?这是ACdreamer(【1】)(应该是个大牛吧!!!)里面写的,后来也到过其他博客写过,代码都极其相似。ACdreamer说mb会可能会超int范围,所以快速幂时二分乘法。???我现在也很懵逼,mb超int怎么了?不超long long不就行了?难道超int模会出错?求解释啊!!!

后来,又看到了有人用拓展欧几里得:

 #include<iostream>
#include<cmath>
#define mod 9901
using namespace std;
typedef long long ll;
const int maxn=;
ll p[maxn],r[maxn];
int tot; void Factor(ll n)
{
tot=;
int m=sqrt(n+0.5);
for (int i=;i<=m;i++)
if (n%i==){
p[++tot]=i;
r[tot]=;
while(n%i==){
++r[tot];
n/=i;
}
}
if(n>){
p[++tot]=n;
r[tot]=;
}
} ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==){
x=,y=;
return a;
}
int ret=exgcd(b,a%b,x,y);
int t=x;
x=y;
y=t-a/b*y;
return ret;
} ll inv(ll n)
{
n%=mod;
ll a=n,b=mod,x,y;
exgcd(a,b,x,y);
x=(x%mod+mod)%mod;
return x;
} ll qpow(ll a,ll b)
{
ll r=;
while(b)
{
if(b&) r=a*r%mod;
b>>=;
a=a*a%mod;
}
return r;
} int main()
{
ll A,B;
cin>>A>>B; Factor(A);
ll ans=;
for (int i=;i<=tot;i++)
{
if(p[i]%mod==){//!!!
ans*=(r[i]*B+)%mod;
ans%=mod;
}else{
ans*=((qpow(p[i],r[i]*B+)-+mod)%mod*inv(p[i]-))%mod;
ans%=mod;
}
}
cout<<ans<<endl;
return ;
}

能拓展欧几里得那么费马小定理也应该差不多啊!!!

所以回头我又写了费马小定理的:

 #include<iostream>
#include<cmath>
#define mod 9901
using namespace std;
typedef long long ll;
const int maxn=;
ll p[maxn],r[maxn];
int tot; void Factor(ll n)
{
tot=;
int m=sqrt(n+0.5);
for (int i=;i<=m;i++)
if (n%i==){
p[++tot]=i;
r[tot]=;
while(n%i==){
++r[tot];
n/=i;
}
}
if(n>){
p[++tot]=n;
r[tot]=;
}
} ll qpow(ll a,ll b)
{
ll r=;
a%=mod;
while(b)
{
if(b&) r=a*r%mod;
b>>=;
a=a*a%mod;
}
return r;
} int main()
{
ll A,B;
cin>>A>>B; Factor(A);
ll ans=;
for (int i=;i<=tot;i++)
{ if(p[i]%mod==){//!!!
ans*=(r[i]*B+)%mod;
ans%=mod;
}else{
ans*=((qpow(p[i],r[i]*B+)-+mod)%mod*qpow(p[i]-,mod-)%mod)%mod;//直接用费马小定理求逆元,qpow(p[i]-1,mod-2);
ans%=mod;
}
}
cout<<ans<<endl;
return ;
}

直到这时,我才明白我以前写费马为什么错了。因为当素因子p[i]%mod==1时,求逆元的结果是错的,或者就像有的博客说这时是求不出逆元的。因为这时qpow(p[i]-1,mod-2)的结果一定为0。因为当求(1+p^1+p^2+p^3...+p^r)%mod时,由于p%mod==1,所以每项%mod都是1,结果就是r+1。也就是上面两个代码特判的结果:r[i]*B+1。

最后提供一个数据: 59407 3

我的结果是:4

没有特判的话,结果会是0。这个就是我找到的第一个大于9901且%9901=1的质数。

感谢网上的广大博客,终于让我看了一天后悟出了这一点。。。。。

本来想巩固一下逆元的概念,结果ACdreamer里面看到的这道题弄了一天。。。

希望以后能看到这个文章的朋友们能够给我解释一下为什么要二分乘法?不理解啊!先谢谢!!

谢谢这些博客们,有可能没有一一列出,你们至多至少都给了我一点启发和帮助!也希望你们写博客的时候在关键能多说几句>_<.

【1】http://blog.csdn.net/acdreamers/article/details/8220787

【2】http://blog.csdn.net/lyy289065406/article/details/6648539

【3】http://blog.csdn.net/a601025382s/article/details/12233213

【4】http://blog.csdn.net/hlmfjkqaz/article/details/9735143

【5】http://www.cnblogs.com/mjy0724/p/4371752.html

【6】http://m.blog.csdn.net/kbdwo/article/details/9798193

【7】http://www.cnblogs.com/linyujun/p/5194184.html