UVa10976 - Fractions Again?!

题意

输入正整数k,找到所有的正整数x >= y,使1/k = 1/x +1/y。

思路

先找到x或y的范围:因为x >= y,所以 1/k <= 2/y 得出 y<=2*k

再找到可以计算出x的方法即可

总结

刚开始用了二重循环找x,结果超时。今天早晨起来想了个更常规且更简单的方法

 1 #include <iostream>
 2 #include <cstdio>
 3 const int maxn = 1e6+5;
 4 typedef long long LL;
 5 using namespace std;
 6 LL a[maxn],b[maxn];
 7 int main()
 8 {
 9     LL k;
10     while(scanf("%lld",&k) == 1) {
11         LL cnt = 0,kk = 2*k,x,y;
12         for(LL y = k+1; y <= kk; y++) {
13             if(y*k%(y-k))
14                 continue;
15             LL x = y*k/(y-k);
16             if(x >= y) {
17                 a[cnt] = x;
18                 b[cnt++] = y;
19             }
20         }
21         cout<<cnt<<endl;
22         for(int i = 0; i < cnt; i++)
23             printf("1/%lld = 1/%lld + 1/%lld\n",k,a[i],b[i]);
24     }
25     return 0;
26 }
时间: 2024-08-07 21:20:56

UVa10976 - Fractions Again?!的相关文章

UVa10976 Fractions Again?! (推公式)

链接:http://acm.hust.edu.cn/vjudge/problem/37234分析:x>=y和1/k=1/x+1/y=>y<=2k再加上x,y,k都是正整数可以推出y>k. 1 #include <iostream> 2 using namespace std; 3 4 int main() { 5 int k; 6 while (scanf("%d", &k) == 1) { 7 int num = 0, a[1000], b

USACO 2.1 Ordered Fractions

Ordered Fractions Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N. Here is the set when N = 5: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 Write a program that, given an integer N between

模拟 --- hdu 12878 : Fun With Fractions

Fun With Fractions Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 152, Accepted users: 32 Problem 12878 : No special judgement Problem description A rational number can be represented as the ratio of two integ

POJ Octal Fractions(JAVA水过)

题目链接:CLICK HERE~ 虽然java一下模拟水过,但是我看到别人的一段神奇代码,贴出和大家共享. import java.math.*; import java.util.*; class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); BigDecimal Eight = new BigDecimal(8); while(cin.hasNext()){ String

POJ 1131 Octal Fractions (Java大数,八进制转十进制)

Octal Fractions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6959   Accepted: 3825 Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0.75 in octal is 0.953125 (7/8 + 5/64) in d

Poj1131-Octal Fractions

Octal Fractions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6669   Accepted: 3641 Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0.75 in octal is 0.953125 (7/8 + 5/64) in d

Codeforces 396B On Sum of Fractions 数论

题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/20076297 题目大意:给出一个n,ans = ∑(2≤i≤n)1/(v(i)*u(i)), v(i)为不大于i的最大素数,u(i)为大于i的最小素数, 求ans,输出以分式形式. 解题思路:一开始看到这道题1e9,暴力是不可能了,没什么思路,后来在纸上列了几项,突然想到高中时候求等差数列时候用到

USACO Section 2.1 Ordered Fractions

/* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; typedef struct Fraction { int numerator; int denominator; Fraction(){} Fraction(int x, int

Project Euler:Problem 33 Digit cancelling fractions

The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that49/98 = 4/8, which is correct, is obtained by cancelling the 9s. We shall consider fractions like, 30/50 = 3/5, to be