Created
February 18, 2024 07:50
-
-
Save natsugiri/36ed4409b68825b1030abb98f78871ad to your computer and use it in GitHub Desktop.
ModIntTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
public class Main { | |
static long state; | |
static long rand() { | |
state = (state * 123456789 + 765321) % 1000000007; | |
return state; | |
} | |
public static void main(String args[]) { | |
new Main().test(); | |
} | |
void test() { | |
state = 0; | |
int n = 10000; | |
long[] a = new long[n]; | |
long[] b = new long[n]; | |
for (int i = 0; i < n; i++) { | |
a[i] = rand(); | |
b[i] = rand(); | |
} | |
long result; | |
result = new Test1().test(a, b); | |
// result = new Test2().test(a, b); | |
// result = new Test3().test(a, b); | |
// result = new Test4().test(a, b); | |
// result = new Test5().test(a, b); | |
// result = new Test6().test(a, b); | |
System.out.println(result); | |
} | |
} | |
class Test1 { | |
long test(long[] a, long b[]) { | |
System.out.println("Test1"); | |
final int mod = 1000000007; | |
long ans = 1; | |
long time = System.currentTimeMillis(); | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < b.length; j++) { | |
ans = (ans * ((a[i] + b[j]) % mod)) % mod; | |
} | |
} | |
System.out.println(System.currentTimeMillis() - time); | |
return ans; | |
} | |
} | |
class Test2 { | |
long test(long[] a, long b[]) { | |
System.out.println("Test2 no-final"); | |
long mod = 1000000007; | |
long ans = 1; | |
long time = System.currentTimeMillis(); | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < b.length; j++) { | |
ans = (ans * ((a[i] + b[j]) % mod)) % mod; | |
} | |
} | |
System.out.println(System.currentTimeMillis() - time); | |
return ans; | |
} | |
} | |
class Test3 extends ModIntStatic { | |
long test(long[] a, long b[]) { | |
System.out.println("Test3 long functions"); | |
long ans = 1; | |
long time = System.currentTimeMillis(); | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < b.length; j++) { | |
ans = mul(ans, add(a[i], b[j])); | |
} | |
} | |
System.out.println(System.currentTimeMillis() - time); | |
return ans; | |
} | |
} | |
class ModIntStatic { | |
static final long mod = 1000000007; | |
static long add(long x, long y) { | |
return (x + y) % mod; | |
} | |
static long mul(long x, long y) { | |
return x * y % mod; | |
} | |
} | |
class Test4 extends ModInt.ModInt1000000007 { | |
long test(long[] a, long b[]) { | |
System.out.println("Test4 long functions override"); | |
long ans = 1; | |
long time = System.currentTimeMillis(); | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < b.length; j++) { | |
ans = mul(ans, add(a[i], b[j])); | |
} | |
} | |
System.out.println(System.currentTimeMillis() - time); | |
return ans; | |
} | |
} | |
interface ModInt { | |
public long mod(); | |
class ModInt998244353 implements ModInt { public long mod() { return 998244353; } } | |
class ModInt1000000007 implements ModInt { public long mod() { return 1000000007; } } | |
default long add(long x, long y) { | |
return (x + y) % mod(); | |
} | |
default long mul(long x, long y) { | |
return x * y % mod(); | |
} | |
} | |
class Test5 { | |
long test(long[] a, long b[]) { | |
System.out.println("Test5 class Mint"); | |
Mint ans = new Mint(1); | |
Mint[] aa = new Mint[a.length]; | |
Mint[] bb = new Mint[b.length]; | |
for (int i = 0; i < a.length; i++) aa[i] = new Mint(a[i]); | |
for (int j = 0; j < b.length; j++) bb[j] = new Mint(b[j]); | |
long time = System.currentTimeMillis(); | |
for (int i = 0; i < aa.length; i++) { | |
for (int j = 0; j < bb.length; j++) { | |
ans = ans.mul(aa[i].add(bb[j])); | |
} | |
} | |
System.out.println(System.currentTimeMillis() - time); | |
return ans.x; | |
} | |
class Mint { | |
static final long mod = 1000000007; | |
final long x; | |
Mint(long x) { this.x = x; } | |
Mint add(Mint y) { | |
return new Mint((x + y.x) % mod); | |
} | |
Mint mul(Mint y) { | |
return new Mint(x * y.x % mod); | |
} | |
} | |
} | |
class Test6 { | |
long test(long[] a, long b[]) { | |
System.out.println("Test6 Mutable Mint"); | |
Mint ans = new Mint(1); | |
Mint[] aa = new Mint[a.length]; | |
Mint[] bb = new Mint[b.length]; | |
for (int i = 0; i < a.length; i++) aa[i] = new Mint(a[i]); | |
for (int j = 0; j < b.length; j++) bb[j] = new Mint(b[j]); | |
Mint tmp = new Mint(0); | |
long time = System.currentTimeMillis(); | |
for (int i = 0; i < aa.length; i++) { | |
for (int j = 0; j < bb.length; j++) { | |
ans.mulAssign(tmp.assign(aa[i]).addAssign(bb[j])); | |
} | |
} | |
System.out.println(System.currentTimeMillis() - time); | |
return ans.x; | |
} | |
class Mint { | |
static final long mod = 1000000007; | |
long x; | |
Mint(long x) { this.x = x; } | |
Mint assign(Mint y) { | |
x = y.x; | |
return this; | |
} | |
Mint addAssign(Mint y) { | |
x = (x + y.x) % mod; | |
return this; | |
} | |
Mint mulAssign(Mint y) { | |
x = x * y.x % mod; | |
return this; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment