解方程

时间:2019-11-11
本文章向大家介绍解方程,主要包括解方程使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

链接:https://ac.nowcoder.com/acm/problem/16499
来源:牛客网

题目描述

已知多项式方程:
a0+a1x+a2x2+...+anxn=0
求这个方程在[1, m]内的整数解(n和m均为正整数)。


输入描述:

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

输出描述:

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

输入

2 10
1
-2
1

输出

1
1
示例2

输入

2 10
2 
-3
1

输出

2
1
2
示例3

输入

2 10
1
3
2

输出

0

备注:

对于30%的数据,0<n>i|≤100,an≠0,m≤100;
对于50%的数据,0<n>i|≤10100,an≠0,m≤100;
对于70%的数据,0<n>i|≤1010000,an≠0,m≤10000;
对于100%的数据,0<n>i|≤1010000,an≠0,m≤1000000。

解析:

我们考虑枚举m,对于每一个m,我们去检验它是否是多项式方程的一个解,检验的方法用到了秦九韶算法,秦九韶算法的作用就是求一个多项式方程的结果时,只需枚举n次,假设我们有一个一元四次方程a0+a1x+a2x2+a3x3+a4x4=0,我们可以将这个式子化为(x(x(x(a4x+a3)+a2)+a1)+a0)=0 ,我们从大到小枚举i,对于每一个i,我们让sum加上ai​再乘上x,最后让sum+a0,看sum是否=0即可。
这题对于100%的数据,0<n>i|≤1010000,an≠0,m≤1000000,所以读入时用类似快读的方法,不断取模就行。

至于什么是秦九韶算法,百度上有比较详细的解释。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <string>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cmath>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <vector>
13 #include <ctime>
14 #include <cctype>
15 #include <bitset>
16 #include <utility>
17 #include <sstream>
18 #include <complex>
19 #include <iomanip>
20 #define inf 0x3f3f3f3f
21 typedef long long ll;
22 using namespace std;
23 #define mod 1000000007
24 using namespace std;
25 int n,m,a[110],ct,jg[1000010];
26 ll read()
27 {
28     ll sum=0;
29     char str;
30     int fg=1;
31     while(str=getchar(),!((str>='0'&&str<='9')||str=='-'));
32     do
33     {
34         if(str=='-')
35         {
36             fg=-1;
37             continue;
38         }
39         sum=sum*10+str-'0';
40         sum%=mod;
41     }
42     while((str=getchar(),(str>='0'&&str<='9')||str=='-'));
43     return fg*sum;
44 }
45 void print(ll x)
46 {
47     if(!x)
48         return;
49     print(x/10);
50     putchar(x%10+'0');
51 }
52 bool pd(ll x)
53 {
54     ll sum=0;
55     for(int i=n; i>=1; i--)
56     {
57         sum=(sum+a[i])*x;
58         sum%=mod;
59     }
60     sum+=a[0];
61     sum%=mod;
62     return !sum;
63 }
64 int main()
65 {
66     ios::sync_with_stdio(false);
67     n=read();
68     m=read();
69     for(int i=0; i<=n; i++)
70         a[i]=read();
71     for(int i=1; i<=m; i++)
72         if(pd(i))
73             jg[++ct]=i;
74     cout<<ct<<endl;
75     for(int i=1; i<=ct; i++)
76     {
77         print(jg[i]);
78         printf("\n");
79     }
80     return 0;
81 }

原文地址:https://www.cnblogs.com/mxnzqh/p/11834344.html