Visible Trees(hdu2841)

Visible Trees

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

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

2009 Multi-University Training Contest 3 - Host by WHU

题意:从(0,0)往一个矩阵看,看矩阵中的那些点,问能看到多少个,如果有多个点与(0,0)在一条直线上那么只能看到最前端的点,在这个点后面是看不到的。

思路:欧拉函数+容斥原理;

可以把矩阵分成三块,一个上三角一个下三角,还有一个对角线,先单独讨论对角线,就只有一种(对角线指的是斜率为负一的那一条)

上三角(x,y)---(x<y);关于每一个横坐标,我们的目的是找所有的不同种的斜率,那么假如取(x,y)(x与y不互质,那么(x/(gcd(x,y),y/gcd(x,y)))));得到新坐标

(a,b)(a,b互质),所以我们要求的斜率的不同种类就是求oula[x];下三角=上三角(矩阵为正方形)直接欧拉函数;

由于给的不一定是正方形;n,m;

所以在min(n,m)中的所有点我们可以直接oula函数,ans=sum(oula[i])*2( 1<=i<=min(n,m) );

所以在另一个比较长的边,还剩下(min(n,m)+1,max(n,m))没有处理这里就转换成在(1,min(n,m))中求(min(n,m)+1,max(n,m))中每个数在(1,min(n,m))

区间有多少个数与之互素的个数的和;这里用容斥原理;求(min(n,m)+1,max(n,m))每个数的素因子,然后容斥求在(1,min(n,m))与之不互质的数的个数,再区间长度减去这个就是与之互素的个数;因为每个数的不同素因子不超过9个,所以复杂度为n*512

  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<stdlib.h>
  5 #include<string.h>
  6 #include<queue>
  7 int oula[100005];
  8 bool prime[100005];
  9 int ans[100005];
 10 long long bns[100005];
 11 int id[100005];
 12 using namespace std;
 13 typedef long long LL;
 14 queue<int>que;
 15 int main(void)
 16 {
 17         int i,j,k;
 18         for(i=0; i<100005; i++)
 19                 oula[i]=i;
 20         oula[1]=0;
 21         memset(prime,0,sizeof(prime));
 22         for(i=2; i<1000; i++)
 23         {
 24                 if(!prime[i])
 25                 {
 26                         for(j=i; i*j<=100000; j++)
 27                         {
 28                                 prime[i*j]=true;
 29                         }
 30                 }
 31         }
 32         int cnt=0;
 33         for(i=2; i<=100000; i++)
 34                 if(!prime[i])
 35                         ans[cnt++]=i;
 36         for(i=0; i<cnt; i++)
 37         {
 38                 for(j=1; j*ans[i]<=100000; j++)
 39                 {
 40                         oula[j*ans[i]]/=ans[i];
 41                         oula[j*ans[i]]*=(ans[i]-1);
 42                 }
 43         }
 44         scanf("%d",&k);
 45         int n,m;
 46         while(k--)
 47         {
 48                 scanf("%d %d",&n,&m);
 49                 memset(id,0,sizeof(id));
 50                 int xx=max(n,m);
 51                 int cc=min(n,m);
 52                 if(cc==1)
 53                         printf("%d\n",xx);
 54                 else
 55                 {
 56                         long long sum=0;
 57                         for(i=2; i<=cc; i++)
 58                         {
 59                                 sum+=oula[i];
 60                         }
 61                         sum*=2;
 62                         for(i=cc+1; i<=xx; i++)
 63                         {
 64                                 int vv=i;
 65                                 int t=0;
 66                                 int flag=0;
 67                                 while(vv>1)
 68                                 {
 69                                         if(vv%ans[t])
 70                                         {
 71                                                 t++;
 72                                                 flag=0;
 73                                         }
 74                                         else if(vv%ans[t]==0&&flag==0)
 75                                         {
 76                                                 que.push(ans[t]);
 77                                                 flag=1;
 78                                                 vv/=ans[t];
 79                                         }
 80                                         else if(vv%ans[t]==0&&flag)
 81                                         {
 82                                                 vv/=ans[t];
 83                                         }
 84                                         if(vv==1)
 85                                                 break;
 86                                 }
 87                                 int ak=0;
 88                                 while(!que.empty())
 89                                 {
 90                                         id[ak++]=que.front();
 91                                         que.pop();
 92                                 }
 93                                 LL ff=0;
 94                                 int ii;
 95                                 for(ii=1; ii<=(1<<ak)-1; ii++)
 96                                 {
 97                                         int tt=0;
 98                                         LL pp=1;
 99                                         for(j=0; j<ak; j++)
100                                         {
101                                                 if(ii&(1<<j))
102                                                 {
103                                                         tt++;
104                                                         pp*=id[j];
105                                                 }
106                                         }
107                                         if(tt%2==0)
108                                         {
109                                                 ff-=cc/pp;
110                                         }
111                                         else ff+=cc/pp;
112                                 }
113                                 sum+=cc-ff;
114
115                         }
116                         sum+=1;
117                         printf("%lld\n",sum);
118                 }
119         }
120         return 0;
121 }
时间: 2024-11-08 19:51:02

Visible Trees(hdu2841)的相关文章

hdu 2841 Visible Trees 容斥原理

Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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 ma

hdu2848 Visible Trees (容斥定理)

题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看到多少个点. 知识点: 容斥原理:(容许) 先不考虑重叠的情况,把包含于某条件中的所有对象的数目先计算出来,(排斥)然后再把计数时重复计算的数目排斥出去,使得计算的结果既无遗漏又无重复. 公式:          奇加偶减 一般求互质个数若用欧拉函数不好解决,则从反面考虑,用容斥. 模板: void

HDU 2841 Visible Trees

Visible Trees Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 284164-bit integer IO format: %I64d      Java class name: Main There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherl

hdu 2841 Visible Trees【容斥原理】

Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1602    Accepted Submission(s): 661 Problem Description There are many trees forming a m * n grid, the grid starts from (1,1). Farm

hdu 4135 Co-prime +hdu 2841 Visible Trees(容斥原理)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2263    Accepted Submission(s): 847 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDU 2841 Visible Trees(数论)

题目大意:给你一个m*n的方格,方格从(1,1)开始.每个点上有一棵树,然后让你从(0,0)点向下看,问你能看见几颗树. 解题思路:如果你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊.挡住的话就是这个小的方格内对角线的连线过顶点,如图: 建设A为(0,0)如果B能遮挡住C,那么B与C组成的矩形中nx, mx是不互质的. 所以x从1开始枚举n如果m中某一项与x互质那么就会有一个格子显示出来,所以这里需要找的是1-n中与枚举的x互质的个数.这里的话我们可以用到分解质因数,然后用状态压

hdu 2841 Visible Trees(计数问题)

题目链接:hdu 2841 Visible Trees 题目大意:一个n?m的矩阵,每个整数点上有树,人站在(0,0)点,问可以看见多少棵树. 解题思路:和uva1393是一道相同类型的题目,只不过这道题目的n比较大,不能预处理.必须用另外一种方法. 将矩阵按照(0,0)和(n,m)两天连成的直线分成两部分,分别计算,但是(n,m)这条线被计算了两次,于是减掉1. dp[i]表示这一列上有多少个点是可以被看见的,因为矩阵被对半分了,所以点的个数为m?in,同时还要计算每次看见的点会把后面的给挡住

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

题目链接: 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 f

Visible Trees HDU - 2841

Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4331    Accepted Submission(s): 1991 Problem Description There are many trees forming a m * n grid, the grid starts from (1,1). Farm