问题描述
蒜头君在求解一个 n 元的高次方程:
假设未知数 1≤xi≤M,i=1…n。你能帮蒜头君算出这个方程的整数解个数吗?
输入格式
第一行输入一个整数 n(1≤n≤4)。
第二行输入一个整数 M(1≤M≤150)。
第 3 行到第 n+2 行,每行输入两个整数,分别表示 k(|K|<=20)pi(1<=pi<=4)。两个整数之间用一个空格隔开。
输出格式
输出一行,输出一个整数,表示方程的整数解的个数
样例输入
3
100
1 2
-1 2
1 2
样例输出
104
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MANY=3500000;
const int N=11;
struct zk { int k,p; }; zk part1[N],part2[N];
int n,m,half,ans,top1,top2; int reach1[MANY],reach2[MANY];
void read()
{
int i;
scanf("%d%d",&n,&m);
half=n/2;
for (i=1;i<=half;i++)
scanf("%d%d",&part1[i].k,&part1[i].p);
for (i=1;i<=n-half;i++)
scanf("%d%d",&part2[i].k,&part2[i].p);
return;
}
int quick(int p,int q)
{
int temp;
if (q==1) return p;
temp=quick(p,q/2);
if (q%2==0)
return temp*temp;
else
return temp*temp*p;
}
void dfs1(int step,int now)
{
int i;
if (step==half+1)
{
reach1[++top1]=now;
return;
}
for (i=1;i<=m;i++)
dfs1(step+1,now+quick(i,part1[step].p)*part1[step].k);
return;
}
void dfs2(int step,int now)
{
int i;
if (step==n-half+1)
{
reach2[++top2]=now;
return;
}
for (i=1;i<=m;i++)
dfs2(step+1,now+quick(i,part2[step].p)*part2[step].k);
return;
}
void work()
{
int i=1,j=top2,last1,last2;
sort(reach1+1,reach1+1+top1);
sort(reach2+1,reach2+1+top2);
for (i=1;i<=top1;i++)
{
while (reach1[i]+reach2[j]>0&&j>0) j--;
if (j<=0) break;
if (reach1[i]+reach2[j]!=0) continue;
last1=1; last2=1;
while (reach1[i+1]==reach1[i]&&i<top1) { i++; last1++; }
while (reach2[j-1]==reach2[j]&&j>1) { j--; last2++; }
ans=ans+last1*last2;
}
printf("%d",ans);
return;
}
int main()
{
read();
dfs1(1,0);
dfs2(1,0);
work();
return 0;
}