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
2≤q≤1018,1≤P≤1018,1≤T≤10.
Output
Output 1 number to each testcase,answer mod P.
Sample Input
Sample Output
Source
BestCoder Round #80
Recommend
wange2014 | We have carefully selected several similar problems for you: 5674 5673 5672 5671 5670
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
2≤q≤1018,1≤P≤1018,1≤T≤10.
Output
Output 1 number to each testcase,answer mod P.
Sample Input
Sample Output
Source
BestCoder Round #80
Recommend
wange2014 | We have carefully selected several similar problems for you: 5674 5673 5672 5671 5670
题目大意:
给定一条斜率为-1截距为q的直线,求解他在第一象限围住的整点(不含边界)有多少个。
解题思路:
规律是很明显的,最下面一行是q-2个点,上面一行依次减1,最后一直加到1,所以一共有(q-1)*(q-2)/2个点,注意会爆long long,要用快速乘解决。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#define ll unsigned long long
#define INF 0x3f3f3f3f
#define C(a) memset(a,0,sizeof a)
#define C_1(a) memset(a,-1,sizeof a)
#define C_i(a) memset(a,0x3f,sizeof a)
#define F(i,n) for(int i=0;i<n;i++)
#define F(n) for(int i=0;i<n;i++)
#define F_1(n) for(int i=n;i>0;i--)
#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define SL(a) scanf("%I64d",&a)
#define SD(a) scanf("%lf",&a)
#define P(a) printf("%d\n",a)
#define PL(a) printf("%I64d\n",a)
#define PD(a)printf("%lf\n",a)
#define rush() int t;scanf("%d",&t);while(t--)
using namespace std;
ll qmul(ll a, ll b, ll p)
{
ll ans = 0;
while (b)
{
if (b & 1)ans = (ans + a) % p;
a =(a*2)%p ;
b >>= 1;
}
return ans;
}
int main()
{
rush()
{
ll q, p,ans;
cin >> q >> p;
if ((q - 1) % 2)ans = qmul((q - 2) / 2, q - 1, p);
else ans = qmul((q - 1) / 2, q - 2, p);
cout << (ans%p+p)%p << endl;
}
}
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
2≤q≤1018,1≤P≤1018,1≤T≤10.
Output
Output 1 number to each testcase,answer mod P.
Sample Input
Sample Output
Source
BestCoder Round #80
Recommend
wange2014 | We have carefully selected several similar problems for you: 5674 5673 5672 5671 5670
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
2≤q≤1018,1≤P≤1018,1≤T≤10.
Output
Output 1 number to each testcase,answer mod P.
Sample Input
Sample Output
Source
BestCoder Round #80
Recommend
wange2014 | We have carefully selected several similar problems for you: 5674 5673 5672 5671 5670