洛谷P2312解方程
生活随笔
收集整理的這篇文章主要介紹了
洛谷P2312解方程
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
傳送門
思路分析
怎么求解呢?
其實(shí)我們可以把左邊的式子當(dāng)成一個(gè)算式來計(jì)算,從1到 $ m $ 枚舉,只要結(jié)果是0,那么當(dāng)前枚舉到的值就是這個(gè)等式的解了。可以通過編寫一個(gè) $ bool $ 函數(shù)來判斷算式的值是不是0
至于如何計(jì)算這個(gè)多項(xiàng)式,用秦九韶算法就可以解決
細(xì)節(jié)提示 :
1.防爆 $ int $ 常用方法:模大~質(zhì)數(shù)!(另:好像模一個(gè)質(zhì)數(shù)有的時(shí)候會(huì)出事233可以多模幾個(gè)大質(zhì)數(shù)~)
2.最好用上讀入優(yōu)化,而且邊讀邊取模。
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; }轉(zhuǎn)載于:https://www.cnblogs.com/Stephen-F/p/9930791.html
總結(jié)
以上是生活随笔為你收集整理的洛谷P2312解方程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 安装tar 命令
- 下一篇: 写给正在入坑linux系统的伙伴