hdu 5666

来源:互联网 发布:怎么复制汇总数据 编辑:程序博客网 时间:2024/06/10 14:28

题意:求x+y=q与坐标轴围成的三角形中,有多少个整数点坐标(不包括边界)

解析:注意数据很大,要用到快速乘

#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;typedef long long ll;long long q,p;int t;ll qmul(ll a,ll b,ll m) {    ll ans=0;    while(b) {        if(b&1) (ans+=a) %= m;        (a=a*2) %= m;        b/=2;    }    return ans;}int main(){    scanf("%d",&t);    while(t--)    {        scanf("%lld%lld",&q,&p);        ll ans;        if(!((q-1)%2))        ans=qmul((q-1)/2,q-2,p);        else        ans=qmul(q-1,(q-2)/2,p);        printf("%lld\n",ans%p);    }    return 0;}


0 0
原创粉丝点击