日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

洛谷P2312 解方程题解

發布時間:2025/4/16 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷P2312 解方程题解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

洛谷P2312 解方程題解

題目描述

已知多項式方程:

\[a_0+a_1x+a_2x^2+\cdots+a_nx^n=0\]
求這個方程在 \([1,m]\) 內的整數解(\(n\)\(m\) 均為正整數)。

輸入格式

輸入共 \(n + 2\) 行。

第一行包含 \(2\) 個整數 \(n, m\) ,每兩個整數之間用一個空格隔開。

接下來的 \(n+1\) 行每行包含一個整數,依次為 \(a_0,a_1,a_2\ldots a_n\).

輸出格式

第一行輸出方程在 \([1,m]\) 內的整數解的個數。

接下來每行一個整數,按照從小到大的順序依次輸出方程在 [1,m][1,m] 內的一個整數解。

輸入輸出樣例

輸入 #1 復制

2 10
1
-2
1

輸出 #1 復制

1
1

輸入 #2 復制

2 10
2
-3
1

輸出 #2 復制

2
1
2

輸入 #3 復制

2 10
1
3
2

輸出 #3 復制

0
說明/提示
對于 \(30%\) 的數據:\(0<n\le 2\),\(|a_i|\le 100\),\(a_n≠0\),\(m<1000\)
對于 \(50%\) 的數據:\(0<n\le 100,|a_i|\le 10^{100},a_n≠0,m<1000\)
對于 \(70%\) 的數據:\(0<n\le 100,|a_i|\le 10^{10000},a_n≠0,m<10^4\)

對于 \(100%\) 的數據:\(0<n\le 100,|a_i|\le 10^{10000},a_n≠0,m<10^6\)

解析:

秦九韶公式 + 快讀
輸入要注意,因為輸入的\(a[i]\)范圍比較大,
所以就對一個質數取模
\(1\)\(m\)進行枚舉,枚舉的是\(x\)
然后利用秦九韶公式進行求解
如果返回的值是\(0\),那么就記錄
反之繼續。

#include <cstdio> #include <iostream> #include <cmath> #include <queue> #include <stack> #include <cstring> #include <vector> #include <algorithm> #include <iomanip> #define Max 105 #define re register #define D double #define int long long int n,m,a[Max],ans = 0, Ans[1000012]; const int mod = 19260817; int read() {char ch = getchar(); int f = 1, s = 0;while(ch < '0' || ch > '9') {if(ch == '-') f = -1;ch =getchar();}while(ch >= '0' && ch <= '9') {s = (10 * s + ch - '0') % mod;ch = getchar();}return s * f; } int work(int x) {int ANS = 0;for(re int i = n ; i >= 1 ; -- i)ANS = ((ANS + a[i]) * x)% mod;ANS = (ANS + a[0]) % mod;return ANS; } void Main() {scanf("%lld%lld",&n,&m);for(re int i = 0; i <= n; ++ i) a[i] = read();for(re int i = 1; i <= m; ++ i)if(work(i) == 0) ans ++, Ans[ans] = i;printf("%lld\n",ans);for(re int i = 1; i <= ans; ++ i) printf("%lld\n",Ans[i]); } signed main() {Main();return 0; }

轉載于:https://www.cnblogs.com/handsomegodzilla/p/11482644.html

總結

以上是生活随笔為你收集整理的洛谷P2312 解方程题解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。