CodeForces 614A . Link/Cut Tree【坑精度!!!】

A. Link/Cut Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure.

Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed to help if Rostislav solves a simple task (and if he doesn't, then why would he need Splay trees anyway?)

Given integers lr and k, you need to print all powers of number k within range from l to r inclusive. However, Rostislav doesn't want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!

Input

The first line of the input contains three space-separated integers lr and k (1 ≤ l ≤ r ≤ 10182 ≤ k ≤ 109).

Output

Print all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print "-1" (without the quotes).

Examples
input
1 10 2
output
1 2 4 8 
input
2 4 5
output
-1
Note

Note to the first sample: numbers 20 = 121 = 222 = 423 = 8 lie within the specified range. The number24 = 16 is greater then 10, thus it shouldn't be printed.


题意:

给出一个区间范围,和一个k 值,输出区间范围内k 的幂


题解:

错了n次,主要是溢出问题


两种思路解决:

1,计算出两端的边界情况,然后无脑累乘输出

注意log 的精度,double 和 long long 的转化精度是不够的,需要额外逼近一下

/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<cmath>
typedef long long ll;
const double eps=1e-6;
double log(ll n,ll m)
{
	return log(n*1.0)/log(m*1.0);
}
ll power(ll n,ll m)//快速幂
{
	ll ans=1;
	while(m)
	{
		if(m&1)
		{
			ans*=n;
		}
		n*=n;
		m>>=1;
	}
	return ans;
}
void slove(ll l,ll r,ll k)
{
	ll bg=log(l,k)+eps,ed=log(r,k)+eps;//大约值
	while(power(k,bg)<l)//尝试逼近一下
	{
		++bg;
	}
	while(power(k,ed)>r)//同上
	{
		--ed;
	}
	if(bg>ed)
	{
		printf("-1\n");
		return;
	}
	ll ans=power(k,bg);
	printf("%I64d",ans);
	for(ll i=bg+1;i<=ed;++i)
	{
        ans*=k;
        printf(" %I64d",ans);
	}
	printf("\n");
}
int main()
{
	ll l,r,k;
	//freopen("shuju.txt","r",stdin);
	while(~scanf("%I64d%I64d%I64d",&l,&r,&k))
	{
		slove(l,r,k);
	}
	return 0;
}


2,累乘会超出范围,导致输出出错,那么就可以处理成除的情况


/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<cmath>
typedef long long ll;

void slove(ll l,ll r,ll k)
{
	ll ans=1,kase=0,cnt=0;
	while(1)
	{
		if(ans>=l&&ans<=r)
		{
			kase=1;
			if(cnt++)
			{
				printf(" ");
			}
			printf("%I64d",ans);
		}
		if(ans>r/k) 
		{
			break;
		}
		ans*=k;
	}
	if(!kase)
	{
		printf("-1\n");
	}
	printf("\n");
}
int main()
{
	ll l,r,k;
	//freopen("shuju.txt","r",stdin);
	while(~scanf("%I64d%I64d%I64d",&l,&r,&k))
	{
		slove(l,r,k);
	}
	return 0;
}

对最基础的东西还是没有弄清楚,这么简单的问题还是错了这么多次.........


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值