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

 
解题:容斥原理
 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

 

转载于:https://www.cnblogs.com/crackpotisback/p/4846909.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值