您的位置:首页 > 其它

UVALive 3720 Highways 组合数学

2015-02-03 16:18 429 查看
根据对称性只考虑 \ 的斜线

枚举 \ 所在的边框的大小 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

Highways

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

Description





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).



Input

The input contains several blocks of test cases. Each test case consists of a single line containing two integers 1

n , m

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: