提交记录 5164


用户 题目 状态 得分 用时 内存 语言 代码长度
noi18std noi18d. 【NOI2018】屠龙勇士 Wrong Answer 15 950.293 ms 11000 KB C++11 2.89 KB
提交时间 评测时间
2018-08-08 21:53:46 2020-08-01 00:12:30
#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
#include <utility>
using namespace std;

typedef long long LL;
const int N = 200010;

LL gcd(LL a, LL b) { return a % b == 0 ? b : gcd(b, a % b); }

void e_gcd(LL a, LL b, LL& d, LL& x, LL& y) {
    if (!b) { d = a; x = 1; y = 0; } else {
        e_gcd(b, a%b, d, y, x); y -= x * (a / b);
    }
}

LL inverse(LL a, LL n) {  
    LL d,x,y;
    e_gcd(a, n, d, x, y);
    return d == 1 ? (x + n) % n : -1;
} 

LL Product_Mod(LL a, LL b, LL mod) {
    LL sum = 0;
    int flag = b >= 0 ? 1 : -1;
    if(flag < 0) { b = -b; }
    while(b) {
        if(b & 1) sum = (sum + a) % mod;
        a = (a << 1) % mod;
        b >>= 1;
    }
    return (sum * flag + mod) % mod;
}   // (a * b) % mod;

LL chineseRemain(int n, LL mod[], LL rem[]) {
    LL lcm = 1;
    for(int i = 0; i < n; ++i) {
        // K = x[i]*mod[i] + rem[i];
        // mod positive
        if(mod[i] < 0) {
            mod[i] = -mod[i];
        } 
        // r positive
        rem[i] = (rem[i] % mod[i] + mod[i]) % mod[i];
    } 
    // calculate lcm
    for(int i = 0; i < n; ++i) {
        LL g = gcd(lcm, mod[i]);
        lcm = lcm / g * mod[i];
    }

    LL A, B, C, X, Y, d;
    for(int i = 1; i < n; i++) {
        A = mod[0];
        B = -mod[i];
        C = rem[i] - rem[0];
        LL g = gcd(A, B);
        if( C % g ) {
            return -1;
        }
        A /= g, B /= g, C /= g;
        // make 'Ax + By = 1'
        if(A < 0) {
            // turn A and B to positive
            A = -A, B = -B, C = -C;
            B = ((B % A) + A) % A;
        }
        e_gcd(A, B, d, X, Y);
        Y = Product_Mod(Y, C, A);
        mod[0] = A * mod[i];
        rem[0] = (rem[i] +  Product_Mod(mod[i], Y, mod[0])) % mod[0];
    }
    return rem[0];
}   // return -1 if no solution

int n, m;
LL a[N], p[N], d[N], w[N], B[N], r[N];

int main() {

	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d%d", &n, &m);
		for(int i = 0; i < n; ++i)
			scanf("%lld", &a[i]);
		for(int i = 0; i < n; ++i)
			scanf("%lld", &p[i]);
		for(int i = 0; i < n; ++i)
			scanf("%lld", &d[i]);
		multimap<LL, int> mp;
		for(int i = 0; i < m; ++i) {
			scanf("%lld", &w[i]);
			mp.insert(make_pair(w[i], i));
		}
		for(int i = 0; i < n; ++i) {	// each dragon
			multimap<LL, int>::iterator it = mp.lower_bound(a[i]);
            if(it != mp.begin() && (*it).first != a[i]) { --it; }
			int id = (*it).second;
			B[i] = w[id];
			w[id] = d[i];
			mp.erase(it);
			mp.insert(make_pair(w[id], id));
		}
        bool flag = true;
        for(int i = 0; i < n; ++i) {
            LL tmp = inverse(B[i], p[i]);
            if(tmp == -1) {
                flag = false;
                printf("-1\n");
                break;
            }
            // B[i] * x = a[i] (mod p[i])
            r[i] = Product_Mod(tmp, a[i], p[i]);
        }
        if(flag == false) { continue; }
		printf("%lld\n", chineseRemain(n, p, r));
	}

	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1189.246 ms3 MB + 896 KBWrong AnswerScore: 0

Testcase #2191.268 ms3 MB + 896 KBWrong AnswerScore: 0

Testcase #3194.002 ms3 MB + 896 KBWrong AnswerScore: 0

Testcase #4194.543 ms3 MB + 896 KBWrong AnswerScore: 0

Testcase #51.203 ms164 KBWrong AnswerScore: 0

Testcase #61.18 ms164 KBWrong AnswerScore: 0

Testcase #71.153 ms164 KBWrong AnswerScore: 0

Testcase #842.34 us60 KBWrong AnswerScore: 0

Testcase #942.6 us60 KBWrong AnswerScore: 0

Testcase #1042.08 us60 KBWrong AnswerScore: 0

Testcase #1141.75 us60 KBWrong AnswerScore: 0

Testcase #1242.38 us60 KBAcceptedScore: 5

Testcase #1340.09 us60 KBWrong AnswerScore: 0

Testcase #14541.649 ms10 MB + 760 KBWrong AnswerScore: 0

Testcase #15542.527 ms10 MB + 760 KBWrong AnswerScore: 0

Testcase #16939.653 ms10 MB + 760 KBAcceptedScore: 5

Testcase #17950.293 ms10 MB + 760 KBAcceptedScore: 5

Testcase #18381.32 ms9 MB + 1004 KBWrong AnswerScore: 0

Testcase #19388.907 ms9 MB + 1004 KBWrong AnswerScore: 0

Testcase #20365.789 ms9 MB + 1004 KBWrong AnswerScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2026-04-12 07:54:33 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠