zoukankan      html  css  js  c++  java
  • [LightOJ1282]Leading and Trailing

    题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26992

    Description

    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 starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).

    Output

    For each case, print the case number and the three leading digits (most significant) and three trailing digits (least significant). You can assume that the input is given such that nk contains at least six digits.

    Sample Input

    5

    123456 1

    123456 2

    2 31

    2 32

    29 8751919

    Sample Output

    Case 1: 123 456

    Case 2: 152 936

    Case 3: 214 648

    Case 4: 429 296

    Case 5: 665 669

    求x^k的前3项和后3项,数据保证这个数有6位。

    卡PE卡了好久,求前3位的思路同上一题fibnacci的相似,推一下过程就能写出来。后三位直接快速幂%1000

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 
     6 using namespace std;
     7 
     8 typedef long long LL;
     9 const int mod = 1000;
    10 LL n, k;
    11 
    12 LL quickmul(LL x, LL n) {
    13     LL ans = 1;
    14     LL t = x;
    15     while(n) {
    16         if(n & 1) {
    17             ans = (ans * t) % mod;
    18         }
    19         t = t * t % mod;
    20         n >>= 1;
    21     }
    22     return ans;
    23 }
    24 
    25 LL mul(LL x, LL n) {
    26     LL a, b;
    27     LL answer;
    28     double ans = n * log10(x);
    29     b = (LL)ans;
    30     a = (LL)(ans * 10000000) - b * 10000000;
    31     answer = (LL)(pow(10, 1.0 * a / 10000000) * 100);
    32     return answer;
    33 }
    34 
    35 pair<LL, LL> solve() {
    36     LL l;
    37     LL t;
    38     l = mul(n, k);
    39     t = quickmul(n, k) % mod;
    40     return make_pair(l, t);
    41 }
    42 int main() {
    43     int T;
    44     scanf("%d", &T);
    45     for(int ii = 1; ii <= T; ii++) {
    46         scanf("%I64d %I64d", &n, &k);
    47         solve();
    48         pair<LL, LL> tmp = solve();
    49         printf("Case %d: %d %03d
    ", ii, (int)tmp.first, (int)tmp.second);
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    C# winform 打包成安装程序(exe)
    gitHub----【Mac】sourcetree连接github,报错:fatal:Authentication failed for'https://git…。或提示password required 解决方案
    python3-----往一个字符串中循环添加数据
    python3.8----从多层嵌套Json中解析所需要的值
    记录一些工作知识
    【转】彻底搞懂 async & defer
    【转】判断JS数据类型的四种方法
    动态表单设计
    封装,继承,多态
    语义化版本
  • 原文地址:https://www.cnblogs.com/kirai/p/4743927.html
Copyright © 2011-2022 走看看