洛谷P4598 解高次方程,数论

题意:

给出序列 [ a 0 , a 2 , a 3 , . . . , a n ] [a_{0},a_{2},a_{3},...,a_{n}] [a0,a2,a3,...,an],求方程
a 0 + a 1 x + a 2 x 2 + . . . + a n x n = 0 a_{0}+a_{1}x+a_{2}x^{2}+...+a_{n}x^{n}=0 a0+a1x+a2x2+...+anxn=0
的所有有理数解

Solution:

x = p q x=\frac{p}{q} x=qp是一个解,并且 g c d ( p , q ) = 1 gcd(p,q)=1 gcd(p,q)=1,代入得到
∑ i = 0 n a i ( p q ) i = 0 \sum_{i=0}^{n}a_{i}(\frac{p}{q})^{i}=0 i=0nai(qp)i=0
通分得到
∑ i = 0 n a i p i q n − i = 0 \sum_{i=0}^{n}a_{i}p^{i}q^{n-i}=0 i=0naipiqni=0
在模 p p p时,有
∑ i = 0 n a i p i q n − i ≡ 0   ( m o d   p ) ⇒ a 0 q n ≡ 0   ( m o d   p ) \sum_{i=0}^{n}a_{i}p^{i}q^{n-i} \equiv 0\ (mod\ p)\Rightarrow a_{0}q^{n}\equiv 0\ (mod\ p) i=0naipiqni0 (mod p)a0qn0 (mod p)
在模 q q q时,有
∑ i = 0 n a i p i q n − i ≡ 0   ( m o d   q ) ⇒ a n p n ≡ 0   ( m o d   q ) \sum_{i=0}^{n}a_{i}p^{i}q^{n-i} \equiv 0\ (mod\ q)\Rightarrow a_{n}p^{n}\equiv 0\ (mod\ q) i=0naipiqni0 (mod q)anpn0 (mod q)
由于 g c d ( p , q ) = 1 gcd(p,q)=1 gcd(p,q)=1,于是一定存在
p ∣ a 0 , q ∣ a n p|a_{0},q|a_{n} pa0,qan
只需要枚举 a 0 , a n a_{0},a_{n} a0,an的因子分别作为 p , q p,q p,q,然后检查原式是否成立即可。原式可能非常大,一种检验的方法是在多模数下运算值都为 0 0 0

// #include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

using ll=long long;
const int N=105,inf=0x3fffffff;
const long long INF=0x3fffffffffffffff,mod1=1e9+7,mod2=1e9+9;

template<class T>
T abs(T x){return x>=0?x:-x;}

int n,a[N];
vector<pair<int,int>>ans;

ll qpow(ll a,ll b,ll mod)
{
    ll ret=1,base=a;
    while(b)
    {
        if(b&1) ret=ret*base%mod;
        base=base*base%mod;
        b>>=1;
    }
    return ret;
}

ll qm(ll x,ll mod)
{
    return (x%mod+mod)%mod;
}

bool check(int p,int q,ll mod)
{
    ll ret=0;
    for(int i=0;i<=n;i++) ret=(ret+qm(1ll*a[i]*qpow(p,i,mod)%mod*qpow(q,n-i,mod)%mod,mod))%mod;
    return ret==0;
}

void solve(int p,int q)
{
    int tmp=__gcd(abs(p),abs(q));
    p/=tmp; q/=tmp;
    if(check(p,q,mod1)&&check(p,q,mod2)) ans.push_back(make_pair(p,q));
}

int main()
{
    #ifdef stdjudge
        freopen("in.txt","r",stdin);
    #endif
    cin>>n;
    for(int i=0;i<=n;i++)
    {
        cin>>a[i];
        if(a[i]==0) n--,i--,ans.push_back(make_pair(0,1));
    }
    for(int i=1,limit1=sqrt(abs(a[0]));i<=limit1;i++)
    {
        if(a[0]%i) continue;
        for(int j=1,limit2=sqrt(abs(a[n]));j<=limit2;j++)
        {
            if(a[n]%j||__gcd(i,j)!=1) continue;
            solve(i,j); solve(-i,j);
            solve(a[0]/i,j); solve(-a[0]/i,j);
            solve(i,a[n]/j); solve(-i,a[n]/j);
            solve(a[0]/i,a[n]/j); solve(-a[0]/i,a[n]/j);
        }
    }
    for(auto i:ans)
    {
        if(i.first<0&&i.second<0) i.first=-i.first,i.second=-i.second;
        else if(i.second<0) i.first=-i.first,i.second=-i.second;
    }
    sort(ans.begin(),ans.end(),[&](pair<int,int>&x,pair<int,int>&y){
        return 1.0*x.first/x.second<1.0*y.first/y.second;
    });
    ans.erase(unique(ans.begin(),ans.end(),[&](pair<int,int>&x,pair<int,int>&y){
        return fabs(1.0*x.first/x.second-1.0*y.first/y.second)<1e-8;
    }),ans.end());
    cout<<ans.size()<<endl;
    for(auto i:ans)
    {
        if(i.first<0&&i.second<0) i.first=-i.first,i.second=-i.second;
        else if(i.second<0) i.first=-i.first,i.second=-i.second;
        if(i.second>1) printf("%d/%d\n",i.first,i.second);
        else printf("%d\n",i.first);
    }
    return 0;
}

你可能感兴趣的:(算法)