您的位置:首页 > 其它

Codeforces Round #306 (Div. 2)

2015-06-07 22:15 375 查看
C:给一个100位的数,问能不能删除一些,使得能被8整除

如果后三位能被8整除,则可以被8整除

#include<bits/stdc++.h>
using namespace std;
char str[110];
int main()
{
    while(scanf("%s",str)!=EOF)
    {
        int len=strlen(str);
        int i,j,k,flag=0;
        for(i=len-1;i>=0;i--)
        {
            if((str[i]-'0')%8==0)
            {
                flag=1;
                break;
            }
            for(j=i-1;j>=0;j--)
            {
                int sum=str[i]-'0'+10*(str[j]-'0');
                if(str[j]!='0'&&sum%8==0)
                {
                    flag=2;
                    break;
                }
                for(k=j-1;k>=0;k--)
                {
                    int sum=str[i]-'0'+10*(str[j]-'0')+100*(str[k]-'0');
                    if(sum%8==0&&k>=0)
                    {
                        flag=3;
                        break;
                    }
                }
                if(flag)break;
            }
            if(flag)break;
        }
        if(!flag)printf("NO\n");
        else
        {
            printf("YES\n");
            if(flag==1)printf("%c\n",str[i]);
            else if(flag==2)printf("%c%c\n",str[j],str[i]);
            else
            {
                for(int p=0;p<=k;p++)printf("%c",str[p]);
                printf("%c%c\n",str[j],str[i]);
            }
        }
    }
    return 0;
}


D:构造,使得构造出来的图,至少有一个桥,每个点的度恰好为k

#include<bits/stdc++.h>
using namespace std;
int K;
int main()
{
    while(scanf("%d",&K)!=EOF)
    {
        if((K&1)==0)
        {
            printf("NO\n");
            continue;
        }
        if(K==1)
        {
            printf("YES\n2 1\n1 2\n");
            continue;
        }
        printf("YES\n");
        int inc = 2;
        int n = 4 * (K - 1) + 2;
        int m = (n * K) / 2;
        printf("%d %d\n", n, m);
        printf("1 2\n");
        for(int i = 1; i <= K - 1; ++i)
            printf("%d %d\n", 1, (i + inc));
        for(int i = 1; i <= K - 1; ++i)
            for(int j = 1; j <= K - 1; ++j)
                printf("%d %d\n", i + inc, j + inc + K - 1);
        for(int i = 1; i <= K - 1; i += 2)
            printf("%d %d\n", i + inc + K - 1, i + inc + K);
        inc += 2 * (K - 1);
        for(int i = 1; i <= K - 1; ++i)
            printf("%d %d\n", 2, (i + inc));
        for(int i = 1; i <= K - 1; ++i)
            for(int j = 1; j <= K - 1; ++j)
                printf("%d %d\n", i + inc, j + inc + K - 1);
        for(int i = 1; i <= K - 1; i += 2)
            printf("%d %d\n", i + inc + K - 1, i + inc + K);
    }
    return 0;
}


E. Brackets in Implications

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false.

Implication is written by using character '

', and the arguments and the result
of the implication are written as '0' (false) and '1' (true). According to the definition of the implication:









When a logical expression contains multiple implications, then when there are no brackets, it will be calculated from left to fight. For example,


.

When there are brackets, we first calculate the expression in brackets. For example,


.

For the given logical expression

determine if it is possible to place there brackets so that the value of a logical
expression is false. If it is possible, your task is to find such an arrangement of brackets.

Input
The first line contains integer n (1 ≤ n ≤ 100 000) — the number of arguments in a logical expression.

The second line contains n numbers
a1, a2, ..., an (

),
which means the values of arguments in the expression in the order they occur.

Output
Print "NO" (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0.

Otherwise, print "YES" in the first line and the logical expression with the required arrangement of brackets in the second line.

The expression should only contain characters '0', '1', '-' (character with ASCII code 45), '>' (character
with ASCII code 62), '(' and ')'. Characters '-' and '>' can occur in an expression only paired like
that: ("->") and represent implication. The total number of logical arguments (i.e. digits '0' and '1') in the expression must be equal to
n. The order in which the digits follow in the expression from left to right must coincide with
a1, a2, ..., an.

The expression should be correct. More formally, a
correct expression is determined as follows:

Expressions "0", "1" (without the quotes) are correct.

If v1,
v2 are correct, then
v1->v2 is a correct expression.

If v is a correct expression, then
(v) is a correct expression.

The total number of characters in the resulting expression mustn't exceed
106.

If there are multiple possible answers, you are allowed to print any of them.

Sample test(s)

Input
4
0 1 1 0


Output
YES
(((0)->1)->(1->0))


Input
2
1 1


Output
NO


Input
1
0


Output
YES
0


E:构造,构造出一个运算顺序,使得运算结果为false

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000010;
int N;
char ans[maxn];
int a[maxn];
int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        for(int i=1;i<=N;i++)scanf("%d",&a[i]);
        if(N==1)
        {
            if(a[1]==0)printf("YES\n0\n");
            else printf("NO\n");
            continue;
        }
        else if(N==2)
        {
            if(a[1]==1&&a[2]==0)printf("YES\n1->0\n");
            else printf("NO\n");
            continue;
        }
        if(a
==1)
        {
            printf("NO\n");
            continue;
        }
        if(a[N-1]==1)
        {
            printf("YES\n");
            for(int i=1;i<=N;i++)
            {
                printf("%d",a[i]);
                if(i==N)printf("\n");
                else printf("->");
            }
        }
        else
        {
            if(a[N-2]==0)
            {
                printf("YES\n");
                for(int i=1;i<=N-3;i++)
                {
                    printf("%d->",a[i]);
                }
                printf("(0->0)->0\n");
                continue;
            }
            int st=0;
            for(int i=N-1;i>1;i--)
            {
                if(a[i]==1&&a[i-1]==0)
                {
                    st=i-1;
                    break;
                }
            }
            if(!st)printf("NO\n");
            else
            {
                printf("YES\n");
                for(int i=1;i<st;i++)
                {
                    printf("%d->",a[i]);
                }
                printf("(0->(");
                for(int i=st+1;i<N;i++)
                {
                    printf("%d",a[i]);
                    if(i!=N-1)printf("->");
                }
                printf("))->0\n");
            }
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: