CodeForces 507D The Maths Lecture constructs EXACTLY n digits and there is a suffix that can be divisible by k dp, codeforces507d

Source: Internet
Author: User
Tags integer numbers

CodeForces 507D The Maths Lecture constructs EXACTLY n digits and there is a suffix that can be divisible by k dp, codeforces507d

Question link: Click the open link

D. The Maths Lecturetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Amr doesn't like Maths as he finds it really boring, so he usually sleeps in Maths lectures. but one day the teacher suspected that Amr is sleeping and asked him a question to make sure he wasn' t.

First he gave Amr two positive integersNAndK. Then he asked Amr, how many integer numbersXLatency> limit 0 exist such that:

  • Decimal representationX(Without leading zeroes) consists of exactlyNDigits;
  • There exists some integerYLatency> limit 0 such that:
    • ;
    • Decimal representationYIs a suffix of decimal representationX.

As the answer to this question may be pretty huge the teacher asked Amr to output only its remainder modulo a numberM.

Can you help Amr escape this embarrassing situation?

Input

Input consists of three integersN, Bytes,K, Bytes,M(1 digit ≤ DigitNLimit ≤ limit 1000, 1 limit ≤ limitKLimit ≤ limit 100, 1 limit ≤ limitMLimit ≤ limit 109 ).

Output

Print the required number moduloM.

Sample test (s) input
1 2 1000
Output
4
Input
2 2 1000
Output
45
Input
5 3 1103
Output
590
Note

A suffix of a stringSIs a non-empty string that can be obtained by removing some number (possibly, zero) of first characters fromS.

Question:

Given n, k, m

The number of integers EXACTLY n digits. A suffix of this integer can be divisible by k. The answer is m.

Ideas:

Because it is a suffix, it is constructed from low to high.

Dp [I] [j] [0] indicates an integer with an I-bit length. The result of mod k is j, and there is no number of suffixes that can divide k.

Dp [I] [j] [1] indicates an integer with an I-bit length. The result of mod k is j, and there is a number of suffixes that can divide k.

Then, add a number before a certain State to determine whether the number can be divisible by k.

#include <cstdio>#include <algorithm>#include <string.h>#include <queue>#include <cstring>#include <cmath>#include <iostream>#include <vector>using namespace std;#define y1 Y1typedef long long ll;const int N = 1005;ll ten[N], dp[N][100][2];ll n, k, m;void add(ll &x, ll y){x += y;if (x >= m)x -= m;}int main(){while (cin >> n >> k >> m){ten[0] = 1 % m;for (int i = 1; i < N; i++)ten[i] = (ten[i - 1] * 10LL) % k;memset(dp, 0, sizeof dp);dp[0][0][0] = 1;for (int i = 0; i < n; i++){for (int j = 0; j < k; j++){for (int z = 0; z < 10; z++){if (i == n - 1 && z == 0)continue;ll now = (z*ten[i] + j) % k;if (now == 0 && z){add(dp[i + 1][now][1], dp[i][j][0]);}elseadd(dp[i + 1][now][0], dp[i][j][0]);add(dp[i + 1][now][1], dp[i][j][1]);}}}ll ans = 0;for (int i = 0; i < k; i++)ans += dp[n][i][1];cout << ans%m << endl;}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.