秦九韶算法学习笔记
快速求多项式 —— 秦九韶算法
计算
1. 朴素算法
算出每一项的值再相加,总共要进行
2. 秦九韶算法
例题
题目描述
已知多项式方程:
求这个方程在
对于
对于这道题我们可以利用秦九韶算法求解,枚举
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll Mod = 1e9 + 7;
inline ll read(){
ll f = 1,x = 0;
char ch = getchar();
while(!isdigit(ch)){
if(ch == '-')f = -1;
ch = getchar();
}
while(isdigit(ch)){
x = (x << 1) + (x << 3) + (ch ^ 48);
x %= Mod;
ch = getchar();
}
return x * f;
}
inline void print(int x){
if(x > 9)print(x / 10);
putchar(x % 10 + '0');
}
ll a[101];
int n, m;
bool check(ll x){
ll sum = 0;
for(ll i = n; i >= 1; i--)
sum = ((a[i] + sum) % Mod * x) % Mod;
sum = (sum + a[0]) % Mod;
return !(sum);
}
ll ans[1000010], tot = 0;
signed main(){
cin >> n >> m;
for(int i = 0; i <= n; i++){
a[i] = read();
}
for(ll i = 1; i <= m; i++){
if(check(i))ans[++tot] = i;
}
cout << tot << endl;
for(int i = 1; i <= tot; i++){
cout << ans[i] << endl;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具