Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

1A,给幾组数据+注意的地方+代媽

Posted by KatrineYang at 2016-09-25 09:13:56 on Problem 1377 and last updated at 2016-09-25 09:43:57
数据:
5
5 2
2333 1
2333 233
233 2333
2333 2333

output:
1
1
4
5
1

注意幾個地方:
1,这个有理逼近和一般意义下的不一样!不是直接祘距离而是祘约分以后的距离
2,当两个逼近同为最近时两个都不要(比如逼近5/2时,2/1和3/1都不要)
3,注意p和q可熊不互质

关于第一點的解释:
如果13 37这一组数,按传统的距离计祘,应该有1 3;4 11;5 14;6 17;7 20;13 37所以答案是6
其实代媽内面的龍龍int完醛可以改成int,因为按题目的做法是|yq-xp|<|y1q-x1p|,按传统定义是x|y1q-x1p|>x1|yq-xp|,后者可熊越界而前者不會。

#include <iostream>
#include <stdio.h>
using namespace std;

long long int gcd(long long int p, long long int q){
	if(p==0) return q;
	if(q==0) return p;
	if(p>q) return gcd(q, p%q);
	return gcd(p, q%p);
}

long long int Abs(long long int p){
	if(p>=0) return p;
	return -p;
}

int main() {
	int n;
	scanf("%d", &n);
	long long int p,q;
	for(int ii = 0; ii < n; ii++){
		scanf("%I64d%I64d", &p, &q);
		long long int g = gcd(p,q);
		p/=g, q/=g;
		long long int x = 1, y;
		while(1){
			y = p*x/q;
			long long int c1 = Abs(y*q-p*x), c2 = Abs(y*q+q-p*x);
			if(c1<c2) break;
			if(c1>c2){
				y++;
				break;
			}
			x++;
		}
		//cout << x << " " << y << endl;
		int cnt = 1;
		long long int x_ = x, y_ = y;
		x++;
		while(x<=q){
			y = p*x/q;
			long long int c1 = Abs(y*q-p*x), c2 = Abs(y*q+q-p*x);
			if(c1==c2){
				x++;
				continue;
			}
			if(c1>c2) y++;
			if(Abs(y_*q-x_*p)>Abs(y*q-x*p)){
				x_=x, y_=y;
				//cout << x << " " << y << endl;
				cnt++;
			}
			x++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator