Segment
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Problem 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 segmentx+y=q . The segment intersect the axis and produce a delta. She links some line between< Span id= "mathjax-span-21" class= "Mrow" > ( 0, 0) and the node on the segment whose coordinate is integers.
&NBSP; 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.
q is a prime Number,and2≤Q≤1018,1≤< Span id= "mathjax-span-66" class= "Mi" >p≤ 1018,1≤< Span id= "mathjax-span-76" class= "Mi" >t≤ 10. /span>
OutputOutput 1 number to each Testcase,answer mod P.
Sample Input12 107
Sample Output0
Sourcebestcoder Round #80题解: The answer is ((p-1) * (p-2)/2)%mod;p is 10^18 within the direct multiplication will explode, using the Russian multiplication of the additive nature of the answer; (live do only think of Java large number.) Sure enough, rookie ... ) Russian multiplication: http://baike.baidu.com/link?url=vVo1zdml29g80N-BYvpdm2hNGpYwSnGoJsnAJmook4AJBiYUVL_ ORT5F7XQFJ0YX6ZXB5HA90Q6-1LD6HXPIAA Russian Multiplication Code:
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>using namespacestd;#definell __int64#defineINF 2000000001intScan () {intres =0, ch; while( ! (ch = getchar ()) >='0'&& CH <='9' ) ) { if(ch = = EOF)return 1<< - ; } Res= CH-'0' ; while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;} ll Eluosimul (ll x,ll y,ll MoD) {ll sum=0; while(x) {if(x&1) {sum+=y; Sum%=MoD; } x>>=1; Y*=2; Y%=MoD; } returnsum;}intMain () {ll x,y,z,i,t,m,q; scanf ("%i64d",&x); while(x--) {ll ans; scanf ("%i64d%i64d",&q,&m); if(q%2) ans=eluosimul (q2, (q1)/2, M); Elseans=eluosimul (q1, (q2)/2, M); printf ("%i64d\n", ans); } return 0;}
View Code
Java:
Import java.util.*; import Java.math.*; Public classMain { Public Static voidMain (string[] args) {Scanner cin=NewScanner (System.inch); intx; X=Cin.nextint (); while(x!=0) {x--; BigInteger C=NewBigInteger ("2"); BigInteger e=NewBigInteger ("1"); BigInteger a=Cin.nextbiginteger (); BigInteger D=A.subtract (c); BigInteger F=A.subtract (e); BigInteger b=Cin.nextbiginteger (); BigInteger ans=f.multiply (d); Ans=Ans.divide (c); System. out. println (Ans.remainder (b)); } }}
View Code
Hdu 5666 Segment Russian multiplication or set of large boards