您的位置:首页 > 其它

hdu----(5047)Sawtooth(大数相乘+数学推导)

2014-09-27 20:39 337 查看

Sawtooth

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 422 Accepted Submission(s): 134


[align=left]Problem Description[/align]
Think about a plane:

● One straight line can divide a plane into two regions.
● Two lines can divide a plane into at most four regions.
● Three lines can divide a plane into at most seven regions.
● And so on...

Now we have some figure constructed with two parallel rays in the
same direction, joined by two straight segments. It looks like a
character “M”. You are given N such “M”s. What is the maximum number of
regions that these “M”s can divide a plane ?

#include<cstdio>
#include<cstring>
char aa[50],bb[50];
int ans[50];
int mul( char *a, char *b, int temp[])
{

int i,j,la,lb,l;
la=strlen(a);
lb=strlen(b);

for ( i=0;i<la+lb;i++ )
temp[i]=0;
for ( i=0;i<=la-1;i++ ) {
l=i;
for ( j=0;j<=lb-1;j++ ) {
temp[l]=(b[j]-'0')*(a[i]-'0')+temp[l];
l++;
}
}
while ( temp[l]==0 )
l--;
for ( i=0;i<=l;i++ ) {
temp[i+1]+=temp[i]/10;
temp[i]=temp[i]%10;
}
if ( temp[l+1]!=0 )
l++;

while ( temp[l]/10!=0 ) {
temp[l+1]+=temp[l]/10;
temp[l]=temp[l]%10;
l++;
}
if ( temp[l]==0 )
l--;
return l;
}
void cal(__int64 a,char *str)
{
int i=0;
while(a>0)
{
str[i++]=(a%10)+'0';
a/=10;
}
}
int main()
{
int cas;
__int64 n;
scanf("%d",&cas);
for(int i=1;i<=cas;i++)
{
scanf("%I64d",&n);
printf("Case #%d: ",i);
if(n==0)printf("1\n");
else
{
memset(aa,'\0',sizeof(aa));
memset(bb,'\0',sizeof(bb));
memset(ans,0,sizeof(ans));
//,(8*n-7)*n+1
cal(8*n-7,aa);
cal(n,bb);
int len=mul(aa,bb,ans);
ans[0]++;
int c=0;
for(int j=0;j<=len;j++)
{
ans[j]+=c;
if(ans[j]>9)
{
c=ans[j]/10;
ans[j]%=10;
}
}
if(c>0)
printf("%d",c);
for(int j=len;j>=0;j--)
printf("%d",ans[j]);
printf("\n");
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: