ACM学习历程—HDU5666 Segment(数论)

http://acm.hdu.edu.cn/showproblem.php?pid=5666

这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q。

那么直线方程则为y = y0/x0x,如果存在点(x1, y1)在此直线上,

那么y1 = y0*x1/x0,而y0 = q-x0,

于是y1 = (q-x0)*x1/x0 = q*x1/x0-x1,

因为x0 < q,于是(x0, q) = 1,

于是x0 | x1,

而x1 < x0,于是x1 = x0,

也就是说三角形内部的整点都不在线上,

于是计算三角形内部的整点即可。

但是中间计算数据很大,需要用大数,这里采用了Java。

 

代码:

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

public class Main
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int T = input.nextInt();
        BigInteger p, q;
        BigInteger two = new BigInteger("2");
        BigInteger one = new BigInteger("1");
        for (int times = 1; times <= T; ++times)
        {
            q = input.nextBigInteger();
            p = input.nextBigInteger();
            q = q.subtract(two);
            q = q.multiply(q.add(one));
            q = q.divide(two);
            System.out.println(q.mod(p));
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/andyqsmart/p/5427828.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值