UV-1393 highways

Source: Internet
Author: User

Description

Hackerland is a happy democratic countryM×NCities, arranged in a rectangularMByNGrid and connectedMRoads in the east-west direction andNRoads in the North-South direction. by public demand, this orthogonal road system is to be supplemented by a system of highways in sucha way that there will be a direct connection between any pair of cities. each highway is a straight line going through two or more cities. if two cities lie on the same highway, then they are directly connected. if two cities are in the same row or column, then they are already connected by the existing orthogonal road system (each East-West Road connects allMCities in that row and each north-south road connects allNCities in that column), thus no new highway is needed to connect them. your task is to count the number of highway that has to be built (a highway that goes through several cities on a straight line is counted as a single highway ).

Input

The input contains several blocks of test cases. Each test case consists of a single line containing two integers1N,M300, specifying the number of cities. The input is terminated by a test caseN=M= 0.

Output

For each test case, output one line containing a single integer, the number of highways that must be built.

Sample Input
2 43 30 0
Sample output
1214 question: there is a dot matrix with N rows and M columns. I would like to ask how many non-horizontal and non-vertical lines should pass through at least two of them. Idea: This question is divided into two steps. First, the line between the boundary [1, 1] and [n, m] grids is calculated. Then, we need to remove duplicates, A new number can be added. It is possible that when dimension I and j are mutually dependent, one more is added, and then the total number of grids of the recursive N * m is obtained, we also need to remove duplicates. This is to subtract the number halved. This is because it will repeat the line, and finally symmetric, from top left to bottom right, and from sit down to top right.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 310;int n, m;ll dp[maxn][maxn], ans[maxn][maxn];int gcd(int a, int b) {return b==0?a:gcd(b, a%b);}void init() {memset(dp, 0, sizeof(dp));memset(ans, 0, sizeof(ans));for (int i = 1; i <= 300; i++)for (int j = 1; j <= 300; j++)dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + (gcd(i, j) == 1);for (int i = 1; i <= 300; i++)for (int j = 1; j <= 300; j++)ans[i][j] = ans[i-1][j] + ans[i][j-1] - ans[i-1][j-1] + dp[i][j] - dp[i>>1][j>>1];}int main() {init();while (scanf("%d%d", &n, &m) != EOF && n+m) {printf("%lld\n", ans[n-1][m-1] * 2);}return 0;}

 

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.