當前位置:
首頁 >
洛谷P2312解方程
發布時間:2025/7/14
42
豆豆
生活随笔
收集整理的這篇文章主要介紹了
洛谷P2312解方程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
思路分析
怎么求解呢?
其實我們可以把左邊的式子當成一個算式來計算,從1到 $ m $ 枚舉,只要結果是0,那么當前枚舉到的值就是這個等式的解了。可以通過編寫一個 $ bool $ 函數來判斷算式的值是不是0
至于如何計算這個多項式,用秦九韶算法就可以解決
細節提示 :
1.防爆 $ int $ 常用方法:模大~質數!(另:好像模一個質數有的時候會出事233可以多模幾個大質數~)
2.最好用上讀入優化,而且邊讀邊取模。
3 . $ sum $ 每次都要清零
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define re register using namespace std; const long long mod = 1000000007;inline int read(){char ch = getchar();long long f = 1 , x = 0 ;while(ch > '9' || ch < '0') {if(ch == '-') f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = ((x << 1) + (x << 3) + ch - '0') % mod ;ch = getchar();}return x * f; }long long n,m,ans,cnt,sum; bool flag = true;//用來判斷是否有解 long long a[110],key[1000005];inline bool calc(long long x) {sum = 0 ;for(re long long i = n ; i >= 1 ; --i) {sum = ((a[i] + sum) * x) % mod;}sum = (sum + a[0]) % mod;return !sum; }int main(){n = read(); m = read();for(re long long i = 0 ; i <= n ; ++i) {a[i] = read();}for(re long long i = 1 ; i <= m ; ++i) {if(calc(i)){flag = false;ans++;key[++cnt] = i ;}}if(flag) {printf("%lld\n",ans);return 0;}printf("%lld\n",ans);for(re long long i = 1 ; i <= cnt ; ++i)printf("%lld\n" , key[i]);return 0; }轉載于:https://www.cnblogs.com/Stephen-F/p/9930791.html
總結
以上是生活随笔為你收集整理的洛谷P2312解方程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 安装tar 命令
- 下一篇: 约束 抛异常