zoukankan      html  css  js  c++  java
  • 【BZOJ】3751: [NOIP2014]解方程【秦九韶公式】【大整数取模技巧】

    3751: [NOIP2014]解方程

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 4856  Solved: 983
    [Submit][Status][Discuss]

    Description

     已知多项式方程:

    a0+a1*x+a2*x^2+...+an*x^n=0
    求这个方程在[1,m]内的整数解(n和m均为正整数)。
     

    Input

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

    Output

     第一行输出方程在[1,m]内的整数解的个数。

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

    Sample Input

    2 10
    2
    -3
    1

    Sample Output

    2
    1
    2

    HINT

    对于100%的数据,0<n≤100,|ai|≤1010000,an≠0,m≤1000000。


    Solution

    暴力枚举即可,难点主要是读入和快速计算。

    大整数读入解决方法是mod大~质数,题解大佬说mod一个可能会出问题所以有时候要mod几个~

    快速计算的话就是秦九韶公式了QAQ,很好理解的,不过这道题要控制mod的次数!不然多100次都t了QAQ!

    Code

    #include<bits/stdc++.h>
    #define LL long long
    #define mod 1000000007
    using namespace std;
    
    inline LL read() {
        LL x = 0; int t = 1; char ch = getchar();
        while(ch > '9' || ch < '0')    { if(ch == '-') t = -1; ch = getchar(); }
        while(ch >= '0' && ch <= '9') { x = ((x << 1) % mod + (x << 3) % mod + ch - '0') % mod;    ch = getchar(); }
        return x * t;
    }
    
    LL a[105];
    int n, m, ans[1000005], tot;
    inline bool cal(int x) {
        LL res = a[n];
        for(int i = n - 1; i >= 0; i --)
            res = (res * x % mod + a[i]);
        return res == 0;
    }
    
    int main() {
        n = read(); m = read();
        for(int i = 0; i <= n; i ++)    a[i] = read();
        for(int i = 1; i <= m; i ++)
            if(cal(i))    ans[++tot] = i;
        printf("%d
    ", tot);
        for(int i = 1; i <= tot; i ++)
            printf("%d
    ", ans[i]);
        return 0;
    }
  • 相关阅读:
    剑指OFFER----面试题17- 打印从1到最大的n位数
    剑指OFFER----面试题16. 数值的整数次方
    剑指OFFER----面试题15. 二进制中1的个数
    剑指OFFER----面试题14- II. 剪绳子II
    07 多层if判断
    08 while循环
    06 if 流程控制
    03 身份运算符、逻辑运算符
    04 位运算符、运算符优先级
    02 赋值运算符、成员运算符
  • 原文地址:https://www.cnblogs.com/wans-caesar-02111007/p/9873166.html
Copyright © 2011-2022 走看看