求約數個數最多問題

來源:互聯網
上載者:User
求1 - n當中約數個數最多的數,若有多解則輸出最小的數。一般解法:從1到n枚舉 根據 約數個數定理 公式求出約數個數這需要先求出1到n的所有質數以此來對第i個數分解質因數 質因數變化即 乘以 (上一個質因數個數+1)對於n非常大時候效率很低可以看出 約數最多的數a1. a中較小的質因數個數必定大於等於大一級的質因數個數2. 隨著a的遞增 質因數的"種類"也按照質數的出現順序遞增質數數組為prim(n)  則有約數最多的數a = prim(0) ^ x0 + prim(1) ^ x1 + ...... + prim(n - 1) ^ x(n - 1)a 的約數個數 = (x0 + 1) * (x1 + 1) * (x2 + 1) * ...... * (x(n - 1) + 1) x0 >= x1 >= x2 >= x3 >= ...... >= x(n - 1)
#include <iostream>using namespace std;long nb; int mc;int p[] = {2,3,5,7,11,13,17,19};//d 素數深度 c素數次方限制 tot約數個數 num當前數 up上限值void ss(int d, int c, int tot, long long num, long up){if((tot > mc) || ((tot == mc) && (num < nb))){mc = tot; nb = num;}if(d > 7) return;long j = p[d], m = 1;long long n = num * j;while(m <= c && n <= up){ss(d + 1, m, tot * (m + 1), n, up);n *= j; m++;}}int main(){int t; long n;cin>>t;while(t--){cin >> n;mc = nb = 1;ss(0, 27, 1, 1, n);cout << nb << " " << mc << 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.