HDU 5666 Segment(BestCoder Round #80 1002)

传送门

Segment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 787    Accepted Submission(s): 301


Problem Description
     Silen August does not like to talk with others.She like to find some interesting problems.

     Today she finds an interesting problem.She finds a segment  x+y=q .The segment intersect the axis and produce a delta.She links some line between  (0,0)  and the node on the segment whose coordinate are integers.

     Please calculate how many nodes are in the delta and not on the segments,output answer mod P.
 

Input
     First line has a number,T,means testcase number.

     Then,each line has two integers q,P.

    q  is a prime number,and  2q1018,1P1018,1T10.
 

Output
     Output 1 number to each testcase,answer mod P.
 

Sample Input
  
  
1 2 107
 

Sample Output
  
  
0
 

Source

题目大意:

    Rivendell非常神,喜欢研究奇怪的问题.

\ \ \ \    今天他发现了一个有趣的问题.找到一条线段x+y=qx+y=q,令它和坐标轴在第一象限围成了一个三角形,然后画线连接了坐标原点和线段上坐标为整数的格点.

\ \ \ \    请你找一找有多少点在三角形的内部且不是线段上的点,并将这个个数对PP取模后告诉他.


官方题解:

考虑一条以(0,0)(0,0)为起点,(x,y)(x,y)为终点的线段上格点的个数(不包含端点时),一定是gcd(x,y)-1gcd(x,y)1,这个很显然吧.

然后整个网格图范围内的格点数目是\frac {q*(q-1)} 22q(q1).

所以答案就是\frac {q*(q-1)} 2 -2q(q1) 所有线段上的格点的个数.

因为gcd(a,b)=gcd(a,b-a)\ (b>a)gcd(a,b)=gcd(a,ba) (b>a),所以gcd(x,y)=gcd(x,p-x)=gcd(x,p)gcd(x,y)=gcd(x,px)=gcd(x,p),p是质数,所以gcd(x,y)=1gcd(x,y)=1,所以线段上都没有格点,所以答案就是\frac {q*(q-1)} 22q(q1).

个人想法:

其实自己就是画一个图,一定是方格图,然后自己画一下,就会发现一些规律,其实每个点一行一行的可以构成一个等差数列,所以相加得到的公式就是 (q-1)*(q-2)/2,注意 两个数相乘会爆long long,所以采用快速乘法 二进制那个给优化一下就搞定了:


My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
LL Mul_mod(LL a, LL b, LL m)
{
    LL ans = 0;
    while(b)
    {
        if(b & 1)
            ans = (ans+a)%m;
        b>>=1;
        a = (a+a)%m;
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        LL q, mod;
        cin>>q>>mod;
        LL x1 = q - 1;
        LL x2 = q - 2 ;
        if(x1%2==0)
            x1>>=1;
        else
            x2>>=1;
        printf("%lld\n",Mul_mod(x1,x2,mod));
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值