NOIP 2014 解方程

洛谷 P2312 解方程

洛谷传送门

JDOJ 2890: [NOIP2014]解方程 D2 T3

JDOJ传送门

Description

已知多项式方程:

a0+a1x+a2x2+⋯+anxn=0

求这个方程在[1, m]内的整数解( n和 m均为正整数)。

Input

输入共 n+2行。
第一行包含 2个整数 n、m,每两个整数之间用一个空格隔开。
接下来的 n+1行每行包含一个整数,依次为 a0,a1,a2,…,an。

Output

第一行输出方程在 [1, m]内的整数解的个数。
接下来每行一个整数,按照从小到大的顺序依次输出方程在 [1, m]内的一个整数解。

Sample Input

Input I: 2 10 1 -2 1 Input II: 2 10 2 -3 1 Input III: 2 10 1 3 2

Sample Output

Output I: 1 1 Output II: 2 1 2 Output III: 0

HINT

【数据说明】
对于 30%的数据,0< n ≤2, |ai|≤100,an≠0, m≤100;
对于 50%的数据,0< n ≤100,|ai| ≤10100,an≠0,m≤100;
对于 70%的数据,0< n ≤100,|ai| ≤1010000,an≠0,m≤10000;
对于 100%的数据,0< n ≤100,|ai| ≤1010000,an≠0,m≤1000000。

Source

NOIP2014提高组

题解:

高一重点实验班同学(比如\(JDFZ\)高一6班)和高二同学应该很熟悉这个式子(滑稽.jpg)

这就是秦九韶算法的裸题嘛!

(为了照顾高一非重点实验班同学和高一以下同学,友情提供秦九韶算法的讲解:)

浅谈秦九韶算法

但是真的这么简单么?

注意数据范围,这一看就是高精度的题。但是可以不用高精做(高精要狗命)可以在读入和计算的时候不断取模。

\(f(x)=0\),那么肯定会有\(f(x)mod \,\,\,p=0\)\(p\)最好是质数。

这种方法很玄学,正确性也无法保证(多试试就好了)。

代码:

#include<cstdio>
#define int long long
using namespace std;
const int maxn=110;
const int mod=1e9+7;
int a[maxn],ans,n,m,b[1000010],tot;
bool flag;
char *p1,*p2,buf[100000];
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
inline int read()
{
    int x=0,f=1;
    char ch=nc();
    while(ch<48){if(ch=='-')f=-1;ch=nc();}
    while(ch>47)    x=((((x<<2)+x)<<1)+ch-48)%mod,ch=nc();
    return x*f;
}
inline bool check(int x)
{
    int ret=a[n];
    for(register int i=n-1;i>=0;i--)
        ret=(ret*x+a[i])%mod;
    return ret?0:1;
}
inline void print(int x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        print(x/10);
    putchar(x%10+48);
}
signed main()
{
    n=read(),m=read();
    for(register int i=0;i<=n;i++)
        a[i]=read();
    for(register int i=1;i<=m;i++)
        if(check(i))
        {
            flag=1;
            ans++;
            b[++tot]=i;
        }
    if(!flag)
    {
        print(0);
        return 0;
    }
    else
    {
        print(ans);
        puts("");
        for(register int i=1;i<=tot;i++)
            print(b[i]),puts("");
    }
    return 0;
}
posted @ 2019-11-02 16:41  Seaway-Fu  阅读(427)  评论(0编辑  收藏  举报