【题解】洛谷P2312 解方程(高精 数学)

一道非常玄学的问题。

如果想要拿到全分,有时候还要靠一点运气

由于我们发现系数非常非常大,所以我们用字符串读入,然后将其转化为数字类型,存到数组里。这里为了通过这道题,我们开三个数组,其意义是相同的,但在计算过程中分别对三个不同的数取模。然后我们从1开始枚举到模数1,利用秦九韶公式,按照题目要求将枚举的数带入计算 如果结果是0,就在mod1后扩展许多类似1-mod1的数组,计算答案,连续两次后我们对三个不同的数去过模了,如果现在答案还是0,证明这个答案很有可能是正确的,记录下来就好了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
int n,m,x;
int a1[110];
int a2[110];
int a3[110];
int jie[1000010];
int qw=0;
const int mod1=73771;
const int mod2=10707707;
const int mod3=73737579;
//bool f(int x,int mod,int a[])
//{
//	qw=0;
//	for(int i=n;i>=1;i--)
//	{
//		qw=((ll)(qw+a[i])*x%mod)%mod;
//	//	x=(ll)x*w%mod;
//	}
//	qw=(qw+a[0])%mod;
//	if(qw==0) return true;
//	else return false;
//}
int main()
{
	//freopen("equation.in","r",stdin);
	//freopen("equation.out","w",stdout);
	scanf("%d%d",&n,&m);
	int ans=0;
	int tot=0,w=0;
	for(int i=0;i<=n;i++)
	{
		char s[11000];
		cin>>s;
		int sum=0;
		int sum2=0;
		int sum3=0;
		int q=1;
		int q2=1;
		int q3=1;
		int pd=1;
		int len=strlen(s);
		for(int j=len-1;j>=0;j--)
		{
			if(s[j]=='-') 
			{
				pd=-1;
				continue;
			}
			sum=(sum+(ll)(s[j]-'0')*q%mod1)%mod1;
			sum2=(sum2+(ll)(s[j]-'0')*q2%mod2)%mod2;
			sum3=(sum3+(ll)(s[j]-'0')*q3%mod3)%mod3;
			q=(ll)q*10%mod1;
			q2=(ll)q2*10%mod2;
			q3=(ll)q3*10%mod3;
		}
		a1[tot]=sum*pd;
		a2[tot]=sum2*pd;
		a3[tot]=sum3*pd;
		tot++;
	//	cout<<sum<<endl;
	}
	
	int qww=0;
	for(int i=1;i<=mod1;i++)
	{
		ll tmp=0;
		for(int j=n;j>=0;j--) tmp=(tmp*i+a1[j])%mod1;
		if(tmp==0)
		{
			for(int j=i;j<=m;j+=mod1) 
			{
				for(int k=n;k>=0;k--)
				{
					tmp=(tmp*j+a2[k])%mod2;
					if(tmp==0)
					{
						for(int k=n;k>=0;k--)
						{
							tmp=(tmp*j+a3[k])%mod3;
							if(tmp==0) 
							{
								jie[++qww]=j;
							}
						}
					}
				}
				tmp=0;
			}
		}
	}
	sort(jie+1,jie+qww+1);
	printf("%d\n",qww);
	for(int i=1;i<=qww;i++)
	{
		printf("%d\n",jie[i]);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Rem_Inory/article/details/81609782