编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#41651 #1002. B. 说服合伙人 Compile Error 0 0 ms 0 K C++ / 1.1 K 电类907-王清楠 2020-07-21 23:00:16
显示原始代码
#include "stdio.h"
#include "iostream"
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int ex_gcd(int a, int b, int &x, int &y) {
    if (!b) {
        x = 1, y = 0;
        return a;
    }
    int d = ex_gcd(b, a % b, y, x);
    y -= (a / b) * x;
    return d;
}
int CRT(int A[], int N[], int M) {
    int n = 1;
    int ans = 0;
    for (int i = 1; i <= M; i++) n *= N[i];
    for (int i = 1; i <= M; i++) {
        int x, y;
        int Mi = n / N[i];
        ex_gcd(Mi, N[i], x, y);
        ans = (ans + Mi * x * A[i]) % n;
    }
    if (ans < 0)
        ans += n;
    return ans;
}
int main() {
    int T, M, *N, *A, i, *sum;
    cin >> T;
    sum = (int *)malloc(sizeof(int) * T);
    for (int t = 0; t < T; t++) {
        cin >> M;
        int flag = 0;
        N = (int *)malloc(sizeof(int) * M);
        A = (int *)malloc(sizeof(int) * M);
        for (i = 1; i <= M; i++) {
            cin >> N[i];
            cin >> A[i];
        }
        for (i = 1; i <= M; i++)  //找是否互质
        {
            for (int j = i + 1; j <= M; j++) {
                if (gcd(N[i], N[j]) != 1) {
                    flag = 1;
                    break;
                }
            }
        }
        if (flag == 1)
            sum[t] = -1;
        else
            sum[t] = CRT(A, N, M);
    }
    for (int t = 0; t < T; t++) {
        cout << sum[t] << endl;
    }
    return 0;
}

编译信息

/sandbox/1/a.cpp: In function 'int main()':
/sandbox/1/a.cpp:35:13: error: 'malloc' was not declared in this scope
  sum=(int*) malloc(sizeof(int)*T);
             ^~~~~~
/sandbox/1/a.cpp:35:13: note: 'malloc' is defined in header '<cstdlib>'; did you forget to '#include <cstdlib>'?
/sandbox/1/a.cpp:3:1:
+#include <cstdlib>
 using namespace std;
/sandbox/1/a.cpp:35:13:
  sum=(int*) malloc(sizeof(int)*T);
             ^~~~~~