HDU 5047 sawtooth (Big Data Simulation) Shanghai division Network Competition 1006

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5047

Solution Report: Ask a "M" type to divide the plane of a rectangle into a maximum of several blocks.

The input has n "M". Now the formula is 8 * n ^ 2-7 * n + 1, but the range of N is 10 ^ 12, as long as the square is within the range of long, what should we do with a large number?

I 've tried it all. It's strange that it will time out. I don't think it will happen according to estimation. Maybe it's because the intermediate results are big. I'm still thinking about this, however, the 10 ^ 12 square is multiplied by eight at a time and only reaches the 10 ^ 25 power level. Therefore, we can use four _ int64 to simulate this result, so that the computation is much faster. Why only eight bits are saved for each result, because the square operation is performed in the middle, and the eight bits are below the square, within the range of long, if it is larger than long, it will not work, and the multiplication in the intermediate computation will easily overflow, and the eight bits are just convenient for computation. When multiplying, We need to multiply the long pairs on the corresponding bits of the large number.

Note that the final output result cannot be output directly. The leading 0 in the middle must also be output. Otherwise, it seems that a few zeros are missing, the intermediate result is output in a fixed 8-bit format.

 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstdlib> 7 using namespace std; 8 typedef __int64 INT; 9 struct Big10 {11     INT d[4];12     Big()13     {14         memset(d,0,sizeof(d));15     }16     void print()17     {18         int flag = 0;19         for(int i = 3;i >= 0;--i)20         if(d[i] != 0 && !flag)21         {22             printf("%I64d",d[i]);23             flag = 1;24         }25         else if(flag) printf("%08I64d",d[i]);26         puts("");27     }28 };29 Big operator * (Big a,Big b)30 {31     Big c;32     INT flag = 0;33     for(int i = 0;i < 4;++i)34     for(int j = 0;j < 4;++j)35     {36         INT temp = a.d[i] * b.d[j] + flag;37         if(temp) c.d[i+j] += temp % 100000000;38         flag = temp / 100000000;39     }40     return c;41 }42 Big operator - (Big a,Big b)43 {44     Big c;45     for(int i = 0;i < 4;++i)46     {47         if(a.d[i] < b.d[i])48         {49             a.d[i+1] -= 1;50             a.d[i] += 100000000;51         }52         c.d[i] = a.d[i] - b.d[i];53     }54     return c;55 }56 Big operator + (Big a,Big b)57 {58     Big c;59     INT flag = 0;60     for(int i = 0;i < 4;++i)61     {62         INT temp = a.d[i] + b.d[i] + flag;63         c.d[i] = temp % 100000000;64         flag = temp / 100000000;65     }66     return c;67 }68 Big valueof(INT x)69 {70     int f = 0;71     Big ans;72     while(x)73     {74         ans.d[f++] = x % 100000000;75         x /= 100000000;76     }77     return ans;78 }79 int main()80 {81     int T,kase = 1;82     INT a;83     scanf("%d",&T);84     while(T--)85     {86         scanf("%I64d",&a);87         Big ans = valueof(a) * valueof(a);88         ans = ans * valueof(8);89         ans = ans - (valueof(a) * valueof(7));90         ans = ans + valueof(1);91         printf("Case #%d: ",kase++);92         ans.print();93     }94     return 0;95 }
View code

 

HDU 5047 sawtooth (Big Data Simulation) Shanghai division Network Competition 1006

Related Article

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.