1+ /**
2+ * @verified
3+ * <ul>
4+ * <li> https://atcoder.jp/contests/arc050/tasks/arc050_c
5+ * <li> https://atcoder.jp/contests/abc129/tasks/abc129_f
6+ * </ul>
7+ */
18class ModIntFactory {
29 private final ModArithmetic ma ;
310 private final int mod ;
@@ -23,6 +30,12 @@ private ModInt(int value) {
2330 public int mod () {
2431 return mod ;
2532 }
33+ public int value () {
34+ if (ma instanceof ModArithmetic .ModArithmeticMontgomery ) {
35+ return ((ModArithmetic .ModArithmeticMontgomery ) ma ).reduce (value );
36+ }
37+ return value ;
38+ }
2639 public ModInt add (ModInt mi ) {
2740 return new ModInt (ma .add (value , mi .value ));
2841 }
@@ -41,11 +54,21 @@ public ModInt inv() {
4154 public ModInt pow (long b ) {
4255 return new ModInt (ma .pow (value , b ));
4356 }
44- public int value () {
45- if (ma instanceof ModArithmetic .ModArithmeticMontgomery ) {
46- return ((ModArithmetic .ModArithmeticMontgomery ) ma ).reduce (value );
47- }
48- return value ;
57+ public ModInt addAsg (ModInt mi ) {
58+ this .value = ma .add (value , mi .value );
59+ return this ;
60+ }
61+ public ModInt subAsg (ModInt mi ) {
62+ this .value = ma .sub (value , mi .value );
63+ return this ;
64+ }
65+ public ModInt mulAsg (ModInt mi ) {
66+ this .value = ma .mul (value , mi .value );
67+ return this ;
68+ }
69+ public ModInt divAsg (ModInt mi ) {
70+ this .value = ma .div (value , mi .value );
71+ return this ;
4972 }
5073 @ Override
5174 public String toString () {
@@ -151,7 +174,8 @@ public int inv(int a) {
151174 public int pow (int a , long b ) {
152175 if (b < 0 ) throw new ArithmeticException ("negative power" );
153176 long res = 1 ;
154- long pow2 = a , idx = 1 ;
177+ long pow2 = a ;
178+ long idx = 1 ;
155179 while (b > 0 ) {
156180 long lsb = b & -b ;
157181 for (; lsb != idx ; idx <<= 1 ) {
@@ -201,7 +225,8 @@ public int inv(int a) {
201225 public int pow (int a , long b ) {
202226 if (b < 0 ) throw new ArithmeticException ("negative power" );
203227 long res = 1 ;
204- long pow2 = a , idx = 1 ;
228+ long pow2 = a ;
229+ long idx = 1 ;
205230 while (b > 0 ) {
206231 long lsb = b & -b ;
207232 for (; lsb != idx ; idx <<= 1 ) {
@@ -323,7 +348,8 @@ public int inv(int a) {
323348 public int pow (int a , long b ) {
324349 if (b < 0 ) throw new ArithmeticException ("negative power" );
325350 int res = 1 ;
326- int pow2 = a , idx = 1 ;
351+ int pow2 = a ;
352+ long idx = 1 ;
327353 while (b > 0 ) {
328354 long lsb = b & -b ;
329355 for (; lsb != idx ; idx <<= 1 ) {
0 commit comments