From 3c14a9b5a842d62e03806850a4be1376d79c9a67 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sat, 16 Apr 2016 20:35:46 +0800 Subject: [PATCH] Create 1002.cpp --- BestCoder/Round-80/1002.cpp | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 BestCoder/Round-80/1002.cpp diff --git a/BestCoder/Round-80/1002.cpp b/BestCoder/Round-80/1002.cpp new file mode 100644 index 0000000..7779d0e --- /dev/null +++ b/BestCoder/Round-80/1002.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; +long long q_mul( long long a, long long b, long long mod ) //快速计算 (a*b) % mod +{ + long long ans = 0; // 初始化 + while(b) //根据b的每一位看加不加当前a + { + if(b & 1) //如果当前位为1 + { + b--; + ans =(ans+ a)%mod; //ans+=a + } + b /= 2; //b向前移位 + a = (a + a) % mod; //更新a + + } + return ans; +} + +int main() +{ + int t; + scanf("%d",&t); + while(t--) + { + long long q,p; + scanf("%I64d %I64d",&q,&p); + long long a=q-1; + long long b=q-2; + long long ans; + if(a%2==0) + { + ans=q_mul(a/2,b,p); + } + else + { + ans=q_mul(a,b/2,p); + } + printf("%I64d\n",ans); + } + return 0; +}