NOIP2014 解方程

本文介绍了一种使用秦九韶算法结合暴力枚举解决特定方程的方法,通过预先处理变量a,简化了计算过程,避免了大范围枚举带来的效率问题。代码实现展示了如何在读入数据时进行模运算,随后利用秦九韶算法求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

还有这种操作?

我看到这道题的时候想到暴力枚举,不过a的范围极大令人头疼。Ssy说他要用Python然后他T了

不过后来发现,因为我们要解的方程左边是等于0的,所以我们可以直接把所有的a对于某一个数取模,这个在读入的时候就能处理,之后直接暴力枚举m用秦九韶算法硬算即可。

这都是什么神仙操作

看一下代码。

 

// luogu-judger-enable-o2
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<queue>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n')

using namespace std;
typedef long long ll;
const int M = 1000005;
const ll INF = 1e17+9;
const ll mod = 19260817;

ll read()
{
    ll ans = 0,op = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
    if(ch == '-') op = -1;
    ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
    ans *= 10;
    ans += ch - '0';
    ans %= mod;
    ch = getchar();
    }
    return ans * op;
}

ll n,m,a[105],cnt,ans[M];

ll calc(ll x)
{
    ll ans = 0;
    per(i,n,0) ans *= x,ans += a[i],ans %= mod;
    return ans;
}

int main()
{
    n = read(),m = read();
    rep(i,0,n) a[i] = read();
    rep(i,1,m)
    {
    ll k = calc(i);
    if(k == 0) ans[++cnt] = i;
    }
    printf("%lld\n",cnt);
    rep(i,1,cnt) printf("%lld\n",ans[i]);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/captain1/p/9834455.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值