Advertisement
Guest User

Untitled

a guest
Mar 16th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <vector>
  2. #include <map>
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cassert>
  6. #include <algorithm>
  7. using namespace std;
  8. #define pb push_back
  9. #define mp make_pair
  10. typedef long long ll;
  11. //typedef long double ld;
  12. typedef vector<int> Vint;
  13.  
  14. const long double eps = 1e-10;
  15. struct ld
  16. {
  17.     long double val;
  18.     unsigned long long estimate;
  19.  
  20.     ld ()
  21.     {
  22.         val = 0;
  23.         estimate = 0;
  24.     }
  25.  
  26.     ld (ll _e)
  27.     {
  28.         val = logl((long double)_e);
  29.         estimate = _e;
  30.     }
  31. };
  32.  
  33. ld operator * (const ld &left, const ld &right)
  34. {
  35.     ld ans;
  36.     ans.val = left.val + right.val;
  37.     ans.estimate = left.estimate * right.estimate;
  38.     return ans;
  39. }
  40. bool operator < (const ld &left, const ld &right)
  41. {
  42.     if (left.val + eps < right.val)
  43.         return true;
  44.     if (left.val > right.val + eps)
  45.         return false;
  46.  
  47.     unsigned long long diff = left.estimate - right.estimate;
  48.     if (diff > (1ULL << 63))
  49.         return true;
  50.     return false;
  51. }
  52.  
  53. const int mod = (int)1e9 + 9;
  54. Vint gen_primes(int n)
  55. {
  56.     vector<char> isprime(n + 1, 1);
  57.  
  58.     Vint ans;
  59.     for (int i = 2; i <= n; i++)
  60.     if (isprime[i])
  61.     {
  62.         if (i != 2)
  63.             ans.pb(i);
  64.         for (int j = 2 * i; j <= n; j += i)
  65.             isprime[j] = 0;
  66.     }
  67.  
  68.     return ans;
  69. }
  70.  
  71. ll powmod(ll x, ll y)
  72. {
  73.     ll res = 1;
  74.     while (y)
  75.     {
  76.         if (y & 1)
  77.         {
  78.             y--;
  79.             res *= x;
  80.             res %= mod;
  81.         }
  82.         else
  83.         {
  84.             y >>= 1;
  85.             x *= x;
  86.             x %= mod;
  87.         }
  88.     }
  89.  
  90.     return res;
  91. }
  92.  
  93. void solve(int n)
  94. {
  95.     const int bound = 20;
  96.     Vint primes = gen_primes(200);
  97.  
  98.     primes.resize(bound);
  99.  
  100.     vector<Vint> divisors(n + 1);
  101.     for (int i = 1; i <= n; i++)
  102.     for (int j = i; j <= n; j += i)
  103.         divisors[j].pb(i);
  104.  
  105.     ld inf;
  106.     inf.val = 1e18;
  107.     inf.estimate = 0;
  108.  
  109.     vector<vector<ll> > dp(bound, vector<ll>(n + 1, 1));
  110.     vector<vector<ld> > minval(bound, vector<ld>(n + 1, inf));
  111.     vector<vector<ld> > Pows(bound, vector<ld>(n + 1, 1));
  112.  
  113.     for (int i = 0; i < bound; i++)
  114.     for (int j = 1; j <= n; j++)
  115.         Pows[i][j] = Pows[i][j - 1] * ld(primes[i]);
  116.  
  117.     ll curt = 1;
  118.     ld other = 1;
  119.  
  120.     for (int i = 1; i <= n; i++)
  121.     {
  122.         dp[bound - 1][i] = curt;
  123.         minval[bound - 1][i] = other;
  124.  
  125.         curt *= primes[bound - 1];
  126.         curt %= mod;
  127.         other = other * primes[bound - 1];
  128.     }
  129.  
  130.     for (int it = bound - 2; it >= 0; it--)
  131.     for (int x = 1; x <= n; x++)
  132.     if (binary_search(divisors[n].begin(), divisors[n].end(), x))
  133.     {
  134.         int p = primes[it];
  135.         for (int d : divisors[x])
  136.         {
  137.             ld cur = Pows[it][d - 1];
  138.  
  139.             if (cur * minval[it + 1][x / d] < minval[it][x])
  140.             {
  141.                 minval[it][x] = cur * minval[it + 1][x / d];
  142.                 dp[it][x] = powmod(p, d - 1) * dp[it + 1][x / d];
  143.                 dp[it][x] %= mod;
  144.             }
  145.         }
  146.     }
  147.  
  148.     cout << dp[0][n] << endl;
  149. }
  150.  
  151. int main()
  152. {
  153.     int n;
  154.     while (cin >> n)
  155.         solve(n);
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement