uva 11317,

來源:互聯網
上載者:User

uva 11317,

題目連結:uva 11317 - GCD+LCM

題目大意:給定n,求出1~n裡面兩兩的最大公約的積GCD和最小公倍數的積LCM,在10100進位下的位元。

解題思路:在n的情況下,對於最大公約數為i的情況又phi[n/i]次。求LCM就用兩兩乘積除以GCD即可。

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1000000;const double logt = log(10.0);int N;double g[maxn+5], s[maxn+5];int phi[maxn+5];void phi_table (int n) {    memset(phi, 0, sizeof(phi));    phi[1] = 1;    for (int i = 2; i <= n; i++) {        if (!phi[i]) {            for (int j = i; j <= n; j += i) {                if (!phi[j])                    phi[j] = j;                phi[j] = phi[j] / i * (i-1);            }        }    }}void init (ll n) {    for (int i = 1; i <= n; i++) {        double tmp = log(i);        for (int j = 2 * i; j <= n; j += i)            g[j] += phi[j/i] * tmp;    }    for (int i = 1; i <= n; i++)        g[i] += g[i-1];    for (int i = 1; i <= n; i++)        g[i] /= logt;}int main () {    phi_table(maxn);    init(maxn);    int cas = 1;    while (scanf("%d", &N) == 1 && N) {        double ans = 0;        for (int i = 1; i <= N; i++)            ans += (N-1) * log(i);        ans /= logt;        ans -= g[N];        printf("Case %d: %lld %lld\n", cas++, (ll)(g[N] / 100) + 1, (ll)(ans / 100) + 1);    }    return 0;}



相關文章

聯繫我們

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