当前位置:网站首页>【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
2022-07-06 09:26:00 【火焰车】
翻译:
输入正整数k,找到所有的正整数x≥y,使得
1 k = 1 x + 1 y \frac{1}{k}=\frac{1}{x}+\frac{1}{y} k1=x1+y1
这个题也是很有意思,搞一些提前的运算:
既然要求找出所有的x,y,枚举的对象自然也就是x,y了。可问题在于,枚举的范围如何?从1/12=1/156+1/13可以看出,x可以比y大很多。难道要无休止地枚举下去?当然不是。由于x≥y,有1/x ≤1/y,因此 1/k - 1/y ≤ 1/y,即 y≤2k。这样只需要在[k+1,2k]范围内枚举y,然后根据y尝试计算出x即可。
(内容来自紫书)
这也太好了!
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const ll mod = 1e9+7;
int main()
{
int n;
while(cin>>n && n)
{
int cnt = 0;
for(int i=n+1;i<=2*n;i++)
if((i*n)%(i-n)==0)
cnt++;
cout<<cnt<<endl;
for(int i=n+1;i<=2*n;i++)
if((i*n)%(i-n)==0)
printf("1/%d = 1/%d + 1/%d\n",n,(i*n)/(i-n),i);
}
}
直接写两遍一边算cnt一边输出是因为我太懒了。。。可能有简单的方法??额应该差不多吧。
中间的判断思路是这样的:1/k = 1/x +1/y,也就是说 1/k - 1/y = 1/x就是满足条件的。
等式左边化简一下就是 y/ky - k/ky ==》 (y-k)/yk 也就是说只要这个式子能化简成 1/?就是满足条件的。
也就是 1/(yk/(y-k))的分母是个整数。也就是yk%(y-k) ==0
因此得到了代码中的那个算式。
总结:
这样算式的题,要用未知数来推出几个新的算式简化题目。
主要就是推式子,推不出来就完犊子了。。。
边栏推荐
- ucore lab 2
- STM32 learning record: LED light flashes (register version)
- Research Report on market supply and demand and strategy of China's Medical Automation Industry
- Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
- Research Report on medical toilet industry - market status analysis and development prospect forecast
- 学习记录:STM32F103 时钟系统概述工作原理
- 洛谷P1102 A-B数对(二分,map,双指针)
- 1010 things that college students majoring in it must do before graduation
- Research Report on shell heater industry - market status analysis and development prospect forecast
- 0-1背包問題(一)
猜你喜欢
随机推荐
Cost accounting [17]
Truck History
STM32学习记录:LED灯闪烁(寄存器版)
信息安全-威胁检测引擎-常见规则引擎底座性能比较
Research Report on market supply and demand and strategy of China's land incineration plant industry
Research Report on market supply and demand and strategy of geosynthetics industry in China
Research Report on medical anesthesia machine industry - market status analysis and development prospect prediction
Borg Maze (BFS+最小生成树)(解题报告)
Flink 使用之 CEP
HDU - 6024 Building Shops(女生赛)
Learning record: use STM32 external input interrupt
China potato slicer market trend report, technical dynamic innovation and market forecast
Learning record: understand systick system timer and write delay function
Accounting regulations and professional ethics [1]
Market trend report, technological innovation and market forecast of pneumonia drugs obtained by Chinese hospitals
TCP的三次握手与四次挥手
Research Report on market supply and demand and strategy of Chinese hospital cleaning chemicals industry
Cost accounting [13]
0-1背包問題(一)
Market trend report, technical innovation and market forecast of Chinese hospital respiratory humidification equipment