zl程序教程

您现在的位置是:首页 >  其它

当前栏目

HDU 2841 Visible Trees

HDU Trees visible
2023-09-11 14:15:28 时间

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