当前位置 博文首页 > ramay7:BC #80 B Segment(快速乘法、坑)

    ramay7:BC #80 B Segment(快速乘法、坑)

    作者:[db:作者] 时间:2021-09-04 09:21

    题目链接:
    BC #80 B Segment
    题意:
    Rivendell非常神,喜欢研究奇怪的问题.
    今天他发现了一个有趣的问题.找到一条线段 x+y=q ,
    令它和坐标轴在第一象限围成了一个三角形,然后画线连接了坐标原点和线段上坐标为整数的格点.
    请你找一找有多少点在三角形的内部且不是线段上的点,并将这个数对P取模后告诉他.
    pqqq1018,1P1018
    ??分析:
    答案的公式是 ans=(q?2)?(q?1)/2 ;
    但是因为p和q的范围太大!直接乘的话会爆long long!!!
    所以需要手动写快速乘法,类似于快速幂的写法!!!

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <climits>
    #include <cmath>
    using namespace std;
    
    int T;
    long long q,p;
    
    long long mul(long long a,long long b)
    {
        long long res=0,tmp=a;
        while(b){
            if(b&1) res=(res+tmp)%p;
            tmp=(tmp+tmp)%p;
            b>>=1;
        }
        return res;
    }
    
    int main()
    {
        freopen("80Bin.txt","r",stdin);
        scanf("%d",&T);
        while(T--){
            cin>>q>>p;
            if(q==2){
                printf("0\n");
                continue;
            }
            long long t=(q-1)/2%p;
            long long ans=mul(q-2,t);
            cout<<ans<<endl;
        }
        return 0;
    }
    cs