Question Link: Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2841
Question:
A person is at (0, 0), and there is a grid M * n in front. Each grid node has a tree and asks the person how many trees can be seen in the same place.
If the two trees are in a straight line, you can only see the first tree.
Analysis:
If the coordinates of a number are (a, B), none of the coordinates (A * k, B * k) can be seen. If, if B has a common factor C, we can only see (A/C, B/C );
Therefore, we come to the conclusion that the horizontal and vertical coordinates of the tree must be mutually qualitative. Then we can convert the problem into the number of [1, N] and the number in M.
Then, we only need to break down the all-prime factor in [1, N], and then let it go.
The Code is as follows::
# Include <iostream> # include <cstring> # include <cstdio> # include <vector> using namespace STD; const int maxn = 100010; typedef long ll; int prim [maxn], num [maxn] [20]; void Init () {int I, j; memset (Prim, 0, sizeof (prim )); for (I = 1; I <= 100000; I ++) num [I] [0] = 0; for (I = 2; I <= 100000; I ++) if (prim [I] = 0) {num [I] [1] = I; num [I] [0] ++; For (j = I * 2; j <= 100000; j + = I) {num [J] [++ num [J] [0] = I; prim [J] = 1 ;}}} ll DFS (int id, int B, int now) // calculates the number of numbers that are not in the same quality as that of 'B'. {ll ans = 0; for (INT I = ID; I <= num [now] [0]; I ++) ans + = B/num [now] [I]-DFS (I + 1, B/num [now] [I], now); Return ans;} int main () {int m, n, T; CIN> T; while (t --) {CIN> m> N; Init (); long sum = 0; for (INT I = 2; I <= m; I ++) sum + = N-DFS (1, n, I); printf ("% i64d \ n ", sum + n);} return 0 ;}
Hdu2841 visible trees)