HDU 2841 Visible Trees

Visible Trees

Time Limit: 1000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 2841
64-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 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\leq m, n\leq 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

解题:容斥原理

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 200010;
 5 vector<int>g[maxn];
 6 void init(){
 7     for(int i = 2; i < maxn; ++i){
 8         for(int j = 1; i*j < maxn; ++j)
 9             g[i*j].push_back(i);
10     }
11 }
12 int main(){
13     int kase,n,m;
14     init();
15     scanf("%d",&kase);
16     while(kase--){
17         scanf("%d%d",&n,&m);
18         LL ret = 0;
19         for(int i = 2; i <= n; ++i){
20             int cnt = 0,ans = 0;
21             for(int j = 1,t = g[i].size();j < (1<<t); ++j){
22                  LL tmp = 1;
23                  for(int k = cnt = 0; k < t; ++k){
24                     if((j>>k)&1){
25                         cnt++;
26                         tmp *= g[i][k];
27                     }
28                 }
29                 if(cnt&1) ans += m/tmp;
30                 else ans -= m/tmp;
31             }
32             ret += m - ans;
33         }
34         printf("%I64d\n",ret + m);
35     }
36     return 0;
37 }

时间: 2024-10-26 15:06:38

HDU 2841 Visible Trees的相关文章

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

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

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(容斥)

原文链接 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

【容斥+素数】 HDU 2841 Visible Trees

通道 题意:求一个区间[1,m]内与i的互质的数的个数.这里1<=i<=n, 思路:对i分解质因子然后容斥 代码: #include <cstdio> #include <cstring> #include <algorithm> typedef long long LL; const int maxn = 100010; LL ans; int n,m; int fac[maxn]; int prime[maxn]; int facCnt; void ge

HDU 2841 Visible Trees 数论+容斥原理

容斥原理 题意:给n*m的矩阵有点,左下角的点为(1,1),右上角的点(n,m),(其实转回来也是没影响的即m*n),一个人站在(0,0)看这些点,在一条直线的视线上,它只能看到最前面的那个点,后面的点将会被档住他看不到,问你,这个人一共能看到多少个点. 这个问题只要画一下图不难发现,如果一个点(x,y),x和y有非1的公约数z,那么他们其实可以一起缩小为(x/z,y/z),试着把这两个点和(0,0)连线,发现他们其实是同一条直线,而(x/z,y/z) 在前面,所以其实(x,y)被挡住了看不到的