UVA - 11029 - Leading and Trailing (快速幂+公式变形)

题目传送:UVA - 11029

思路:后三位可以直接快速幂取模,然后前三位可以有两种做法,一个是利用double,一个是利用公式法,具体看代码吧

注意,后三位不足三位要补0,即用%03d

AC代码①:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 1000000000
using namespace std;

#define MOD 1000

int T;
int n, k;

int kmod(int x, int n) {	//快速幂
	int ret = 1;
	while(n) {
		if(n & 1) ret = (ret * x) % MOD;
		x = (x * x) % MOD;
		n >>= 1;
	}
	return ret;
}

double kkmod(double x, int n) {		//利用double来求前三位
	double ret = 1;
	while(n) {
		if(n & 1) ret = ret * x;
		while(ret >= INF) ret /= INF;
		x = x * x;
		while(x >= INF) x /= INF;
		n >>= 1;
	}
	return ret;
}

int main() {
	scanf("%d", &T);
	while(T --) {
		scanf("%d %d", &n, &k);

		int ttt = n % 1000;
		ttt = kmod(ttt, k);

		double lll = kkmod((double)n, k);
		lll *= 1000;	//可能lll本来就小于1000,可能还不足三位
		while(lll >= 1000) {
			lll /= 10;
		}
		/*char str[1234];
		sprintf(str, "%lf", 1000 * lll);
		str[3] = '\0';*/  //也可以这样输出前三位
		//printf("%lf\n", lll);

		printf("%d...%03d\n", (int)lll, ttt);	//记住后三位用%03d,要严格按照格式输出
	}
	return 0;
}

AC代码②:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int T;
int n, k; 

int kmod(int x, int n) {
	int ret = 1;
	while(n) {
		if(n & 1) ret = (ret * x) % 1000;
		x = x * x % 1000;
		n >>= 1;
	}
	return ret;
}

int main() {
	scanf("%d", &T);
	while(T --) {
		scanf("%d %d", &n, &k);
		int lll, ttt;
		lll = kmod(n % 1000, k);
		ttt = (int)pow(10, 2 + fmod(k * log10(n), 1));	//利用公式变形来求前三位
		printf("%d...%03d\n", ttt, lll);
	}
	return 0;
}
时间: 2024-08-04 04:57:48

UVA - 11029 - Leading and Trailing (快速幂+公式变形)的相关文章

UVA 11029 Leading and Trailing(大数n^k的前x位高精度问题)(好题)

Problem C Leading and Trailing Time limit: 2 seconds   Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double da

Uva 11029 Leading and Trailing (求n^k前3位和后3位)

题意:给你 n 和 k ,让你求 n^k 的前三位和后三位 思路:后三位很简单,直接快速幂就好,重点在于如何求前三位,注意前导0 资料:求n^k的前m位 博客连接地址 代码: #include <iostream> #include <cmath> #include <cstdio> #include <algorithm> #define ll long long using namespace std; ll qmod(ll a,ll b,ll mod)

UVa 11029 Leading and Trailing

题目要求输出N的K次方的前三位和后三位.后三位的解法不用多说了,用二分法快速去模即可.关键是前三位怎么求?题目中说N能用32位带符号整数表示,K最大是10的六次方.因此N^K的解ans最多不过10^(9*10^6),因此我们完全可以用以十为底的对数x+y表示,其中x表示对数的整数部分,y表示对数的小数部分.显然,ans的具体数字是由10^y来表示的,而x只是用来将小数以为成整数而已.并且可以确定的是,10^y必小于10且大于1,因为大于10的话,y就大于1了,而小于1的话,y就小于0了显然不可能

UVA 10655 - Contemplation! Algebra(矩阵快速幂)

UVA 10655 - Contemplation! Algebra 题目链接 题意:给定p, q, n代表p=a+b,q=ab求an+bn 思路:矩阵快速幂,公式变换一下得到(an+bn)(a+b)=an+1+bn+1+ab(an?1+bn?1),移项一下得到an+1+bn+1=(an+bn)p?q(an?1+bn?1) 这样就可以用矩阵快速幂求解了 代码: #include <stdio.h> #include <string.h> long long p, q, n; str

UVA - 10006 - Carmichael Numbers (快速幂+素数判断)

题目传送:UVA - 10006 思路:就是快速幂暴力过去就行了,然后要注意点细节,就是快速幂的时候会爆int,然后就是先判断是否为素数,是素数就直接输出结果is normal,不然会超时 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #inclu

Uva 11551 - Experienced Endeavour ( 矩阵快速幂 )

Uva 11551 - Experienced Endeavour ( 矩阵快速幂 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 50 #define MOD 1000 #define CLR( a, b ) memset( a, b, sizeof(a) ) struct Mat { int n; int mat[MAX

ZOJ 3690 &amp; HDU 3658 (矩阵快速幂+公式递推)

ZOJ 3690 题意: 有n个人和m个数和一个k,现在每个人可以选择一个数,如果相邻的两个人选择相同的数,那么这个数要大于k 求选择方案数. 思路: 打表推了很久的公式都没推出来什么可行解,好不容易有了想法结果WA到天荒地老也无法AC.. 于是学习了下正规的做法,恍然大悟. 这道题应该用递推 + 矩阵快速幂. 我们设F(n) = 有n个人,第n个人选择的数大于k的方案数: G(n) = 有n个人,第n个人选择的数小于等于k的方案数: 那么递推关系式即是: F(1)=m?k,G(1)=k F(n

HDU 3117 Fibonacci Numbers(矩阵快速幂+公式)

题目地址:HDU 3117 对于后四位可以用矩阵快速幂快速求出来,但前四位就没办法了.要知道斐波那契数列是有通项公式的,所以只能通过通项公式来求前四位,但公式不能求后四位,因为公式使用浮点数求的,精度显然不够,求前四位要用到对数. 通项公式为: f(n)=1/sqrt(5)(((1+sqrt(5))/2)^n+((1-sqrt(5))/2)^n) 假设F[n]可以表示成 t * 10^k(t是一个小数),那么对于F[n]取对数log10,答案就为log10 t + K,此时很明显log10 t<

UVa 10006 Carmichael Numbers (快速幂 + 素性测试)

Carmichael Numbers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people even think that cryptography is t