SDUACM16级寒假热身1

来源:互联网 发布:苹果双系统切换回mac 编辑:程序博客网 时间:2024/04/28 08:00

Pro A - Bachgold Problem

[题目描述]

给定一整数n(<100 000),尽可能多的将其分成若干素数之和

[分析]显然,若n为偶数,将其分成(n/2)个2;若其为奇数,分成(n/2)个2,一个3即可

#include<cstdio>using namespace std;int main(){    int n;    scanf("%d",&n);    if(n%2==1)    {      printf("%d\n",n/2);      n-=3;      for(int i=1;i<=n/2;i++)            printf("2 ");      printf("3");    }else    {        printf("%d\n",n/2);        for(int i=1;i<n/2;i++)            printf("2 ");       printf("2");    }    return 0;}

Pro B - Parallelogram is Back

[问题描述]

给定三个点,找出第四个点,使这四个点构成平行四边形

[分析]

把这三个点两两当做对角线,求出中点,即可的到第四个点

#include<cstdio>using namespace std;int x[4],y[4];int main(){    for(int i=1;i<=3;i++)         scanf("%d%d",&x[i],&y[i]);    printf("3\n");    int xx,yy;    xx=(x[1]+x[2])-x[3];    yy=(y[1]+y[2])-y[3];    printf("%d %d\n",xx,yy);    xx=(x[1]+x[3])-x[2];    yy=(y[1]+y[3])-y[2];    printf("%d %d\n",xx,yy);    xx=(x[3]+x[2])-x[1];    yy=(y[3]+y[2])-y[1];    printf("%d %d\n",xx,yy);    return 0;}

Pro C - Lesha and array splitting

[问题描述]

给定一序列(长度<100),将其分为若干子序列,要各求子序列的和均不为0,且各子序列可以组成此序列

[分析]

原谅我写此博客时突然怀疑自己当时提交的假代码,过了假数据(逃..)

子序列和不为0,如果让每个数都是一个序列的话,只有0这种序列是不满足条件的,直接就近依附一个不为

0的数即可(我选择依附左边的第一个非0数)

复杂度O(n)

#include<cstdio>using namespace std;const int maxn=110;int a[maxn],f[maxn];int main(){    int n;    scanf("%d",&n);    for(int i=1;i<=n;i++)    {        scanf("%d",&a[i]);        if(a[i]!=0)        {            f[0]++;            f[f[0]]=i;        }    }    if(f[0]==0)        printf("NO"); else    {        printf("YES\n%d\n",f[0]);        f[1]=1;        f[f[0]+1]=n+1;        for(int i=1;i<=f[0];i++)            printf("%d %d\n",f[i],f[i+1]-1);    }    return 0;}

Pro D - Ilya and tic-tac-toe game

[问题描述]

给定4*4棋盘的一种状态,判断再下一子能否获得胜利(三子连线即为胜利)

[分析]

裸 裸 的 模拟,还是特地打了一个函数判断(x,y)是否可行,另外对于(x,y)此点是否构成三子连线有12

种情况(中间节点4+边缘节点8)

#include<cstdio>using namespace std;char s[4][4];bool cal(int x,int y){    if((x<0)||(y<0)||(x>3)||(y>3))        return (false);    if(s[x][y]!='x')        return (false);    return true;}bool ok(int x,int y){    if((cal(x,y-1))&&(cal(x,y+1)))        return(true);    if(cal(x-1,y)&&cal(x+1,y))        return true;    if(cal(x-1,y+1)&&cal(x+1,y-1))        return true;    if(cal(x-1,y-1)&&cal(x+1,y+1))        return true;    if((cal(x,y+1))&&(cal(x,y+2)))        return(true);    if(cal(x,y-1)&&cal(x,y-2))        return true;    if(cal(x-1,y)&&cal(x-2,y))        return true;    if(cal(x+2,y)&&cal(x+1,y))        return true;    if((cal(x+1,y+1))&&(cal(x+2,y+2)))        return(true);    if(cal(x-1,y-1)&&cal(x-2,y-2))        return true;    if(cal(x-1,y+1)&&cal(x-2,y+2))        return true;    if(cal(x+2,y-2)&&cal(x+1,y-1))        return true;    return false;}int main(){    for(int i=0;i<4;i++)        scanf("%s",s[i]);    for(int i=0;i<4;i++)        for(int j=0;j<4;j++)          if(s[i][j]=='.')    {        if(ok(i,j))        {           // printf("%d  %d \n",i,j);            printf("YES");            return 0;        }    }    printf("NO");    return 0;}

Pro E - New Year and Hurry

[直接上代码]

#include<cstdio>using namespace std;int main(){    int n,k;    scanf("%d%d",&n,&k);    k=240-k;    int i=1;    for(i=1;i<=n+1;i++)    {       if(i==n+1)  break;       if(i*(i+1)*5/2>k)            break;    }    printf("%d",i-1);    return 0;}

Pro F - New Year and North Pole

[问题描述]

假设地球是标准的球体,llya现在站在北极,他可以选择东南西北四个方向行走,给定行走方式,判断行走

方式是否合法,以及最后是否回归起点

[分析]

讨厌人的模拟….符合以下几种条件之一即为不合法

1.在北(南)极向北(南)走,或者向南(北)走的距离过大

2.在极点向东(西)走

#include<cstdio>char s[8];using namespace std;int main(){    int n;    scanf("%d",&n);    int sum=0;    for(int i=1;i<=n;i++)    {        int t;        scanf("%d",&t);        scanf("%s",s);        if(s[0]=='N')            sum-=t; else        if(s[0]=='S')            sum+=t; else        if((sum==0)||(sum==20000))        {            printf("NO");            return 0;        }        if((sum<0)||(sum>20000))        {            printf("NO");            return 0;        }    }    if (sum==0)    printf("YES"); else      printf("NO");    return 0;}

Pro G - Santa Claus and a Place in a Class

[问题描述]

n*m张桌子,每张桌子坐两人,依次编号,问编号为k的人在哪张桌?

[分析]

这种编号所属组,如果k%(2*m)=0 则位于第(k/(2 *m))组,否则+1

#include<cstdio>char s[8];using namespace std;int main(){    int n,m,k;    scanf("%d%d%d",&n,&m,&k);    if(k%(2*m)==0)        printf("%d %d %c",k/(2*m),m,'R'); else        {            int t=k%(2*m);            if (t%2==1)                printf("%d %d L",k/(2*m)+1,t/2+1);             else                printf("%d %d R",k/(2*m)+1,t/2);        }    return 0;}

Pro H - Santa Claus and Keyboard Check

[问题描述]

给定两个长度相同的全由小写字母组成的字符串,是否可以通过字母变化相互转换

#include<cstdio>#include<cstring>using namespace std;const int maxn=1200;char s1[maxn],s2[maxn];char tot[29][2];int f[maxn];int top=0;int main(){    scanf("%s",s1);    scanf("%s",s2);    for(int i=0;i<strlen(s2);i++)    {        char m=s1[i],n=s2[i];        if(((f[m]!=0)&&(f[m]!=n))||((f[n]!=0)&&(f[n]!=m)))        {            printf("-1");            return 0;        }        if((f[m]==n)||(f[n]==m))            continue;        f[m]=n;        f[n]=m;        if(n==m)continue;        top++;        tot[top][0]=m;        tot[top][1]=n;    }    printf("%d\n",top);    for(int i=1;i<=top;i++)        printf("%c %c\n",tot[i][0],tot[i][1]);    return 0;}

Pro I - Felicity is Coming!

[题目描述]

It’s that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has n gyms. The i-th gym has gi Pokemon in it. There are m distinct Pokemon types in the Himalayan region numbered from 1 to m. There is a special evolution camp set up in the fest which claims to evolve any Pokemon. The type of a Pokemon could change after evolving, subject to the constraint that if two Pokemon have the same type before evolving, they will have the same type after evolving. Also, if two Pokemon have different types before evolving, they will have different types after evolving. It is also possible that a Pokemon has the same type before and after evolving.

Formally, an evolution plan is a permutation f of {1, 2, …, m}, such that f(x) = y means that a Pokemon of type x evolves into a Pokemon of type y.

The gym leaders are intrigued by the special evolution camp and all of them plan to evolve their Pokemons. The protocol of the mountain states that in each gym, for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. They now want to find out how many distinct evolution plans exist which satisfy the protocol.

Two evolution plans f1 and f2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon type in the two plans, i. e. there exists an i such that f1(i) ≠ f2(i).

Your task is to find how many distinct evolution plans are possible such that if all Pokemon in all the gyms are evolved, the number of Pokemon of each type in each of the gyms remains the same. As the answer can be large, output it modulo 109 + 7.

[分析]

本来特别头疼的一道题,没想到直接用vector就可以(汗颜…)vector支持直接判断相等,支持sort

神奇的vector….不知道为啥,突然想到了P党该怎么办(老P党路过…)

#include<cstdio>#include<vector>#include<algorithm>#include<iostream>using namespace std;const int maxn=1000020;vector<int > a[maxn];const int P=1e9+7;int main(){    int n,m;    scanf("%d%d",&n,&m);    for(int i=1;i<=m;i++)        a[i].clear();    for(int i=1;i<=n;i++)    {        int num;        scanf("%d",&num);        while(num--)        {            int k;            scanf("%d",&k);            a[k].push_back(i);        }    }    sort(a+1,a+m+1);    long long ans=1,t=1;    for(int i=2;i<=m;i++)        if(a[i]==a[i-1])    {        t++;        ans=(ans*t)%P;    } else  t=1;    cout<<ans;    return 0;}
1 0
原创粉丝点击