UVALive 3720 Highways 组合数学


根据对称性只考虑 \ 的斜线

枚举 \ 所在的边框的大小 a,b 只有在 gcd(a,b) 不为1的情况下才是不重复的直线

有(n-a)*(m-b)个, 如果边框的左上点接着一个边框的右下点则是重复的直线 需要减去 max(n-2*a,0)*max(m-2*b,0)个重复的边框

所以对于一个边长为a,b的边框来说 有 (n-a)*(m-b) - max(n-2*a,0)*max(m-2*b,0) 条不同的直线

最后结果*2


Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

 Status

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




/* ***********************************************
Author        :CKboss
Created Time  :2015年02月03日 星期二 15时08分29秒
File Name     :UVALive3720.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

LL n,m;

LL GCD[400][400];

int gcd(int a,int b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}

void init()
{
	for(int i=1;i<=310;i++)
		for(int j=1;j<=310;j++)
			if(GCD[i][j]==0)
				GCD[i][j]=GCD[j][i]=gcd(i,j);
}

LL solve(LL n,LL m)
{
	LL ret=0;
	for(LL i=1;i<=n;i++)
	{
		for(LL j=1;j<=m;j++)
		{
			if(GCD[i][j]!=1) continue;
			LL c = max(0LL,n-2LL*i)*max(0LL,m-2LL*j);
			ret+=(n-i)*(m-j)-c;
		}
	}
	return ret*2LL;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	init();
	
	while(cin>>n>>m)
	{
		if(n==0LL&&m==0LL) break;
		cout<<solve(n,m)<<endl;
	}
    
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值