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
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
Sample Output
/* ***********************************************
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;
}
枚举 \ 所在的边框的大小 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 |
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;
}
相关文章推荐
- UVALive 3720 Highways(组合计数、递推)
- UVALive 3720-Highways -组合计数+gcd
- UVALive - 3720 Highways 组合计数
- UVALive 7040 Color (容斥原理 + 组合数学递推公式 + 求逆元 + 基础数论)
- UVALive 7040 Color 容斥原理 + 组合数学递推公式 +lucas
- UVALive 3720 UVA 1393 Highways
- UVALive 7040 Color (容斥原理 + 组合数学递推公式 + 求逆元 + 基础数论)
- UVALive 6909 Kevin's Problem 数学排列组合
- The queue uvalive 5002 light 1382 uva 12257 (dfs加组合数学)
- UVALive 3720 Highways
- UVaLIve 2889 | LA 2889 | UVa 12050 - Palindrome Numbers (组合数学)
- UVALive 6469 Deranged Exams 组合数学+容斥原理
- UVA 10910 Marks Distribution(组合数学 或 递推)
- UVAlive7040(组合数,容斥原理,逆元)
- UVa 11538 - Chess Queen (组合数学)
- POJ1306 UVA369 UVALive5341 Combinations【组合计算】
- uva 11609 组合数学
- UVALive 7147-World Cup-数学推导-水题
- UVa 10892 - LCM Cardinality (因式分解 组合数学)
- UVa 11076 - Add Again (排列之和 组合数学)