试除法

问题 E: E

时间限制: 3 Sec   内存限制: 512 MB
提交: 6   解决: 2
[ 提交][ 状态][ 讨论版][ Edit] [ TestData]

题目描述

有n个正整数a1,a2,...,an和m个正整数b1,b2,...,bm

定义分数,其中A与B互质。

处理k个询问,每个询问给出一个M,求一个整数C满足0<=C<M且

不存在C的话则输出DIVISION BY ZERO

输入

第一行输入n m

第二行输入a1,a2,...,an

第三行输入b1,b2,...,bm

第四行一个数k

第五行一个数M

1<=n,m<5000,1<=k<=50,2<=M<=1e18,

1<=ai,bj<=1e18(i=1,2,...,n,j=1,2,...m)

输出

一个整数C,不存在C的话则输出DIVISION BY ZERO

样例输入

3 2
8 11 14
9 12
4
8
9
10
11

样例输出

4
DIVISION BY ZERO
4
0




//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define DB double
#define ll long long
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
//const int maxn=1e3+10;
const int maxx=1e6+100;
const double EPS=1e-8;
const double eps=1e-8;
//const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}

inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
const int maxn = 5001, maxd = 19, maxp = 1000001;
int tot, pr[maxp], d[maxp];
int n, m, q, sz, lim[maxd], cnt[maxd];
LL a[maxn], b[maxn], mod, p[maxd];

void exgcd(LL a, LL b, LL &x, LL &y)
{
    if(!b)
    {
        x = 1;
        y = 0;
        return;
    }
    exgcd(b, a % b, y, x);
    y -= a / b * x;
}
inline LL mod_inv(LL x, LL p)//逆元
{
    LL u, v;
    exgcd(x, p, u, v);
    return u < 0 ? u + p : u;
}
inline LL mod_mul(LL x, LL y, LL p)//膜乘
{
    LL ret = x * y - (LL)((DB)x * y / p + 0.01) * p;
    return ret < 0 ? ret + p : ret;
}
int main()
{
    //freopen("1.in","r",stdin);
    //freopen("1.txt","w",stdout);
    //欧拉筛法
    for(int i = 2; i < maxp; ++i)
    {
        if(!d[i])
            pr[tot++] = d[i] = i;
        for(int j = 0, k; (k = i * pr[j]) < maxp; ++j)
        {
            d[k] = pr[j];
            if(d[i] == pr[j])
                break;
        }
    }
    scan_d(n);scan_d(m);
    for(int i = 0; i < n; ++i)
        scan_d(a[i]);
    for(int i = 0; i < m; ++i)
        scan_d(b[i]);
    scan_d(q);
    while(q--)
    {
        scan_d(mod);
        sz = 0;
        LL tmp = mod;
        for(int i = 0; i < tot; ++i)
        {
            if(tmp % pr[i])
                continue;
            for(tmp /= pr[i], lim[sz] = 1; tmp % pr[i] == 0; tmp /= pr[i], ++lim[sz]);
            p[sz++] = pr[i];//p[]为质因子及lim[]为幂次
        }
        if(tmp > 1)
            for(int i = 0; i < n; ++i)
            {
                LL r = std::__gcd(a[i], tmp);
                if(r > 1 && r < tmp)
                {
                    for(tmp /= r, lim[sz] = 1; tmp % r == 0; tmp /= r, ++lim[sz]);
                    p[sz++] = r;
                    break;
                }
            }
        if(tmp > 1)
            for(int i = 0; i < m; ++i)
            {
                LL r = std::__gcd(b[i], tmp);
                if(r > 1 && r < tmp)
                {
                    for(tmp /= r, lim[sz] = 1; tmp % r == 0; tmp /= r, ++lim[sz]);
                    p[sz++] = r;
                    break;
                }
            }
        if(tmp > 1) // may not prime, but combined effect with numerator and denominator
        {
            lim[sz] = 1;
            p[sz++] = tmp;
        }
        memset(cnt, 0, sz * sizeof(int));
        LL fz = 1, fm = 1;
        for(int i = 0; i < n; ++i)
        {
            tmp = a[i];
            for(int j = 0; j < sz; ++j)
                for( ; tmp % p[j] == 0; tmp /= p[j], ++cnt[j]);
            fz = mod_mul(fz, tmp, mod);//分子
        }
        for(int i = 0; i < m; ++i)
        {
            tmp = b[i];
            for(int j = 0; j < sz; ++j)
                for( ; tmp % p[j] == 0; tmp /= p[j], --cnt[j]);
            fm = mod_mul(fm, tmp, mod);//分母
        }
        bool flag = 1;
        for(int i = 0; i < sz && flag; ++i)
            flag &= cnt[i] >= 0;
        if(!flag)
        {
            puts("DIVISION BY ZERO");
            continue;
        }
        for(int i = 0; i < sz; ++i)
            for( ; cnt[i]--; fz = mod_mul(fz, p[i], mod));
        fz = mod_mul(fz, mod_inv(fm, mod), mod);
        print(fz);
    }
    return 0;
}
//另:希望旺神如果对此题有所idea,能写一个推导和证明最好=。=
//因为出题人的水平太菜,仅仅只是能理解代码及大概做路而已,并不能给出证明。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值