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;
    }
     
  • 相关阅读:
    Nginx中如何配置中文域名?
    VS2012找不到EF框架实体模型的解决方法
    来自一位家长的电话
    孩子大了真是不好管了
    springboot项目不加端口号也可以访问项目的方法
    分享几个上机案例题
    今晚在学校值班……
    3班的第二次模拟面试
    Sword 09
    Sword 06
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5813158.html
Copyright © 2011-2022 走看看