hdu 5666 Segment(简单整点计数题+快速乘)(bestcoder#80 1001)

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
 

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  2q1018,1P1018,1T10.
 

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

Sample Input
  
  
1 2 107
 

Sample Output
  
  
0
 

Source
 

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  2q1018,1P1018,1T10.
 

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

Sample Input
   
   
1 2 107
 

Sample Output
   
   
0
 

Source
 

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  2q1018,1P1018,1T10.
 

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

Sample Input
   
   
1 2 107
 

Sample Output
   
   
0
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5674  5673  5672  5671  5670 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值