void primeFactors(ll n, vii &factors) { ll PF_idx = 0, PF = primes[PF_idx]; while (PF * PF <= n) { while (n % PF == 0) { if(factors.size() == 0) factors.push_back(ii(PF, 1)); else if(factors.back().first == PF) factors.back().second++; else factors.push_back(ii(PF, 1)); n /= PF; } PF = primes[++PF_idx]; } if (n != 1) factors.push_back(ii(n, 1)); // special case if n is a prime }
int main() { int m; scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%d", &nums[i]); nums[i]--; } scanf("%d", &m); newCycles(); if(fp==m){ }else if(fp<m){ while(m-fp>0){ for(int i=0; i<n && m-fp>0; i++){ int v=-1; for(int j=0; j<n; j++) if(vis[j]!=vis[i]) {v=j; break;} if(v!=-1){ res.push_back(ii(nums[i], nums[v])); if(res.back().first>res.back().second) swap(res.back().first, res.back().second); swap(nums[i],nums[v]); }else break; newCycles(); } } }else{ while(fp-m>0){ for(int i=0; i<n && fp-m>0; i++){ if(nodeCnt[vis[i]]>1){ int v=-1; for(int j=0; j<n; j++) if(vis[j]==vis[i] && j!=i) {v=j; break;} if(v!=-1){ res.push_back(ii(nums[i], nums[v])); if(res.back().first>res.back().second) swap(res.back().first, res.back().second); swap(nums[i],nums[v]); }else break; } newCycles(); } } } printf("%d\n", res.size()); if(res.size()){ printf("%d %d", res[0].first+1, res[0].second+1); for(int i=1; i<res.size(); i++) printf(" %d %d", res[i].first+1, res[i].second+1); printf("\n"); } return 0; }