[usaco] Chapter2-Bigger Challenges(Section 2.2)

 

/*
ID: bbezxcy1
PROG: subset
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdio>
using namespace std;
ifstream fin("subset.in");
ofstream fout("subset.out");
long long dp[50][1300];
int main()
{
    int n,i,j,a,b,c;
    while(fin>>n)
    {
        a=n*(n+1)/2;
        if(a%2==1)
        {
            fout<<0<<endl;
            continue;
        }
        memset(dp,0,sizeof(dp));
        dp[1][0]=1;
        dp[1][1]=1;
      //  cout<<a<<endl;
        for(i=2;i<=n;i++)
        {
            for(j=0;j<=a;j++)
            {
                if(j>=i)
                {
                    dp[i][j]=dp[i-1][j]+dp[i-1][j-i];
                }
                else
                {
                    dp[i][j]=dp[i-1][j];
                }
            }
        }
        fout<<dp[n][a/2]/2<<endl;
    }
    return 0;
}
/*
ID: bbezxcy1
PROG: runround
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdio>
using namespace std;
ifstream fin("runround.in");
ofstream fout("runround.out");
char num[20],sum[100];
bool vis[10];
bool check(unsigned long long n){
	unsigned long long a=n;
	int i,j;
	memset(vis,0,sizeof(vis));
	while(a){
		if(vis[a%10]==1||a%10==0){
			return 0;
		}
		vis[a%10]=1;
		a/=10;
	}
	for(i=0;i<10*strlen(num);i++){
	    sum[i]=num[i%strlen(num)];
	}
	int l=10*strlen(num);
	sum[l]='\0';
	memset(vis,0,sizeof(vis));
	i=0;
	j=0;
	while(i<l){
		i+=sum[i]-'0';
		j++;
		if(sum[i]==sum[0]){
		    if(j==strlen(num))return 1;
		    else return 0;
		}
		if(vis[sum[i]-'0']==1){
			return 0;
		}
		vis[sum[i]-'0']=1;
	}
	return 0;
}
void chnum(int a)
{
    char str[20];
    int l=0;
    while(a)
    {
        str[l++]=a%10+'0';
        a/=10;
    }
    str[l]='\0';
    for(int i=0;i<l;i++)
    {
        num[i]=str[l-i-1];
    }
    num[l]='\0';
}
int main(){
    unsigned long long n,i,j;
	while(fin>>n){
//		for(i=0;i<strlen(num);i++){
//			n*=10;
//			n+=num[i]-'0';
//		}
		//cout<<n<<endl;
		//cout<<check(n)<<endl;
		for(i=n+1;;i++){
		    chnum(i);
		  //  cout<<num<<endl;
			if(check(i)){
				fout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}
 

                            

你可能感兴趣的:(ACM,USACO,bbezxcy,ICPC)