hdu 2841 Visible Trees【容斥原理】,hdu2841

來源:互聯網
上載者:User

hdu 2841 Visible Trees【容斥原理】,hdu2841
Visible TreesTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1602    Accepted Submission(s): 661


Problem DescriptionThere are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.

If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him. 
InputThe first line contains one integer t, represents the number of test cases. Then there are multiple test cases. For each test case there is one line containing two integers m and n(1 ≤ m, n ≤ 100000) 
OutputFor each test case output one line represents the number of trees Farmer Sherlock can see. 
Sample Input

21 12 3
Sample Output
15
題目翻譯:給你一個(m,n)的矩陣,每個點上有一顆樹,你站在(0,0)點看矩陣,前面的樹會擋著後面的樹,問你此時一共可以看到多少樹!結題思路:由於是站在(0,0),因此此時被擋的那棵樹(X,Y)和擋它的那棵樹(x,y)有一些關係,此時和(0,0)三點共線,因此X / x == Y / y;因此(X,Y)必有公約數,因此點任意一點(A,B)只要A,B有約數,則一定看不到,反之,A,B互質則一定可以看到,因此問題轉化為求圖中的互質點對;說到求互質,第一想起來的就是gcd,但是gcd求起來必然耗時,因此可以用容斥原理,前面的部落格已經介紹了利用容斥原理求M和1-N中有多少互質,那麼現在是求1-M中的所有數與1-N中有多少互質,因此迴圈1—N即可!
#include<cstdio>#define LL long longint p[10],top;void getp(int v){    top=0;    for(int i=2;i*i<=v;i++){        if(v%i==0){            p[top++]=i;            while(v%i==0) v/=i;        }    }    if(v>1)p[top++]=v;}LL nop(int n,int t){    int i;    LL num=0;    for(i=t;i<top;i++)        num+=n/p[i]-nop(n/p[i],i+1);    return num;}int main(){    int t,m,n,i;    LL sum;    scanf("%d",&t);    while(t--){        scanf("%d%d",&m,&n);        sum=n;        for(i=2;i<=m;i++){            getp(i);            sum+=(n-nop(n,0));        }        printf("%lld\n",sum);    }}
說實話代碼超短,但是還是WA了3次,原因很簡單,nop忘了寫返回值,而且getp函數中,把while誤寫成if,真不知道自己寫代碼的時候想的啥,過了一天靜下心來才改對翻別人的部落格,發現直接進行預先處理素數表,可以省一部分時間,但是耗記憶體,下面貼代碼
#include<cstdio>#define LL long longint prime[100005][20];int cnt[100005]={0};void Init(){    for(int i=2;i<=100000;i++){        if(cnt[i]) continue;        prime[i][0]=i;        cnt[i]=1;        for(int j=2;j*i<=100000;j++)            prime[i*j][cnt[i*j]++]=i;    }}LL dfs(int m,int n,int idx){    LL ret=0;    for(int i=idx;i<cnt[m];i++)        ret+=n/prime[m][i]-dfs(m,n/prime[m][i],i+1);    return ret;}int main(){    Init();    int t,n,m;    scanf("%d",&t);    while(t--){        scanf("%d%d",&n,&m);        LL ans=n;        for(int i=2;i<=m;i++)            ans+=(n-dfs(i,n,0));        printf("%I64d\n",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.