• UVA 11361


    An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is divisible
    by 3 and 12(3+7+0+2) is also divisible by 3. This property also holds for the integer 9.
    In this problem, we will investigate this property for other integers.
    Input
    The first line of input is an integer T (T < 100) that indicates the number of test cases. Each case is
    a line containing 3 positive integers A, B and K. 1 ≤ A ≤ B < 2
    31 and 0 < K < 10000.
    Output
    For each case, output the number of integers in the range [A, B] which is divisible by K and the sum
    of its digits is also divisible by K.
    Sample Input
    3
    1 20 1
    1 20 2
    1 1000 4
    Sample Output
    20
    5
    64

    题意:给出a,b,k,问说在[a,b]这个区间有多少n,满足n整除k,以及n的各个为上的数字之和也整除k。

    题解:dp[i][j][k] 表示  i位  j=数%K,k=位数和%K

    //meek///#include<bits/stdc++.h>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include<iostream>
    #include<bitset>
    #include<vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <stack>
    using namespace std ;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    #define fi first
    #define se second
    #define MP make_pair
    typedef long long ll;
    
    const int N = 100+100;
    const int M = 1000001;
    const int inf = 0x3f3f3f3f;
    const ll MOD = 1000000000;
    
    
    ll a,b,k,len;
    ll vis[100][N][N],dp[100][N][N],d[100];
    void init(int n) {
        len = 1;
        mem(d);
        while(n) d[len++] = n%10,n /= 10;
    
         for(int i = 1;i <= len/2; i++)
            swap(d[i],d[len-i+1]);
    }
    ll solve(ll n) {
        if(n == 0) return 1;
        init(n);
        mem(dp);
        int  p = 0, q = 0;
    
        for(int i=1;i<=len;i++) {
    
            for(int j=0;j<=k;j++)
                for(int t = 0;t <= k; t++) {
                    for(int x = 0;x < 10; x++) {
                        dp[i][(j*10+x)%k][(t+x)%k] += dp[i-1][j][t];
                    }
                }
    
            for(int j = 0; j < d[i]; j++)
                dp[i][(p*10+j)%k][(q+j)%k]++;
    
            p = (p*10+d[i])%k;
            q = (q+d[i])%k;
        }
        if(p == 0 && q == 0) dp[len][0][0]++;
        return dp[len][0][0];
    }
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
            scanf("%lld%lld%lld",&a,&b,&k);
            if(k>100) printf("0
    ");
            else 
            printf("%lld
    ",solve(b)-solve(a-1));
        }
        return 0;
    }
    bear
    //meek///#include<bits/stdc++.h>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include<iostream>
    #include<bitset>
    #include<vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <stack>
    using namespace std ;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    #define fi first
    #define se second
    #define MP make_pair
    typedef long long ll;
    
    const int N = 100+100;
    const int M = 1000001;
    const int inf = 0x3f3f3f3f;
    const ll MOD = 1000000000;
    
    
    ll a,b,k;
    ll vis[100][N][N],dp[100][N][N],d[100];
    ll dfs(int dep,int f,int sum,int P) {
        if(dep<0) return sum%k==0&&P%k==0;
        if(f&&vis[dep][sum][P]) return  dp[dep][sum][P];
        if(f) {
            ll& ret = dp[dep][sum][P];
            vis[dep][sum][P] = 1;
            for(int i=0;i<=9;i++) {
                ret += dfs(dep-1,f,(sum*10+i)%k,P+i);
            }
        return ret;
        }
        else {
            ll ret = 0;
            for(int i=0;i<=d[dep];i++) {
                ret +=dfs(dep-1,i<d[dep],(sum*10+i)%k,P+i);
            }
            return ret;
        }
    }
    ll solve(int n) {
        mem(vis),mem(dp);
        int len = 0;
        while(n) d[len++] = n%10,n /= 10;
        return dfs(len-1,0,0,0);
    }
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
            scanf("%lld%lld%lld",&a,&b,&k);
            printf("%lld
    ",solve(b)-solve(a-1));
        }
        return 0;
    }
    meek
  • 相关阅读:
    spring jdbcTemplate使用queryForList示例
    凡事预则立,不立则废:好计划是成功的开始——布利斯定理
    传世智库:初识IPD-集成产品开发
    骑手送外卖获奖1500多万元又被撤销 法律专家:不能一扣了之
    leetcode-----129. 求根到叶子节点数字之和
    leetcode-----128. 最长连续序列
    leetcode-----127. 单词接龙
    leetcode-----126. 单词接龙 II
    leetcode-----125. 验证回文串
    leetcode-----124. 二叉树中的最大路径和
  • 原文地址:https://www.cnblogs.com/zxhl/p/5085364.html
Copyright © 2020-2023  润新知