CodeForces 772C. Vulnerable Kerbals

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.

Input

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.

Output

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.

Examples
input
0 5
output
5
1 2 4 3 0
input
3 10
2 9 1
output
6
3 9 2 9 8 0
Note

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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值