bzoj3751 / P2312 解方程

P2312 解方程

bzoj3751(数据加强)

暴力的一题

数据范围:$\left | a_{i} \right |<=10^{10000}$。连高精都无法解决。

然鹅面对这种题,有一种常规套路:取模

显然方程两边同时$mod$结果不会改变

于是我们牺牲了正确性使答案允许我们暴力枚举。

为了提高正确性我们可以$mod$多个较小质数进行判断

至于代入解方程,用秦九韶算法

(bzoj数据真的强,压线过的)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cctype>
 5 #define re register
 6 using namespace std;
 7 char gc(){
 8     static char buf[100000],*p1=buf,*p2=buf;
 9     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
10 }
11 const int mod[6]={30011,11261,14843,19997,10007,21893};//从bzoj讨论版拿的一组(%%%mcfx)
12 int n,m,ans,c[1000002],tp,a[7][103];
13 void read(int i){
14     char c=gc();bool f=1;
15     for(int j=0;j<6;++j) a[j][i]=0;
16     while(!isdigit(c)) f=(f&&c!='-'),c=gc();
17     while(isdigit(c)){
18         for(int j=0;j<6;++j)
19             a[j][i]=((a[j][i]<<3)+(a[j][i]<<1)+(c^48))%mod[j];
20         c=gc();
21     }
22     if(!f) for(int j=0;j<6;++j) a[j][i]=-a[j][i];
23 }
24 void write(int x){
25     if(x<0) putchar('-'),x=-x;
26     if(x>9) write(x/10);
27     putchar(x%10|48);
28 }
29 int F(int *p,int x,int mod){
30     int res=0;
31     for(re int i=n;i>=0;--i) res=1ll*(1ll*res*x+p[i])%mod;
32     return !res;
33 }//秦九韶算法解方程
34 int main(){
35     scanf("%d%d",&n,&m);
36     for(re int i=0;i<=n;++i) read(i);
37     
38     for(re int j=0;j<6;++j){
39         for(re int i=1;i<mod[j];++i)
40             if(F(a[j],i,mod[j]))
41                 for(re int u=i;u<=m;u+=mod[j]) ++c[u];
42     }
43     for(re int i=1;i<=m;++i)
44         if(c[i]==6)
45             ++ans,c[++tp]=i;
46     write(ans),putchar('\n');
47     for(re int i=1;i<=tp;++i) write(c[i]),putchar('\n');
48     return 0;
49 }
View Code

 

posted @ 2018-10-29 16:02  kafuuchino  阅读(127)  评论(0编辑  收藏  举报