Skip to content

Commit

Permalink
Optimize PrimitiveRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
kzrnm committed Jan 17, 2023
1 parent 1c3eacd commit b4174db
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions Source/AtCoderLibrary/Math/Internal/InternalMath.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
#if NETCOREAPP3_1_OR_GREATER
using System.Numerics;
#endif

namespace AtCoder.Internal
{
Expand Down Expand Up @@ -46,26 +49,22 @@ public static int PrimitiveRoot<TMod>() where TMod : struct, IStaticMod
}
static int PrimitiveRootCalculate<TMod>() where TMod : struct, IStaticMod
{
int m = (int)default(TMod).Mod;
Span<int> divs = stackalloc int[20];
var m = default(TMod).Mod;
Span<uint> divs = stackalloc uint[20];
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;

while (x % 2 == 0)
{
x >>= 1;
}
var x = m - 1;
x >>= BitOperations.TrailingZeroCount(x);

for (int i = 3; (long)i * i <= x; i += 2)
for (uint i = 3; (long)i * i <= x; i += 2)
{
if (x % i == 0)
{
divs[cnt++] = i;
while (x % i == 0)
do
{
x /= i;
}
} while (x % i == 0);
}
}

Expand All @@ -81,7 +80,7 @@ static int PrimitiveRootCalculate<TMod>() where TMod : struct, IStaticMod
if (new StaticModInt<TMod>(g).Pow((m - 1) / d).Value == 1)
goto NEXT;
return g;
NEXT: { }
NEXT:;
}
}

Expand Down

0 comments on commit b4174db

Please sign in to comment.