Codefores 507D The Maths Lecture( 数位DP )

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.

 

题意是统计n位数x , 它有一个后缀y,能够满足( y%k==0)的 x 的个数.我做法是开一个3维数组 , dp[i][j][y] 。 表示符合前 i 位 , 余数是j, 是否有前导 0 的数的个数。

首先要预处理了 i*j^10 % k 的结果,( i = 1~9 , j = 1~n )用于对新的状态的转移。

预处理好排列数 10^j % m ,当出现后缀y符合条件且没前导0 ,可进行计算。

剩下就是状态转移了 。

转移的过程中 无前导0 且 余数等于0 的可以进行一次计数。

否则就继续进行转移就OK 。

代码写得比较恶心...然后要注意处理好边界就没什么问题了。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1010 ;
const int M = 110 ;
LL dp[N][M][2] , n , m , k , cnt[N] , rest[10][N];

void init() {
    cnt[0] = 1 ;
    for( int i = 1 ; i <= n ; ++i ) {
        cnt[i] = cnt[i-1] * 10 % m ;
    }
    for( int j = 1 ; j <= n ; ++j ) {
        for( int i = 1 ; i < 10 ; ++i ) {
            if( j == 1 ) rest[i][j] = i % k ;
            else rest[i][j] = ( 10 % k * rest[i][j-1] ) % k ;
//            cout << i << ' ' << j << ' ' << rest[i][j] << endl ;
        }
    }
    memset( dp , 0 , sizeof dp );
    for( int i = 0 ; i < 10 ; ++i ) dp[1][i%k][!i]++;
//    cout << "Run" << endl ;
}

void Run() {
    LL ans = 0 ;
    if( n == 1 ) {
        for( int i = 1 ; i < 10 ; ++i ) if( i % k == 0 ) ans ++ , ans %= m;
    }
    else {
        for( int i = 1 ; i <= n ; ++i ) {
            for( int j = 0 ; j < k ; ++j ) {
                for( int y = 0 ; y < 2 ; ++y ) {
                    if( !y && !j ) {             // no leading zero and reminder equal zero
                        if( i < n ) ans = ( ans + 9 * cnt[n-i-1] % m * dp[i][j][y] ) % m ;
                        else ans += dp[i][j][y] , ans %= m ;
                    }
                    else {                      // leading zero or reminder not equal zero
                        for( int z = 0 ; z < 10 ; ++z ) {
                            int i1 = i + 1 , j1 = ( rest[z][i1] + j ) % k ;
                            dp[i1][j1][!z] += dp[i][j][y] , dp[i1][j1][!z] %= m ;
                        }
                    }
                }
            }
        }
    }
    cout << ans << endl ;
}
int main()
{
//    freopen("in.txt","r",stdin);
    while( cin >> n >> k >> m )
        init() , Run();
}
View Code

 

转载于:https://www.cnblogs.com/hlmark/p/4246708.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值