hdu 5666 Segment(BC 규칙 문제)

제목 링크:http://acm.hdu.edu.cn/showproblem.php?pid=5666
Segment
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1187    Accepted Submission(s): 433
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

   
   
   
   
1 2 107

 
Sample Output

   
   
   
   
0

 
Source
BestCoder Round #80
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:   5674  5673  5672  5671  5670 
 
제목 대의:선분 x+y=q 와 좌표 축 이 삼각형 으로 둘러싸 인 좌표 가 정점 인 개 수 를 찾 습 니 다.마지막 결 과 는 p 에 대해 모델 링 을 해 야 한다.
문제 풀이 사고:규칙 을 찾 아 x+y=5 라 고 가정 하고 요구 에 따라 전체 점 을 모두 쓰 면 좌표 x+y<5 를 발견 하면 됩 니 다.개 수 를 계산 하 는 공식 은 다음 과 같 습 니 다.(q-1)*(q-2)/2 입 니 다.
데이터 양 이 많 기 때문에 자바 로 해결 합 니 다.
코드 참조.
import java.util.*;
import java.math.*;

public class Main{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int t;
        BigInteger q,p,s,ss;
        Scanner cin = new Scanner(System.in);
        t=cin.nextInt();
        while(t>0){
            t--;
            q=cin.nextBigInteger();
            p=cin.nextBigInteger();
            s= q.subtract(new BigInteger("1"));

            //System.out.println(s);
            ss=q.subtract(new BigInteger("2"));

            //System.out.println(ss);
            s=s.multiply(ss);
            s=s.divide(new BigInteger("2"));
            s=s.mod(p);
            System.out.println(s);
        }
    }

}

좋은 웹페이지 즐겨찾기