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

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

Problem Description

在这里插入图片描述
在这里插入图片描述

Input Sample

2
11 8 1
7 6 2

Output Sample

4
2

Solution:

在这里插入图片描述
在这里插入图片描述

由于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;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值