码迷,mamicode.com
首页 > 其他好文 > 详细

慢速乘模板(支持负数与零

时间:2019-08-07 20:46:54      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:++i   color   char   cstring   for   void   clu   efi   har   

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)
//by war
//2019.8.7
using namespace std;
long long a,b,p;
void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<0||c>9){if(c==-)y=-1;c=getchar();}
    while(c<=9&&c>=0){ x=(x<<1)+(x<<3)+c-0;c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p(-);x=-x;}
    if(x>9)o(x/10);
    p(x%10+0);
}

long long msc(long long a,long long b){
    long long r=0,y=1;
    if((a^b)<0)  y*=-1;
    a=abs(a);b=abs(b);
    while(b>0){
        if(b&1)
            r=(r+a)%p;
        a=(a<<1)%p;
        b>>=1;
    }
    return r*y;
}

signed main(){
    in(a);in(b);in(p);
    o(msc(a,b));
    return 0;
}

 

慢速乘模板(支持负数与零

标签:++i   color   char   cstring   for   void   clu   efi   har   

原文地址:https://www.cnblogs.com/war1111/p/11317574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!