Algorithm


problem link- https://www.spoj.com/problems/TIP1/

TIP1 - Totient in permutation (easy)

no tags 

 

In number theory, Euler's totient (or PHI function), is an arithmetic function that counts the number of positive integers less than or equal to a positive integer N that are relatively prime to this number N.

That is, if N is a positive integer, then PHI(N) is the number of integers K for which GCD(N, K) = 1 and 1 ≤ K ≤ N. We denote GCD the Greatest Common Divisor. For example, we have PHI(9)=6.

Interestingly, PHI(87109)=79180, and it can be seen that 87109 is a permutation of 79180.

Input

The input begins with the number T of test cases in a single line.
In each of the next T lines there are an integer M.

Output

For each given M, you have to print on a single line the value of N, for which 1 < N < M, PHI(N) is a permutation of N and the ratio N/PHI(N) produces a minimum. If there's several answers output the greatest, or if need, "No solution." without quotes.
Leading zeros are not allowed for integers greater than 0.

Example

Input:
3
22
222
2222

Output:
21
63
291

Explanations : For the first case, in the range ]1..22[, the lonely number n for which phi(n) is in permutations(n) is 21, (we have phi(21)=12). So the answer is obviously 21.
For the second case, in the range ]1..222[, there's two numbers n for which phi(n) is in permutations(n), we have phi(21)=12 and phi(63)=36. But as 63/36 is equal to 21/12, we're taking the greater : 63.
For the third case, in the range ]1..2222[, there's four numbers n for witch phi(n) is in permutations(n), phi(21)=12, phi(63)=36, phi(291)=192 and phi(502)=250. Within those solutions 291/192 is the minimum, we output 291.

Constraints

1 < T < 10^5
1 < M < 10^7

Code size limit is 10kB ; less than 500B of python3 code can get AC under 2s.
After that you may try TIP2.
@Speed addicts : my C code ran in 0.02s, and my fastest python3.2 code ran in 1.21s, (0.90s in py2.7)

Edit 2017-02-11, after compiler updates. My old C code ends in 0.00s, my old Python code ends in 0.05s !!!

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define fori(i,a,b) for(ll i=a;i<b;i++)
#define forr(i,a,b) for(ll i=a;i>=b;i--)
#define forit(it,x) for (auto it=(x).begin();it!=(x).end(); it++)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sll set<ll>
#define vll vector<ll>
#define msl map<string,ll>
#define mll map<ll,ll>
#define NO_OF_CHARS 256

const ll MAXN = (ll)1e7;

ll i, j, k;

vll LCM(MAXN+1);
vector<ll> phi(MAXN+1);

vll permuted;

void phi_1_to_n() {

    phi[0] = 0;
    phi[1] = 1;
    for ( i = 2; i <= MAXN; i++)
        phi[i] = i;

    for (i = 2; i <= MAXN; i++) {
        if (phi[i] == i) {
            for (j = i; j <= MAXN; j+=i)
                phi[j] -= phi[j] / i;
        }
    }

    for(i=21 ; i< MAXN+1 ; i++)
    {
        string s1, s2;
        int check = 1;
        s1 = to_string(i);
        s2 = to_string(phi[i]);
        int count[NO_OF_CHARS] = {0};
        for (j = 0; s1[j] && s2[j];  j++)
        {
            count[s1[j]]++;
            count[s2[j]]--;
        }
        if (s1[j] || s2[j])
          check = 0;

        for (i = 0; i < NO_OF_CHARS; i++)
            if (count[i])
                check =0;
         if(check == 1)
            permuted.push_back(i);
    }

}



int main()
{
    phi_1_to_n();
    int t;
    cin >> t;
    while(t--)
    {
        int n, ans, c=0 ;
        double x=INT_MAX;
        cin >> n;
        for(i=0; i<permuted.size(); i++)
        {
            if(permuted[i]<=n)
            {
                double division = ((double)permuted[i]/(double)phi[permuted[i]]);
                if(x >= division)
                {
                    ans = permuted[i];
                    x=division;
                    c=1;
                }

            }
            else
                break;
        }
        if(c==1)
        cout << ans << endl;
        else
            cout << "No solution" << endl;

    }

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
22
222
2222

Output

x
+
cmd
21
63
291
Advertisements

Demonstration


SPOJ Solution-Totient in permutation (easy)-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python