HDU - 3556 - Continued Fraction

先上题目:

Continued Fraction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 332    Accepted Submission(s): 106

Problem Description

Dumbear loves numbers very much.
One day Dumbear found that each number can be expressed as a continued fraction. See below.

Formally, we say a number k can be expressed as a continued faction if

where a0, a1, …, an are positive integers except that a0 maybe be 0 and an cannot be 1.
Dumbear also found a sequence which looks like the Farey sequence. Initially the sequenceand if we insert an elementbetween all the two adjacent element,in Di, then we get a sequence Di+1. So you can seeandAssume initially you are on the elementin D0, and if now you are on the element k in Di, then if you go left(‘L’)(or right(‘R’)) you will be on the left(or right) element of k in Di+1. So a sequence composed of ‘L’ and ‘R’ denotes a number. Such as ‘RL’ denotes the number 

Now give you a sequence composed of ‘L’ and ‘R’, you should print the continued fraction form of the number. You should use ‘-‘ to show the vinculum(the horizontal line), you should print one space both in front and back of ‘+’, and all parts up or down the vinculum should be right aligned. You should not print unnecessary space, ‘-‘ or other character. See details in sample.

Input

There are several test cases in the input.
For each test case, there is a single line contains only a sequence composed of ‘L’ and ‘R’. The length of the sequence will not exceed 10000.
The input terminates by end of file marker.

Output

For each test case, output the continued fraction form of the number which the input sequence denotes. The total amount of output will not exceed 4MB.

Sample Input

LR

RL

Sample Output

1

0 + -----

1

1 + -

2

1

1 + -

2

    题意:给出一个只有L和R的字符串,根据它给出的定义移动,然后将最终的结果按照他给出的那种分式输出。

    模拟+找规律。首先先根据它的定义模拟它的移动,然后可以根据自己的分析求出a[]数组的每一项是多少,然后在按照它的格式输出。这里求a[]数组分析一下就可以知道怎么怎么求了,然后关于输出,通过前后项的长度关系就可以得出要输出多少个空格多少‘-‘了,但是这样做的话直接提交可能会wa,这里可以通过输出前几项找一下规律。将L,LL,LR,LLL,LLR,LRL,LRR以及R,RL,RR,RLL,RLR,RRL,RRR(也就是前几项的变化的输有情况)输出,然后通过观察,我们可以发现,对于第2项开始,如果当前项等于前一项,那么a[tot]++,否则a[tot]--;tot++;a[tot]=2;当前其实如果写得好的话可以顺便把前一项的情况也包含在公式里面。然后直接构造a[],在打印,速度和准确性上面都会有保证。

上代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <algorithm>
 5 #define MAX 10002
 6 #define PUTS(M,x) for(int k=0;k<M;k++) putchar(x)
 7 #define ll long long
 8 using namespace std;
 9
10 char c[MAX];
11 int l;
12 ll a[MAX];
13 char ss[MAX<<4][20];
14 int len[MAX];
15 int tot;
16 typedef struct{
17     ll fz,fm;
18 }fs;
19
20 fs A[3],p;
21
22 void cons(int l){
23     if(c[0]==‘L‘){
24         a[0]=0; a[1]=2; tot=1;
25     }else{
26         a[0]=2; tot=0;
27     }
28     for(int i=1;i<l;i++){
29         if(c[i]==c[i-1]) a[tot]++;
30         else{
31             a[tot]--;
32             a[++tot]=2;
33         }
34     }
35     tot++;
36 }
37
38 int main()
39 {
40     int M;
41     //freopen("data.txt","r",stdin);
42     while(scanf("%s",c)!=EOF){
43         l=strlen(c);
44 //        A[0].fz=0;  A[0].fm=1;
45 //        A[1].fz=1;  A[1].fm=1;
46 //        A[2].fz=1;  A[2].fm=0;
47 //        for(int i=0;i<l;i++){
48 //            if(c[i]==‘L‘){
49 //                A[2]=A[1];
50 //            }else{
51 //                A[0]=A[1];
52 //            }
53 //            A[1].fz=A[0].fz+A[2].fz;
54 //            A[1].fm=A[0].fm+A[2].fm;
55 //        }
56 //        tot=0;
57 //        p=A[1];
58 //        while(1){
59 //            a[tot]=p.fz/p.fm;
60 //            sprintf(ss[tot],"%I64d",a[tot]);
61 //            tot++;
62 //            p.fz=p.fz%p.fm;
63 //            if(p.fz==0) break;
64 //            else if(p.fz==1){
65 //                a[tot]=p.fm;
66 //                sprintf(ss[tot],"%I64d",a[tot]);
67 //                tot++;
68 //                break;
69 //            }
70 //            swap(p.fz,p.fm);
71 //        }
72         cons(l);
73         for(int i=0;i<tot;i++) sprintf(ss[i],"%I64d",a[i]);
74         len[tot-1]=strlen(ss[tot-1]);
75         len[tot-2]=strlen(ss[tot-2]) + 3 + strlen(ss[tot-1]);
76         for(int i=tot-3;i>=0;i--){
77             len[i]=strlen(ss[i]) + 3 + len[i+1];
78         }
79
80 //        for(int i=0;i<tot;i++) printf("%I64d ",a[i]);
81 //        printf("\n");
82 //        for(int i=0;i<tot;i++) printf("%d ",len[i]);
83 //        printf("\n");
84         M=len[0];
85         for(int i=0;i<tot-1;i++){
86             PUTS(M-1,‘ ‘);  putchar(‘1‘);   putchar(‘\n‘);
87             PUTS(M-len[i],‘ ‘);
88             printf("%s + ",ss[i]);
89             PUTS(len[i+1],‘-‘);
90             putchar(‘\n‘);
91         }
92         PUTS(M-(int)strlen(ss[tot-1]),‘ ‘);
93         printf("%s",ss[tot-1]);
94         printf("\n");
95     }
96     return 0;
97 }

/*3556*/

HDU - 3556 - Continued Fraction

时间: 2024-10-19 16:08:42

HDU - 3556 - Continued Fraction的相关文章

CSUOJ 1638 Continued Fraction

1638: Continued Fraction Time Limit: 1 Sec  Memory Limit: 128 MB Description Input Output Sample Input 4 3 5 1 1 2 5 2 2 Sample Output 11 0 5 30 4 6 1 27 HINT Source 解题:主要任务是把任一一个分数化成连分式. 方法就是分子分母同时不断的除以分子,直到分子为0. 至于加,减,乘,除都是先算出分数,然后把分数化成连分数.. 1 #inc

HDU 6223 Infinite Fraction Path(BFS+剪枝)

The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solicited an audience with the king. He recounted how he had built a happy path in the kingdom of happiness. The king affirmed Welly's talent and hoped

hdu 6223 Infinite Fraction Path

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大.规则:从数字串S的某一个下标为x的数字出发,可以到达的下一个数字是下标为(x*x+1)%n的数字. 思路:BFS+剪枝.剪枝技巧: 1:遍历到某一层的节点时,记录已经到达过的节点,下次如果还经过就直接不考虑. 2:当前遍历到的某一层节点的数字较之前的小,直接不考虑. AC代码: #define _

Continued Fractions CodeForces - 305B (java+高精 / 数学)

A continued fraction of height n is a fraction of form . You are given two rational numbers, one is represented as  and the other one is represented as a finite fraction of height n. Check if they are equal. Input The first line contains two space-se

欧拉工程第65题:Convergents of e

题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the important mathematical constant,e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...]. The first ten terms in the sequence of convergents for e are: 2, 3,

欧拉计划(python) problem 65

Convergents of e Problem 65 The square root of 2 can be written as an infinite continued fraction. √2 = 1 + 1   2 + 1     2 + 1       2 + 1         2 + ... The infinite continued fraction can be written, √2 = [1;(2)], (2) indicates that 2 repeats ad

欧拉计划(python) problem 64

Odd period square roots Problem 64 All square roots are periodic when written as continued fractions and can be written in the form: √N = a0 + 1   a1 + 1     a2 + 1       a3 + ... For example, let us consider √23: √23 = 4 + √23 - 4 = 4 +  1  = 4 +  1

C++开源库集合

| Main | Site Index | Download | mimetic A free/GPL C++ MIME Library mimetic is a free/GPL Email library (MIME) written in C++ designed to be easy to use and integrate but yet fast and efficient. It is based on the C++ standard library and heavily us

Infinite Expressions for Pi

John Wallis (1655) took what can now be expressed as and without using the binomial theorem or integration (not invented yet) painstakingly came up with a formula for  to be  . William Brouncker (ca. 1660's) rewrote Wallis' formula as a continued fra