Skip to content

Fix modint stream input normalization - #6

Open
ucsd-zhl135 wants to merge 1 commit into
cp-algorithms:mainfrom
ucsd-zhl135:fix-modint-input-normalization
Open

Fix modint stream input normalization#6
ucsd-zhl135 wants to merge 1 commit into
cp-algorithms:mainfrom
ucsd-zhl135:fix-modint-input-normalization

Conversation

@ucsd-zhl135

Copy link
Copy Markdown

Summary

  • read modint stream input as int64_t
  • normalize extracted values through the constructor path
  • keep dynamic modint input compatible with Montgomery storage
  • regenerate the committed minified modint header

Verification

  • focused compile/runtime test for static and dynamic modint input, including negative values and int64_t endpoints
  • g++ -std=c++23 -Wall -Wextra -Wconversion -Werror -pedantic -O2 -I. verify/linalg/prod_dynamic_modint.test.cpp -o /tmp/prod_dynamic_modint_test
  • g++ -std=c++23 -Wall -Wextra -Wconversion -Werror -pedantic -O2 -I. verify/number_theory/dirichlet_mul.test.cpp -o /tmp/dirichlet_mul_test
  • git diff --check

@adamant-pwn adamant-pwn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! I think we'd need to make sure it doesn't affect tasks with already reduced input before merging it.

#include <iostream>
#include <cassert>
namespace cp_algo::math{template<typename modint,typename _Int>struct modint_base{using Int=_Int;using UInt=std::make_unsigned_t<Int>;static constexpr size_t bits=sizeof(Int)*8;using Int2=std::conditional_t<bits<=32,int64_t,__int128_t>;using UInt2=std::conditional_t<bits<=32,uint64_t,__uint128_t>;constexpr static Int mod(){return modint::mod();}constexpr static Int remod(){return modint::remod();}constexpr static UInt2 modmod(){return UInt2(mod())*mod();}constexpr modint_base()=default;constexpr modint_base(Int2 rr){to_modint().setr(UInt((rr+modmod())%mod()));}constexpr modint inv()const{return bpow(to_modint(),mod()-2);}modint operator-()const{modint neg;neg.r=std::min(-r,remod()-r);return neg;}modint&operator/=(const modint&t){return to_modint()*=t.inv();}modint&operator*=(const modint&t){r=UInt(UInt2(r)*t.r%mod());return to_modint();}modint&operator+=(const modint&t){r+=t.r;r=std::min(r,r-remod());return to_modint();}modint&operator-=(const modint&t){r-=t.r;r=std::min(r,r+remod());return to_modint();}modint operator+(const modint&t)const{return modint(to_modint())+=t;}modint operator-(const modint&t)const{return modint(to_modint())-=t;}modint operator*(const modint&t)const{return modint(to_modint())*=t;}modint operator/(const modint&t)const{return modint(to_modint())/=t;}auto operator==(const modint&t)const{return to_modint().getr()==t.getr();}auto operator!=(const modint&t)const{return to_modint().getr()!=t.getr();}auto operator<=(const modint&t)const{return to_modint().getr()<=t.getr();}auto operator>=(const modint&t)const{return to_modint().getr()>=t.getr();}auto operator<(const modint&t)const{return to_modint().getr()<t.getr();}auto operator>(const modint&t)const{return to_modint().getr()>t.getr();}Int rem()const{UInt R=to_modint().getr();return R-(R>(UInt)mod()/2)*mod();}constexpr void setr(UInt rr){r=rr;}constexpr UInt getr()const{return r;}static uint64_t modmod8(){return uint64_t(8*modmod());}void add_unsafe(UInt t){r+=t;}void pseudonormalize(){r=std::min(r,r-modmod8());}modint const&normalize(){if(r>=(UInt)mod()){r%=mod();}return to_modint();}void setr_direct(UInt rr){r=rr;}UInt getr_direct()const{return r;}protected:UInt r;private:constexpr modint&to_modint(){return static_cast<modint&>(*this);}constexpr modint const&to_modint()const{return static_cast<modint const&>(*this);}};template<typename modint>concept modint_type=std::is_base_of_v<modint_base<modint,typename modint::Int>,modint>;template<modint_type modint>decltype(std::cin)&operator>>(decltype(std::cin)&in,modint&x){typename modint::UInt r;auto&res=in>>r;x.setr(r);return res;}template<modint_type modint>decltype(std::cout)&operator<<(decltype(std::cout)&out,modint const&x){return out<<x.getr();}template<auto m>struct modint:modint_base<modint<m>,decltype(m)>{using Base=modint_base<modint<m>,decltype(m)>;using Base::Base;static constexpr Base::Int mod(){return m;}static constexpr Base::UInt remod(){return m;}auto getr()const{return Base::r;}};template<typename Int=int>struct dynamic_modint:modint_base<dynamic_modint<Int>,Int>{using Base=modint_base<dynamic_modint<Int>,Int>;using Base::Base;static Base::UInt m_reduce(Base::UInt2 ab){if(mod()%2==0)[[unlikely]]{return typename Base::UInt(ab%mod());}else{typename Base::UInt2 m=typename Base::UInt(ab)*imod();return typename Base::UInt((ab+m*mod())>>Base::bits);}}static Base::UInt m_transform(Base::UInt a){if(mod()%2==0)[[unlikely]]{return a;}else{return m_reduce(a*pw128());}}dynamic_modint&operator*=(const dynamic_modint&t){Base::r=m_reduce(typename Base::UInt2(Base::r)*t.r);return*this;}void setr(Base::UInt rr){Base::r=m_transform(rr);}Base::UInt getr()const{typename Base::UInt res=m_reduce(Base::r);return std::min(res,res-mod());}static Int mod(){return m;}static Int remod(){return 2*m;}static Base::UInt imod(){return im;}static Base::UInt2 pw128(){return r2;}static void switch_mod(Int nm){m=nm;im=m%2?inv2(-m):0;r2=static_cast<Base::UInt>(static_cast<Base::UInt2>(-1)%m+1);}auto static with_mod(Int tmp,auto callback){struct scoped{Int prev=mod();~scoped(){switch_mod(prev);}}_;switch_mod(tmp);return callback();}private:static thread_local Int m;static thread_local Base::UInt im,r2;};template<typename Int>Int thread_local dynamic_modint<Int>::m=1;template<typename Int>dynamic_modint<Int>::Base::UInt thread_local dynamic_modint<Int>::im=-1;template<typename Int>dynamic_modint<Int>::Base::UInt thread_local dynamic_modint<Int>::r2=0;}
namespace cp_algo::math{template<typename modint,typename _Int>struct modint_base{using Int=_Int;using UInt=std::make_unsigned_t<Int>;static constexpr size_t bits=sizeof(Int)*8;using Int2=std::conditional_t<bits<=32,int64_t,__int128_t>;using UInt2=std::conditional_t<bits<=32,uint64_t,__uint128_t>;constexpr static Int mod(){return modint::mod();}constexpr static Int remod(){return modint::remod();}constexpr static UInt2 modmod(){return UInt2(mod())*mod();}constexpr static UInt mod_normalize(Int2 rr){Int2 m=Int2(mod());rr%=m;return UInt(rr+(rr<0?m:0));}constexpr modint_base()=default;constexpr modint_base(Int2 rr){to_modint().setr(mod_normalize(rr));}constexpr modint inv()const{return bpow(to_modint(),mod()-2);}modint operator-()const{modint neg;neg.r=std::min(-r,remod()-r);return neg;}modint&operator/=(const modint&t){return to_modint()*=t.inv();}modint&operator*=(const modint&t){r=UInt(UInt2(r)*t.r%mod());return to_modint();}modint&operator+=(const modint&t){r+=t.r;r=std::min(r,r-remod());return to_modint();}modint&operator-=(const modint&t){r-=t.r;r=std::min(r,r+remod());return to_modint();}modint operator+(const modint&t)const{return modint(to_modint())+=t;}modint operator-(const modint&t)const{return modint(to_modint())-=t;}modint operator*(const modint&t)const{return modint(to_modint())*=t;}modint operator/(const modint&t)const{return modint(to_modint())/=t;}auto operator==(const modint&t)const{return to_modint().getr()==t.getr();}auto operator!=(const modint&t)const{return to_modint().getr()!=t.getr();}auto operator<=(const modint&t)const{return to_modint().getr()<=t.getr();}auto operator>=(const modint&t)const{return to_modint().getr()>=t.getr();}auto operator<(const modint&t)const{return to_modint().getr()<t.getr();}auto operator>(const modint&t)const{return to_modint().getr()>t.getr();}Int rem()const{UInt R=to_modint().getr();return R-(R>(UInt)mod()/2)*mod();}constexpr void setr(UInt rr){r=rr;}constexpr UInt getr()const{return r;}static uint64_t modmod8(){return uint64_t(8*modmod());}void add_unsafe(UInt t){r+=t;}void pseudonormalize(){r=std::min(r,r-modmod8());}modint const&normalize(){if(r>=(UInt)mod()){r%=mod();}return to_modint();}void setr_direct(UInt rr){r=rr;}UInt getr_direct()const{return r;}protected:UInt r;private:constexpr modint&to_modint(){return static_cast<modint&>(*this);}constexpr modint const&to_modint()const{return static_cast<modint const&>(*this);}};template<typename modint>concept modint_type=std::is_base_of_v<modint_base<modint,typename modint::Int>,modint>;template<modint_type modint>decltype(std::cin)&operator>>(decltype(std::cin)&in,modint&x){int64_t r;auto&res=in>>r;if(res){x=modint(r);}return res;}template<modint_type modint>decltype(std::cout)&operator<<(decltype(std::cout)&out,modint const&x){return out<<x.getr();}template<auto m>struct modint:modint_base<modint<m>,decltype(m)>{using Base=modint_base<modint<m>,decltype(m)>;using Base::Base;static constexpr Base::Int mod(){return m;}static constexpr Base::UInt remod(){return m;}auto getr()const{return Base::r;}};template<typename Int=int>struct dynamic_modint:modint_base<dynamic_modint<Int>,Int>{using Base=modint_base<dynamic_modint<Int>,Int>;using Base::Base;static Base::UInt m_reduce(Base::UInt2 ab){if(mod()%2==0)[[unlikely]]{return typename Base::UInt(ab%mod());}else{typename Base::UInt2 m=typename Base::UInt(ab)*imod();return typename Base::UInt((ab+m*mod())>>Base::bits);}}static Base::UInt m_transform(Base::UInt a){if(mod()%2==0)[[unlikely]]{return a;}else{return m_reduce(a*pw128());}}dynamic_modint&operator*=(const dynamic_modint&t){Base::r=m_reduce(typename Base::UInt2(Base::r)*t.r);return*this;}void setr(Base::UInt rr){Base::r=m_transform(rr);}Base::UInt getr()const{typename Base::UInt res=m_reduce(Base::r);return std::min(res,res-mod());}static Int mod(){return m;}static Int remod(){return 2*m;}static Base::UInt imod(){return im;}static Base::UInt2 pw128(){return r2;}static void switch_mod(Int nm){m=nm;im=m%2?inv2(-m):0;r2=static_cast<Base::UInt>(static_cast<Base::UInt2>(-1)%m+1);}auto static with_mod(Int tmp,auto callback){struct scoped{Int prev=mod();~scoped(){switch_mod(prev);}}_;switch_mod(tmp);return callback();}private:static thread_local Int m;static thread_local Base::UInt im,r2;};template<typename Int>Int thread_local dynamic_modint<Int>::m=1;template<typename Int>dynamic_modint<Int>::Base::UInt thread_local dynamic_modint<Int>::im=-1;template<typename Int>dynamic_modint<Int>::Base::UInt thread_local dynamic_modint<Int>::r2=0;}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change /min/, it's done by bot.

Comment on lines +102 to +104
if(res) {
x = modint(r);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(res) {
x = modint(r);
}
x = modint(r);

This is competitive programming, we don't expect input to ever fail.

Comment on lines +24 to +28
constexpr static UInt mod_normalize(Int2 rr) {
Int2 m = Int2(mod());
rr %= m;
return UInt(rr + (rr < 0 ? m : 0));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds a lot of overhead in input-heavy tasks. In particular, for Convolution (Large):

So, +32% / 110 ms on input reading on most tasks that already give input mod-reduced.

I tried changing to rr %= m to rr = std::abs(rr) >= m ? rr % m : rr;, but it only reduced time to 421 ms, still a lot of overhead on the case that almost never happens.

dynamic_modint is also affected, going from from 393 ms to 484 ms, as it needs to use idiv, instead of Barrett reduction.

I think if we really want to add reduction to input reading, we should somehow make sure it doesn't affect tasks where it is not needed, and I'm not fully sure whether there is a good way to do it. And if we do, we likely need to write a specialization for dynamic_modint that uses m_transform(m_reduce(rr)) instead of % (I didn't check the details here yet).

@adamant-pwn

Copy link
Copy Markdown
Member

convolution_mul.test.cpp fails to compile without warnings now...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants