您的位置:首页 > 其它

HDU 5666 Segment(快速乘法/快速幂改)

2016-04-17 09:24 302 查看
题目: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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: