CF1808E3.Minibuses on Venus (hard version)

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The only difference between the three versions is the constraints on nn and kk . You can make hacks only if all versions of the problem are solved.

Maxim is a minibus driver on Venus.

To ride on Maxim's minibus, you need a ticket. Each ticket has a number consisting of nn digits. However, as we know, the residents of Venus use a numeral system with base kk , rather than the decimal system. Therefore, the ticket number can be considered as a sequence of nn integers from 00 to k1k-1 , inclusive.

The residents of Venus consider a ticket to be lucky if there is a digit on it that is equal to the sum of the remaining digits, modulo kk . For example, if k=10k=10 , then the ticket 71357135 is lucky because 7+1+53(mod10)7 + 1 + 5 \equiv 3 \pmod{10} . On the other hand, the ticket 71367136 is not lucky because no digit is equal to the sum of the others modulo 1010 .

Once, while on a trip, Maxim wondered: how many lucky tickets exist? At the same time, Maxim understands that this number can be very large, so he is interested only in the answer modulo some prime number mm .

输入格式

The only line of the input contains three integers nn , kk and mm ( 1n10181 \le n \le 10^{18} , 1k20001 \le k \le 2000 , 108m109+710^8 \le m \le 10^9 + 7 , mm is a prime number) — the number of digits on the ticket, the base of the numeral system on Venus, and the module for answer calculation.

输出格式

Print one integer — the number of lucky tickets modulo mm , i. e. the remainder after dividing the answer by mm .

输入输出样例

  • 输入#1

    3 2 1000000007

    输出#1

    4
  • 输入#2

    3 4 1000000007

    输出#2

    28

说明/提示

In the first example, there are only four lucky tickets: 000000 , 011011 , 101101 , and 110110 .

首页