zoukankan      html  css  js  c++  java
  • 2018上海大都会 J-Beautiful Numbers(数位dp-模未知)

    J-Beautiful Numbers

    链接:https://www.nowcoder.com/acm/contest/163/J

    来源:牛客网

    时间限制:C/C++ 8秒,其他语言16秒
    空间限制:C/C++ 262144K,其他语言524288K
    64bit IO Format: %lld

    题目描述

    NIBGNAUK is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by the sum of its digits.
    We will not argue with this and just count the quantity of beautiful numbers from 1 to N.

    输入描述:

    The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
    Each test case contains a line with a positive integer N (1 ≤ N ≤ 10
    12
    ).

    输出描述:

    For each test case, print the case number and the quantity of beautiful numbers in [1, N].
    示例1

    输入

    复制
    2
    10
    18

    输出

    复制
    Case 1: 10
    Case 2: 12



    对于模未知的数位dp,暴力枚举模即可。


    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<queue>
    #include<stack>
    #include<string>
    #include<map>
    #include<set>
    #include<vector>
    #include<algorithm>
    #define MAX 13
    #define INF 0x3f3f3f3f
    using namespace std;
     
    typedef long long ll;
     
    int a[MAX];
    int poss,MOD;
    ll dp[MAX][110][110][110];
    ll dfs(int pos,ll sta,int sum,bool limit){
        int i;
        if(pos==-1) return sum==MOD&&sta==0;
        if(!limit&&dp[pos][sta][sum][MOD]!=-1) return dp[pos][sta][sum][MOD];
        int up=limit?a[pos]:9;
        ll cnt=0;
        for(i=0;i<=up;i++){
            cnt+=dfs(pos-1,(sta*10+i)%MOD,sum+i,limit&&i==a[pos]);
        }
        if(!limit) dp[pos][sta][sum][MOD]=cnt;
        return cnt;
    }
    ll solve(ll x){
        int pos=0;
        while(x){
            a[pos++]=x%10;
            x/=10;
        }
        poss=pos-1;
        ll ans=0;
        for(MOD=1;MOD<=108;MOD++){
            ans+=dfs(pos-1,0,0,true);
        }
        return ans;
    }
    int main()
    {
        int t,tt=0;
        ll r;
        scanf("%d",&t);
        memset(dp,-1,sizeof(dp));
        while(t--){
            scanf("%lld",&r);
            printf("Case %d: %lld
    ",++tt,solve(r));
        }
        return 0;
    }
  • 相关阅读:
    POJ 2987:Firing(最大权闭合图)
    BZOJ 1001:[BeiJing2006]狼抓兔子(最小割)
    HDU 1007:Quoit Design(分治求最近点对)
    POJ 1986:Distance Queries(倍增求LCA)
    HDU 3879 && BZOJ 1497:Base Station && 最大获利 (最大权闭合图)
    BZOJ-1011 遥远的行星
    BZOJ-1044 木棍分割
    BZOJ-1042 硬币购物
    BZOJ-1050 旅行
    BZOJ-1037 生日聚会
  • 原文地址:https://www.cnblogs.com/yzm10/p/9425571.html
Copyright © 2011-2022 走看看