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;
    }
     
  • 相关阅读:
    CSS 实现半圆环的两种方式
    传统js和jsx ts和tsx的区别
    echarts 实现正负轴双柱状图
    vue 封装 axios 代码
    访问某个网站特别卡,怎么办?
    创建自己的github
    自动化测试平台构想与实现
    【sqlserver】之学习总结
    shell脚本中浮点数运算
    远程执行shell脚本
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5813158.html
Copyright © 2011-2022 走看看