HOJ 12983 Integer Estate Agent(搜索优化)

题意是说给你一个数n,它可能是一些连续的整数串的和(整数最小是2),求对于每一个n,这样的串有多少个。

太年轻了,对于每一个n都去枚举串的长度,一直TLE,做了读入优化,记忆化搜索也不管用,后来看了另外一个妹子的代码才发现,应该先打一次表,但是姿势略有不同。先枚举串的长度,接着枚举串的起始点,然后用公式计算出值,在合法范围内就给该位置加一。对于每一个内循环,值超过最大值就跳出。

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;

const int maxn = 1e6 + 10;
int ans[maxn];

template <class T>
char scanf(T *argu)
{
    char ch = getchar();
    (*argu) = 0;
    while(ch > '9' || ch < '0')
    {
        if(ch == EOF)
            return ch;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        (*argu) *= 10;
        (*argu) += ch - '0';
        ch = getchar();
    }
    return ch;
}

void init()
{
	memset(ans, 0, sizeof(ans));
	int mt = 2 * sqrt(maxn) + 1;
	long long sum;
	for(int t = 1; t < mt; t++)
	{
		for(int x = 1; x < maxn; x++)
		{
			sum = x * t + t * (t + 1) / 2;
			if(sum > 1e6)
				break;
			else
				ans[sum]++;
		}
	}
}

int main()
{
    int n;
    init();
    while(scanf(&n) && n)
        printf("%d\n", ans[n]);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值