POJ 1019 Number Sequence(DP?)

给出一个序列,要求求出第i位的值,首先dp1求出范围内每个子串的位数,然后dp2求出前n个dp1子串和的长度


根据给出的i,先找到比dp2大的,减去之后就能知道剩下范围,然后在dp1中找比ans大的,就可以知道当前是第几个数字,注意但ans==0的情况


讲道理,用二分可能会更快一点


#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#include<functional>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)

#define inf 1000000000+7

using namespace std;
typedef long long ll;

ll dp1[320000];
ll dp2[320000];

int main()
{
	ll ch = 1,we=1;
	mt(dp1);
	mt(dp2);
	for (int i = 1; i < 320000; i++)
	{
		if (i / ch >= 10)
		{
			we++;
			ch *= 10;
		}
		dp1[i] = dp1[i - 1] + we;
	}
	for (int i = 1; i < 320000; i++)
		dp2[i] = dp2[i - 1] + dp1[i];
	int T;
	cin >> T;
	while (T--)
	{
		ll ans = 0;
		scanf("%lld", &ans); ll start;
		for (int i = 0; i < 320000; i++)
		{
			if (dp2[i]>ans){ ans -= dp2[i - 1]; if (ans == 0)start = i - 1; break; }
		}
		if (ans!=0)
		for (int i = 0; i < 320000; i++)
		{
			if (dp1[i]>ans){ ans -= dp1[i - 1]; if (ans == 0)start = i - 1; else start = i; break; }
		}
		//if (ans == 0)start--;
		int wei = (int)log10(start)+1;
		stringstream ss;
		string str;
		ss << start;
		ss >> str;
		if (ans == 0)printf("%c\n", str[str.size() - 1]);
		else printf("%c\n", str[ans - 1]);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值