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

HDU 2841 Visible Trees

时间:2015-09-29 18:51:44      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

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

 
解题:容斥原理
技术分享
 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 }
View Code

 

HDU 2841 Visible Trees

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4846909.html

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