主题:第43次编程比赛第一题题目
thomas [专家分:180] 发布于 2006-10-01 09:15:00
PROBLEM:
N个1和N个0组成一个2N位的二进制数,要求从左到右扫描,1的累计数不小于0的累计数,试求满足这种条件的数的个数P
INPUT:
输入文件为:DATA1.IN
内有[color=FF0000]一个[/color]整数N(N<=100)
OUTPUT
输出文件为:ANS1.OUT
输出一个数P,P精确到个位,即输出所有数位
SAMPLE INPUT
3
SAMPLE OUTPUT
5
解释:
例如N=3时,P=5,为
111000
110100
110010
101010
101100
HINT:不要想复杂了[em2]
回复列表 (共19个回复)
11 楼
lw8484654 [专家分:30] 发布于 2006-10-01 20:58:00
dddddd
12 楼
hwjian [专家分:0] 发布于 2006-10-01 21:11:00
#include<iostream>
#include<stdio.h>
using namespace std;
int record;
int N[1];
void dfs(int sum,int n)
{
if(sum<0)
return ;
if(n==N[0]){
record++;
return;}
dfs(sum+1,n+1);
dfs(sum-1,n);
}
int main()
{
record=0;
FILE *fp;
fp=fopen("DATA1.IN","r");
fread(N,sizeof(int),1,fp);
fclose(fp);
dfs(1,1);
N[1]=record;
fp=fopen("ANS1.OUT","w");
fwrite(N,sizeof(int),1,fp);
fclose(fp);
return 0;
}
13 楼
118sgy [专家分:180] 发布于 2006-10-02 08:47:00
14 楼
火海时代 [专家分:230] 发布于 2006-10-02 11:37:00
#include <iostream>
using namespace std;
int n;
int p = 0;
int num0 = 0,num1 = 0;
void suan(int i)
{
if(i==2*n)
p++;
else
{
if(num0<n&&num0+1<=num1)
{
num0++;
suan(i+1);
num0--;
}
if(num1<n)
{
num1++;
suan(i+1);
num1--;
}
}
return;
}
int main(void)
{
FILE *f = fopen("DATA1.IN","r");
fscanf(f,"%d",&n);
fclose(f);
suan(1);
f = fopen("ANS1.OUT","w");
fprintf(f,"%d",p);
fclose(f);
return 0;
}
15 楼
ITER [专家分:680] 发布于 2006-10-02 11:50:00
/**********************
不懂文件输入输出 呵呵
所以只把分析出来的数学公式写出来了
具体接口麻烦thomas兄帮我修改下勒:)
以下我抽离出来的纯粹数学公式
**********/
int count(int n) //输入N 输出个数p
{
int i = n-1;
int j(1);
int count(n);
while(j<=n-2)
{
count=count+i*(i-1)*j;
i--;
j++;
}
return count;
}
16 楼
willzhanglala [专家分:1350] 发布于 2006-10-02 17:22:00
ms是,C(N,2N)/(N+1)
受不了了。
不比速度的话,这个就是答案了。不写程序了。
17 楼
孤独的猫 [专家分:3370] 发布于 2006-10-02 19:19:00
#include <stdio.h>
#include <math.h>
void mulex(int n)
{
int primeArray[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,
47,53,59,61,67,71,73,79,83,89,97,101,
103,107,109,113,127,131,137,139,149,
151,157,163,167,173,179,181,191,
193,197,199};
int primeCount1[46];
int primeCount2[15];
int i,j,k;
int isPrime=0;
int quotient;
int res[100];
int carry,digit=1,temp;
FILE *fp;
primeCount1[0]=(int)((n+1)/2);
primeCount2[0]=1;
for(i=1;i<46;i++)
{
primeCount1[i]=0;
if(i<15)primeCount2[i]=0;
}
primeCount2[1]=1;
for(k=4;k<=n-primeCount1[0];k++)
{
i=k;
while(!isPrime)
{
for(j=0;j<15;j++)
{
if(i%primeArray[j]==0)
{
quotient=i/primeArray[j];
primeCount2[j]++;
if(quotient==1)
isPrime=1;
else
i=quotient;
break;
}
}
}
isPrime=0;
}
for(k=primeCount1[0]*2+3;k<=2*n-1;k=k+2)
{
i=k;
while(!isPrime)
{
for(j=0;j<46;j++)
{
if(i%primeArray[j]==0)
{
quotient=i/primeArray[j];
primeCount1[j]++;
if(quotient==1)
isPrime=1;
else
i=quotient;
break;
}
}
}
isPrime=0;
}
for(i=0;i<15;i++)
{
if(primeCount2[i])
primeCount1[i]=primeCount1[i]-primeCount2[i];
}
res[0]=1;
for(i=0;i<46;i++)
{
if(primeCount1[i])
{
for(j=0;j<primeCount1[i];j++)
{
for(k=1,carry=0;k<=digit;k++)
{
temp=res[k-1]*primeArray[i]+carry;
res[k-1]=temp%10;
carry=temp/10;
}
while(carry)
{
res[++digit-1]=carry%10;
carry/=10;
}
}
}
}
fp=fopen("ANS1.OUT","a+");
for(i=digit;i>=1;--i)
{
fprintf(fp,"%d",res[i-1]);
}
fclose(fp);
}
int main()
{
int n;
int i;
unsigned long res,res1=1,res2=1;
FILE *fp;
fp=fopen("DATA1.IN","r");
fscanf(fp,"%d",&n);
fclose(fp);
if(n>8)
{
mulex(n);
return 0;
}
if(n)
{
for(i=2*n;i>n;i--)
res1*=i;
for(i=1;i<=n;i++)
res2*=i;
res=res1/res2/(n+1);
}
else res=0;
fp=fopen("ANS1.OUT","a+");
fprintf(fp,"%d",res);
fclose(fp);
return 0;
}
18 楼
thomas [专家分:180] 发布于 2006-10-02 20:02:00
time's out[em1]
19 楼
hotzenplotz [专家分:40] 发布于 2006-10-02 20:04:00
看起来象是C(n,2n)/(n+1)
但是没有证明,就不写程序了
十一大家都要出去玩的,看起来参加的人不多
thomas,不好意思催了你一下,主要是想在十一假期以前找点事做
你要是喜欢这种貌似用搜索的题目,我还能给你一道更狠的,看不出来的
呵呵
我来回复