hdu GCD 【歐拉函數,素因子分解,篩選法,容斥原理】

來源:互聯網
上載者:User

一道不錯的數論題,可惜自己想不出,只能參考大牛們的代碼~~

http://acm.hdu.edu.cn/showproblem.php?pid=1695

#include<iostream>#include<vector>#include<map>#include<stack>#include<algorithm>#include<queue>#include<list>#include<set>#include<string.h>#include<stdlib.h>#include<math.h>#include<stdio.h>#include<ctype.h>#include<iomanip>using namespace std;#define LL long long#define pi acos(-1)/*(1)題目要求gcd(x, y) = k , 可以轉換為gcd(x / k , y / k) = gcd(n, m) = 1。相應地,題目中,x 和y的範圍也可以相應地轉換為[1, b / k] 和 [1, d / k]。(2)題目中,x, y是無序的, 所以,我們可以假設b <= d。(3)在[1, d / k] 中枚舉每一個 m ,在[1, min(d / k, m - 1)]中找出和 m 互質的數的個數。①當 m <= b / k的時候,可以直接根據歐拉函數求出小於等於m且與m互質的數的個數。②當 b / k <= m <= d / k, 要用容斥原理來求。。。*/#define N 100000+10LL eul[N];//每個數的歐拉函數(包括之前所有的數)int num[N];//每個數的素因子有多少int prim[N][50];//記錄每個數的素因子void EulerPrime(){    int i,j,k;    eul[1]=1;    for(i=2;i<=N;i++)    {        if(eul[i]==0)        {//凡進去的i都是質數            for(j=i;j<=N;j+=i)            {                if(eul[j]==0)                eul[j]=j;                eul[j]=eul[j]*(i-1)/i;                prim[j][num[j]++]=i;            }//cout<<i<<endl;        }        eul[i]+=eul[i-1];    }}LL dfs(int index,int res,int cur)//計算小於等於res的數中,與cur不互質的個數{    LL ret=0;    int i;    for(i=index;i<num[cur];i++)//容斥原理來求A1並A2並A3.....並Ak的元素的數的個數.(還不是很懂!)    {        //printf("%d/%d=%d\n",res,prim[cur][i],res/prim[cur][i]);        ret+=res/prim[cur][i]-dfs(i+1,res/prim[cur][i],cur);    }    return ret;}int main(){ //   freopen("a.txt","r",stdin);    int a,b,c,d,k;    int ca;    int i,j,t;    EulerPrime();//預先處理    while(scanf("%d",&ca)!=EOF)    {        for(t=1;t<=ca;t++)        {            scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);            if(k==0)            {                printf("Case %d: 0\n",t);                continue;            }            b/=k;            d/=k;            if(b>d)            swap(b,d);            LL ans=eul[b];            for(i=b+1;i<=d;i++)            ans+=b-dfs(0,b,i);            printf("Case %d: %I64d\n",t,ans);        }    }    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.