Codeforces 300-A/B/C

A题就是水题,直接按要求分类就行了。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int n,cnt1,cnt2,cnt3,a[110];
    int b1[110],b2[110],b3[110];
    while(scanf("%d",&n)!=EOF)
    {
        cnt1=cnt2=cnt3=0;
        for(int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&x);
            if(x<0&&cnt1==0)
            {
                b1[cnt1++]=x;
            }
            else if(x<0&&cnt1==1)
            {
                b3[cnt3++]=x;
            }
            if(x>0)
            {
                b2[cnt2++]=x;
            }
         }
            printf("1 %d\n",b1[0]);
            if(cnt2==0)
            {
                b2[cnt2++]=b3[--cnt3];
                b2[cnt2++]=b3[--cnt3];
            }
            printf("%d ",cnt2);
            for(int i=0;i<cnt2;i++)
                printf("%d ",b2[i]);
            printf("\n%d 0 ",cnt3+1);
            for(int i=0;i<cnt3;i++)
                printf("%d ",b3[i]);
            printf("\n");
    }
  return 0;
}

B题的话我们先把必须在一个队的人标记成同一个数,然后对那些没有组队要求的人,我们也随意给他们分成几组(标记);然后我们检查一下,如果有哪个队的人数大于三或则是小于3就是不可能的情况,输出-1;其它情况按标记输出组队情况。代码写的有点乱

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int M[50][50],vis[50];
int m,n;

int dfs(int x,int cnt)
{
    vis[x]=cnt;
    for(int i=1;i<=n;i++)
        if(M[x][i])
        {
            M[x][i]=M[i][x]=0;
            dfs(i,cnt);
        }
}

int main()
{

    while(scanf("%d%d",&n,&m)!=EOF)
    {
         memset(vis,0,sizeof(vis));
         memset(M,0,sizeof(M));
         for(int i=1;i<=m;i++)
         {
             int a,b;
             scanf("%d%d",&a,&b);
             if(a==b)
                continue;
             M[a][b]=1; M[b][a]=1;
         }
         int flag=0,cnt=0;
         for(int i=1;i<=n;i++)
         {
             int j;
             for(j=1;j<=n;j++)
             {
                 if(M[i][j])
                    break;
             }
             if(j<=n)
               dfs(i,++cnt);
         }
         for(int i=1;i<=n/3;i++)
         {
             int d=0;
             for(int j=1;j<=n;j++)
                if(vis[j]==i)
                    d++;
             if(d>3)
             {
                flag=1;
                break;
             }
             while(d<3)
             {
                 for(int k=1;k<=n;k++)
                    if(vis[k]==0)
                    {
                       vis[k]=i;
                       break;
                    }
                d++;
             }
         }
         for(int i=1;i<=n/3;i++)
         {
             int d=0;
             for(int j=1;j<=n;j++)
             {
                 if(vis[j]==i)
                        d++;
             }
             if(d<3)
             {
                 flag=12;
                 break;
             }
         }
         if(flag)
         {
             printf("-1\n");
             continue;
         }
         for(int i=1;i<=n/3;i++)
         {
            for(int j=1;j<=n;j++)
            {
                if(vis[j]==i)
                    printf("%d ",j);
            }
            printf("\n");
         }
    }
 return 0;
}

C题的话我们应该枚举(构造)结果,然后检查是否符合要求。然后的话就是一个排列组合的问题了。

注意一个公式:(a/b)%mod==a*pow(b,mod-2),mod是素数。这个公式可以用来快速算组合数。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

#define mod 1000000007
#define ll long long

ll f[1000009];

ll pow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ans%mod;
}

int a,b,n;

bool check(ll x)
{
    while(x)
    {
        int i=x%10;
        if(i!=a&&i!=b)
            return false;
        x/=10;
    }
    return true;
}

int main()
{

    f[0]=1;
    for(int i=1;i<=1000001;i++)
        f[i]=f[i-1]*i%mod;
    while(scanf("%d%d%d",&a,&b,&n)!=EOF)
    {
        ll ans=0;
        for(int i=0;i<=n;i++)
        {
            ll sum=(ll)i*a+(ll)(n-i)*b;
            if(check(sum))
            {
                ll d=f[i]*f[n-i]%mod;
                ans=(ans+f[n]*pow(d,mod-2)%mod)%mod;
            }
        }
        printf("%lld\n",ans);
    }
  return 0;
}
时间: 2024-08-11 00:14:23

Codeforces 300-A/B/C的相关文章

CodeForces #300 B Quasi Binary

大致题意:给定一个整数,比如32,32=11+11+10...给定一个整数n,它可以由0 和1 组成的整数的和组成,求最少需要几个这样0 和 1的整数.比如32至少需要3个. 分析:把输入的这个整数当成字符串处理.比如s=“32”  s[0]=' 3 ' ,s[1]=' 2 '; 那么,我们做一个二维数组.a[][];  用来存储这个字符串s 例如“ 32 ” 存储为 1 1 1 1 1 0 例如3451 存储为: 1 1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1 0

【codeforces #300】EF题解

E. Demiurges Play Again time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the

Codeforces Round #300 (A,B,C,D)

题目传送:Codeforces Round #300 A. Cutting Banner 思路:一看题就会错意了,然后一顿猛敲,果不其然的被hack了,然后才发现只需要剪中间那一段就可以了,然后又傻逼得少写一个等号,还是被hack了,心累啊 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #in

Codeforces Round #300——C贪心——Tourist&#39;s Notes

Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, me

贪心 Codeforces Round #300 A Cutting Banner

题目传送门 1 /* 2 贪心水题:首先,最少的个数为n最大的一位数字mx,因为需要用1累加得到mx, 3 接下来mx次循环,若是0,输出0:若是1,输出1,s[j]--: 4 注意:之前的0的要忽略 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <string> 10 #include <algorithm> 11 #include

水题 Codeforces Round #300 A Cutting Banner

题目传送门 1 /* 2 水题:一开始看错题意,以为是任意切割,DFS来做:结果只是在中间切出一段来 3 判断是否余下的是 "CODEFORCES" :) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 #include <cmath>

Codeforces Round #300

A. Cutting Banner 关键字:[读题][字符串][枚举] 坑点:能不能只减去某一段字符串把剩下的按原来的顺序连起来组成"CODEFORCES",而减下一段"CODEFORCES"是不行的 e.g. "CODEFAAAAORCES"是可行的         而 "AACODEFORCESAA"是不可行的 B. Quasi Binary 题意:给出一个数n,求最少用几个形如"1010"这样只包含'

Codeforces Round #300 B

B. Quasi Binary A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 - are quasibinary and numbers 2, 12, 900 are not. You are given a positive integer n. Represent it as a s

Codeforces Round #300 - Quasi Binary(贪心)

Quasi Binary Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 - ar

Codeforces Round #300 E - Demiurges Play Again

E - Demiurges Play Again 感觉这种类型的dp以前没遇到过... 不是很好想.. dp[u] 表示的是以u为子树进行游戏得到的值是第几大的. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PII pair<int, int> #define y1 skldjfskldjg #define y