刷题集--方程的解数

已知一个n元高次方程:
k1x1^p1+k2x2^p2+...+knxn^pn=0
其中:x1, x2, …,xn是未知数,k1,k2,…,kn是系数,p1,p2,…pn是指数。且方程中的所有数均为整数。
假设未知数1≤ xi ≤M, i=1,,,n,求这个方程的整数解的个数。

1<=n<=6,1<=m<=150

折半枚举,搜出的东西拿hash存。

#pragma GCC optimize(3,"inline","Ofast")
#include<bits/stdc++.h>
using namespace std;
int n,m,tp;
int k[20],p[20],res[151][40],ans=0,vis[4000000],h[4000000];
int qpow(int a,int b)
{return res[a][b];}
int fk(int x)
{
    static int y;
    y=1LL*abs(x)*233%3926081;
    while(h[y]&&h[y]!=x)++y;
    h[y]=x;
    return y;
}
void dfs1(int pos,int x)
{
    if(pos>tp){vis[fk(x)]++;return;}
    for(int i=1;i<=m;i++)
        dfs1(pos+1,k[pos]*qpow(i,p[pos])+x);
}
void dfs2(int pos,int x)
{
    if(pos>n){ans+=vis[fk(x)];return;}
    for(int i=1;i<=m;i++)
        dfs2(pos+1,x-k[pos]*qpow(i,p[pos]));
}
void init()
{
    int nw,M;M=INT_MAX;
    for(int i=1;i<=m;i++)
    {
        nw=i;
        for(int j=1;j<39;j++)
        {
            res[i][j]=nw;
            if(1LL*nw*i>M)break;
            nw*=i;
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&k[i],&p[i]);
    init(); 
    tp=n/2;
    if(n==1)vis[0]=1;
    else dfs1(1,0);
    dfs2(tp+1,0);
    cout<<ans;
}

猜你喜欢

转载自blog.csdn.net/caoyang1123/article/details/82619820