[luogu2312] 解方程 题面 具体代码 完

秦九韶公式

​ 看了上面这个之后大家应该都会了, 就是读入的时候边读入边取模, 从(1)(m)间将每一个数带进去试一下就可以了, 复杂度是(O(nm))的.

​ 古人的智慧是无穷的!!!

具体代码

#include <iostream>
#include <cstdio>
#define N 105
#define mod 1000000007
using namespace std;

int n, m, stack[100005], ans, top; 
long long a[N]; 

inline long long read()
{
    long long x = 0, w = 1;
    char c = getchar();
    while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
    while(c >= '0' && c <= '9') { x = (x * 10 + c - '0') % mod; c = getchar(); }
    //读入优化的时候边读边摸就可以了.
    return x * w;
}

inline bool get_ans(int num)
{
    long long res = a[n]; 
    for(int i = n - 1; i >= 0; i--) res = (res * num + a[i]) % mod;
    //这里是秦九韶算法, 再次说明, 古人的智慧是无穷的
    return !res; 
}

int main()
{
    n = read(); m = read();
    for(int i = 0; i <= n; i++) a[i] = read();
    for(int i = 1; i <= m; i++)
        if(get_ans(i)) { ans++; stack[++top] = i; }
    printf("%d
", ans);
    for(int i = 1; i <= ans; i++) printf("%d
", stack[i]); 
    return 0;
}

​ 古人的智慧是无穷的!!!