USACO Section 3.4 A Electric Fence

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<32000, 0<m<32000), then to a lattice point on the positive x axis [p,0] (p>0), 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
题意:在一个坐标系中求一个给定三角形中的整点数,输入 n m p,构成的三角形是 (0,0) (n,m) (p,0)
分析:直接枚举从 y = (1,m)
View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: fence9
*/
#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

int main()
{
    freopen("fence9.in","r",stdin);
    freopen("fence9.out","w",stdout);
    int n,m,p,x1,x2,i,ans;
    while(scanf("%d%d%d",&n,&m,&p)==3)
    {
        ans=0;
        for(i=1;i<m;i++)
        {
            x1=n*i*1.0/m+1;
            x2=p-i*(p-n)*1.0/m;
            if((p-n)*i%m==0) x2--;
            ans+=(x2-x1+1);
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(USACO Section 3.4 A Electric Fence)