卧薪尝胆,厚积薄发。
NOIP2014D2T3 解方程
Date: Mon Sep 10 07:12:57 CST 2018 In Category: NoCategory

Description:

已知多项式方程: $a_0+a_1x+a_2x^2+\cdots+a_nx^n=0$
求这个方程在 $[1,m] $ 内的整数解。
$1\le n\le 100,1\le m \le 10^6,1\le a_i\le 10^{10000}$

Solution:

首先 $x$ 只能枚举,这样我们就已经有了 $10^6$ 的复杂度,还有大概 $100$ 左右可以用,发现刚好 $n$ 够用,于是 $a$ 必须 $O(1)$ 计算,肯定不能把 $a$ 存下来,由于最后是判 $0$ ,所以把 $a$ 在模意义下处理就行了。其实就是 $hash$ 。

Code:


#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define MOD1 998244353
#define MOD2 1000000007
int n,m;
#define MAXN 110
typedef long long ll;
ll a1[MAXN],a2[MAXN];
int ans[1000010];
inline void read(int k)
{
register char c = getchar();
a1[k] = a2[k] = 0;
int f = 1;
while(!isdigit(c))
{
if(c == '-')f = -1;
c = getchar();
}
while(isdigit(c))
{
a1[k] = ((a1[k] << 1) + (a1[k] << 3) + (c - '0') * f + MOD1) % MOD1;
a2[k] = ((a2[k] << 1) + (a2[k] << 3) + (c - '0') * f + MOD2) % MOD2;
c = getchar();
}
return;
}
bool get(ll k)
{
ll res1 = 0,res2 = 0;
for(int i = n;i >= 0;--i)
{
res1 = (res1 * k + a1[i]) % MOD1;
res2 = (res2 * k + a2[i]) % MOD2;
}
if(res1 == 0 && res2 == 0)return true;
else return false;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 0;i <= n;++i)read(i);
for(ll i = 1;i <= m;++i)
{
if(get(i))ans[++ans[0]] = i;
}
cout << ans[0] << endl;
for(int i = 1;i <= ans[0];++i)
{
cout << ans[i] << endl;
}
return 0;
}
In tag: 字符串-hash
Copyright © 2020 wjh15101051
ღゝ◡╹)ノ♡