507D The Maths Lecture

D. The Maths Lecture
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 integers n and k. Then he asked Amr, how many integer numbers x > 0 exist such that:

  • Decimal representation of x (without leading zeroes) consists of exactly n digits;
  • There exists some integer y > 0 such that:
    • ;
    • decimal representation of y is a suffix of decimal representation of x.

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

Can you help Amr escape this embarrassing situation?

Input

Input consists of three integers n, k, m (1 ≤ n ≤ 1000, 1 ≤ k ≤ 100, 1 ≤ m ≤ 109).

Output

Print the required number modulo m.

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 string S is a non-empty string that can be obtained by removing some number (possibly, zero) of first characters from S.

以前自己做过一小段时间的数位DP,但掌握不精。。今天碰到一道,希望弄透,以后写同类题解可能就不详细解释了。

题意:问你n位数,任意尾缀可以被k整除就符合,问给你n,k,m(取模用)你能弄出多少个数字

思路:数据范围,数位DP

代码1(便于理解的DP): dp[i][j][0]代表i位数,被k取余余下j,不存在某个后缀能整除k的个数, dp[i][j][1]代表i位数,被k取余余下j,存在某个后缀能整除k的个数

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

typedef long long ll;
const int N = 1005;
ll dp[N][100][2];
ll n, k, m, ten;

int main()
{
    while (~scanf("%d%d%d",&n,&k,&m))
    {
        ten = 1;
        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++)//z代表往前面添加的数字
                {
                    if (i == n - 1 && z == 0)continue;
                    ll now = (z*ten + j) % k;

                    if (now == 0 && z)       //本不符合当添加后符合,前面加1~9都OK
                        dp[i + 1][now][1] += dp[i][j][0]%m;
                    else                            //前面添加0做下一位计算用(算在不符合中),还有添加后还是不符合
                        dp[i + 1][now][0] += dp[i][j][0]%m;

                    dp[i + 1][now][1] += dp[i][j][1]%m;   //后面尾缀符合了前面就随意添加
                }
            }
            ten = ten * 10LL %k;//ten代表往前添加数字时候要加上z*ten,比如98前加个2就是加上2*100

       }
        ll ans = 0;
        for (int i = 0; i < k; i++)
            ans += dp[n][i][1];
        printf("%d\n",ans%m);
    }
    return 0;
}

 

代码2:这种代码我以前见得比较多,简单巧妙,这里利用了数的范围只会缩小的特点dfs。

#include<stdio.h>
#include<string.h>
int dp[1005][105][2],shi[1005];
int k,MOD,n;
int dfs(int pos,int preMOD,int flag)//pos位数,preMOD前面的余数,flag=1符合,flag=0不符合
{
    int tmp,i,ans=0;
    if (pos>n) return flag;
    if (dp[pos][preMOD][flag]!=-1)
        return dp[pos][preMOD][flag];
    tmp=(pos==n?1:0);     //看是否到最后一位,不然前面不能加0
    for (i=tmp; i<=9; i++)
        ans=(ans+dfs( pos+1, (preMOD+i*shi[pos-1])%k, flag||((preMOD+i*shi[pos-1])%k==0&&i!=0) ))%MOD;
    return dp[pos][preMOD][flag]=ans;
}
int main()
{
    int cnt,i;
    scanf("%d%d%d",&n,&k,&MOD);
    shi[0]=1;
    for (i=1; i<=1000; i++) shi[i]=shi[i-1]*10%k;//这里跟代码1的ten作用相同
    memset(dp,-1,sizeof(dp));
    printf("%d\n",dfs(1,0,0));
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
data = pd.read_csv("data.csv") data.replace("M",1,inplace=True) data.replace("B",0,inplace=True) #获取特征x和特征y X = data.iloc[:, 3:5].values x = np.array(X) y = data.diagnosis y = np.array(y) #创建决策树算法对象 tree_clf = DecisionTreeClassifier(max_depth=2) #构建决策树 tree_clf.fit(x,y) #绘制决策树结构 tree.plot_tree(tree_clf) from matplotlib.colors import ListedColormap plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams["axes.unicode_minus"] = False #定义绘制决策树边界的函数 def plot_decision_boundary(clf, X, y, axes=[0, 10 , 0 , 5], data=True, legend=False, plot_training=True): x1s = np.linspace(axes[0], axes[1], 100) x2s = np.linspace(axes[2], axes[3], 100) x1, x2 = np.meshgrid(x1s, x2s) X_new = np.c_[x1.ravel(), x2.ravel()] y_pred = clf.predict(X_new).reshape(x1.shape) custom_cmap = ListedColormap(['#fafab0', '#0909ff', '#a0faa0']) plt.contourf(x1, x2, y_pred, alpha=0.3, cmap=custom_cmap) if not data: custom_cmap2 = ListedColormap(['#7d7d58', '#4c4c7f', '#507d50']) plt.contour(x1, x2, y_pred, cmap=custom_cmap2, alpha=0.8) if plot_training: plt.plot(X[:, 0][y == 0], X[:, 1][y == 0], "yo", label="0") plt.plot(X[:, 0][y == 1], X[:, 1][y == 1],"bs", label="1") plt.axis(axes) if data: plt.xlabel("属性",fontsize=14) plt.ylabel("特征",fontsize=14) else: plt.xlabel(r"$x_1$", fontsize=18) plt.xlabel(r"$x_2$", fontsize=18,rotation=0) if legend: plt.legend(loc="lower right", fontsize=14) tree_clf1 = DecisionTreeClassifier(random_state=42) tree_clf2 = DecisionTreeClassifier(min_samples_leaf=4,random_state=43) tree_clf1.fit(x,y) tree_clf2.fit(x,y) plt.figure(figsize=(15,6)) plt.subplot(121) plot_decision_boundary(tree_clf1, x, y, axes=[0, 40, 50, 150], data=False) plt.title('圖一') plt.subplot(122) plot_decision_boundary(tree_clf2, x, y, axes=[0, 40, 50, 150], data=False) plt.title('圖二')
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值