码迷,mamicode.com
首页 > 其他好文 > 详细

[思维+容斥原理] hdu 2841 Visible Trees

时间:2014-05-08 04:45:47      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

题目链接:

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

Visible Trees

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


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
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2837 2844 2843 2842 2840 

题目意思:

给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可以看到多少棵树。同一直线上的树只能看到最靠近人的那颗。

解题思路:

容斥原理

假设当前格子坐标为(i,j)如果记a=i/gcd(i,j),b=j/gcd(i,j) 显然人与该格子的连线上的格子坐标(x,y)满足x=i+(-)k*a y=j+(-)k*b,显然最靠近人的那棵树坐标为(a,b),显然gcd(a,b)=1

所以可以推导出人看的见的格子坐标满足横纵坐标互质,所以问题就转化为在(1~n,1~m)间满足横纵坐标互质的坐标有多少个。

所以可以在1~n范围内枚举i,然后容斥原理求出在1~m间与i互质的个数。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define N 1000

bool isp[N+10];
int pri[N+10],cnt,pp[N+10],cnt0,n,m;
ll ans;

void init() //筛选出1~1000内的质因数
{
    cnt=0;
    for(int i=1;i<=N;i++)
        isp[i]=true;

    for(int i=2;i<=N;i++)
    {
        if(isp[i])
        {
            pri[++cnt]=i;
            for(int j=i*2;j<=N;j+=i)
                isp[j]=false;
        }
    }
    //printf("cnt:%d\n",cnt);
}
void cal(int cur) //将cur分解质因数
{
    cnt0=0;

    for(int i=1;pri[i]*pri[i]<=cur;i++)
    {
        if(cur%pri[i]==0)
        {
            pp[++cnt0]=pri[i];
            while(!(cur%pri[i]))
                cur/=pri[i];
        }
    }
    if(cur!=1)
        pp[++cnt0]=cur;
}

void dfs(int hav,int cur,int num) //容斥原理求与i互质的个数
{
    if(hav>m||cur>cnt0)
        return ;
    for(int i=cur;i<=cnt0;i++)
    {
        int temp=hav*pp[i];

        if(num&1)
            ans-=m/temp;
        else
            ans+=m/temp;
        dfs(temp,i+1,num+1);
    }

}
int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   init();
   int t;

   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d",&n,&m);
       ans=m;

       for(int i=2;i<=n;i++)
       {
           cal(i);
           ans+=m;

           for(int j=1;j<=cnt0;j++)
           {
               ans-=m/pp[j];
               dfs(pp[j],j+1,2);
           }
       }
       printf("%I64d\n",ans);
   }
   return 0;
}




[思维+容斥原理] hdu 2841 Visible Trees,布布扣,bubuko.com

[思维+容斥原理] hdu 2841 Visible Trees

标签:des   style   blog   class   code   java   

原文地址:http://blog.csdn.net/cc_again/article/details/25245509

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!