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

题目链接:Play with Floor and Ceil

UVA - 10673

Play with Floor and Ceil

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

SubmitStatus

Description

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:

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, andfit
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: [email protected]
	> 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;
}
时间: 2024-12-11 18:15:54

UVA - 10673 - Play with Floor and Ceil (简单数学!)的相关文章

UVa 10673 Play with Floor and Ceil

方法 : 数论? 用k,x/k, x%k表示 ceil 和 floor,观察求解. code: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack> #include <bitset> #include &

UVA - 10673 Play with Floor and Ceil(手动解方程)

题目: 这个题目,不是在O(T)时间内解决的,估计是因为数论学的不好,只能套一些模板了, 比如我看网上基本上都是用欧几里得做的. 好了,说正事,就从欧几里得的理论基础开始说起--带余除法 算了,还是看百科吧点击打开百科 设s=x/k,那么,x=s*k+t,0≤t<k 那么方程化为s*k+t=p*k+q*(k+(t>0)) 如果t=0,那么s=p+q,取p=s,q=0 如果t>0,那么s*k+t=(p+q)*k+q,取p=s-t,q=t 所以上面2种情况是一样的. 代码: #include

拓展欧几里得详解 及其题目 POJ 1061 2115 2142 UVA 10673 10090

最近做了一些拓展欧几里得的题目呢,嘛,从一开始的不会到现在有点感觉,总之把我的经验拿出来和大家分享一下吧. 普通的欧几里得是用于解决求两个数a,b的gcd的,但是我们知道,gcd是线性组合 { ax+by | x,y∈Z }里的最小正元素(什么?不知道怎么来的?好吧...算法导论里数论算法那一章有证明),假若我们能够把这个x和y找出来,那么可以用来解决很多问题. (以下的gcd和lcm均指(gcd(a,b)和lcm(a,b)) 首先,假设ax+by=gcd这一个方程有一个特解x*,y*.那么显然

Uva10673 - Play with Floor and Ceil ( 扩展欧几里定理 )

Uva10673 - Play with Floor and Ceil ( 扩展欧几里定理 )  实际上是一道很裸的扩展欧几里德定理的题目,结果把Floor和Ceil搞反了WA一次悲剧啊 #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long LL; void ex_gcd(LL a, L

问题:oracle floor;结果:Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 (2011-04-06 16:10:35) 转载▼ 标签: 谈 分类: 渐行渐远 FLOOR——对给定的数字取整数位 SQL> select floor(2345.67) from dual; FLOOR(2345.67) -------------- 2345 CEIL-- 返回大于或等于给出数字的最小整数 SQL> select ceil(3.1415927) from dual; CEIL(3.14

Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 FLOOR——对给定的数字取整数位SQL> select floor(2345.67) from dual; FLOOR(2345.67)--------------2345 CEIL-- 返回大于或等于给出数字的最小整数SQL> select ceil(3.1415927) from dual; CEIL(3.1415927)---------------              4 ROUND——按

hdu 2200 Eddy&#39;s AC难题(简单数学。。)

题意: N个人,每个人AC的题数都不一样. Eddy想从中选出一部分人(或者全部)分成两组.必须满足第一组中的最小AC数大于第二组中的最大AC数. 问共有多少种不同的选择方案. 思路: 简单数学.. 代码: ll C(int n,int x){ ll ans=1; rep(i,1,x){ ans = ans*(n+1-i)/i; } return ans; } int main(){ int n; while(cin>>n){ ll ans = 0; rep(i,2,n){ ans += (C

UVA 11605 - Lights inside a 3d Grid(概率+数学)

UVA 11605 - Lights inside a 3d Grid 题目链接 题意:给定一个NxMxP的三维网格,每个格子上一盏灯,现在每次随机选择两点,把这两点构成立方体中间那一块开关灯状态转换,问K步之后网格中亮灯的期望 思路:概率问题,把x,y,z轴分开考虑,算出每一个点xi,yi,zi分别能被选到的情况数,然后根据乘法原理相乘起来除以总情况就能算出一点的概率,然后问题就是K次了,对于K次,每次开到的概率为P的情况下,总情况为∑k1Pi(1?P)k?ii为奇数,那么根据组合公式很容易化

uva 10620 - A Flea on a Chessboard(暴力+数学)

题目链接:10620 - A Flea on a Chessboard 题目大意:在一个国际象棋的棋盘上,以左下角作为坐标轴建立坐标系,并且左下角的格子为黑色,每个格子边长为s.假定棋盘无限大,给定跳蚤的起始位置和方向,问这个苦逼的跳蚤能否跳到白格子. 解题思路:枚举前s*2步即可,因为2*2的格子形成了2白两黑的最小单位,边长为2*s,2*s步等于是跳回了相应的起始位置. #include <cstdio> #include <cstring> int s, x, y, dx,