Friday 27 May 2016

C. Prime Swaps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times):
  • choose two indexes, i and j (1 ≤ i < j ≤ n(j - i + 1) is a prime number);
  • swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments:tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable).
You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations.
Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).
Output
In the first line, print integer k (0 ≤ k ≤ 5n) — the number of used operations. Next, print the operations. Each operation must be printed as "i j" (1 ≤ i < j ≤ n(j - i + 1) is a prime).
If there are multiple answers, you can print any of them.
Examples
input
3
3 2 1
output
1
1 3
input
2
1 2
output
0
input
4
4 2 3 1
output
3
2 4
1 2
2 4





  HERE WE GO THROUGH A GREEDY STRATEGY AND ALWAYS BRING THE NUMBER TO ITS NEAREST POSITION THAT CAN BE DONE USING A VALID SWAP. IT IS ALWAYS CORRECT BECAUSE WE CAN SWAP TWO NEIGHBORING NUMBERS ALWAYS SO THIS WILL LEAD TO A SOLUTION.

            LOOK AT THE EASY CODE
#include<bits/stdc++.h>
using namespace std;
#define ll long long int 
bool pr[100010];
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
vector<int> v;
int l;
void pre()
{
   for(int i=2;i<=100000;i++)
   {
      if(!pr[i])
      {
        v.pb(i);
        for(int j=2*i;j<=100000;j+=i)
        {
              pr[j]=1;
  }
  }
}
l=v.size();
}
int pos[100010];
int a[100010];
int main()
{
    pre();
     int n;
     cin>>n;
     for(int i=1;i<=n;i++)
     {
       cin>>a[i];
       pos[a[i]]=i;
 }
 vector<pair<int,int> > res;
 
 for(int i=1;i<=n;i++)
 {
        while(pos[i]!=i)
        {
          int lst=pos[i];
       
             int shiftm=abs(pos[i]-i)+1;
   vector<int> ::iterator it;
   it=upper_bound(v.begin(),v.end(),shiftm);
it--;
int val=*it;
// cout<<"value "<<val<<endl;
     int idx=pos[i]+1-val;
//      cout<<"idx "<<idx<<endl;
     res.pb(mp(pos[i],idx));
     
     pos[a[idx]]=pos[i];
     pos[i]=idx;
     
    // cout<<"sert pos of "<<i<<" as "<<pos[i]<<" and pos of "<<a[idx]<<" = "<<pos[a[idx]]<<endl;
     swap(a[idx],a[lst]);
}
 
}
int sz=res.size();
cout<<sz<<endl;
for(int i=0;i<sz;i++)
cout<<min(res[i].ff,res[i].ss)<<" "<<max(res[i].ff,res[i].ss)<<endl;
 return 0;
 }


Monday 2 May 2016

All submissions for this problem are available.

Read problems statements in Mandarin Chinese and Russian.

You are given a array A of N positive integers, and you can perform the following operation on the array

1) Pick any two indices i and j in the array

2) Divide A[i] and A[j] by some common factor of A[i] and A[j]



You can perform the above operation as many number of times as you want, and the aim is to minimize the product of the resulting array. Find this minimum product. Since the answer can be a large number, print the product modulo 1000000007 (109+7).



INPUT:

First line contains T, number of testcases. Each testcase contains 2 lines.

First line of each testcase contains single integer N, size of the array

Second line of each testcase contains N space separated integers, the array A



OUTPUT:

For each testcase, output single line indicating the answer to that testcase



CONSTRAINTS:

1<=T<=10

30 points : 1<=N<=2000, 1<=A[i]<=106

70 points : 1<=N<=20000, 1<=A[i]<=108



SAMPLE INPUT:

1

3

2 3 6



SAMPLE OUTPUT:

1



EXPLANATION:

First divide first and third numbers by 2, then the second and third by 3. This makes all numbers equal to 1, hence the product is 1.




CODE IS AS FOLLOWS

//Coder: Balajiganapathi
#define TRACE
#define DEBUG

#include <algorithm>
#include <bitset>
#include <deque>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<string> vs;

// Basic macros
#define st          first
#define se          second
#define all(x)      (x).begin(), (x).end()
#define ini(a, v)   memset(a, v, sizeof(a))
#define re(i,s,n)   for(int i=s;i<(n);++i)
#define rep(i,s,n)  for(int i=s;i<=(n);++i)
#define fr(i,n)     re(i,0,n)
#define repv(i,f,t) for(int i = f; i >= t; --i)
#define rev(i,f,t)  repv(i,f - 1,t)
#define frv(i,n)    rev(i,n,0)
#define pu          push_back
#define mp          make_pair
#define sz(x)       (int)(x.size())

const int oo = 2000000009;
const double eps = 1e-9;

#ifdef TRACE
    #define trace1(x)                cerr << #x << ": " << x << endl;
    #define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
    #define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
    #define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
    #define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
    #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

    #define trace1(x)
    #define trace2(x, y)
    #define trace3(x, y, z)
    #define trace4(a, b, c, d)
    #define trace5(a, b, c, d, e)
    #define trace6(a, b, c, d, e, f)

#endif

const int mx = 20004, mod = 1000000007;
int a[mx], n;

set<int> primes;
map<int, vector<pi> > pr;  // Key is prime and the value is a vector of number of times the prime divides a[i]

//Decompose each number into its prime factors and populate the above map
void getPrimes() {
    primes.clear();
    pr.clear();
    fr(i, n) {
        int m = a[i];
        for(int p = 2; p <= m / p; ++p) {
            int cnt = 0;
            while(m % p == 0) {
                ++cnt;
                m /= p;
            }
            if(cnt > 0) {
                primes.insert(p);
                pr[p].pu(mp(cnt, i));
                //trace3(i, p, cnt);
            }
        }
        if(m > 1) {
            primes.insert(m);
            pr[m].pu(mp(1, i));
        }
    }
}

int main() {
    int t;
    cin >> t;
    assert(1 <= t && t <= 10);

    while(t--) {
        cin >> n;
        assert(1 <= n && n <= 20000);
        fr(i, n) cin >> a[i];
        fr(i, n) assert(1 <= a[i] && a[i] <= 100000000);
        getPrimes();
        vi v(all(primes));

        //Solve for each prime separately using a greedy strategy: Take the two elements with the largest no. of the prime currently and divide them by the prime. Decrement the count and update them.
        fr(j, sz(v)) {
            int p = v[j];
            //trace1(p);
            priority_queue<pi> q;
            fr(j, sz(pr[p])) {
                q.push(pr[p][j]);
                //trace3(j, pr[p][j].st, pr[p][j].se);
            }
            while(sz(q) > 1) { //Atleast two elements needed
                pi p1 = q.top(); q.pop();
                pi p2 = q.top(); q.pop();
                a[p1.se] /= p;
                a[p2.se] /= p;
                if(p1.st > 1) q.push(mp(p1.st - 1, p1.se));
                if(p2.st > 1) q.push(mp(p2.st - 1, p2.se));
            }
        }

        ll ans = 1;
        fr(i, n) {
            ans *= a[i];
            ans %= mod;
        }
        cout << ans << endl;

    }
 return 0;
}