A Rational Sequence (UVALive 7786 UVALive 7096)

2023-12-03 01:48

本文主要是介绍A Rational Sequence (UVALive 7786 UVALive 7096),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

两个题的区别在于,一个是给出n求第n个是多少。一个是已知这个数,求它的下一个数。

在做第二题的时候,因为我做过第一题有一种先入为主的思想,以为求它的下一个数就是先求出它是这个数塔里的第n个,然后求n+1个即可。完全错误,直接找规律求解即可。

7786

Sample Input

4

1 1

2 4

3 11

4 1431655765

Sample Output

1 1/1

2 1/3

3 5/2

4 2178309/1346269

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<stack>
#include<climits>
#include<map>
#include<ctime>
#define INF 0x3f3f3f3f
typedef long long LL;
using namespace std;map<LL,LL> mp1;
map<LL,LL> mp2;void f(int n)
{if(n==1){mp1[1]=1;mp2[1]=1;}else{if(n%2==0){f(n/2);mp1[n]=mp1[n/2];mp2[n]=mp1[n/2]+mp2[n/2];}if(n%2==1){f(n/2);mp2[n]=mp2[n/2];mp1[n]=mp1[n/2]+mp2[n/2];}}
}int main()
{int t,k,n;cin>>t;while(t--){cin>>k>>n;f(n);cout<<k<<" "<<mp1[n]<<"/"<<mp2[n]<<endl;}return 0;}

7096

Sample Input

5

1 1/1

2 1/3

3 5/2

4 2178309/1346269

5 1/10000000 

Sample Output

1 1/2

2 3/2

3 2/5

4 1346269/1860498

5 10000000/9999999

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
typedef long long LL;
using namespace std;
int main()
{int t,s;cin>>t;int k,x,y,xx,yy,xxx,yyy,ans;char c;while(t--){cin>>k>>x>>c>>y;if(y==1){cout<<k<<" "<<"1"<<c<<x+y<<endl;}else{if(x<y){cout<<k<<" "<<y<<c<<y-x<<endl;}else{ans=x/y;x=x%y;xx=y;yy=y-x;xxx=xx;yyy=yy+(y*ans);cout<<k<<" "<<xxx<<c<<yyy<<endl;}}}return 0;
}

 

这篇关于A Rational Sequence (UVALive 7786 UVALive 7096)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/447641

相关文章

浙大数据结构:02-线性结构4 Pop Sequence

这道题我们采用数组来模拟堆栈和队列。 简单说一下大致思路,我们用栈来存1234.....,队列来存输入的一组数据,栈与队列进行匹配,相同就pop 机翻 1、条件准备 stk是栈,que是队列。 tt指向的是栈中下标,front指向队头,rear指向队尾。 初始化栈顶为0,队头为0,队尾为-1 #include<iostream>using namespace std;#defi

【UVA】1626-Brackets sequence(动态规划)

一道算是比较难理解的动规。 状态转移分2个: (用d[i][j]表示在i~j内最少需要添加几个括号,保持平衡) 1.如果s[i]和s[j]是一对括号,那么d[i][j] = d[i + 1][j - 1] 2.否则的话 d[i][j] = min(d[i][k],[k + 1][j]); 边界是d[i + 1][i] = 0; d[i][i] = 1; 13993644 162

【UVA】10534 - Wavio Sequence(LIS最长上升子序列)

这题一看10000的数据量就知道必须用nlog(n)的时间复杂度。 所以特意去看了最长上升子序列的nlog(n)的算法。 如果有2个位置,该位置上的元素为A[i]和A[j],并且他们满足以下条件: 1.dp[i] = dp[j]    (dp[x]代表以x结尾的最长上升子序列长度) 2.A[i] < A[j] 3.i < j 那么毫无疑问,选择dp[i] 一定优于选择dp[j] 那么

2015年多校联合训练第一场OO’s Sequence(hdu5288)

题意:给定一个长度为n的序列,规定f(l,r)是对于l,r范围内的某个数字a[i],都不能找到一个对应的j使得a[i]%a[j]=0,那么l,r内有多少个i,f(l,r)就是几。问所有f(l,r)的总和是多少。 公式中给出的区间,也就是所有存在的区间。 思路:直接枚举每一个数字,对于这个数字,如果这个数字是合法的i,那么向左能扩展的最大长度是多少,向右能扩展的最大长度是多少,那么i为合法的情况

NYOJ 763 Vawio Sequence

OJ题目 : 戳这里~ 描述 Vawio Sequence is very funny,it is a sequence of integers. It has some interesting properties. ·   Vawio is of odd length i.e. L = 2*n + 1. ·  The first (n+1) integers of  Vawio s

python中使用FormatDataLibsvm转为txt文件后报错illegal multibyte sequence

‘gbk’ codec can’t decode byte 0xff in position 0: illegal multibyte sequence 这个报错是因为编码不对,正确的编码是ANSI编码,txt文件打开后另存为可以看到当前的文本文档编码 但是excel不能直接保存ANSI编码的txt文件 所以不能直接保存为ANSI编码 有两种解决办法 1.新建一个txt文件(新建的txt文件

[LeetCode] 128. Longest Consecutive Sequence

题:https://leetcode.com/problems/longest-consecutive-sequence/description/ 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should

【UVALive】6709 Mosaic 二维线段树

传送门:【UVALive】6709 Mosaic 题目大意: 每次选择矩阵中的一个点,求以他为中心,边长为D(D一定是奇数)的矩阵中的最小值min和最大值max,输出ans =(min+max)/ 2(向下取整)。然后将该点的值修改为ans。 题目分析: 赤果果的二维线段树单点修改、区间查询。 又一次学习了线段树,发现原来所有的更新操作全部放在二维中即可。 很不错,一定要经常看看,温

【UVALive】3887 Slim Span 枚举+最小生成树

传送门:【UVALive】3887 Slim Span 题目大意:给出一个n(2 <= n <= 100)个结点的无向图,找一棵苗条度(最大边减最小边的值)最小的生成树。图中不含自环或重边。 题目分析:枚举最小边求生成树即可。模板用用萌萌哒~ 代码如下: #include <cstdio>#include <cstring>#include <algorit

【UVALive】5713 Qin Shi Huang's National Road System 最小生成树

传送门:【UVALive】5713 Qin Shi Huang's National Road System 题目大意:秦朝有n个城市,需要修建一些道路使得任意两个城市之间都可以连通。道士徐福声称他可以用法术修路,不花钱,也不用劳动力,但只能修一条路,因此需要慎重选择用法术修哪一条路。秦始皇不仅希望其他道路的总长度B尽量短(这样可以节省劳动力),还希望法术连接的两个城市的人口之和A尽量大,因此下