uva11029 - Leading and Trailing

题目:

  求n的k次方,然后将答案用前三位和最后三位表示。

Sample Input 2 123456 1 123456 2 Sample Output 123...456 152...936

分析:  题目中其实有提示,用double来表示n的k次方,double神奇的地方在于能转化为string类型的字符串。用到了sprintf这个函数。代码:  
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int INF = 1000000000;
#define MAX 200

int n, k;

ll power_mod(ll a, ll n, ll mod)
{
    if(n == 0) return 1LL;
    ll ans = power_mod(a, n/2, mod);
    ans = ans*ans%mod;
    if(n%2) ans = ans*a%mod;
    return ans;
}

double pow(double a, int n)
{
    if(n == 0) return 1;
    double ans = pow(a, n/2);
    ans = ans*ans;
    if(n%2) ans = ans*a;
    while( ans > INF ) ans /= INF;
    return ans;
}

int main()
{
//    freopen("input.txt", "r", stdin);
    int caseNum;
    scanf("%d", &caseNum);
    while(caseNum--)
    {
        scanf("%d %d", &n, &k);
        double head = pow( (double)n, k );
        char str[MAX];
        sprintf(str, "%lf", 1000*head);
        str[3] = ‘\0‘;

        ll last = power_mod(n, k, 1000);
        printf("%s...%03lld\n", str, last);
    }

    return 0;
}
时间: 2024-10-01 06:07:35

uva11029 - Leading and Trailing的相关文章

[题解]UVA11029 Leading and Trailing

链接:http://vjudge.net/problem/viewProblem.action?id=19597 描述:求n^k的前三位数字和后三位数字 思路:题目要解决两个问题.后三位数字可以一边求高次幂一边取模,下面给出求前三位数字的方法. n^k = (10^lg n)^k = 10^(k*lg n) 为了描述方便,令X=n^k .则 lg X 的整数部分表示X有多少位.设整数部分为zs,小数部分为xs,则X=(10^zs)*(10^xs) . (10^zs)的形式就是100……,跟X的位

UVA11029 Leading and Trailing【快速模幂+数学】

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 data type format, but you won't get all the digits of the

UVA 11029 || Lightoj 1282 Leading and Trailing 数学

Leading and Trailing You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk. Input Input starts with an integer T (≤ 1000), denoting the number of test cases. Each case st

LightOJ 1282 Leading and Trailing (快数幂 + 数学)

http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1282 Description You are given two integers: n and k, your task is t

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 Leading and Trailing 11029【数学+快速幂】

11029 - Leading and Trailing Time limit: 3.000 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

Leading and Trailing (数论)

Leading and Trailing https://vjudge.net/contest/288520#problem/E You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk. Input Input starts with an integer T (≤ 1000), den

快速幂 E - Leading and Trailing LightOJ - 1282

E - Leading and Trailing LightOJ - 1282 快速幂主要是把n拆成2进制位,如果这一位有那么就乘,没有就不乘,而计数器也就是x是不断推进的,从x->x^2->x^4直到n的最高位精髓在于取模,后一步的要求结果只与前一步的模后数据有关 . 对于后三个数用了log10.log函数对求n^k这种问题还是很有用的.没想出来. 1 #include <iostream> 2 #include <cstring> 3 #include <st

E - Leading and Trailing (log的应用)

E - Leading and Trailing 题目链接:https://vjudge.net/problem/LightOJ-1282#author=yyb 题目大意: 给定两个数n,k 求n^k的前三位和最后三位. 解题思路: $b = a^{n}$  可以推出 $10^{n\log_{10}a} = b$. 然后计算n*log10(a),他可能大于1所以对1取余得到k,然后计算pow(10,2+k),得到前三位即可. 后三位ksm对1000取余就能得到. 代码: 1 #include <