跳到正文

个人记录

Barret约减可以加速取模

Helioca约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年11月24日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;

#define ll long long
struct Barret{ // 模数 long long
    ll m,p;
    void init(ll pp){
		m=((__int128)1<<64)/pp;
		p=pp;
	}
    inline ll operator ()(__int128 x){
        return x-((x*m)>>64)*p;
    }
} mod;

int main(){
	mod.init(5);
	cout<<mod(2*3);
}
struct Barret{ // 模数 int。
    int m,p;
    void init(int pp){
		m=((ll)1<<32)/pp;
		p=pp;
	}
    inline int operator ()(ll x){
        return x-((x*m)>>32)*p;
    }
} mod;

来自Hagasei的模板

原文