@hdu - 6427@ Problem B. Beads


@description@

There are m different colored beads, color respectively 1 ~ m, each color of the arbitrary number of beads. N You need to choose the beads to form a necklace.

If at any time after the operation following a necklace A to B, we believe that A and B are equal.

(1) rotating the necklace.
(2) The necklace flip.
(3) while each bead necklace replaced in its color, the color will be replaced i i mod m + 1.

The number of final demand different necklace.

Input
first line T represents an integer number of data sets.
For each set of data comprises two integers n, m, and the length of the necklace described color number.
1 ≤ T ≤ 20, 3 ≤ n ≤ 10 ^ 18, 2 ≤ m ≤ 10 ^ 18, 998244353 not divisible by n, m

The Output
998 244 353 For each test, the answer mode output.

Sample Input
5
3 2
4 2
8 5
9 5
2333 333
Sample Output
2
4
5079
22017
544780894

Hint
for n = 3, m = 2:

  • [1, 1, 1], [2, 2, 2] is equivalent
  • [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2] is equivalent

@solution@

More typical burnside lemma counting problem.

First we subtract a full color, becomes 0 ~ m 1-between color. This can be directly modulo.

Operation can be divided into two categories title: Color shift operation and operation.
Wherein the rotation shift operation is the classic problem of flip +, constituting the permutation group G1, G1 comprise 2 * n permutation.
The operation consisting of color group G2 contains the CCP permutation m, respectively corresponding to the operation color i becomes i, (i + 1) mod m, (i + 2) mod m, ....
For a necklace may be substituted with a substituted displacement put a replacement color composition, so that this group is substituted G3, there | G3 | = | G1 | * | G2 | = 2 * n * m.

Consider the case where the number of substitution with a color point permutation is nested in a fixed displacement.
Analogy polya theorem, we consider the number of programs as displacement loop formed dyed, and then multiplied by the number of cycles of all programs.

Suppose one cycle (a1, a2, a3, ... ak), to be the same if the color change amount d of the case, have to satisfy a1 + d = a2, a2 + d = a3, ..., ak + d = a1 (where "addition" is redefined under addition modulo sense).
So there are k * d = 0 mod m. Since d is a number between 0 ~ 1 m-, so the total value of d gcd (m, k) species.
Note that the value d should be satisfied for each cycle, for the same size so that each cycle of rotation is not important, but the size of flipping may exist in cycle 1 and 2.

If the displacement is reversed, it can be classified discussions directly.
If the displacement is a rotation, it is enumerated as the size factor n cycles, corresponding to the calculated contributions calculated Euler function.
Then because a large n, it is necessary to accelerate the process of decomposition of pollard-rho factor.

@accepted code@

#include<map>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
typedef long long ll;
const int MOD = 998244353;
const int PRM[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
map<ll, int>mp;
int pow_mod(int b, int p) {
    int ret = 1;
    while( p ) {
        if( p & 1 ) ret = 1LL*ret*b%MOD;
        b = 1LL*b*b%MOD;
        p >>= 1;
    }
    return ret;
}
ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a%b);}
ll mul_mod(ll a, ll b, ll mod) {
    a %= mod, b %= mod;
    ll ret = 0;
    while( b ) {
        if( b & 1 ) ret = (ret + a) % mod;
        a = (a + a) % mod;
        b >>= 1;
    }
    return ret;
}
ll pow_mod(ll b, ll p, ll mod) {
    ll ret = 1;
    while( p ) {
        if( p & 1 ) ret = mul_mod(ret, b, mod);
        b = mul_mod(b, b, mod);
        p >>= 1;
    }
    return ret;
}
bool Miller_Rabin(ll n, ll m, int a) {
    if( n == a ) return true;
    else if( n % a == 0 ) return false;
    ll x = pow_mod(a, m, n);
    if( x == 1 || x == n-1 )
        return true;
    while( m != n-1 ) {
        x = mul_mod(x, x, n), m *= 2;
        if( x == 1 ) return false;
        else if( x == n-1 ) return true;
    }
    return false;
}
bool IsPrime(ll n) {
    ll m = n-1;
    while( m % 2 == 0 ) m /= 2;
    for(int i=0;i<10;i++)
        if( !Miller_Rabin(n, m, PRM[i]) )
            return false;
    return true;
}
ll PollardRho(ll n) {
    if( n == 1 ) return n;
    if( n % 2 == 0 ) return 2;
    ll x = rand() % (n-2) + 2, y = x;
    ll c = rand() % (n-1) + 1, d = 1;
    while( d == 1 ) {
        x = (mul_mod(x, x, n) + c) % n;
        y = (mul_mod(y, y, n) + c) % n;
        y = (mul_mod(y, y, n) + c) % n;
        d = gcd(n, x > y ? x-y : y-x);
    }
    return d;
}
void GetDiv(ll x) {
    if( x == 1 ) return ;
    if( IsPrime(x) )
        mp[x]++;
    else {
        ll k = PollardRho(x);
        GetDiv(k), GetDiv(x/k);
    }
}
int ans; ll n, m;
vector<pair<ll, int> >d;
void dfs(int x, ll nw, ll phi) {
    if( x == d.size() ) {
        ans = (ans + 1LL*(gcd(m, nw)%MOD)*pow_mod((m%MOD), (n/nw)%(MOD-1))%MOD*(phi%MOD)%MOD)%MOD;
        return ;
    }
    dfs(x + 1, nw, phi);
    nw *= d[x].first, phi *= d[x].first - 1;
    dfs(x + 1, nw, phi);
    for(int i=2;i<=d[x].second;i++) {
        nw *= d[x].first, phi *= d[x].first;
        dfs(x + 1, nw, phi);
    }
}
void solve() {
    scanf("%lld%lld", &n, &m);
    mp.clear(), d.clear(), GetDiv(n);
    for(map<ll, int>::iterator it=mp.begin();it!=mp.end();it++)
        d.push_back(*it);
    ans = 0; dfs(0, 1, 1);
    if( n & 1 )
        ans = (ans + (n%MOD)*pow_mod((m%MOD), ((n+1)/2)%(MOD-1))%MOD)%MOD;
    else {
        ans = (ans + ((n/2)%MOD)*pow_mod((m%MOD), (n/2)%(MOD-1))%MOD*gcd(2, m)%MOD)%MOD;
        ans = (ans + ((n/2)%MOD)*pow_mod((m%MOD), (n/2+1)%(MOD-1))%MOD)%MOD;
    }
    printf("%lld\n", 1LL*ans*pow_mod((2*n%MOD)*(m%MOD)%MOD, MOD-2)%MOD);
}
int main() {
    srand(20041112);
    int T; scanf("%d", &T);
    for(int i=1;i<=T;i++) solve();
}

@details@

Regarded as the classic mid-range difficulty of the title bar. There is no write up the details of particular note.

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11233214.html