HDOJ5047-Sawtooth

Sawtooth



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 ≤ 10 12)
 

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 Input

2 1 2
 
Sample Output

Case #1: 2 Case #2: 19 题意:M型的线能把平面分成多少部分。 思路1:N条直线能把平面分成1+N(N+1)/2部分。 把M看成四条直线,带入公式1+4N(4N+1)/2得到 N==1时 11,N==2时 37 跟M型正确的时候相差9N 所以推出公式
1+4N(4N+1)/2-9N 化简成N(8N-7)+1
思路2:用待定系数法。二维则设ax^2+bx+c=0 三维则设成ax^3+bx^2+cx+d=0  现在用二维求得a,b,c分别为8 -7 1  化成



N(8N-7)+1

化成



N(8N-7)+1
求解,因为N^2在这里可能会超范围。把大数分成两部分前部分为sum/1000000 后部分为sum%1000000 然后进位输出。





#include
#include
using namespace std;
#define mod 1000000
int main()
{
    long long n,x,t=1;
    cin>>n;
    while(n--)
    {
        cin>>x;
        long long  m=(x<<3)-7;  ;
        long long rm,lm;
        rm=m/mod;
        lm=m%mod;
        rm=rm*x;
        lm=lm*x+1;
        rm=rm+lm/mod;
        lm=lm%mod;
          printf("Case #%d: ",t);  
        if(rm)
           printf("%I64d%06I64d\n",rm,lm);
        else
           printf("%I64d\n",lm);  
        t++;
    }
    return 0;
}


你可能感兴趣的:(思路,水题,思路,HD)