题目链接:
BC #80 B Segment
题意:
Rivendell非常神,喜欢研究奇怪的问题.
今天他发现了一个有趣的问题.找到一条线段
x+y=q
,
令它和坐标轴在第一象限围成了一个三角形,然后画线连接了坐标原点和线段上坐标为整数的格点.
请你找一找有多少点在三角形的内部且不是线段上的点,并将这个数对P取模后告诉他.
输入p和q。q是质数且
q≤1018
,
1≤P≤1018
分析:
答案的公式是
ans=(q−2)∗(q−1)/2
;
但是因为p和q的范围太大!直接乘的话会爆long long!!!
所以需要手动写快速乘法,类似于快速幂的写法!!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
using namespace std;
int T;
long long q,p;
long long mul(long long a,long long b)
{
long long res=0,tmp=a;
while(b){
if(b&1) res=(res+tmp)%p;
tmp=(tmp+tmp)%p;
b>>=1;
}
return res;
}
int main()
{
freopen("80Bin.txt","r",stdin);
scanf("%d",&T);
while(T--){
cin>>q>>p;
if(q==2){
printf("0\n");
continue;
}
long long t=(q-1)/2%p;
long long ans=mul(q-2,t);
cout<<ans<<endl;
}
return 0;
}