码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 10673 - Play with Floor and Ceil (简单数学!)

时间:2014-12-01 22:26:57      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:uva   数学   ceil   floor   icpc   

题目链接:Play with Floor and Ceil


UVA - 10673

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

SubmitStatus

Description

bubuko.com,布布扣

Problem A
Play with Floor and Ceil
Input:
standard input
Output: standard output
Time Limit: 1 second
 

Theorem

For any two integers x and k there exists two more integersp and q such that:

bubuko.com,布布扣

It’s a fairly easy task to prove this theorem, so we’d not ask you to do that. We’d ask for something even easier! Given the values ofx and k, you’d only need to find integers p and q that satisfies the given equation.

 

Input

The first line of the input contains an integer, T (1≤T≤1000) that gives you the number of test cases. In each of the following T lines you’d be given two positive integersx and k. You can safely assume that x andk will always be less than 108.

 

Output

For each of the test cases print two integers: p and q in one line. These two integers are to be separated by a single space. If there are multiple pairs ofp and q that satisfy the equation, any one would do. But to help us keep our task simple, please make sure that the values,bubuko.com,布布扣 andbubuko.com,布布扣fit in a64 bit signed integer.

 

Sample Input                              Output for Sample Input

3

5 2

40 2

24444 6

1 1

1 1

0 6

 


Problem setter: Monirul Hasan, Member of Elite Problemsetters‘ Panel

Special Thanks: Shahriar Manzoor, Member of Elite Problemsetters‘ Panel

 

Source

Root :: Prominent Problemsetters :: Monirul Hasan
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Mathematics :: Number Theory ::Extended Euclid
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Mathematics :: Number Theory ::Extended Euclid
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 6. Mathematical Concepts and Methods
Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 2. Mathematics :: Number Theory ::Exercises: Beginner



数学类简单题。。

题意:就是找有没有符合题中那个式子的p和q,有就输出p和q

思路:先从p入手,0到k扫一边,如果存在有q可以使式子满足就break,再输出p和q


简单说下floor和ceil,他们都是math头文件中的库函数,floor表示向下取整,ceil表示向上取整


在C语言的库函数中,floor函数的语法如下
#include <math.h>
double floor( double arg );
功能: 函数返回参数不大于arg的最大整数。例如,
x = 6.04;
y = floor( x );
y的值为6.0.

ceil则类似。。


AC代码:


/*************************************************************************
	> File Name: b.cpp
	> Author: zzuspy
	> Mail: zzuspy@qq.com 
	> Created Time: 2014年12月01日 星期一 21时41分53秒
 ************************************************************************/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#define LL long long
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
using namespace std;

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int x, k;
		scanf("%d %d", &x, &k);
		int p, q, fl = (int)floor((double)x/k), ce = (int)ceil((double)x/k);
		for(p = 0; p <= k; p++)
		{
			q = (x-p*fl)/ce;
			if((LL)p*fl+(LL)q*ce == (LL)x)       //判断q是否成立,这里p*fl要加个(LL),防止int溢出
				break;
		}
		printf("%d %d\n", p, q);
	}
	return 0;
}


UVA - 10673 - Play with Floor and Ceil (简单数学!)

标签:uva   数学   ceil   floor   icpc   

原文地址:http://blog.csdn.net/u014355480/article/details/41654159

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!