题目:http://acm.hdu.edu.cn/showproblem.php?pid=5666
代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
__int64 q,p;
scanf("%I64d%I64d",&q,&p);
__int64 ans=0,a=q-1,b=q-2;
if(b%2==0) //规律是(q-1)*(q-2)/2;
b=b/2;
else<span style="white-space:pre"> </span>//讨论<span style="font-family: Arial, Helvetica, sans-serif;">(q-1)与(q-2)的奇偶,</span>
a=a/2;
while(b)
{
if(b%2==1)
ans=(ans+a)%p;
b=(b/2)%p;
a=(a+a)%p;
}
printf("%I64d\n",ans);
}
}