题目大意: 给出一个真分数,把它分解成最少的埃及分数的和.同时给出了k个数,不能作为分母出现,要求解的最小的分数的分母尽量大. 分析: 迭代加深搜索,求埃及分数的基础上,加上禁用限制就可以了.具体可以参考一下紫书. #include<cstdio> #include<cstring> #include<algorithm> #include<set> using namespace std; typedef long long LL; LL ans[],v[…
UVA12558 Egyptian Fractions (HARD version) 题解 迭代加深搜索,适用于无上界的搜索.每次在一个限定范围中搜索,如果无解再进一步扩大查找范围. 本题中没有分数个数和分母的上限,只用爆搜绝对TLE.故只能用迭代加深搜索. #include<cstdio> #include<cstring> #include<set> using namespace std; typedef long long ll; int num,T,t,k;…
Egyptian Fractions (HARD version) 题解:迭代深搜模板题,因为最小个数,以此为乐观估价函数来迭代深搜,就可以了. #include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<algorithm> #include<cstdlib> #define LL long long #define N 10 usin…
IDA* 就是iterative deepening(迭代深搜)+A*(启发式搜索) 启发式搜索就是设计估价函数进行的搜索(可以减很多枝哦~) 这题... 理论上可以回溯,但是解答树非常恐怖,深度没有明显上界,加数的选择理论上也是无限的. 我们可以从小到大枚举深度maxd, 设计估价函数,当扩展到第i层,前i个分数的和为c/d,第i的分数为1/e,接下来至少需要(a/b+c/d)/(1/e)个分数,如果超过maxd-i+1,那么直接回溯就好了.. #include<cstdio> #inclu…
题目大意:经典的埃及分数问题. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std; # define LL long long int num[5],a,b,k; LL ans[10000],v[10000]; LL gcd(LL a,LL b) { return (b==0)?a:gcd(b,a%b)…
传送门 题目大意 给出一个真分数 a/b,要求出几个互不相同的埃及分数(从大到小),使得它们之和为 a/b (埃及分数意思是分子为1的分数,详见百度百科) 如果有多组解,则分数数量少的优先 如果分数数量一样则分母最大的要尽量小,如果最大的分母同样大,则第二大的分母尽量小,以此类推 为了加大难度,会给出k个不能作为分母的数 (2<=a,b<=876,k<=5 并且 a,b 互质) 首先想的是数论,但是呢 推不出来... 然后发现a,b好像不大 貌似可以搜索 但是呢 不知道上界... 那就迭…
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 枚举最大量maxdep 在dfs里面传剩余的要凑的分子.分母 以及上一次枚举的值是多少. 然后找到最小的k,满足1/k<=分子/分母 然后从max(k,last+1)开始枚举. ->剪枝就是剩余的全都用这个最大的分数.如果都不行就肯定不行了. 二分找这个k. 不能用的数字就直接跳过就行. [代码] /* 1.Shoud it use long long ? 2.Have you ever test severa…
Problem UVA12558-Efyptian Fractions(HARD version) Accept:187  Submit:3183 Time Limit: 3000 mSec  Problem Description Given a fraction a/b, write it as a sum of different Egyptian fraction. For example, 2/3 = 1/2 + 1/6. Thereisonerestrictionthough: th…
题目大意:给一个分数,对其进行化简.因为分子.分母最大为1030,所以用要用大数. import java.io.*; import java.util.*; import java.math.*; class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int T = cin.nextInt(); while (T-- > 0) { BigInteger a = ci…
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 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…
题意描述 题目描述的翻译挺清楚的了. 和原题的区别是多了禁用的分母.(还有毒瘤输入输出) 算法分析 显然这道题没有什么很好的数学方法来解决,所以可以使用搜索. 由于不确定深度,深搜显然无穷无尽. 所以一开始考虑使用广搜,如果不加改变空间复杂度显然呈指数级增长. 使用启发式搜索来实现,但是此题显然没有必要.(有兴趣的可以自行尝试实验) 使用迭代加深(ID)实现,代码较上一方法更容易实现. 不熟悉 ID 的同学可以先找别的题目了解一下. 假设当前已经到了 \(dep\) 个数,上一次使用的分母是 \…
Problem EEg[y]ptian Fractions (HARD version)Given a fraction a/b, write it as a sum of different Egyptian fraction. Forexample, 2/3=1/2+1/6.There is one restriction though: there are k restricted integers that should notbe used as a denominator. For…
P3172 [CQOI2015]选数 gcd 为 \(K\) 不太好办,所以我们先把它转化成 gcd 为 1 的问题: scanf("%d%d%d%d",&n,&k,&l,&r); l=l/k+!!(l%k),r/=k,L=r-l; // L 表示区间长度 现在我们需要求的是:在 \([l,r]\) 区间内选出 \(n\) 个数,使它们的最大公约数为 1 的方案数. 有两种做法:莫反+杜教筛 \(O(r^{2/3})\) / 容斥 \(O(L\log L…
题目传送门 /* x>=y, 1/x <= 1/y, 因此1/k - 1/y <= 1/y, 即y <= 2*k */ #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstring> #include <string> #include <map> #include <…
UVA.10986 Fractions Again (经典暴力) 题意分析 同样只枚举1个,根据条件算出另外一个. 代码总览 #include <iostream> #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <sstream> #include <set> #include <map&g…
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…
直接暴力 没技巧 y应该从k+1开始循环,因为不然y-k<0的时候 你相当于(x*y) % (负数) 了. #include <iostream> using namespace std; ]; ]; int main() { int k,cnt; while(cin>>k) { cnt=; ;y<=*k;y++) { ) ) { X[cnt]=k*y/(y-k); Y[cnt]=y; cnt++; } } cout<<cnt<<endl; ;i…
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB3gAAAM+CAIAAAB31EfqAAAgAElEQVR4nOzdO7KtPJum69GEpAcVQQfSoA80ATddvDLSogMV9ICINLaJkUZZ2NvAKGtH4FcQZaZR9IFtvP96fy0OQkgCxuG+rO9bc86BACGJBw3xmgEAAAAAAAAAcPD/7Xg9XTAAAAAAAAAAwGcgaAYAAAAAAAAABCFoBgAAAAAAAAAEIWgGAAAAA…
It is easy to see that for every fraction in the form  (k > 0), we can always find two positive integers x and y,x ≥ y, such that: . Now our question is: can you write a program that counts how many such pairs of x and y there are for any givenk? Inp…
基本思路就是Bfs: 本题的一个关键就是如何判段状态重复. 1.如果将状态用一个int型数组表示,即假设为int state[17],state[0]代表机器人的位置,从1到M从小到大表示障碍物的位置.那么如果直接用STL中的set是会超时的,但如果自己建立一个hash方法,像这样: int getKey(State& s) { long long v = 0; for(int i=0; i<=M; ++i ) { v = v * 10 + s[i]; } return v % hashSi…
题意:给三种操作 1.在p位置插入一个字符串. 2.从p位置开始删除长度为c的字符串 3.输出第v个历史版本中从p位置开始的长度为c的字符串 解法:可以用平衡树做,但是不会.后来又听说可一用一个叫rope的神奇的STL,学习了一下,用法基本和string一样.roap的内部是用平衡树实现的,历史版本和当前版本可以共享一些内存,插入和删除整段字符串效率很高.是可持久化的数据结构. //Time: 952 MS #include <iostream> #include <ext/rope&g…
题目大意:给一个正整数n,求出在[1, n]区间内和n互质的正整数的个数.Euler's Totient(欧拉函数)的直接应用. #include <cstdio> #include <vector> #include <algorithm> #include <bitset> using namespace std; typedef vector<int> vi; typedef long long ll; #define MAXN 10000…
题目链接:https://vjudge.net/problem/UVA-10976 It is easy to see that for every fraction in the form 1k(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 ma…
[题意]:给你一个数k,求所有使得1/k = 1/x + 1/y成立的x≥y的整数对. [分析]:枚举所有在区间[k+1, 2k]上的 y 即可,当 1/k - 1/y 的结果分子为1即为一组解. [代码]: #include<bits/stdc++.h> using namespace std; int x[10005]; int y[10005]; #define LL long long int main() { int k,c; while(~scanf("%d",…
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…
紫薯例题 #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f; ],ch[N*][],tot,n,d,ver; ]; #define l(u) ch[u][0] #define r(u) ch[u][1] int rnd() { )-,X=; )%M; return seed=(ll)seed*X%M; } ,l(u)=r(u)=,val[u]=c; return u;} int…
这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见错误) 能用结构体,就别用数组,容易出错(暂时还不知道为什么)=>现在知道申请的数组空间在运行期间被释放,除非用malloc去申请数组 代码要规范,空格该有就要有 有些不规范表达式,不同编译器出现不同结果,注意应避免使用这类语句 像这道题主要坑在了第三点上,以后要注意避免 以下是AC代码 第一次完成…
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=995 Problem D: The Necklace  My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace shared a c…
参考:http://www.cnblogs.com/xiaobaibuhei/p/3301110.html 算法学到很弱,连这么简单个问题都难到我了.但我偏不信这个邪,终于做出来了.不过,是参照别人的,是 xiaobaibuhei 到博客让我找到到感觉,不过我只看了一遍他到代码,后面都是自己写的,虽然写的很像... Write a program that finds and displays all pairs of 5-digit numbers that between them use…
题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=258 http://poj.org/problem?id=1138 题目描写叙述:  Ships  Probably everyone who ever attended school knows the game where two opposing players place…