hdu5666 Segment

题目链接: hdu5666 ( Segment )

\(c\)++ \(AC\) 代码:

/**
 * hdu5666 Segment
 * q为质数,故题意为求解x+y=p与坐标轴围成的三角形区域内的整数坐标点的数目:(q-1)*(q-2)/2
 */

#include <iostream>
using namespace std;

typedef long long LL;

int main()
{
    int T;
    cin >> T;
    while (T--) {
        LL q, P;
        cin >> q >> P;
        LL a = q-1, b = q-2;
        if (a&1) b /= 2;
        else a /= 2;
        LL ans = 0;
        while (b) {
            if (b&1) ans = (ans+a)%P;
            a = (a+a)%P;
            b >>= 1;
        }
        cout << ans << endl;
    }
    return 0;
}

\(java\ AC\) 代码:

/**
 * hdu5666 Segment
 * q为质数,故题意为求解x+y=p与坐标轴围成的三角形区域内的整数坐标点的数目:(q-1)*(q-2)/2
 */

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T-- != 0) {
            BigInteger q = in.nextBigInteger();
            BigInteger P = in.nextBigInteger();
            BigInteger one = BigInteger.ONE;
            BigInteger two = BigInteger.valueOf(2);
            BigInteger three = BigInteger.valueOf(3);

            // ans = (q-1)*(q-2)/2;
            BigInteger ans = q.subtract(one).multiply(q.subtract(two)).divide(two);

            System.out.println(ans.mod(P));

        }
    }
}

posted @ 2021-01-28 18:18  Zewbie  阅读(129)  评论(0)    收藏  举报