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));
}
}
}

ACM学习历程—HDU5666 Segment(数论)的更多相关文章

  1. ACM学习历程—HDU5668 Circle(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...

  2. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  3. ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...

  4. ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)

    http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...

  5. ACM学习历程—广东工业大学2016校赛决赛-网络赛F 我是好人4(数论)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=5 这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对lcm进行容斥. ...

  6. ACM学习历程—HDU5637 Transform(数论 && 最短路)

    题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给两种操作,然后给你一个s,一个t,求s至少需要多少次操作到t. 考虑到第一种操作是将 ...

  7. ACM学习历程—BZOJ2956 模积和(数论)

    Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...

  8. ACM学习历程—SNNUOJ1132 余数之和(数论)

    Description F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数.例如F(6) = 6 % 1 + 6 % ...

  9. ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)

    Problem Description Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109 ...

随机推荐

  1. Loadrunder之脚本篇——参数化在场景中的运用

    Action() { lr_eval_string("{NewParam}"); lr_eval_string("{NewParam}"); return 0; ...

  2. $python正则表达式系列(2)——re模块常用函数

    本文主要介绍正则re模块的常用函数. 1. 编译正则 import re p = re.compile(r'ab*') print '[Output]' print type(p) print p p ...

  3. hadoop02---高可用网站架构

    tomcat每个请求都会占用内存cpu,tomcat没有代理功能.nginx是俄国人写的,nginx是静态资源服务器,既可以自己返回请求,也可以做代理进行转发,和负载均衡.Tomcat是动态资源jav ...

  4. linux systemctl 指令 —— 阮一峰

    Systemd 指令 原文链接如果有效,请点击原文链接查看.原文:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.ht ...

  5. CMD 删除脚本

    CMD 删除脚本 forfiles /p D:\BACKUP\WindowsImageBackup /s /m *.* /d -14 /c "cmd /c del @file"; ...

  6. MySQL备份账号权限

    grant select,show view,lock tables,trigger on confluence.* to 'DBbackup'@'127.0.0.1' identified by ' ...

  7. 关于读取本地text文件,自动被添加空格的问题

    最近做一个小程序,读取本地指定路径下的text文件,逐行获取text文本然后再进行处理,结果遇到了一个奇葩问题,先插个图片给各位看官 坑:本地text文件中数据为1123/10(数据反复检查无空格,换 ...

  8. ceph安装各种报错

    [ceph_deploy][ERROR ] RuntimeError: Failed to execute command: ceph-disk-activate –mark-init sysvini ...

  9. 【P1582】倒水(数论??暴力!!)

    这个题我很无语,一开始看绿题,还是数论,应该不会特别简单,应该要动笔写上好一会,过了一会旁边 #祝神 说这原来是个蓝题,我顿时觉得十分迷茫... 结果看了这个题看了一会,仔细一想,woc,这题怕不是可 ...

  10. EF Code-First 学习之旅 Fluent API

    Mappings To Database Model-wide Mapping Set default Schema Set Custom Convetions Entity Mapping To S ...