HDU ---- (5047) sawtooth (multiplication of large numbers + mathematical derivation)

Source: Internet
Author: User
Sawtooth

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 422 accepted submission (s): 134


Problem description 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?

 

 

Input the first line of the input is t (1 ≤ T ≤ 100000), which stands for the number of test cases you need to solve.

Each case contains one single non-negative integer, indicating number of "M" S. (0 ≤ n ≤0 ≤ 1012)

 

Output for each test case, print a line "Case # T:" (without quotes, t means the index of the test case) at the beginning. then an integer that is the maximum number of regions n the "M" figures can divide.

 

Sample input212

 

Sample outputcase #1: 2 case #2: 19

 

Source 2014 ACM/ICPC Asia Regional Shanghai Online in fact, the question has clearly told us that it is derived from the cable fragment .... for the line distribution plane 0 11 1 + 1 2 1 + 1 + 23 1 + 1 + 2 + 34 1 + 1 + 2 + 3 + 4 ............ N 1 + n (n + 1)/2; then for an M model, we can regard it as a combination of four line segments, and this formula becomes: 4N * (4n + 1)/2 + 1 ----> obviously, the answer is more than enough. I am 0 11 11 11 2 92 37 19 9*2 ...... push to get: 4N * (4n + 1)/2 + 1-8 * n ----> 8n ^ 2-7n + 1 code:
 1 #include<cstdio> 2 #include<cstring> 3 char aa[50],bb[50]; 4 int ans[50]; 5 int mul( char *a, char *b, int temp[]) 6 { 7  8     int i,j,la,lb,l; 9     la=strlen(a);10     lb=strlen(b);11 12     for ( i=0;i<la+lb;i++ )13         temp[i]=0;14     for ( i=0;i<=la-1;i++ ) {15           l=i;16         for ( j=0;j<=lb-1;j++ ) {17             temp[l]=(b[j]-‘0‘)*(a[i]-‘0‘)+temp[l];18             l++;19         }20     }21     while ( temp[l]==0 )22         l--;23     for ( i=0;i<=l;i++ ) {24         temp[i+1]+=temp[i]/10;25         temp[i]=temp[i]%10;26     }27     if ( temp[l+1]!=0 )28         l++;29 30     while ( temp[l]/10!=0 ) {31         temp[l+1]+=temp[l]/10;32         temp[l]=temp[l]%10;33         l++;34     }35     if ( temp[l]==0 )36         l--;37     return l;38 }39 void cal(__int64 a,char *str)40 {41     int i=0;42     while(a>0)43     {44      str[i++]=(a%10)+‘0‘;45      a/=10;46     }47 }48 int main()49 {50     int cas;51     __int64 n;52     scanf("%d",&cas);53     for(int i=1;i<=cas;i++)54     {55       scanf("%I64d",&n);56       printf("Case #%d: ",i);57       if(n==0)printf("1\n");58       else59       {60       memset(aa,‘\0‘,sizeof(aa));61       memset(bb,‘\0‘,sizeof(bb));62       memset(ans,0,sizeof(ans));63       //,(8*n-7)*n+164       cal(8*n-7,aa);65       cal(n,bb);66       int len=mul(aa,bb,ans);67        ans[0]++;68        int c=0;69      for(int j=0;j<=len;j++)70      {71          ans[j]+=c;72        if(ans[j]>9)73         {74           c=ans[j]/10;75           ans[j]%=10;76         }77      }78       if(c>0)79         printf("%d",c);80       for(int j=len;j>=0;j--)81         printf("%d",ans[j]);82     printf("\n");83     }84     }85  return 0;86 }
View code

 

HDU ---- (5047) sawtooth (multiplication of large numbers + mathematical derivation)

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: info-contact@alibabacloud.com 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.