Advertisement
Guest User

WF Factors

a guest
Jul 10th, 2013
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <queue>
  6. #include <deque>
  7. #include <stack>
  8. #include <bitset>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. #include <utility>
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cstdio>
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <ctime>
  20. #include <cstring>
  21. #include <assert.h>
  22.  
  23. using namespace std;
  24.  
  25. typedef vector<int> vi;
  26. typedef vector<vi> vvi;
  27. typedef pair<int,int> ii;
  28. typedef long long ll;
  29. #define sz(a) int((a).size())
  30. #define pb push_back
  31. #define all(c) (c).begin(),(c).end()
  32. #define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
  33. #define present(c,x) ((c).find(x) != (c).end())
  34. #define cpresent(c,x) (find(all(c),x) != (c).end())
  35. #define mp make_pair
  36. #define go(i,n) for(int i=0;i<n;i++)
  37. #define go3(i,j,n) for(int i=j;i<n;i++)
  38. #define inf 1000000000
  39. #define fi first
  40. #define se second
  41. #define un unsigned long long
  42. #define oo 9223372036854775808ull // 2^63
  43.  
  44.  
  45. int a[1000001];
  46. int primes[10000];
  47. int yk=0;
  48.  
  49. void sieve(){  
  50.   go(i,100000) a[i]=1;
  51.  
  52.   a[0]=a[1] = 0;
  53.  
  54.   for(int i=2;i*i<=100000;i++)
  55.    if(a[i])
  56.     for(ll j= i*1ll*i;j<=100000;j+=i)
  57.       {
  58.        a[j]=0;
  59.       }
  60.   go(i,100000) if(a[i]) primes[yk++] = i;
  61. }
  62.  
  63.  
  64. int cnt=0;
  65. int nx=0;
  66.  
  67. map<un,un> memo;
  68. set<un> dan;
  69.  
  70. int col[20];
  71.  
  72. // calculates the value S! / (s1! * s2! * S3! *s4! ...sk!)
  73. // checks whether it is number  < 2^63
  74. // if yes, registers it
  75. // si are in array col, y is their number
  76. void check(int y,un san){
  77.  
  78.   int mf = 0;
  79.   int sp = 2;
  80.   un s=1ll;
  81.  
  82.   go(i,y) mf+=col[i];
  83.  
  84.  
  85.   int uk = 0;
  86.   int ad = col[0];
  87.  
  88.   while(uk<y) {
  89.  
  90.    while(ad<=1 && uk<y)
  91.      ad=col[++uk];
  92.  
  93.    
  94.    if(uk==y) break;
  95.    
  96.    assert(ad!=0);
  97.  
  98.    while( (s%ad)==0 && ad>1){
  99.      s=s/ad;
  100.      ad--;
  101.    }
  102.  
  103.    if(ad>1) s*=1ull*(mf--);
  104.  
  105.   }
  106.  
  107.  
  108.   bool flag = false;
  109.  
  110.   for(;mf>=2;mf--)
  111.    {
  112.     if(oo/mf > s)
  113.      s*=1ull*mf;
  114.     else flag = true;
  115.    }
  116.  
  117.    if(flag) return;
  118.  
  119.    if(present(dan,s)){
  120.      if(!memo.count(s))
  121.       memo[s]=san;
  122.      memo[s] = min(memo[s],san);
  123.    }
  124. }
  125.  
  126.  
  127. void rec(int y,un san){
  128.    cnt++;
  129.    nx=max(nx,y);
  130.  
  131.    check(y+1,san);
  132.  
  133.    if( oo/primes[y] >= san)
  134.     {
  135.      col[y]++;
  136.      rec(y,san*1ull*primes[y]);
  137.      col[y]--;
  138.     }
  139.  
  140.     if(y>1 && col[y-1]<col[y]) return; //guarantees that prev value is not less than current
  141.  
  142.    if( oo/primes[y+1] >= san)  
  143.      {
  144.       col[y+1]++;
  145.       rec(y+1,san*1ull*primes[y+1]);
  146.       col[y+1]--;
  147.      }
  148. }
  149.  
  150.  
  151. void read(){
  152.  sieve();
  153.  un n;
  154.  vector<un> v;
  155.  string s;
  156.  
  157.  while(cin>>n){
  158.   dan.insert(n);
  159.   v.pb(n);
  160.  }
  161.  
  162.  memset(col,0,sizeof col);
  163.  
  164.  rec(0,1ull);
  165.  
  166.  memo[1] = 2; //treat one by hand
  167.  
  168.  
  169.  go(i,sz(v))
  170.    cout<<v[i]<<" "<<memo[v[i]]<<endl;
  171.  
  172.  
  173.  
  174. }
  175.  
  176. int main(){
  177.  
  178. read();
  179.  
  180. return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement