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

 

转载于:https://www.cnblogs.com/whatbeg/p/3997840.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值