UVA 1393 - Highways

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



#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <stdio.h>
#include <cmath>
#include <math.h>
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,b,a) for(int i = (b); i >= (a); --i)
#define clr(a,x) memset(a,(x),sizeof(a))
#define LL long long
#define eps 1e-10
using namespace std;
const int maxn = 300 + 5;

vector<int> v[maxn];
int num[maxn][maxn];
LL dp[maxn][maxn];

int gcd(int a,int b)
{
    while (a && b) {
        if (a > b) a %= b;
        else b %= a;
    }
    return a + b;
}

void pre_init()
{
    rep(i,1,maxn) {
        rep(j,1,maxn) if (gcd(i,j) == 1) {
            v[i].push_back(j);
            num[i][j] = 1;
        }
        rep(j,1,maxn) num[i][j] += num[i][j-1];
    }

    rep(j,1,maxn) rep(i,1,maxn) {
        num[i][j] += num[i-1][j];
    }
    clr(dp,0);
    rep(i,1,maxn)
    rep(j,1,maxn-1) {
        dp[i][j+1] = dp[i][j];
        rep(k,1,i+1) {
            dp[i][j+1] += num[k-1][j];
            dp[i][j+1] -= num[j/2][(k-1)/2];
            dp[i][j+1] += num[i-k][j];
            dp[i][j+1] -= num[(i-k)/2][j/2];
        }
    }
}

int main()
{
    #ifdef ACM
        freopen("in.txt", "r", stdin);
       // freopen("out.txt","w",stdout);
    #endif // ACM
    pre_init();
    int n,m;
    while (scanf("%d%d",&n,&m),n+m) {
        printf("%lld\n",dp[n][m]);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值