UESTC 618 無平方因子數 (容斥 + 莫比烏斯反演)

來源:互聯網
上載者:User

標籤:uestc   莫比烏斯反演   容斥   

無平方因子數

Time Limit: 4000/2000MS (Java/Others)   

Memory Limit: 65535/65535KB (Java/Others)


Submit  Status
無平方因子數即對於任意一個素數p,p2都不會整除那個數,如1 , 5=5 , 15=3×5都是無平方因子數,而20=22×5不是。現在給定一個n (1≤n<1012) ,求區間[1,n]中無平方因子數的個數。

Input
第一行有個整數T,代表資料群組數(T≤10)

接下來有T行,每行有個整數n (1≤n<1012)

Output
輸出T行,每行輸出一個整數代表區間[1,n]內的無平方因子數的個數。

Sample Input   
3
1
10

30

Sample Output

1
7

19


Source

UESTC Training for Math


題目連結:http://acm.uestc.edu.cn/#/problem/show/618

題目分析:又是無平方因子數,比BZOJ那題簡單很多,直接算就行了,參照BZOJ 2440

#include <cstdio>#include <cstring>#include <algorithm>#define ll long longusing namespace std;int const MAX = 1e6 + 5;int mob[MAX], p[MAX];bool prime[MAX];void Mobius(){    int pnum = 0;    memset(prime, true, sizeof(prime));    mob[1] = 1;    for(int i = 2; i < MAX; i++)    {        if(prime[i])        {            p[pnum ++] = i;            mob[i] = -1;        }        for(int j = 0; j < pnum && i * p[j] < MAX; j++)        {            prime[i * p[j]] = false;            if(i % p[j] == 0)            {                mob[i * p[j]] = 0;                break;            }            mob[i * p[j]] = -mob[i];        }    }}ll cal(ll n){    ll cnt = 0;    for(ll i = 1; i * i <= n; i++)        cnt += (ll) mob[i] * (n / (i * i));    return cnt;}int main(){    Mobius();    int T;    scanf("%d", &T);    while(T --)    {        ll n;        scanf("%lld", &n);        printf("%lld\n", cal(n));    }}



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

UESTC 618 無平方因子數 (容斥 + 莫比烏斯反演)

聯繫我們

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