hdu 2841 Visible Trees(容斥原理),hdu2841

來源:互聯網
上載者:User

hdu 2841 Visible Trees(容斥原理),hdu2841

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


有一個n*m的方格,從(1,1)開始,每個點有一棵樹,一個人站在(0,0)點,問他能看到幾棵樹。當(0,0)和另外的點在一條直線上時他只能看到最近的一棵。


題目意在求在m*n的方格中有多少種y/x,因為兩個y/x相等的點只能看到一個。有多少種y/x也就是有多少 個(x,y)x與y互質。其中(1<=x<=m,1<=n<=y)。

這樣就上一題類似了,求一個區間[1,m]內與i的互質的數的個數。這裡1<=i<=n,先求出與i不互質的,對i分解質因子然後容斥。


#include <stdio.h>#include <iostream>#include <map>#include <set>#include <bitset>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL __int64//#define LL long long#define eps 1e-9#define PI acos(-1.0)using namespace std;const LL INF = 1<<30;const int maxn = 100010;LL ans;int n,m;int fac[maxn];int prime[maxn];int facCnt;void getPrime(){    bool flag[maxn];    memset(flag,false,sizeof(flag));    prime[0] = 0;    for(int i = 2; i < maxn; i++)    {        if(flag[i] == false)            prime[++prime[0]] = i;        for(int j = 1; j <= prime[0]&&i*prime[j]<maxn; j++)        {            flag[i*prime[j]] = true;            if(i % prime[j] == 0)                break;        }    }}void getFac(int num){    int tmp = num;    facCnt = 0;    for(int i = 1; i <= prime[0] && prime[i]*prime[i] <= tmp; i++)    {        if(tmp % prime[i] == 0)        {            fac[facCnt++] = prime[i];            while(tmp%prime[i] == 0)                tmp /= prime[i];        }        if(tmp == 1)            break;    }    if(tmp > 1)        fac[facCnt++] = tmp;}LL solve(int m){    //隊列數組    int que[110];    int l = 0;    que[l++] = 1;    for(int i = 0; i < facCnt; i++)    {        int k = l;        for(int j = 0; j < k; j++)            que[l++] = fac[i]*que[j]*(-1);    }    LL anw = 0;    for(int i = 0; i < l; i++)        anw += m/que[i];    return anw;}int main(){    int test;    scanf("%d",&test);    getPrime();    for(int item = 1; item <= test; item++)    {        scanf("%d %d",&n,&m);        if(n > m)            swap(n,m);        ans = 0;        for(int i = 1; i <= n; i++)        {            getFac(i);            ans += solve(m);        }        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.