zoukankan      html  css  js  c++  java
  • [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;
    }
    
    

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

  • 相关阅读:
    hdu 1251(字典树)(3种方法)
    HDU 2203(KMP算法)
    九度oj 题目1335:闯迷宫
    poj3894 bfs+记录路径
    状压dp--P2704
    状压dp--洛谷P2622
    动态规划--牛客多校number
    完全背包
    01背包--hdu
    莫比乌斯反演模板--Gym 101982B
  • 原文地址:https://www.cnblogs.com/ztlztl/p/10447134.html
Copyright © 2011-2022 走看看