码迷,mamicode.com
首页 > 其他好文 > 详细

解方程

时间:2019-09-08 13:40:56      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:解方程   www   false   for   通过   getchar   int   cal   char   

题面

怎么求解呢?

其实我们可以把左边的式子当成一个算式来计算,从1到m枚举,只要结果是0,那么当前枚举到的值就是这个等式的解了。可以通过编写一个bool函数来判断算式的值是不是0~

至于如何计算这个看起来又臭又长(雾)的多项式,用秦九韶算法就可以解决啦~

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int p=1000000007; 
bool t=true;
int n,m,ans,cnt,sum=0;
int A[103],key[1000003];
ll read()
{
    ll sum=0,fg=1;
    char c=getchar();
    while(c < ‘0‘ || c > ‘9‘)
    {
        if(c==‘-‘) fg=-1;
        c=getchar();
    }
    while(c >=‘0‘ && c <=‘9‘)
    {
        sum=((sum*10)+c-‘0‘)%p;
        c=getchar();
    }
    return sum*fg;
}
void print(int x)
{
    if(x<0)
    {
        putchar(‘-‘);
        x=-x;
    }
    if(x>9)
    {
        print(x/10);
    }
    putchar(x%10+‘0‘);
}
bool calc(ll x)
{
    sum=0;
    for(ll i=n;i>=1;i--)
    {
        sum=((A[i]+sum)*x)%p;
    }
    sum=(sum+A[0])%p;
    return !sum;
}
int main()
{
    n=read();
    m=read();
    for(ll i=0;i<=n;i++)
    {
        A[i]=read();
    }
    for(ll i=1;i<=m;i++)
    {
        if(calc(i))
        { 
            t=false; 
            ans++;
            key[++cnt]=i;
        }
    }
    if(t)
    {
        cout<<ans<<endl;
        return 0;
    }
    print(ans);
    printf("\n");
    for(ll i=1;i<=cnt;i++)
    {
        print(key[i]);
        printf("\n");
    }
    return 0;
}

  

解方程

标签:解方程   www   false   for   通过   getchar   int   cal   char   

原文地址:https://www.cnblogs.com/ainiyuling/p/11485601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!