Codeforces 300-A/B/C

Source: Internet
Author: User

Codeforces 300-A/B/C

Question A is A water question. Just classify it as required.

     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
      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

In question B, we should first mark the number of people in a team as the same number. Then, we will randomly divide the team into several groups (TAGS) for those who do not have Team requirements ); then, let's check whether the number of teams is greater than three or less than three is impossible. Output-1. Other situations are marked as output teams. Code writing is messy.

The Code is as follows:

#include  #include  #include  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;}   

In question C, we should enumerate (construct) the result and check whether it meets the requirements. Then there is a problem of arrangement and combination.
Note a formula: (a/B) % mod = a * pow (B, mod-2). mod is a prime number. This formula can be used to quickly calculate the number of combinations.

The Code is as follows:

#include  #include  #include  using namespace std;#define mod 1000000007#define ll long longll 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;}   

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.