HDU 5047 sawtooth

Source: Internet
Author: User
Sawtooth Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 258 accepted submission (s): 78


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

 
Inputthe 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)
Outputfor 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 Input
212
 
Sample output
Case #1: 2Case #2: 19
 
Source2014 ACM/ICPC Asia Regional Shanghai Online


Question and code:

The formula for derivation is simple: first, we can see that any two m can have 16 intersections, and then label M 1 -- N, the total intersection points are Σ 16 * I (1 = <I <n ),

Then, based on the number of edges + points + 1 = number of divided areas, the formula is 8 * n ^ 2-7 * n + 1. Because the number is relatively large, we will think of using Java. The result is that the kuangbin is very tight. orz ....

Then use the C ++ large number template, through observation we found: the above formula can be converted to N * (8 * N-7) + 1, here 8 * N can be calculated using bitwise operations, we let M = 8 * N-7, m maximum also

N is not 10 ^ 13, but n is 10 ^ 12 at the maximum. In this way, we can use the idea of large numbers to divide m into two parts and multiply them by N, finally, simply process the output.


Time: 187 Ms

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const ll mod=1000000;int main(){    ll n,m,l_m,r_m;    int cas;    scanf("%d",&cas);    for(int ca=1; ca<=cas; ca++)    {        scanf("%I64d",&n);        //printf("%I64d\n",8*n*n-7*n+1);        m=(n<<3)-7;        l_m=m/mod;        r_m=m%mod;        l_m*=n;        r_m=r_m*n+1;        l_m=l_m+r_m/mod;        r_m%=mod;        printf("Case #%d: ",ca);        if(l_m) printf("%I64d%06I64d\n",l_m,r_m);        else printf("%I64d\n",r_m);    }    return 0;}






HDU 5047 sawtooth

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.