2019牛客暑期多校训练营(第三场) D. Big Integer

链接:https://ac.nowcoder.com/acm/contest/883/D
来源:牛客网

Problem Description

2019牛客暑期多校训练营(第三场) D. Big Integer_第1张图片
2019牛客暑期多校训练营(第三场) D. Big Integer_第2张图片

Input Sample

2
11 8 1
7 6 2

Output Sample

4
2

Solution:

2019牛客暑期多校训练营(第三场) D. Big Integer_第3张图片
2019牛客暑期多校训练营(第三场) D. Big Integer_第4张图片

由于9p不是质数,所以应该用mod p来做题,但是由于3与9不是互质的,故p=3的情况不能把9消掉,需要老老实实的用mod 9p,然后用欧拉函数算

另外,2和5的情况,显然是0

AC Code:

/*
 * Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author: Ng Kimbing, HNU.
 * @LastModified:2019-07-26 T 20:39:03.727 +08:00
 */

package ACMProblems.Math.NumberTheory;

import java.util.ArrayList;

import static ACMProblems.ACMIO.*;

public class NumberTheoryBigInteger {

    // Code for IO has been omitted.

    public static long powMod(long a, long n, long mod) {
        long ret = 1;
        long temp = a % mod;
        while (n != 0) {
            if ((n & 1) != 0)
                ret = ret * temp % mod;
            temp = temp * temp % mod;
            n >>= 1;
        }
        return ret;
    }

    static class Term {
        int base;
        int exp;

        Term(int base, int exp) {
            this.base = base;
            this.exp = exp;
        }
    }

    private static long calculateG(ArrayList<Term> factors, int j) {
        long res = 1;
        for (Term curr : factors) {
            int foo = (curr.exp + j - 1) / j; //ceil (exp/j)
            for (int i = 0; i < foo; ++i)
                res *= curr.base;
        }
        return res;
    }

    public static void main(String[] args) throws Exception {
        for (int caseNum = nextInt(); caseNum-- != 0; ) {
            int p = nextInt();
            int n = nextInt();
            int m = nextInt();
            if (p == 2 || p == 5) {
                out.println(0);
                continue;
            }
            int exponent;
            //9p不是质数,但p一定是质数,尽量改为mod p 而不是 mod 9p更方便计算,不然还需要计算欧拉函数
            //这样的话  由于原本有个9,必须排除和9不互质的p (3)
            if (p == 3) {
                //和9不互质,没办法,只能老实的算欧拉函数,而不能用p-1
                p = p * 9;
                exponent = 18;  // phi(27)
            } else exponent = p - 1;
            int recurring = exponent;
            //for all factors i
            for (int i = 1; i * i <= exponent; ++i) {
                if (exponent % i != 0)
                    continue;
                if (powMod(10, i, p) == 1)
                    recurring = Math.min(recurring, i);
                if (powMod(10, exponent / i, p) == 1)
                    recurring = Math.min(recurring, exponent / i);
            }
            ArrayList<Term> factors = getFactors(recurring);
            long ans = 0;
            for (int j = 1; j <= m && j <= 30; ++j)
                ans += n / calculateG(factors, j);
            if (m > 30)
                ans += ((long) (m - 30)) * (n / calculateG(factors, 30));
            out.println(ans);
            out.flush();
        }
        out.flush();
    }

    private static ArrayList<Term> getFactors(int recurring) {
        ArrayList<Term> factors = new ArrayList<>();
        for (int base = 2; base * base <= recurring; ++base) {
            //is not a factor of base
            if (recurring % base != 0)
                continue;
            int exp = 0;
            while (recurring % base == 0) {
                ++exp;
                recurring /= base;
            }
            factors.add(new Term(base, exp));
        }
        if (recurring != 1)
            factors.add(new Term(recurring, 1));
        return factors;
    }
}

你可能感兴趣的:(ACM)