您的位置:首页 > 大数据 > 人工智能

UVA 11029 Leading and Trailing

2013-01-16 22:49 477 查看
大意不再赘述。

思路:

后三位比较简单,前三位其实也比较简单。

如x = 123456,我们转换一下:x = 1.23456 * 10^5,即log(x) = 5 + y;

有了这个式子我们就可以知道,10^y一定等于1.23456,不信你可以去试试。然后前三位就是10^y*100咯。

前4位,前5位...n位其实都类似,关键是掌握题目的方法。

还有一个要注意,可能最后3位mod1000的时候可能为0,所以输出需要%03lld。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
using namespace std;

typedef long long LL;

LL n, m;

void read_case()
{
	scanf("%lld%lld", &n, &m);
}

LL power(LL a, LL n, LL m)  
{
	LL ans = 1;
	while(n)
	{
		if(n & 1) ans = ans*a % m;
		a = a*a % m;
		n /= 2;
	}
	return ans % m;
}

LL cal(int n, LL m)
{
	LL p, q, ans;
	double f = m*log10(n);
	q = (LL)f;
	p = (LL)(f*10000000)-q*10000000;
	double x = 1.0*p/10000000;
	ans = (LL)(pow(10, x)*100);
	return ans;
}

/*LL cal(int n, int m)
{
	double f = m*log10(n);
	double p = f - floor(f);
	LL ans = floor(pow(10, p)*100);
	return ans;
}*/

void solve()
{
	int p;
	LL q;
	read_case();
	if(n < 1000) p = n;
	p = cal(n, m);
	q = power(n, m, 1000);
	printf("%lld...%03lld\n", p, q);
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		solve();
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: