Barret约减可以加速取模

#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的模板

posted @ 2025-02-13 10:30  Helioca  阅读(63)  评论(0)    收藏  举报
Document