C++&Pascal&Python——【USACO 3.4.2】——Electric Fence
2017-10-09 20:08
357 查看
Electric Fence
Don Piele
In this problem, `lattice points' in the plane are points with integer coordinates.
In order to contain his cows, Farmer John constructs a triangular electric fence by stringing a "hot" wire from the origin (0,0) to a lattice point [n,m] (0<=;n<32,000, 0<m<32,000), then to a lattice point on the
positive x axis [p,0] (0<p<32,000), and then back to the origin (0,0).
A cow can be placed at each lattice point within the fence without touching the fence (very thin cows). Cows can not be placed on lattice points that the fence touches. How many cows can a given fence hold?
在这个问题中,平面中的“格点”是具有整数坐标的点。
为了遏制他的母牛,农民约翰通过从原点(0,0)穿过一个“热”线到网格点[n,m](0 <=; n <32,000,0 <m <32,000),然后到正x轴上的点阵[p,0](0 <p <32,000),然后返回到原点(0,0)。
一只牛可以放置在栅栏内的每个格子点,而不会碰到栅栏(很瘦的牛)。 奶牛不能放在栅栏接触的格点上。 给定的围栏内有多少只牛?
程序名称:fence9
输入格式
单个输入行包含三个空格分隔的整数,表示n,m和p。
SAMPLE INPUT(文件fence9.in)
7 5 10
输出格式
单行,单个整数,表示指定围栏可以容纳的牛数。
SAMPLE OUTPUT(文件fence9.out)
20
Tips :
皮克定理是指一个计算点阵中顶点在格点上的多边形面积公式,该公式可以表示为2S=2a+b-2,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积。
/*
ID : mcdonne1
LANG : C++
TASK : fence9
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int gcd(int x,int y){return x%y==0 ? y : gcd(y,x%y);}
int main()
{
freopen("fence9.in","r",stdin);
freopen("fence9.out","w",stdout);
int n,m,p,b;
cin>>n>>m>>p;
b=gcd(n,m)+gcd(abs(p-n),m)+p;
cout<<p*m/2+1-b/2<<endl;
return 0;
}
Don Piele
In this problem, `lattice points' in the plane are points with integer coordinates.
In order to contain his cows, Farmer John constructs a triangular electric fence by stringing a "hot" wire from the origin (0,0) to a lattice point [n,m] (0<=;n<32,000, 0<m<32,000), then to a lattice point on the
positive x axis [p,0] (0<p<32,000), and then back to the origin (0,0).
A cow can be placed at each lattice point within the fence without touching the fence (very thin cows). Cows can not be placed on lattice points that the fence touches. How many cows can a given fence hold?
PROGRAM NAME: fence9
INPUT FORMAT
The single input line contains three space-separated integers that denote n, m, and p.SAMPLE INPUT (file fence9.in)
7 5 10
OUTPUT FORMAT
A single line with a single integer that represents the number of cows the specified fence can hold.SAMPLE OUTPUT (file fence9.out)
20
在这个问题中,平面中的“格点”是具有整数坐标的点。
为了遏制他的母牛,农民约翰通过从原点(0,0)穿过一个“热”线到网格点[n,m](0 <=; n <32,000,0 <m <32,000),然后到正x轴上的点阵[p,0](0 <p <32,000),然后返回到原点(0,0)。
一只牛可以放置在栅栏内的每个格子点,而不会碰到栅栏(很瘦的牛)。 奶牛不能放在栅栏接触的格点上。 给定的围栏内有多少只牛?
程序名称:fence9
输入格式
单个输入行包含三个空格分隔的整数,表示n,m和p。
SAMPLE INPUT(文件fence9.in)
7 5 10
输出格式
单行,单个整数,表示指定围栏可以容纳的牛数。
SAMPLE OUTPUT(文件fence9.out)
20
Tips :
皮克定理是指一个计算点阵中顶点在格点上的多边形面积公式,该公式可以表示为2S=2a+b-2,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积。
/*
ID : mcdonne1
LANG : C++
TASK : fence9
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int gcd(int x,int y){return x%y==0 ? y : gcd(y,x%y);}
int main()
{
freopen("fence9.in","r",stdin);
freopen("fence9.out","w",stdout);
int n,m,p,b;
cin>>n>>m>>p;
b=gcd(n,m)+gcd(abs(p-n),m)+p;
cout<<p*m/2+1-b/2<<endl;
return 0;
}
{ ID : mcdonne1 LANG : PASCAL TASK : fence9 } var n, m, p, b, a : longint; function abs (a : longint) : longint; begin if a > 0 then exit (a) else exit (-a); end; function gcd (x, y : longint) : longint; begin if x mod y = 0 then exit (y) else exit (gcd (y, x mod y)); end; begin assign (input, 'fence9.in'); assign (output, 'fence9.out'); reset (input); rewrite (output); read (n, m, p); b := gcd (n, m) + gcd (abs (p - n), m) + p; a := p * m div 2 + 1 - b div 2; writeln (a); close (input); close (output); end.
""" ID : mcdonne1 LANG : PYTHON TASK : fence9 """ def gcd (x, y) : if x % y == 0 : return y else : return gcd (y, x% y) fin = open ('fence9.in', 'r') fout = open ('fence9.out', 'w') r = fin.readline().split() n = int(r[0]) m = int(r[1]) p = int(r[2]) b = gcd(n, m) + gcd(abs(p - n), m) + p a = p * m / 2 + 1 - b / 2 fout.write (str(a) + '\n') fin.close() fout.close()
相关文章推荐
- C++&Pascal&Python——【USACO 5.3.4】——Big Barn
- C++&Pascal&Python——【USACO 4.2.2】——The Perfect Stall
- C++&Pascal——【USACO 3.3.2】——Shopping Offers
- C++&Python——【USACO 5.4.1】——Canada Tour
- Pascal & C++——USACO Section 1.2 题解
- C++&Pascal——【USACO 5.1.1】——Fencing the Cows
- C++&Pascal——【USACO 3.3.1】——Riding the Fences
- LeetCodeOJ--Remove Duplicates from Sorted Array(c++ && python版本)
- [leetcode-118]Pascal's Triangle(c++)
- 【Python】C++ & Python 混合编程(1)-- C++使用Python
- OpenCV|图片与视频的相互转换(C++&Python)
- [leetcode-119]Pascal's Triangle II (c++)
- C++ & Pascal——NOIP2016提高组day2 t3——愤怒的小鸟
- AtCoder Beginner Contest 070 ABC C++&&python
- 【Python】C++ & Python 混合编程(4)-- Python 调用 C++(SWIG)
- C++ & Pascal & Java ——NOIP2016提高组day2 t1——组合数问题
- Pascal & C++——NOIP模拟题——小X学游泳
- python&nbsp;中用SWIG包装C和C++和接口…
- 【leetcode】338. Counting Bits(C++ & Python)
- Java & Pascal & C++——容斥原理例题——切蛋糕