Hdu2841 visible trees)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.