cf 87

A - Trains

Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.

When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).

We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.

Help Vasya count to which girlfriend he will go more often.

Input

The first line contains two integers a and b (a ≠ b, 1 ≤ a, b ≤ 106).

Output

Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.

Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal


题目大概:

读了之后,感觉就是给出两个数a ,b。 时间从0到 很大。是a的倍数,去dasha,是b的倍数,去masha,如果是a和b的公倍数,那么就是去 以前取得次数少的。最后输出去的多的地方。

思路:

读完之后就知道是要求最小公倍数,然后看完样例,就更明确了。

最小公倍数/a -1就是,除了特判的一个点外,去dasha的次数。

最小公倍数/b -1就是,除了特盘的点,去masha的次数。

最后比较一下。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
long long gcd(long long a,long long b)
{
    long long r;
    while(b>0)
    {
        r=a%b;
        a=b;
        b=r;
    }
    return a;
}
int main()
{
    long long n,m;
    scanf("%I64d%I64d",&n,&m);
    long long gc=gcd(n,m);
    long long gr=n/gc*m;
    long long sum1=gr/n-1;
    long long sum2=gr/m-1;
    if(sum1>sum2)sum2++;
    else if(sum1<sum2)sum1++;
    if(sum1>sum2)
    {
        printf("Dasha\n");
    }
    else if(sum2>sum1)
    {
        printf("Masha\n");
    }
    else printf("Equal\n");
    return 0;
}


B - Vasya and Types


Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's description below carefully and follow it and not the similar rules in real programming languages.

There is a very powerful system of pointers on &K* — you can add an asterisk to the right of the existing type X — that will result in new type X * . That is called pointer-definition operation. Also, there is the operation that does the opposite — to any type of X, which is a pointer, you can add an ampersand — that will result in a type &X, to which refers X. That is called a dereference operation.

The &K* language has only two basic data types — void and errtype. Also, the language has operators typedef and typeof.

  • The operator "typedef A B" defines a new data type B, which is equivalent to AA can have asterisks and ampersands, and B cannot have them. For example, the operator typedef void** ptptvoid will create a new type ptptvoid, that can be used as void**.
  • The operator "typeof A" returns type of A, brought to void, that is, returns the type void**...*, equivalent to it with the necessary number of asterisks (the number can possibly be zero). That is, having defined the ptptvoid type, as shown above, the typeof ptptvoid operator will return void**.

An attempt of dereferencing of the void type will lead to an error: to a special data type errtype. For errtype the following equation holds true: errtype* = &errtype = errtype. An attempt to use the data type that hasn't been defined before that will also lead to the errtype.

Using typedef, we can define one type several times. Of all the definitions only the last one is valid. However, all the types that have been defined earlier using this type do not change.

Let us also note that the dereference operation has the lower priority that the pointer operation, in other words &T *  is always equal to T.

Note, that the operators are executed consecutively one by one. If we have two operators "typedef &void a" and "typedef a* b", then at first a becomes errtype, and after that b becomes errtype* = errtype, but not &void* = void (see sample 2).

Vasya does not yet fully understand this powerful technology, that's why he asked you to help him. Write a program that analyzes these operators.

Input

The first line contains an integer n (1 ≤ n ≤ 100) — the number of operators. Then follow n lines with operators. Each operator is of one of two types: either "typedef A B", or "typeof A". In the first case the B type differs from void and errtypetypes, and besides, doesn't have any asterisks and ampersands.

All the data type names are non-empty lines of no more than 20 lowercase Latin letters. The number of asterisks and ampersands separately in one type in any operator does not exceed 10, however if we bring some types to void with several asterisks, their number may exceed 10.

Output

For every typeof operator print on the single line the answer to that operator — the type that the given operator returned.

Examples
Input
5
typedef void* ptv
typeof ptv
typedef &&ptv node
typeof node
typeof &ptv
Output
void*
errtype
void
Input
17
typedef void* b
typedef b* c
typeof b
typeof c
typedef &b b
typeof b
typeof c
typedef &&b* c
typeof c
typedef &b* c
typeof c
typedef &void b
typeof b
typedef b******* c
typeof c
typedef &&b* c
typeof c
Output
void*
void**
void
void**
errtype
void
errtype
errtype
errtype


题目大概:

模拟一个语言数据的定义和输出返回值。题目很长。

思路:

一般这种题,需要在纸上把所有考虑的情况都写出来。

输出的数据分为

1.定义数据。

定义的数据是基本数据类型怎么样操作

不是基本数据类型,如果已经定义过怎么样,没有定义怎么样。

2.输出数据类型。

输出是基本数据类型怎么样

不是基本数据类型,如果已经定义过怎么样,没有的话又怎么样。

然后细节的问题慢慢写就行了,对照样例调试。

代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
map<string,string>G;
map<string,int>F;
int main()
{
    int n;
    scanf("%d",&n);
    string sf,s1,s2;
    while(n--)
    {
        cin>>sf;
        if(sf[4]=='d')
        {
            cin>>s1>>s2;
            int l=s1.size();
            int f0=0,f1=0;
            int st=0,en=0;
            for(int i=0;i<l;i++)
            {
                if(s1[i]=='&')f0++;
                if(s1[i-1]=='&'||i==0)
                {
                    if(s1[i]!='&')
                    {
                        st=i;
                    }
                }
                if(s1[i]=='*')f1++;
                if(s1[i-1]!='*'&&s1[i]=='*')
                {
                    en=i-1;
                }
            }
            if(f0==0)st=0;
            if(f1==0)en=l-1;
            string s;
            for(int i=st;i<=en;i++)
            {
                s+=s1[i];
            }
            //cout<<f0<<" "<<f1<<" "<<s<<endl;
            if(s=="void")
            {
                G[s2]=s;
                if(f1-f0>=0)
                {
                    G[s2]="void";
                    F[s2]=f1-f0;
                }
                else
                {
                    G[s2]="errtype";
                    F[s2]=-30;
                }
            }
            else
            {
                string ss=G[s];
                if(ss=="void")
                {
                    //cout<<111<<" "<<F[s]<<" "<<f0<<" "<<f1<<endl;
                    if(F[s]+f1-f0>=0)
                    {
                        G[s2]="void";
                        F[s2]=f1-f0+F[s];

                    }
                    else
                    {
                        G[s2]="errtype";
                        //cout<<1111111111111<<endl;
                        F[s2]=-30;
                    }
                }
                else if(ss=="errtype")
                {
                    G[s2]="errtype";
                    F[s2]=-30;

                }
                else
                {
                    G[s2]="errtype";
                    F[s2]=-30;
                    G[s]="errtype";
                    F[s]=-30;
                }
            }
            //cout<<G[s2]<<" "<<F[s2]<<endl;
        }
        else
        {
            cin>>s1;
            int l=s1.size();
            int f0=0,f1=0;
            int st=0,en=0;
            for(int i=0;i<l;i++)
            {
                if(s1[i]=='&')f0++;
                if(s1[i-1]=='&'||i==0)
                {
                    if(s1[i]!='&')
                    {
                        st=i;
                    }
                }
                if(s1[i]=='*')f1++;
                if(s1[i-1]!='*'&&s1[i]=='*')
                {
                    en=i-1;
                }
            }
            if(f0==0)st=0;
            if(f1==0)en=l-1;
            string s;
            for(int i=st;i<=en;i++)
            {
                s+=s1[i];
            }
            //cout<<f0<<" "<<f1<<" "<<s<<endl;
            if(s=="void")
            {
                printf("void\n");
            }
            else
            {
            if(G[s]=="errtype")
            {
                printf("errtype\n");
            }
            else if(G[s]=="void")
            {
                if(F[s]+f1-f0>=0)
                {
                    int sum=F[s]+f1-f0;
                    printf("void");
                    for(int i=0;i<sum;i++)
                    {
                        printf("*");
                    }
                    printf("\n");
                }
                else
                {
                    printf("errtype\n");
                }
            }
            else
            {
                printf("errtype\n");
            }
            }

        }
    }
    return 0;
}


c  博弈,没学习,看懂就补。


d 图论的  明天抽空补。


e 计算几何,先放着吧。




猜你喜欢

转载自blog.csdn.net/a1046765624/article/details/80261564
今日推荐