UVa 880 - Cantor Fractions

题目:按照三角形的形状摆放正整数(从1开始),递归的不断在原来的三角形的斜边上添加一条新边;

现在个你一个数字在这个构造中的序号,输出它的行列值。

分析:数学。第k个三角形包含前k(k+1)/ 2个元素,每次从左上角向下移动,横纵坐标之和为k+1;

计算出比n小的满足k(k+1)/ 2的k值,然后利用n - k(k+1)/ 2计算出位置即可。

说明:要使用long long,否则会溢出。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

long long bs(long long key)
{
	long long l = 0,r = 2147483646;
	while (l < r) {
		int mid = (l+r+1)/2;
		if (0.5*mid*(mid+1) >= key)
			r = mid-1;
		else l = mid;
	}
    return l;
}

int main()
{
	long long n,b,m;
	while (cin >> n) {
		m = bs(n);
		b = n - m*(m+1)/2;
		cout << m+2-b << "/" << b << endl;
	}
    return 0;
}
时间: 2024-10-13 02:29:38

UVa 880 - Cantor Fractions的相关文章

UVA 12558 Egyptian Fractions (HARD version)

题意: 经典的埃及分数问题,即给出一个真分数,求出用个数最少的单位分数来表示这个分数.如果有多种方案,要让每个分数尽量的大,即分母尽量的小.会有K个禁止使用的单位分数. 分析:    IDA*算法.当按照分母递增的顺序排列时, 如果当前考虑的分数为1/e,剩下的maxd - d+1层都是1/e,但仍然到不了目标的a/b的时候,就剪枝. 代码: #include <iostream>#include <cstdio>#include <cstring>#include &

UVa 275 - Expanding Fractions

题目:给你一个分数的分子和分母,求出他的循环节. 分析:模拟.根据鸽巢原理,设分母为m则计算中最多有m中不同的余数,所以循环节小于m. 说明:每次记录余数*10在取新的余数即可. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace

【例题 7-3 UVA - 10976】Fractions Again?!

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] x>=y => \(\frac{1}{x}<=\frac{1}{y}\) => \(\frac{1}{x}=\frac{1}{k}-\frac{1}{y}\) 结合两个式子可以得到 y<=2*k 则枚举y,然后根据式子得到x,判断合法性就ok [代码] /* 1.Shoud it use long long ? 2.Have you ever test several sample(at least ther

UVA - 264 - Count on Cantor (Cantor的数表!)

UVA - 264 Count on Cantor Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Count on Cantor  One of the famous proofs of modern mathematics is Georg Cantor's demonstration that the set of rational nu

暴力枚举 UVA 10976 Fractions Again?!

题目传送门 1 /* 2 x>=y, 1/x <= 1/y, 因此1/k - 1/y <= 1/y, 即y <= 2*k 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <

uva 10976 Fractions Again(简单枚举)

10976 Fractions Again It is easy to see that for every fraction in the form 1 k (k > 0), we can always find two positive integers x and y, x ≥ y, such that: 1 k = 1 x + 1 y Now our question is: can you write a program that counts how many such pairs

uva 10976 fractions again(水题)——yhx

1 #include<cstdio> 2 int a[30010],b[30010]; 3 int main() 4 { 5 int i,j,k,l,m,n,x,y,z; 6 while (scanf("%d",&k)==1) 7 { 8 n=0; 9 for (i=k+1;i<=2*k;i++) 10 if ((k*i)%(i-k)==0) 11 { 12 a[++n]=k*i/(i-k); 13 b[n]=i; 14 } 15 printf("%

Fractions Again?! UVA - 10976

It is easy to see that for every fraction in the form 1k(k > 0), we can always find two positive integersx and y, x ≥ y, such that:1k=1x+1yNow our question is: can you write a program that counts how many such pairs of x and y thereare for any given

Uva 10976 Fractions Again?!

直接暴力 没技巧 y应该从k+1开始循环,因为不然y-k<0的时候 你相当于(x*y) % (负数) 了. 1 #include <iostream> 2 using namespace std; 3 int X[10005]; 4 int Y[10005]; 5 int main() 6 { 7 int k,cnt; 8 while(cin>>k) 9 { 10 cnt=0; 11 for(int y=k+1;y<=2*k;y++) 12 { 13 if( ( (k*