Link: Play with floor and ceil
UV-10673
Play with floor and ceil
Time limit:3000 Ms |
|
Memory limit:Unknown |
|
64bit Io format:% LLD & % LlU |
Submitstatus
Description
Problem
Play with floor and ceil
Input:Standard Input
Output:Standard output
Time limit:1 second
Theorem
For any two integersXAndKThere exists two more IntegersPAndQSuch 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 valuesXAndK, You 'd only need to find IntegersPAndQThat 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 followingTLines you 'd be given two positive integersXAndK. You can safely assume thatXAndKWill always be less108.
Output
For each of the test cases print two integers:PAndQIn one line. These two integers are to be separated by a single space. If there are multiple pairsPAndQThat satisfy the equation, any one wocould do. But to help us keep our task simple, please make sure that the values, AndFit in64Bit 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
A simple math question ..
Q: Is it to find out if there are any p and q that match the formula in the question? If yes, p and q are output.
Idea: Start with P, and scan one side from 0 to K. If Q exists, you can use break to satisfy the formula, and then output p and q.
To put it simply, floor and ceil are both library functions in the math header file. Floor indicates downgrading, And Ceil indicates rounding up.
In C language library functions, the floor function syntax is as follows:
# Include <math. h>
Double floor (double Arg );
Function: The return parameters of a function are not greaterARG. For example,
X = 6.04;
Y = floor (X );
The value of Y is 6.0.
Ceil is similar ..
AC code:
/*************************************** * *********************************> File Name: b. CPP> author: zzuspy> mail: [email protected]> created time: monday ******************************** **************************************** /# include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <cstdlib> # include <cmath> # include <stack> # include <queue> # define ll long # define max3 (, 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 ", & 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) // determines whether Q is true. Here, p * FL must add a (LL ), prevent int overflow break;} printf ("% d \ n", p, q);} return 0 ;}
Ultraviolet-10673-play with floor and ceil (simple mathematics !)