Blue Bridge Cup Exams - decimal bit n (fast power)

topic:

Here Insert Picture Description

input Output:

Here Insert Picture Description

analysis:

What is required is the subject of the latter and n two decimal places, the results can be transformed into the following requirements:
a n s = a b 1 0 n + 2 m The d 1000 ans = \dfrac{a}{b} * 10^{n+2}\quad mod\quad1000
but this time the presence of a / b in the above formula, certainly can not guarantee the accuracy of this time, and therefore to use the following alternative formulas:

a b m The d d = a m The d b d b \dfrac{a}{b}\quad mod\quad d = \dfrac{a\quad mod\quad b*d}{b}
So the beginning of the equation can be converted into the following formula:
a b 1 0 n + 2 m The d 1000 = a 1 0 n + 2 m The d b 1000 b \dfrac{a}{b} * 10^{n+2}\quad mod\quad1000 = \dfrac{a*10^{n+2}\quad mod\quad b*1000}{b}

At this point application of the above problems would not worry about accuracy problems, but this time direct calculation 1 0 n + 2 10^{n+2} or time out, so this time we want to use to solve the rapid power

Code:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

long long a,b,n,mod,ans;

long long QuickMul(long long x,long long y,long long MOD)
{
    long long res = 1;
    while(y)
    {
        if(y&1)
            res = res * x % MOD;
        x = x * x % MOD;
        y = (y>>1);
    }
    return res;
}

int main()
{
    scanf("%lld%lld%lld",&a,&b,&n);
    mod = b*1000;
    ans = (a % mod) * QuickMul(10,n+2,mod) % mod / b;
    printf("%lld\n",ans);
    return 0;
}

Published 61 original articles · won praise 7 · views 3634

Guess you like

Origin blog.csdn.net/weixin_42469716/article/details/104648531