zoukankan      html  css  js  c++  java
  • 【NOIP2014】解方程(枚举)

    题面

    题目描述

    已知多项式方程:
    a0+a1x+a2x^2+..+anx^n=0
    求这个方程在[1, m ] 内的整数解(n 和m 均为正整数)

    输入格式

    输入共n + 2 行。
    第一行包含2 个整数n 、m ,每两个整数之间用一个空格隔开。
    接下来的n+1 行每行包含一个整数,依次为a0,a1,a2..an

    输出格式

    输出文件名为equation .out 。
    第一行输出方程在[1, m ] 内的整数解的个数。
    接下来每行一个整数,按照从小到大的顺序依次输出方程在[1, m ] 内的一个整数解。

    输入样例#1:

    2 10
    1
    -2
    1

    输出样例#1:

    1
    1

    输入样例#2:

    2 10
    2
    -3
    1

    输出样例#2:

    2
    1
    2

    输入样例#3:

    2 10
    1
    3
    2

    输出样例#3:

    0

    说明

    对于30%的数据:0<n<=2,|ai|<=100,an!=0,m<100
    对于50%的数据:0<n<=100,|ai|<=10^100,an!=0,m<100
    对于70%的数据:0<n<=100,|ai|<=10^10000,an!=0,m<10000
    对于100%的数据:0<n<=100,|ai|<=10^10000,an!=0,m<1000000

    题解

    明显对左右两侧取膜呀。。。。
    如果f(x)%p=0
    那么,肯定有f(x+kp)%p=0
    所以,找几个质数,依次计算f(1~p)的值
    如果某个整数是解
    那么,必定有 f(x%pi)%pi=0
    所以枚举一下就可以了。。。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    #define MOD (19260817)
    #define ll long long
    inline int read()
    {
    	register int x=0,t=1;
    	register char ch=getchar();
    	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    	if(ch=='-'){t=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=((x<<1)+(x<<3))%MOD+ch-48;ch=getchar();}
    	return x*t%MOD;
    }
    int N,M,a[3][110];
    int A[110];
    int tot;
    char s[200][12000];
    bool vis[1100000][5];
    int pr[3]={10007,30071,12007};
    inline bool f(int x,int tt)
    {
        ll ans=0;
    	for(int i=N;i>=0;--i)
    		ans=((ans+a[tt][i])*x)%pr[tt];
    	return !ans;
    }
    inline void geta(int tt)
    {
    	for(int i=0;i<=N;++i)
    	{
    		int pos=0,z=1,l=strlen(s[i]);
    		if(s[i][pos]=='-'){z-=2;pos+=1;}
    		for(int j=pos;j<l;++j)
    			a[tt][i]=(a[tt][i]*10+s[i][j]-48)%pr[tt];
    		a[tt][i]*=z;
    	}
    }
    int main()
    {
        N=read();M=read();
    	for(int i=0;i<=N;++i)scanf("%s",s[i]);
    	for(int i=0;i<3;++i)geta(i);
    	for(int tt=0;tt<3;++tt)
    		for(int i=1;i<=min(M,pr[tt]);++i)
    			if(f(i,tt))
    				vis[i][tt]=true;
        for(int i=1;i<=M;++i)
    	{
    		bool fl=true;
    		for(int tt=0;tt<3;++tt)fl&=vis[i%pr[tt]][tt];
    		if(fl)A[++tot]=i;
    	}
    	printf("%d
    ",tot);
    	for(int i=1;i<=tot;++i)
    		printf("%d
    ",A[i]);
    	return 0;
    }
    
    
  • 相关阅读:
    pyenv: python2.7: command not found The `python2.7' command exists in these Python versions: 2.7.5
    Gazebo_02_build_a_robot
    Gazebo_01_getting_start
    vscode等编辑器中报Exception has occurred: ModuleNotFoundError No module named 'requests'
    Ubuntu16.04安装Python3.8以后出现lsb_release/No LSB modules are available的错误
    C语言字符串定义(数组&指针)
    电脑软件更新管理
    VS2017自定义新建模板
    《SQL必知必会-第四版》--学习摘抄
    实体类封装数据库查询信息(工具一)
  • 原文地址:https://www.cnblogs.com/cjyyb/p/7581381.html
Copyright © 2011-2022 走看看