LightOJ_1282 Leading and Trailing 快速幂

13 篇文章 0 订阅
1 篇文章 0 订阅

大意:输入整数n和k,输出n的k次幂的前三位和后三位

亮点:利用快速幂求后三位,利用k*log10(n)相关小数部分pow(10, 小数部分)求前三位

快速幂实现:pow4(int n, int k, int mod);

程序:

#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define LL long long
#define ll __int64
#define mes(a, b) memset(a, b, sizeof(a))
using namespace std;

int n, k;
double fl;
int pow4(ll n, ll k, int mod)
{
    ll ans = 1;
    ll base = n;
    while (k){
        if (k & 1){
            ans *= base;
            ans %= mod;
        }
        base *= (base % mod);   //debug data overflow
                                //ll
        base %= mod;
        k >>= 1;                //debug >>
    }
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    int tt; cin >> tt;
    int ans1, ans2;
    int kase = 0;
    while (tt --){
        cin >> n >> k;
        ans2 = pow4(n, k, 1000);
        fl = k*log10(n+0.0);    //transform the type of data
        fl -= int(fl);          //debug ll()
        ans1 = (int)pow(10.0, fl+2);
                                //association with digit number
        printf("Case %d: %d %03d\n", ++kase, ans1, ans2);
                                //make up the blank place with 0
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sycamore_Ma

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值