hdu4569(优化,数学题,枚举)

Special equations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 572    Accepted Submission(s): 372
Special Judge


Problem Description
  Let f(x) = a nx n +...+ a 1x +a 0, in which a i (0 <= i <= n) are all known integers. We call f(x) 0 (mod m) congruence equation. If m is a composite, we can factor m into powers of primes and solve every such single equation after which we merge them using the Chinese Reminder Theorem. In this problem, you are asked to solve a much simpler version of such equations, with m to be prime's square.
 

Input
  The first line is the number of equations T, T<=50.
  Then comes T lines, each line starts with an integer deg (1<=deg<=4), meaning that f(x)'s degree is deg. Then follows deg integers, representing a n to a 0 (0 < abs(a n) <= 100; abs(a i) <= 10000 when deg >= 3, otherwise abs(a i) <= 100000000, i<n). The last integer is prime pri (pri<=10000). 
  Remember, your task is to solve f(x) 0 (mod pri*pri)
 

Output
  For each equation f(x) 0 (mod pri*pri), first output the case number, then output anyone of x if there are many x fitting the equation, else output "No solution!"
 

Sample Input
  
  
4 2 1 1 -5 7 1 5 -2995 9929 2 1 -96255532 8930 9811 4 14 5458 7754 4946 -2210 9601
 

Sample Output
  
  
Case #1: No solution! Case #2: 599 Case #3: 96255626 Case #4: No solution!
 
题意:
给定方程f(x) = anx^n +...+ a1x+a0的n(即方程有几项,1<=n<=4)和an到a0的值,再给一个prim的值,求是否存在x使得f(x)%(prim*prim)=0,若存在输出任意一个x,若不存在输出No solution!
当n<=3时(0 < abs(a n) <= 100),abs(a i) <= 10000 l当n>3时abs(a i) <= 100000000
解题思路:
这题用枚举就行了但是要优化一下,因为要求的是f(x)%(prim*prim)=0,先找到f(x)%prim=0的x然后x+=prim找有没有f(x)%(prim*prim)=0
直接暴力枚举的话枚举到prim*prim就可以了但是会超时,所以第一次枚举在0到prim中枚举f(x)%prim=0的x然后在这个x的基础上不断加prim(为了满足f(x)%prim=0的必要条件)枚举到prim*prim即可
关于常数项的其实没有影响的根据(a+b)%mod=(a%mod+b%mod)%mod想一下就会发现没影响的
#include <stdio.h>
#include <iostream>
#include<string.h>
#include <string.h>
#include<algorithm>
using namespace std;
long long PowerMod(long long a,long long b,long long c)
{
long long ans = 1;
a = a % c;
while(b>0)
{
if(b%2==1)
ans = (ans * a) % c;
b = b/2;
a = (a * a) % c;
}
return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    int Case=1;
    while(t--)
    {
        int n;
        scanf("%d",&n);
        long long a[n+1];
        for(int i=0;i<n+1;i++)
            scanf("%lld",&a[i]);
        long long mod;
        scanf("%lld",&mod);
        printf("Case #%d: ",Case++);
        for(long long i=0;i<mod;i++)
        {
            long long sum=0;
            for(long long j=0;j<=n;j++)
                sum=(sum+a[j]*PowerMod(i,n-j,mod))%mod;
            if(sum%mod==0)
            {
                long long k=i;
                while(k<mod*mod)
                {
                    sum=0;
                    for(long long j=0;j<=n;j++)
                        sum=(sum+a[j]*PowerMod(k,n-j,mod*mod))%(mod*mod);
                        if(sum==0)
                            {
                                printf("%lld\n",k);
                                goto x;
                            }
                            k+=mod;
                }
            }
        }
        printf("No solution!\n");
        x:;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值