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;
    }
     
  • 相关阅读:
    路由器端口映射
    字符编码笔记:ASCII,Unicode和UTF-8
    2、Spring之AOP
    八、模板方法模式
    三、装饰者模式
    七、适配器模式
    六、命令模式
    五、单件模式
    乐观锁与悲观锁——解决并发问题
    一、策略模式
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5813158.html
Copyright © 2011-2022 走看看