Fork me on GitHub

HDU 5047 推公式+别样输出

题意:给n个‘M'形,问最多能把平面分成多少区域

解法:推公式 : f(n) = 4n(4n+1)/2 - 9n + 1 = (8n+1)(n-1)+2

前面部分有可能超long long,所以要转化一下,令a = 8n+1, b = n-1,将两个数都化为a1*10^8+b1的形式,则

(a1*10^8+b1)(a2*10^8+b2) =(a1a2*10^8 + a1b2 + a2b1)*10^8 + b1b2 + 2,由于a1,a2最多2为10^4左右,中间的数就都不会超过long long 了,先打印出前面,再打8位的后面即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define lll __int64
#define ll long long
using namespace std;

int main()
{
    ll n;
    int t,cs = 1;
    ll e = 100000000LL;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        scanf("%I64d",&n);
        ll a = 8LL*n+1LL;
        ll b = n-1LL;
        ll a1 = a/e;
        ll b1 = a%e;
        ll a2 = b/e;
        ll b2 = b%e;
        ll ans1 = a1*a2*e + a1*b2 + a2*b1 + (b1*b2+2LL)/e;
        ll ans2 = (b1*b2+2LL)%e;
        int res = ans2;
        printf("Case #%d: ",cs++);
        if(ans1 != 0)
        {
            printf("%I64d",ans1);
            printf("%08d\n",res);
        }
        else
            printf("%I64d\n",ans2);
    }
    return 0;
}
View Code

 

posted @ 2014-09-28 10:42  whatbeg  阅读(200)  评论(0编辑  收藏  举报