POJ3421 X-factor Chains

來源:互聯網
上載者:User

標籤:iostream   etc   typedef   分享   span   main   lap   register   ast   

嘟嘟嘟

 

題目大意:給一個數x,讓你求這樣一個最長的序列,以及最長的序列的種數:

1.第0項為1,最後一項為x(序列長度不算這兩項)。

2.每一項都是x的因子。

3.對於任意的ai和ai+1,ai < ai+1且ai | ai+1。

 

每一項都是x的因子,那麼先把x分解質因數,用這些數湊成的數一定都是x的因子。然後要滿足第三條,那麼ai+1一定由ai乘以一個質因數得到,所以最長長度就是質因數指數之和tot。

再求方案:先不考慮pici中,ci = 1,那麼第一個數有tot種選法,第二個數有tot - 1種……所以總方案數是tot!。再考慮重複的數,就再除以 ci!。

 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cctype> 8 #include<vector> 9 #include<stack>10 #include<queue>11 using namespace std;12 #define enter puts("") 13 #define space putchar(‘ ‘)14 #define Mem(a, x) memset(a, x, sizeof(a))15 #define rg register16 typedef long long ll;17 typedef double db;18 const int INF = 0x3f3f3f3f;19 const db eps = 1e-8;20 //const int maxn = ;21 inline ll read()22 {23     ll ans = 0;24     char ch = getchar(), last = ‘ ‘;25     while(!isdigit(ch)) {last = ch; ch = getchar();}26     while(isdigit(ch)) {ans = ans * 10 + ch - ‘0‘; ch = getchar();}27     if(last == ‘-‘) ans = -ans;28     return ans;29 }30 inline void write(ll x)31 {32     if(x < 0) x = -x, putchar(‘-‘);33     if(x >= 10) write(x / 10);34     putchar(x % 10 + ‘0‘);35 }36 37 int n;38 ll fac[21];39 void init()40 {41   fac[1] = 1;42   for(int i = 2; i < 21; ++i) fac[i] = fac[i - 1] * i;43 }44 45 void solve(int n)46 {47   int tot = 0;48   ll und = 1;49   for(int i = 2; i * i <= n; ++i)50     {51       if(!(n % i))52     {53       int cnt = 0;54       for(; !(n % i); n /= i, cnt++);55       tot += cnt;56       und *= fac[cnt];57     }58     }59   if(n > 1) tot++;60   write(tot); space; write(fac[tot] / und); enter;61 }62 63 int main()64 {65   init();66   while(scanf("%d", &n) != EOF) solve(n);67   return 0;68 }
View Code

 

POJ3421 X-factor Chains

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.