CSU 1034: The Composition of Letters 1054: GCDdepth 1068: Count the Number of Cycles 1074: HaHa's Mo

1034: The Composition of Letters

代码:

#include <stdio.h>  
#include <string.h>  
char a[100],c[100],b[100];  
int ans,n,temp;  
  
  
void dfs(int sum,int l,int j)  
{  
    if(sum==ans)  
    {  
        b[j]=0;  
        puts(b);  
        temp=0;  
        return ;  
    }  
    for(int i=l; i<=26; i++)  
        if(a[i])  
        {  
            if(sum+i<=ans)  
            {  
                a[i]--;  
                b[j]=i+64;  
                dfs(sum+i,i,j+1);  
                a[i]++;  
            }  
        }  
}  
  
  
int main()  
{  
    int cas;  
    char cc;  
    scanf("%d",&cas);  
    getchar();  
  
    for(int t=1; t<=cas; t++)  
    {  
        scanf("%d",&n);  
        memset(a,0,sizeof a);  
  
        for(int i=0; i<n; i++)  
        {  
            getchar();  
            scanf("%c",&cc);  
            a[cc-64]++;  
        }  
  
        scanf("%s",c);  
        ans=0;  
        for(int i=0; c[i]!=0; i++)  
            ans+=c[i]-'A'+1;  
  
        printf("Case %d\n",t);  
  
        temp=1;  
        dfs(0,0,0);  
        if(temp)  
            printf("NONE\n");  
    }  
    return 0;  
}  


1054: GCD depth


代码:

#include <algorithm>  
#include <iostream>  
#include <cstdlib>  
#include <cstring>  
#include <iomanip>  
#include <string>  
#include <vector>  
#include <cstdio>  
#include <stack>  
#include <queue>  
#include <cmath>  
#include <list>  
#include <map>  
#include <set>  
  
#define ULL unsigned long long  
#define PI 3.1415926535  
#define INF 0x3f3f3f3f  
#define LL long long  
#define eps 1e-8  
  
using namespace std;  
int num1[200010],num2[200010];  
int gcdDepth(int x,int y){  
    if(y==0)  
        return 0;  
    return gcdDepth(y,x%y)+1;  
}  
int main()  
{  
    //freopen("in.txt", "r", stdin);  
    //freopen("out.txt", "w", stdout);  
    int x,d,y0,y1;    
    while(scanf("%d%d%d%d",&x,&d,&y0,&y1)!=EOF){  
        int ans=0;  
        if(x==0){  
            if(d==0){  
                if(y0==0)  
                    ans=1;  
            }  
            else if(d==1){  
                if(y0==0)  
                    ans=y1;  
                else  
                    ans=y1-y0+1;  
            }  
        }  
        else{  
            int flag=0,tmp;  
            if(d==0){  
                if(y0==0)  
                    ans=1;  
                flag=1;  
            }  
            for(int i=0;i<x&&!flag;++i){  
                tmp=gcdDepth(x,i);  
                if(tmp==d)  
                    num1[i+1]=num1[i]+1;  
                else  
                    num1[i+1]=num1[i];  
                if(tmp+2==d)  
                    num2[i+1]=num2[i]+1;  
                else  
                    num2[i+1]=num2[i];  
            }  
            tmp=y0;  
            if(!flag&&tmp<x){  
                if(y1<x){  
                    ans+=num1[y1+1]-num1[tmp];  
                    flag=1;  
                }  
                else{  
                    ans+=num1[x]-num1[tmp];  
                    tmp=x;  
                }  
            }  
            if(!flag&&tmp%x){  
                if(y1<(tmp/x+1)*x){  
                    ans+=num2[y1%x+1]-num2[tmp%x];  
                    flag=1;  
                }  
                else{  
                    ans+=num2[x]-num2[tmp%x];  
                    tmp+=x-tmp%x;  
                }  
            }  
            //printf("%d\n",tmp);  
            if(!flag)  
                ans+=(y1/x-tmp/x)*num2[x];  
            //printf("%d %d\n",tmp,(y1/x-tmp/x)*num2[x]);  
            if(!flag)  
                ans+=num2[y1%x+1];  
            //printf("%d\n",ans);  
            if(y0<=x&&y1>=x){   
                if(d==1)  
                    ++ans;  
                else if(d==2)  
                    --ans;  
            }  
        }  
        printf("%d\n",ans);  
    }  
    return 0;  
}  

1068: Count the Number of Cycles


#include<iostream>
#include<stdio.h>
using namespace std;
const int maxn=100+5;
int a[maxn][maxn];
int main()
{
 int m,n,i,j,k,s;
 while(scanf("%d%d",&m,&n)!=EOF)
 {
  int cot=0;
  for(i=0;i<m;i++)
   for(j=0;j<n;j++)
   {
    scanf("%d",&a[i][j]);
   }
  s=m-1;
  for(i=0;i<s;i++)
  {
   int sum=0;
   for(j=i+1;j<m;j++)
   {
    for(k=0;k<n;k++)
    {sum+=a[i][k]*a[j][k];}
    cot+=sum*(sum-1)/2;
    sum=0;
   }
  }
  printf("%d\n",cot);
 }
 return 0;
}

1074: HaHa's Morning


#include<stdio.h>  
#include<string.h>  
using namespace std;  
#define ll long long int  
int n,m;  
ll dp[(1<<19)];  
int son[19];  
int main()  
{  
    while(~scanf("%d%d",&n,&m))  
    {  
        memset(son,0,sizeof(son));  
        memset(dp,0,sizeof(dp));  
        for(int i=0;i<m;i++)  
        {  
            int x,y;  
            scanf("%d%d",&x,&y);  
            x--;y--;  
            son[x]|=(1<<y);  
        }  
        int end=(1<<n);  
        dp[0]=1;  
        for(int i=0;i<end;i++)  
        {  
            if(dp[i]>0)  
            {  
                for(int j=0;j<n;j++)  
                {  
                    if((i&(son[j]))==son[j])  
                    {  
                        if((i&(1<<j))==0)  
                        {  
                            dp[(i|(1<<j))]+=dp[i];  
                        }  
                    }  
                }  
            }  
        }  
        printf("%lld\n",dp[end-1]);  
    }  
}  
/**********************************************************************
	Problem: 1074
	User: 3901140225
	Language: C++
	Result: AC
	Time:96 ms
	Memory:5216 kb
**********************************************************************/


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nameofcsdn/article/details/79964100
个人分类: CSU 代码搜集
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭