当前位置:网站首页>「题解」带分数
「题解」带分数
2022-07-24 08:18:00 【北柒kylin】
(数学老师:你这是最简分数吗你!)
原题目链接:link
这道是深搜,平均用时 3000 m s 3000\mathrm{ms} 3000ms;个人做法是暴力枚举,用时 139 m s 139\mathrm{ms} 139ms。
但是我不知道这个同学 (变态) 130 m s 130\mathrm{ms} 130ms 是怎么做到的……

「我的做题历程」:
step1:观察题面。
「带分数中,数字 1 ∼ 9 1\sim 9 1∼9分别出现且只出现一次(不包含 0 0 0),输出该数字 N N N 用数码 1 ∼ 9 1\sim 9 1∼9不重复不遗漏地组成带分数表示的全部种数」,先闪出的是深搜,但由于想不出怎么搜,转战暴力。(题型:DFS or 暴力枚举)
step2:思考解法。
抓住带分数构成特点:整数+假分数,即 x = a + b c ( a , b , c ≠ 0 ) x = a + {b\over c}\ (a,b, c \ne 0) x=a+cb (a,b,c=0)(假分数可化简为整数,即分子可以整除分母 ( b = k c , c ∣ b ) (b = kc,\ c\mid b) (b=kc, c∣b) )。
于是想到先枚举前面的整数 a a a,然后得到后面的假分数化简的结果 k k k,通过枚举 c c c 来得到 b b b。再判断 a , b , c a,b,c a,b,c 是否满足题意即可。
总不能无限枚举吧,来估范围。分子最小为 1 1 1,则整数部分最小为 2 2 2,分母最大可取 9876543 9876543 9876543,还能得到当数 N N N 大于 9876545 9876545 9876545 时无解(然而 N ≤ 1 0 6 N \le 10^6 N≤106 ,这个 N N N 的范围毫无用处)。
step3:完成代码。
代码(抵制学术不端行为,拒绝 Ctrl + C):#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, sum;
bool vis[11]; // 用于判断数码是否重复、遗漏
inline bool nosame(int x) {
// 判断一个数的数码是否重复
memset(vis, 0, sizeof vis);
while (x != 0) {
if (vis[x % 10] || x % 10 == 0) {
return false;
}
vis[x % 10] = true;
x /= 10;
}
return true;
}
inline int len(int x) {
// 获取该数位数(长度)
int l = 0;
while (x != 0) {
l++;
x /= 10;
}
return l;
}
inline bool nopublic(int a, int b, int c) {
// 判断带分数中有无重复的数码
memset(vis, 0, sizeof vis);
while (a != 0) {
vis[a % 10] = true;
if (a % 10 == 0) {
return false;
}
a /= 10;
}
while (b != 0) {
if (vis[b % 10] || b % 10 == 0) {
return false;
}
vis[b % 10] = true;
b /= 10;
}
while (c != 0) {
if (vis[c % 10] || c % 10 == 0) {
return false;
}
vis[c % 10] = true;
c /= 10;
}
return true;
}
inline void get(int x) {
int l = len(n - x);
// 枚举倍数
for (int i = 1; i <= 9876543; i++) {
// 分子至少为一,整数部分就只能为二,分母最大可取 9876543
if (nosame(x * i) && nosame(i) && nopublic(x * i, i, n - x) && len(x * i) + len(i) + l == 9) {
printf("%d=%d+%d/%d\n", n, n - x, x * i, i);
sum++;
}
if (len(x * i) + len(i) + l > 9) {
// 如果数码大于 9 位就不用再算了
break;
}
}
return;
}
int main() {
freopen("fraction.in", "r", stdin);
freopen("fraction.out", "w", stdout);
while (~scanf("%d", &n)) {
sum = 0;
for (int i = 1; i < n; i++) {
if (nosame(i)) {
get(n - i);
}
}
printf("%d\n", sum);
}
return 0;
}
数据 Hack 了呢~你的 Accepted 被 Hack 掉了吗~( ̄y▽, ̄)╭
让我们来解决 『带分数』 叭~
Bye bye!!1
边栏推荐
- jmeter中JSON提取器使用
- P1305新二叉树题解
- 避坑,职场远离PUA,PUA常见的套路与话术你得了解一下!
- VIDAR team team exclusive interview: as we do, as you know
- 国产“火箭心”人工心脏上市 不同人工心脏有什么区别?
- 学习-用do…while循环按公式e=1+1/1!+1/2!+1/3!+…+1/n!计算 e 的值(精度为 1e-6)
- Poj3278 catch the cow
- EZDML逆向工程导入数据库分析实操教程
- What is the difference between domestic "rocket heart" artificial heart and different artificial heart?
- mysql使用explain分析sql执行计划帮助查找性能瓶颈
猜你喜欢

避坑,职场远离PUA,PUA常见的套路与话术你得了解一下!

Wechat applet subscription message development process

What is the NFT concept.. Fully understand NFT market, technology and cases

jmeter中JSON提取器使用
![[matlab] (IV) application of MATLAB in linear algebra](/img/c8/97fddb4105008990173247b1b4a155.png)
[matlab] (IV) application of MATLAB in linear algebra

Markdown basic grammar learning

Introduction of some functions or methods in DGL Library

【游戏合集】手机都要被塞爆了,6款优质Pygame游戏合集降临~(附源码)

Database | simple hospital patient appointment system based on opengauss

Vscode code style notes (vetur)
随机推荐
T-SQL query statement
UVA572油田 Oil Deposits题解
Wechat official account configures custom menu jump applet and automatically replies to jump applet
Introduction to wechat authorized login third-party app applet method
The difference between online learning and offline learning
POJ3278抓住那头牛题解
国产“火箭心”人工心脏上市 不同人工心脏有什么区别?
MySQL date formatting
Kotin fragment the correct way to get ViewModel instances
Alibaba cloud OSS uploads pictures under folders and encounters pits
You can't access this shared folder because your organization's security policies prevent unauthenticated guests from accessing it. These policies can help protect your computer from unsafe or malicio
33 introduction to sparksql, dataframe and dataset
Wechat applet file types and functions
P1135 奇怪的电梯题解
[wechat applet development] (I) development environment and applet official account application
P1739表达式括号匹配题解
Common DOS commands
EZDML reverse engineering import database analysis practical operation tutorial
Full revolutionary Siamese networks for object tracking translation
Markdown basic grammar learning