Special equations(数学筛)

  Let f(x) = anxn+...+a1x+a0 , in which ai (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 an to a0 (0 < abs(an) <= 100; abs(ai) <= 10000 when deg >= 3, otherwise abs(ai) <= 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!

问是否存在x,使得fx模 pri2 为0。

可以注意到
1.pri范围很小
2.f(a)整除pri,则f(a + ka)整除pri
3.若f(a)整除 pri2 ,则f(a)整除pri。

所以我们可以分两步,先把pri剩余系内满足f(a)整除pri的找出来,
然后依次取出,枚举 pre2 范围内的a的倍数,这样就可以快速找到x,如果找不到,就不存在。
类似于素数筛的想法。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
ll a[10];
ll work(ll x)
{
    ll ans = 0;
    for(ll i = 4;i >= 0;i--)
        ans = ans*x + a[i];
    return ans;
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int t,kase = 1;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        ll p;
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        for(ll i = n;i >= 0;i--)
            scanf("%lld",&a[i]);
        scanf("%lld",&p);
        vector<ll>vt;
        for(ll i = 0;i < p;i++)
        {
            if(work(i)%p == 0)
                vt.push_back(i);
        }
        bool ok = false;
        ll ans;
        for(ll i = 0;i < vt.size();i++)
        {
            ll j = vt[i];
            for(;j < p*p;j = j + p)
            {
                if(work(j)%(p*p) == 0)
                {
                    ok = true;
                    ans = j;
                    break;
                }
            }
            if(ok)break;
        }
        printf("Case #%d: ",kase++);
        if(ok)
            printf("%lld\n",ans);
        else
            printf("No solution!\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值