1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "vector" #include "set" #include "map" #include "bitset" #include "cmath" #include "ctime" #include "unordered_map" #include "queue" using namespace std; #define rep( i , a , b ) for( int i = a , i##end = b ; i <= i##end ; ++ i ) #define per( i , a , b ) for( int i = a , i##end = b ; i >= i##end ; -- i ) #define mp make_pair #define fi first #define se second #define pii pair<int,int> #define all( a ) a.begin() , a.end() #define pb push_back #define eb emplace_back #define vi vector<int> const int MAXN = 2e5 + 6; typedef long long ll; const int P = 998244353; int n , k;
int pri[10000006] , mnp[10000006] , mu[10000006] , cn = 0; void sieve( ) { mu[1] = 1; rep( i , 2 , 1e7 ) { if( !pri[i] ) pri[++cn] = i , mu[i] = -1 , mnp[i] = i; for( int j = 1 ; j <= cn && pri[j] * i < 10000000 ; ++j ) { mnp[i * pri[j]] = pri[j] , pri[i * pri[j]] = 1; if( i % pri[j] == 0 ) { mu[i * pri[j]] = 0; break; } mu[i * pri[j]] = -mu[i]; } } }
int buc[10000006]; vi lsj[MAXN]; int deg[MAXN]; int A[MAXN]; set<int> zzh; int era[MAXN]; void caldeg( int r ) { rep( i , 1 , r ) if( !era[i] ) for( int x : lsj[i] ) ++ buc[x]; rep( i , 1 , n ) deg[i] = 0; rep( i , 1 , r ) if( !era[i] ) { for( int x : lsj[i] ) deg[i] += mu[x] * buc[x]; } rep( i , 1 , r ) if( !era[i] ) for( int x : lsj[i] ) buc[x] = 0; } int cal( int r ) { caldeg( r ); int as = 0; rep( i , 1 , r ) if( deg[i] ) { ++ as; } return as; }
int W[300]; int gcd( int a , int b ) { return !b ? a : gcd( b , a % b ); }
void solve() { cin >> n >> k; rep( i , 1 , n ) scanf("%d",A + i); rep( i , 1 , n ) { vi f; int c = A[i]; while( c != 1 ) { int p = mnp[c]; f.pb( p ); while( c % p == 0 ) c /= p; } W[0] = 1; lsj[i].pb( 1 ); for( int t = 1 ; t < ( 1 << f.size() ) ; ++ t ) W[t] = W[t ^ ( t & -t )] * f[__builtin_ctz( t )] , lsj[i].pb( W[t] ); } int ff = 0; caldeg( n ); int ng = 0 , fk = 0; rep( i , 1 , n ) { if( deg[i] == 0 ) ++ ng , era[i] = 1; if( deg[i] > 2 ) fk = i; } if( ng >= k ) { vi as; rep( i , 1 , n ) if( deg[i] == 0 ) as.pb( i ); as.resize( k ); for( int x : as ) printf("%d ",x); puts(""); return; } if( !fk ) { vi as; rep( i , 1 , n ) { int re = 0; for( int x : lsj[i] ) re += mu[x] * buc[x] , ++ buc[x]; if( !re ) as.pb( i ); } as.resize( k ); for( int x : as ) printf("%d ",x); puts(""); return; } vi as; era[fk] = 1; as.pb( fk ); int ccn = 1; rep( i , 1 , n ) if( i != fk ) { if( gcd( A[i] , A[fk] ) == 1 ) ++ ccn , era[i] = 1 , as.pb( i ); if( ccn == 3 ) break; } caldeg( n ); ng = 0; rep( i , 1 , n ) if( deg[i] == 0 && !era[i] ) ++ ng; if( ng >= k ) { as.clear(); rep( i , 1 , n ) if( deg[i] == 0 && !era[i] ) as.pb( i ); as.resize( k ); for( int x : as ) printf("%d ",x); puts(""); return; } rep( i , 1 , n ) if( deg[i] == 0 ) era[i] = 1; as.resize( 3 ); int l = 0 , r = n - 1; while( l <= r ) { int mid = l + r >> 1; if( cal( mid ) + 3 >= k ) r = mid - 1; else l = mid + 1; } ++ r; int t = cal( r ); int ned = t + 3 - k , las = 0 , s = 0; rep( i , 1 , r ) if( deg[i] ) { if( gcd( A[i] , A[t] ) == 1 ) { if( s < ned ) deg[i] = 0 , las = i; ++ s; } } if( s == ned && s ) deg[las] = 1 , as.pop_back(); rep( i , 1 , r ) if( deg[i] ) as.pb( i ); for( int x : as ) printf("%d ",x); puts(""); }
signed main( ) { sieve();
solve(); }
|