2016ACM/ICPC亚洲区沈阳站---题解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37868325/article/details/82953330

题目链接 :https://vjudge.net/contest/181866

A - Thickest Burger(经典a+b+max(a,b))

题意:

ACM ICPC is launching a thick burger. The thickness (or the height) of a piece of club steak is A (1 ≤ A ≤ 100). The thickness (or the height) of a piece of chicken steak is B (1 ≤ B ≤ 100). The chef allows to add just three pieces of meat into the burger and he does not allow to add three pieces of same type of meat. As a customer and a foodie, you want to know the maximum total thickness of a burger which you can get from the chef. Here we ignore the thickness of breads, vegetables and other seasonings.

Input

The first line is the number of test cases. For each test case, a line contains two positive integers A and B.

Output

For each test case, output a line containing the maximum total thickness of a burger. Hint Consider the first test case, since 68+68+42 is bigger than 68+42+42 the answer should be 68+68+42 = 178. Similarly since 1+35+35 is bigger than 1+1+35, the answer of the second test case should be 1+35+35 = 71.

代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int T,A,B;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&A,&B);
        int ans=max(A,B);
        ans=ans+ans;
        ans=ans+min(A,B);
        printf("%d\n",ans);
    }
    return 0;
}

B - Relative atomic mass(水题

 题意:

Relative atomic mass is a dimensionless physical quantity, the ratio of the average mass of atoms of an element (from a single given sample or source) to 1 2 of the mass of an atom of carbon-12 (known as the unified atomic mass unit). You need to calculate the relative atomic mass of a molecule, which consists of one or several atoms. In this problem, you only need to process molecules which contain hydrogen atoms, oxygen atoms, and carbon atoms. These three types of atom are written as ‘H’£¬‘O’ and ‘C’ repectively. For your information, the relative atomic mass of one hydrogen atom is 1, and the relative atomic mass of one oxygen atom is 16 and the relative atomic mass of one carbon atom is 12. A molecule is demonstrated as a string, of which each letter is for an atom. For example, a molecule ‘HOH’ contains two hydrogen atoms and one oxygen atom, therefore its relative atomic mass is 18 = 2 * 1 + 16.

Input

The first line of input contains one integer N (N ≤ 10), the number of molecules. In the next N lines, the i-th line contains a string, describing the i-th molecule. The length of each string would not exceed 10.

Output

For each molecule, output its relative atomic mass

代码:

#include<bits/stdc++.h>
using namespace std;

char ch[110];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",&ch);
        int n=strlen(ch);
        int ans=0;
        for(int i=0;i<n;i++)
        {
            if(ch[i]=='H')
                ans+=1;
            else if(ch[i]=='O')
                ans+=16;
            else
                ans+=12;
        }
        printf("%d\n",ans);
    }
    return 0;
}

C - Recursive sequence(矩阵快速幂)

题意:

Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i − 2)-th number, the (i − 1)-th number, and i 4 . Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.

Input

The first line of input contains an integer t, the number of test cases. t test cases follow. Each case contains only one line with three numbers N, a and b where N, a, b < 2 31 as described above.

Output

For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647. Hint In the first case, the third number is 85 = 2 ∗ 1 + 2 + 34 . In the second case, the third number is 93 = 2 ∗ 1 + 1 ∗ 10 + 34 and the fourth number is 369 = 2 ∗ 10 + 93 + 44 .

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=2147493647;
const int M=7;

ll N,a,b;

struct Matrix
{
    Matrix(){memset(a,0,sizeof(a));}
    ll a[M][M];
    void init()
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<M;i++)
            a[i][i]=1;
    }
}A;

Matrix mul(Matrix a,Matrix b)
{
    Matrix ans;
    int i,j,k;
    for(i=0;i<M;i++)
        for(j=0;j<M;j++)
    {
        for(k=0;k<M;k++)
        {
            ans.a[i][j]=(ans.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
        }
        //ans.a[i][j]%=mod;
    }
    return ans;
}

Matrix pow(Matrix a,ll n)
{
    Matrix ans;
    ans.init();
    while(n)
    {
        if(n&1)
            ans=mul(ans,a);
        n>>=1;
        a=mul(a,a);
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld%lld",&N,&a,&b);
        if(N==1)
        {
            printf("%lld\n",a);
        }else if(N==2)
        {
            printf("%lld\n",b);
        }else{
            A.a[0][0]=1; A.a[0][1]=2; A.a[0][2]=1; A.a[0][3]=0; A.a[0][4]=0; A.a[0][5]=0; A.a[0][6]=0;
            A.a[1][0]=1; A.a[1][1]=0; A.a[1][2]=0; A.a[1][3]=0; A.a[1][4]=0; A.a[1][5]=0; A.a[1][6]=0;
            A.a[2][0]=0; A.a[2][1]=0; A.a[2][2]=1; A.a[2][3]=4; A.a[2][4]=6; A.a[2][5]=4; A.a[2][6]=1;
            A.a[3][0]=0; A.a[3][1]=0; A.a[3][2]=0; A.a[3][3]=1; A.a[3][4]=3; A.a[3][5]=3; A.a[3][6]=1;
            A.a[4][0]=0; A.a[4][1]=0; A.a[4][2]=0; A.a[4][3]=0; A.a[4][4]=1; A.a[4][5]=2; A.a[4][6]=1;
            A.a[5][0]=0; A.a[5][1]=0; A.a[5][2]=0; A.a[5][3]=0; A.a[5][4]=0; A.a[5][5]=1; A.a[5][6]=1;
            A.a[6][0]=0; A.a[6][1]=0; A.a[6][2]=0; A.a[6][3]=0; A.a[6][4]=0; A.a[6][5]=0; A.a[6][6]=1;

            A=pow(A,N-2);
            ll ans=A.a[0][0]*b%mod+A.a[0][1]*a%mod+A.a[0][2]*81%mod+A.a[0][3]*27%mod+A.a[0][4]*9%mod+A.a[0][5]*3%mod+A.a[0][6];
            printf("%lld\n",ans%mod);
        }
    }
    return 0;
}

E - Counting Cliques(思维dfs+剪枝)

题意:

A clique is a complete graph, in which there is an edge between every pair of the vertices. Given a graph with N vertices and M edges, your task is to count the number of cliques with a specific size S in the graph.

Input

The first line is the number of test cases. For each test case, the first line contains 3 integers N, M and S (N ≤ 100, M ≤ 1000, 2 ≤ S ≤ 10), each of the following M lines contains 2 integers u and v (1 ≤ u < v ≤ N), which means there is an edge between vertices u and v. It is guaranteed that the maximum degree of the vertices is no larger than 20.

Output

For each test case, output the number of cliques with size S in the graph.

代码:

#include <bits/stdc++.h>
using namespace std;
int look[110],f[110],s,n,m,T,k,num[110],ans,nn,x,y;
struct AA
{
    int x,next;
}pos[2005];
int dfs(int rt,int no)
{
    if(no==s) {ans++;return 0;}
    int v;
    for(int i=f[rt];i!=-1;i=pos[i].next)
    {
        look[pos[i].x]++;
    }
    for(int i=f[rt];i!=-1;i=pos[i].next)
    {
        v=pos[i].x;
        //cout<<rt<<" "<<v<<" "<<no<<" "<<s<<" "<<ans<<endl;
        if(look[v]==no&&no+num[v]+1>=s)
        {
            if(no==s-1) {
                    ans++;continue;}
            dfs(v,no+1);
        }

    }
    for(int i=f[rt];i!=-1;i=pos[i].next)
    {
        look[pos[i].x]--;
    }
}
int main()
{
    scanf("%d",&T);
   while(T--)
   {
       k=0;ans=0;
       scanf("%d%d%d",&n,&m,&s);
       memset(num,0,sizeof(num));
       memset(f,-1,sizeof(f));
       while(m--)
       {
           scanf("%d%d",&x,&y);
           if(x<y)
           {
               pos[++k].x=y;
               pos[k].next=f[x];
               f[x]=k;
               num[x]++;
           }
           else
           {
               pos[++k].x=x;
               pos[k].next=f[y];
               f[y]=k;
               num[y]++;
           }
       }
       nn=n-s+1;
       for(int i=1;i<=nn;i++)
        {
            if(num[i]+1>=s)
            {
                memset(look,0,sizeof(look));
                look[i]=0;
                dfs(i,1);
            }
        }
        printf("%d\n",ans);
   }
}

猜你喜欢

转载自blog.csdn.net/qq_37868325/article/details/82953330
今日推荐