luogu2312 解方程 (数论,hash)
第一次外出学习讲过的题目,然后被讲课人的一番话惊呆了.
这个题,我想着当年全国只有十几个满分.....
然后他又说了句我考场A这道题时,用了5个模数
确实不好做想不到.
由于\(a\)非常大.转为以下思路.
设
\(f(x) = a_0+a_1x+a_2x^2+\cdots+a_nx^n\)
对于\(f(x) = 0\)则\(f(x)\%p = 0\)
\(f(x \% p) = 0\)
然后这里最好是选择素数.由于luogu数据较水,可以直接选择\(1e9 + 7\)水过.
判断会有误,所以这里选择两个数.
bzoj也有这道题,但是极其考验卡常技巧......
卡到吐血.还是没A,算了,不卡了....
记录:
#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#define ll long long
const int maxN = 100 + 7;
const int maxM = 1e6 + 7;
const int p = 1e9 + 7;
const int p1 = 20030327;
ll a[maxN],b[maxN];
ll n,m;
bool vis[maxM];
inline bool calc(int x)
{
long long sum = 0;
for(int i = n;i >= 1;i --)
{
sum = ( (long long) ( a[i] + sum ) * x ) % p;
}
sum = ( sum + a[0] ) % p;
return !sum;
}
inline void read(ll &x1,ll &x2) {
x1 = 0,x2 = 0;
int f = 1;
char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {
x1 = ( x1 * 10 + c - '0' ) % p;
x2 = ( x2 * 10 + c - '0') % p1;
c = getchar();
}
x1 *= f;
x2 *= f;
}
inline bool calc1(int x)
{
long long sum = 0;
for(int i = n;i >= 1;i --)
{
sum = ( (long long) ( b[i] + sum ) * x ) % p1;
}
sum = ( sum + b[0] ) % p1;
return !sum;
}
void print(int x)
{
if(x < 0)
{
putchar('-');
x = -x;
}
if(x > 9)
{
print(x / 10);
}
putchar(x % 10 + '0');
}
int main() {
scanf("%d%d",&n,&m);
for(int i = 0;i <= n;++ i)
read(a[i],b[i]);
int cnt = 0;
for(int i = 1;i <= m;++ i)
if(calc(i) && calc1(i)) ++ cnt,vis[i] = true;
printf("%d\n",cnt);
for(int i = 1;i <= m;++ i)
if(vis[i]) print(i),puts("");
return 0;
}
原文地址:https://www.cnblogs.com/tpgzy/p/9715362.html
时间: 2024-10-08 11:13:14