You are given an integer m, and a list of n distinct integers between 0 and m - 1.
You would like to construct a sequence satisfying the properties:
- Each element is an integer between 0 and m - 1, inclusive.
- All prefix products of the sequence modulo m are distinct.
- No prefix product modulo m appears as an element of the input list.
- The length of the sequence is maximized.
Construct any sequence satisfying the properties above.
The first line of input contains two integers n and m (0 ≤ n < m ≤ 200 000) — the number of forbidden prefix products and the modulus.
If n is non-zero, the next line of input contains n distinct integers between 0 and m - 1, the forbidden prefix products. If n is zero, this line doesn't exist.
On the first line, print the number k, denoting the length of your sequence.
On the second line, print k space separated integers, denoting your sequence.
0 5
5
1 2 4 3 0
3 10
2 9 1
6
3 9 2 9 8 0
For the first case, the prefix products of this sequence modulo m are [1, 2, 3, 4, 0].
For the second case, the prefix products of this sequence modulo m are [3, 7, 4, 6, 8, 0].
题意:构造一个序列,使得序列前缀积%m互不相同且不等于某些数,使序列最长。
题解:考虑i * x = j(%m)当且仅当gcd(m,j)%gcd(m,i)==0,可以构造出一个图
我们把gcd(i,m)相等的缩到一个SCC,然后dp求出最长路
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill( x, y ) memset( x, y, sizeof x )
#define copy( x, y ) memcpy( x, y, sizeof x )
using namespace std;
typedef long long LL;
typedef pair < int, int > pa;
const int MAXN = 200005;
int n, m, cur, pre = 1, nxt;
pa dp[MAXN];
bool ban[MAXN];
vector < int > v[MAXN], ans;
inline int exgcd(int a, int b, int &x, int &y)
{
if( !b ) { x = 1; y = 0; return a; }
int d = exgcd( b, a % b, y, x );
y -= a / b * x;
return d;
}
int main()
{
#ifdef wxh010910
freopen( "data.in", "r", stdin );
#endif
scanf( "%d%d", &m, &n );
for( int i = 1, x ; i <= m ; i++ ) scanf( "%d", &x ), ban[ x ] = 1;
for( int i = 1 ; i < n ; i++ ) if( !ban[ i ] ) v[ __gcd( n, i ) ].pb( i );
v[ cur = n ].pb( 0 );
for( int i = 1 ; i <= n ; i++ )
{
dp[ i ] = mp( 0, 0 );
if( i ^ 1 )
for( int j = 1 ; j * j <= i ; j++ ) if( i % j == 0 )
{
dp[ i ] = max( dp[ i ], mp( dp[ j ].xx, j ) );
if( j ^ 1 ) dp[ i ] = max( dp[ i ], mp( dp[ i / j ].xx, i / j ) );
}
dp[ i ].xx += v[ i ].size();
}
while( cur )
{
for( auto x : v[ cur ] ) ans.pb( x );
cur = dp[ cur ].yy;
}
reverse( ans.begin(), ans.end() );
if( ban[ 0 ] ) ans.pop_back();
printf( "%d\n", ans.size() );
for( auto cur : ans )
{
int x, y, d = exgcd( pre, n, x, y );
x %= n; if( x < 0 ) x += n;
printf( "%d ", 1LL * ( cur / d ) * x % n );
pre = cur;
}
putchar( 10 );
return 0;
}