zoukankan      html  css  js  c++  java
  • UVA

    Description

    Download as PDF

    Hackerland is a happy democratic country with m×n cities, arranged in a rectangular m by n grid and connected by m roads in the east-west direction and n roads 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 all the m cities in that row and each north-south road connects all the n cities 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).

    epsfbox{p3720.eps}

    Input 

    The input contains several blocks of test cases. Each test case consists of a single line containing two integers 1$ le$n , m$ le$300 , specifying the number of cities. The input is terminated by a test case with n = 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 4
    3 3
    0 0
    

    Sample Output 

    12
    14
    题意:有一个n行m列的点阵,问一共同拥有多少条非水平非竖直的直线至少穿过当中的两个点。
    思路:这题分两步,首先计算出从点[1, 1]到[n, m]格子组成的边界的连线,然后这是须要容斥去重的,这个有新的数能加的可能是当维度i和j是互质的时候就会多一个,
    然后就是递推n*m的格子的总个数。也要容斥去重,这个就是减去减半的数。这个是由于会重线。最后是对称。从左上到右下,和从坐下到右上
    
    #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
    ", ans[n-1][m-1] * 2);	
    	}
    	return 0;
    }

     
  • 相关阅读:
    poj1088滑雪
    百度关键词质量度如何提高?9个质量度影响因素
    百度竞价点击价格怎么算?计算公式准吗
    什么是百度竞价包年?竞价包年骗局揭露
    百度竞价如何调价?不是你想的那么简单
    为什么我的关键词搜不到,没有排名?
    为什么你的百度竞价有点击无咨询
    百度竞价中那些不为人知的规则与猫腻
    百度推广关键词点击价格为什么会高于出价?因为你开通了这8项功能
    百度推广点击价格怎样降低,怎样省钱?
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6747508.html
Copyright © 2011-2022 走看看