ZOJ 2562 More Divisors(高合成數)

來源:互聯網
上載者:User

標籤:acm

ZOJ 2562 More Divisors(高合成數)

ACM

題目地址:ZOJ 2562 More Divisors

題意: 
求小於n的最大的高合成數,高合成數指一類整數,任何比它小的自然數的因子數目均比這個數的因子數目少。

分析: 
網上都叫它反素數,其實我查了一下,翻素數應該是正著寫倒著寫都是素數的素數。這個應該叫高合成數,見Wikipedia: Highly composite number

高合成數有以下特徵: 
where p1<p2<?<pk are prime, and the exponents ci are positive integers. 
Any factor of n must have the same or lesser multiplicity in each prime: 
p1^d1×p2^d2×?×pk^dk, 0≤di≤ci, 0<i≤k 
所以用回溯枚舉。

代碼:

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        2562.cpp*  Create Date: 2014-08-06 20:45:53*  Descripton:  Highly Composite Number*/#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int M = 1000;ll n;ll bestNum;ll bestSum;ll hcm[M][2];ll prim[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67};  // current is num, use the prim[k], sum of divisors, the limit of prim[k] you can usevoid getNum(ll num, int k, ll sum, int limit) {if (sum > bestSum) {bestSum = sum;bestNum = num;} else if (sum == bestSum && num < bestNum) {bestNum = num;}ll p = prim[k];for (int i = 1; i <= limit; i++, p *= prim[k]) {// use i prim[k]sif (num * p > n) break;getNum(num *= prim[k], k + 1, sum * (i + 1), i);}}// clac log2(n)int log2(ll n) {int ret = 0;ll p = 1;while (p < n) {p <<= 1;ret++;}return ret;}// return the number of Highly Composite Number in [1, n]// and save the HCM in hcm[][2]int gethcm() {int ret = 0;n = 500000;// [1, n]while (n > 0) {bestNum = 1;bestSum = 1;getNum(1, 0, 1, log2(n));cout << bestNum << ' ' << bestSum << endl;hcm[ret][0] = bestNum;hcm[ret][1] = bestSum;n = bestNum - 1;ret++;}return ret;}int main() {while (cin >> n) {bestNum = 1;bestSum = 1;getNum(1, 0, 1, 50);cout << bestNum << endl;}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.