HDU-5666 Segment (large digit operation) Good problem

Source: Internet
Author: User

HDU-5666Segment
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %i64d &%i64u

Submit Status

Description

Silen August does not a like-to-talk with others. She like to find some interesting problems. 

Today She finds an interesting problem. She finds a segment  . The segment intersect the axis and produce a delta. She links some line between   and the node on the segment whose coordinate is integers. 

" Span id= "mathjax-span-32" class= "Mtext" style= "" > Calculate how many nodes is 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 a integers q,p. 

 is a prime Number,and 

Output

Output 1 number to each Testcase,answer mod P.

Sample Input

Sample Output

Source

Bestcoder Round #80///Test instructions: Equation x+y=q; tell you how many integer coordinates the line has in the first quadrant, not counting the lines. Idea: It is easy to find the rules [(q-2) * (q-1)/2]%p, but because the number is too large, so in the large number of the remainder of the use of a technique, that is, one of the large number of small, and then multiply to take the remainder.
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #define LL __ Int64using namespace Std;ll p_mod (ll q1,ll q2,ll p) {ll sum=0;while (Q2) {if (q2&1) {sum+=q1;sum%=p;} Q1<<=1;q1%=p;q2>>=1;} return sum;} int main () {int t,n,m;ll p,q;ll q1,q2,pp;int i,j,k;scanf ("%d", &t), while (t--) {scanf ("%lld%lld", &q,&p); q1= Q-1;q2=q-2;if (q1&1) q2>>=1;elseq1>>=1;printf ("%lld\n", P_mod (Q1,q2,p));} return 0;}

HDU-5666 Segment (large digit operation) Good problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.