首页 > 其他分享 >高精度板子

高精度板子

时间:2023-11-24 19:15:39浏览次数:25  
标签:node 10 ch 高精度 int len 板子 while

高精度模板

copy老师的代码

@_xuefeng


#include<bits/stdc++.h>
using namespace std;
char ch[500000];
struct node{
    int s[1000000],len;
    void init(){
        scanf("%s",ch+1);
        len=strlen(ch+1);
        for(int i=1;i<=len;++i) s[len-i+1]=ch[i]-48;
    }
    void multi(){
        ++len;
        for(int i=len;i;--i) s[i]=s[i-1];
    }
    void divid(){
        for(int i=1;i<=len;++i) s[i]=s[i+1];
        --len;
    }
    friend bool operator <= (const node &a,const node &b){
        if(a.len^b.len) return a.len<b.len;
        for(int i=a.len;i;--i) if(a.s[i]^b.s[i]) return a.s[i]<b.s[i];
        return 1;
    }
    friend void operator -= (node &a,const node &b){
        for(int i=1;i<=b.len;++i) a.s[i]-=b.s[i];
        for(int i=1;i<=a.len;++i) if(a.s[i]<0) a.s[i]+=10,a.s[i+1]--;
        while(a.len!=1&&a.s[a.len]==0) a.len--;
    }
    friend void operator += (node &a,const node &b){
        a.len=max(a.len,b.len)+1;
        for(int i=1;i<=a.len;++i) a.s[i]+=b.s[i];
        for(int i=1;i<=a.len;++i) if(a.s[i]>9) a.s[i]-=10,a.s[i+1]++;
        while(a.len!=1&&a.s[a.len]==0) a.len--;
    }
    friend void operator *= (node &a,const node &b){
        node c;c.len=a.len*b.len+10;
        for(int i=1;i<=a.len;++i)
            for(int j=1;j<=b.len;++j)
                c.s[i+j-1]+=a.s[i]*b.s[j];
        for(int i=1;i<=c.len;++i) if(c.s[i]>9) c.s[i+1]+=c.s[i]/10,c.s[i]%=10;
        while(c.len!=1&&c.s[c.len]==0) c.len--;
        a=c;
    }
    friend void operator %= (node &a,const node &d){
        node b=d;int x=b.len;
        while(b<=a) b.multi();b.divid();
        node c;c.s[c.len=0]=0;
        while(b.len>=x){
            ++c.len;
            while(b<=a) a-=b,++c.s[c.len];
            b.divid();
        }
        c.len=max(c.len,1);//防止a<d的情况出不来数
    }
    friend void operator /= (node &a,const node &d){
        node b=d;int x=b.len;
        while(b<=a) b.multi();b.divid();
        node c;c.s[c.len=0]=0;
        while(b.len>=x){
            ++c.len;
            while(b<=a) a-=b,++c.s[c.len];
            b.divid();
        }
        c.len=max(c.len,1);//防止a<d的情况出不来数
        a=c;
    }
}n,m;
int main(){
    n.init(),m.init();
    n/=m;
    for(int i=1;i<=n.len;++i)printf("%d",n.s[i]);

}

标签:node,10,ch,高精度,int,len,板子,while
From: https://www.cnblogs.com/gsczl71/p/17854537.html

相关文章

  • KMP板子
    updateon2023.11.17NOIP前来复习板子,发现KMP整理的不是很到位,所以更新详细一些。模板题抽象的blog浅显易懂的讲解视频:(dalao讲得太好了\(%%%\))备用网址\(kmp\)(字符串匹配)的概念:主串:被匹配的字符串模式串:匹配的串最长前后缀:一个字符串某个前缀后后缀相同,而且长度尽可......
  • 【开源】int,long long去一边去:高精度大合集!
    加法\(add\)stringadd(strings1,strings2){//时间复杂度O(logn)stringres="";intcarry=0,i=0;while(i<int(s1.size())||i<int(s2.size())||carry>0){inta=(i<int(s1.size()))?(s1[int(s1.size())-i-1]-'0'......
  • 计算几何板子
    #definei128longlonginlinei128ABS(i128x){returnx<0?-x:x;}structfrac{ i128x,y; frac(){} frac(i128xx,i128yy=1ll):x(xx),y(yy){ if(y<0)x=-x,y=-y; } friendfracoperator+(fraca,fracb){returnfrac(a.x*b.y+a.y*b.x,a.y*b.y);} friendf......
  • 高精度模版
    高精度加法vector<int>add(vector<int>&A,vector<int>&B){//654321654321vector<int>C;inttemp=0;for(inti=0;i<A.size()||i<B.size();++i){if(i<A.size())temp+=A[i];......
  • 写板子的时候发现的易错点
    KMPvoidget_nt(){intj=0;for(inti=2;i<=tl;++i){while(j&&t[i]!=t[j+1])j=nt[j];if(t[j+1]==t[i])j+=1;nt[i]=j;}}voidKMP(){intj=0;F(i,1,sl){while(j&&s[i]!=t[j+1])j=nt[j]......
  • 大非质数取模算组合数板子
    constintN=1e5+10,M=13;intn,mod,l,r;llans,p[M],br[M],phi;inlinellksm(lla,llb){ lld=1; while(b){ if(b&1)d=d*a%mod; a=a*a%mod; b>>=1; } returnd;}namespacebig_mod{ structnum{ llx,r[M]; }fac[N],I,B; inlinenumoperat......
  • 写了个高精度加法板子
    #include<bits/stdc++.h>using namespace std;const int N=1e4+9;int a1[1000],b1[1000],ans[1000];void add(int a[],int b[],int na,int nb){int t=0;if(na<nb)return add(b,a,nb,na);for(int i=0;i<na;i++){t+=a[i];if(i<nb)t+=b[i];ans[i]=t%10;t/=10;}if(......
  • 【板子申请】Ai-M61-32S开发环境搭建-wuboy19
    【板子申请】Ai-M61-32S开发环境搭建-wuboy19window10vscode环境安装vscode官网下载windows版本图1官网界面图图2安装成功图博主百度网盘下载百度网盘链接提取码:9jydgit安装git官网下载链接图3git安装过程图博主网盘下载git百度网盘链接提取码:n3z......
  • 树套树板子,但是带修莫队+值域分块
    \(\text{Link-LuoguBlog}\)原题传送门没啥重要的事情,就是终于过了这题非常开心,发现自己是莫队的时间戳部分写错了调了114514年我也只能说是十分趣味。以及今天深刻地认识到了带修莫队应该len=pow(n,0.66);。就是裸的带修莫队+值域分块,就不说了,直接放代码了昂。如果有人......
  • 高精度
    #include<bits/stdc++.h>#defineN20005#defineM40005#definelllonglong#defineahz.w#definebkz.w#defineckzkz.w#definedkzzf.w#definehphz.p#definekpkz.p#definecpkzkz.p#definedpkzzf.p#defineP998244353#defineinv(x)ksm(x......