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