题目链接

https://www.luogu.org/problemnew/show/P2312

分析

这道题很毒啊,这么大的数。

但是如果多项式\(\sum_{i=0}^N a[i]*X^i=0\)则\(\sum_{i=0}^N a[i]*X^i \mod P=0\)

于是我们可以暴力膜一模,然后在\([1,m]\)中枚举就好了。但是呢,万一这个多项式的值是\(P\)的倍数,也会变成0,所以保险起见搞几个又大又质的数膜一膜就好了。

但是\(Exciting\)的是呢,我在洛谷上开O2能过,而BZOJ就不那么友好。

然后luogu题解提供一种减少枚举冗杂的方Fa。我们不是选多个数膜一模吗,如果在膜\(P_i\)的意义下已经不是\(0\)了,枚举其他的就没意义了。于是呢,我们先可以选出一个小点的模数\(P_x\),在\([1,P_x]\)中先枚举一遍,记录多项式值为0的是哪些。最后再枚举\([1,m]\),由于先前的限制,就会减少许多无用选择

然后多项式求值有个叫秦九韶算法的\(O(N)\)方法,不了解的可以看一看

https://www.cnblogs.com/Rye-Catcher/p/9260599.html

代码

我选择了两个数来做模数,较小的是23333,较大的是19260817

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#define ll long long
#define ri register int
using namespace std;
const int maxn=105;
const int inf=0x7fffffff;
const int p1=19260817,p2=71806291,p3=23333;
template <class T>inline void read(T &x){
x=0;int ne=0;char c;
while(!isdigit(c=getchar()))ne=c=='-';
x=c-48;
while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48;
x=ne?-x:x;return ;
}
int n,m;
ll a[maxn],c[maxn];
inline void input(int id){
a[id]=c[id]=0;int ne=0;char ch;
while(!isdigit(ch=getchar()))ne=ch=='-';
a[id]=c[id]=ch-48;
while(isdigit(ch=getchar())){
a[id]=((a[id]<<3)%p1+(a[id]<<1)+ch-48)%p1;
c[id]=((c[id]<<3)%p3+(c[id]<<1)+ch-48)%p3;
}
a[id]=ne?-a[id]:a[id];
c[id]=ne?-c[id]:c[id];return ;
}
int ans[1000005],tot=0;
bool ok[1000005];
inline bool pre_calc(ll u){
ll x=0;
for(ri i=n;i>=0;i--){
x=(x*u+c[i])%p3;
}
return x==0?1:0;
}
inline bool calc(ll u){
ll x=0;
for(ri i=n;i>=0;i--){
x=(x*u+a[i])%p1;
}
return x==0?1:0;
}
int main(){
read(n),read(m);
for(ri i=0;i<=n;i++){
input(i);
}
memset(ok,0,sizeof(ok));
for(ri i=1;i<=p3;i++){
if(pre_calc(1ll*i))ok[i]=1;
}
for(ri i=1;i<=m;i++)
if(ok[i%p3]&&calc(1ll*i)){
ans[++tot]=i;
}
printf("%d\n",tot);
for(ri i=1;i<=tot;i++)printf("%d\n",ans[i]);
return 0;
}

luogu题解P2312解方程--暴力模+秦九韶的更多相关文章

  1. 洛谷P2312 解方程(暴力)

    题意 题目链接 Sol 出这种题会被婊死的吧... 首先不难想到暴力判断,然后发现连读入都是个问题. 对于\(a[i]\)取模之后再判断就行了.注意判断可能会出现误差,可以多找几个模数 #includ ...

  2. 洛谷P2312 解方程题解

    洛谷P2312 解方程题解 题目描述 已知多项式方程: \[a_0+a_1x+a_2x^2+\cdots+a_nx^n=0\] 求这个方程在 \([1,m]\) 内的整数解(\(n\) 和 \(m\) ...

  3. 洛谷 P2312 解方程 题解

    P2312 解方程 题目描述 已知多项式方程: \[a_0+a_1x+a_2x^2+\cdots+a_nx^n=0\] 求这个方程在 [1,m][1,m] 内的整数解(\(n\) 和 \(m\) 均为 ...

  4. bzoj3751 / P2312 解方程

    P2312 解方程 bzoj3751(数据加强) 暴力的一题 数据范围:$\left | a_{i} \right |<=10^{10000}$.连高精都无法解决. 然鹅面对这种题,有一种常规套 ...

  5. [noip2014]P2312 解方程

    P2312 解方程 其实这道题就是求一个1元n次方程在区间[1, m]上的整数解. 我们枚举[1, m]上的所有整数,带进多项式中看看结果是不是0即可. 这里有一个技巧就是秦九韶算法,请读者自行查看学 ...

  6. codevs3732==洛谷 解方程P2312 解方程

    P2312 解方程 195通过 1.6K提交 题目提供者该用户不存在 标签数论(数学相关)高精2014NOIp提高组 难度提高+/省选- 提交该题 讨论 题解 记录   题目描述 已知多项式方程: a ...

  7. 洛谷 P2312 解方程 解题报告

    P2312 解方程 题目描述 已知多项式方程: \(a_0+a_1x+a_2x^2+\cdots+a_nx^n=0\)求这个方程在 \([1,m]\) 内的整数解(\(n\) 和 \(m\) 均为正整 ...

  8. P2312 解方程(随机化)

    P2312 解方程 随机化的通俗解释:当无法得出100%正确的答案时,考虑随机化一波,于是这份代码很大可能会对(几乎不可能出错). 比如这题:把系数都模一个大质数(也可以随机一个质数),然后O(m)跑 ...

  9. 洛谷 P2312 & bzoj 3751 解方程 —— 取模

    题目:https://www.luogu.org/problemnew/show/P2312 https://www.lydsy.com/JudgeOnline/problem.php?id=3751 ...

随机推荐

  1. Hbuilder用自有证书打包 ios App上架AppStore流程

    最近在用Hbuilder做跨平台开发,经过一番研究终于在苹果商店上架成功了一款产品!这款产品就很简单,直接用hbuilder打包好,然后上传到商店即可.这里参照ios app提交应用商店 这篇文章结合 ...

  2. UIGestureRecongnizer 手势拦截 对于特殊需求很有用

    手势其实也有代理方法的,通过代理方法可以做到更多关于手势方面的功能 比如在下面的方法中,如果是UIButton的点击就阻止手势的点击事件. // called before touchesBegan: ...

  3. AES对称加密解密类

    import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.spec.Se ...

  4. NLP之电影评分数据的情感分析

    1.基于词袋模型的逻辑回归情感分类 # coding: utf-8 import re import numpy as np import pandas as pd from bs4 import B ...

  5. python 生成词云

    1.知识点 """ WordCloud参数讲解: font_path表示用到字体的路径 width和height表示画布的宽和高 prefer_horizontal可以调 ...

  6. Restful 风格

    大家在做Web开发的过程中,method常用的值是get和post. 可事实上,method值还可以是put和delete等等其他值.既然method值如此丰富,那么就可以考虑使用同一个url,但是约 ...

  7. [PySpark] RDD programming on a large file

    重难点 一.parallelize 方法 一般来说,Spark会尝试根据集群的状况,来自动设定slices的数目.然而,你也可以通过传递给parallelize的第二个参数来进行手动设置. data_ ...

  8. 【JVM学习笔记】系统类加载器

    可以通过“java.system.class.loader"属性指定系统类加载器 默认情况下,该属性值为空: public class Test { public static void m ...

  9. How to remove duplicate lines in a large text file?

    How would you remove duplicate lines from a file that is  much too large to fit in memory? The dupli ...

  10. C# CRC16 modbus

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...