zoukankan      html  css  js  c++  java
  • HDU2841(容斥原理)

    Visible Trees

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2737    Accepted Submission(s): 1196


    Problem Description
    There 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.
     
    Input
    The 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)
     
    Output
    For each test case output one line represents the number of trees Farmer Sherlock can see.
     
    Sample Input
    2
    1 1
    2 3
     
    Sample Output
    1
    5
    思路:若点(x,y)中x与y互质则点(x,y)可以看见,否则被挡住。那么题意就转化为1~m与i互质的点的个数之和,其中(1<=i<=n)。
    #include <cstdio>
    #include <vector>
    using namespace std;
    typedef long long LL;
    const int MAXN=100001;
    vector<int> divisor[MAXN];
    void prep()
    {
        for(int e=1;e<MAXN;e++)
        {
            int x=e;
            for(int i=2;i*i<=x;i++)
            {
                if(x%i==0)
                {
                    divisor[e].push_back(i);
                    while(x%i==0)    x/=i;
                }
            }
            if(x>1)    divisor[e].push_back(x);
        }
    }
    LL sieve(LL m,LL n)
    {
        LL ans=0;
        for(LL mark=1;mark<(1<<divisor[n].size());mark++)
        {
            LL mul=1;
            LL odd=0;
            for(LL i=0;i<divisor[n].size();i++)
            {
                if(mark&(1<<i))
                {
                    mul*=divisor[n][i];
                    odd++;
                }
            }
            LL cnt=m/mul;
            if(odd&1)    ans+=cnt;
            else ans-=cnt;
        }
        return m-ans;
    }
    int n,m;
    int main()
    {
        int T;
        prep();
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&m,&n);
            LL res=0;
            for(int i=1;i<=n;i++)
            {
                res+=sieve(m,i);
            }
            printf("%lld
    ",res);
        }
        return 0;
    }
     
  • 相关阅读:
    iOS开发基础-UITableView基本属性
    iOS开发基础-UITableView控件简单介绍
    iOS开发基础-UIScrollView实现图片缩放
    iOS开发基础-UIScrollView基础
    iOS开发基础-序列帧动画之Tom猫
    iOS开发基础-KVC简单介绍
    iOS开发基础-九宫格坐标(6)
    iOS开发基础-九宫格坐标(5)
    iOS开发基础-九宫格坐标(4)
    iOS开发基础-九宫格坐标(3)之Xib
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5813158.html
Copyright © 2011-2022 走看看