题目链接:
Segment
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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.
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 2≤q≤10^18,1≤P≤10^18,1≤T≤10.
Then,each line has two integers q,P.
q is a prime number,and 2≤q≤10^18,1≤P≤10^18,1≤T≤10.
Output
Output 1 number to each testcase,answer mod P.
Sample Input
1
2 107
Sample Output
0
思路:
直线x+y=q,在第一象限形成的三角形内部有多少个整数点;
题意:
条件转化成x+y<q&&x>0&&y>0;
可以发现当x取1到q-1时y正好取q-2到0,所以和就是(q-1)*(q-2)/2;
但是q和p都是<=1e18,所以算(q-1)*(q-2)/2可以大数乘法然后取模,或者用俄罗斯乘法算这两个数的积;
俄罗斯乘法的方法与快速幂的方法类似;
AC代码:
#include <bits/stdc++.h> using namespace std; const int N=1e4+5; typedef long long ll; const int mod=1e9+7; ll p,q; void solve(ll a,ll b) { ll s=0,base=a; while(b) { if(b&1) { s+=base; s%=q; b--; } else { base*=2; base%=q; b=(b>>1); } } printf("%lld ",s); } int main() { int t; scanf("%d",&t); while(t--) { scanf("%lld%lld",&p,&q); if(p==2) { printf("0 "); } else { ll x=(p-1)/2%q,y=(p-2)%q; solve(x,y); } } return 0; }