[数位dp] Codeforces 401D Roman and Numbers

博客介绍了如何使用数位动态规划(dp)方法解决Codeforces上的一道数学问题,该问题要求找出所有通过重新排列给定数n的数字且不带前导零,并能被m整除的数的数量。博主提供了详细的解题思路和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

http://codeforces.com/problemset/problem/401/D

D. Roman and Numbers
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.

Number x is considered close to number n modulo m, if:

  • it can be obtained by rearranging the digits of number n,
  • it doesn't have any leading zeroes,
  • the remainder after dividing number x by m equals 0.

Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.

Input

The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100).

Output

In a single line print a single integer — the number of numbers close to number n modulo m.

Sample test(s)
input
104 2
output
3
input
223 4
output
1
input
7067678 8
output
47
Note

In the first sample the required numbers are: 104, 140, 410.

In the second sample the required number is 232.


题目意思:

给一个n(n<=10^18),m(m<=100),求把n中各位数字重排后组成的数(不能又前置零)能被m整除的个数。

解题思路:

数位dp

dp[s][m]表示当剩下的数组成的状态为s,已用的数字组成的余数为m时,能组成满足要求的种数。

最每一个剩下 的数枚举,去掉即可。注意每一位可能有相同的数,只需要一个。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

ll dp[1<<19][101]; //dp[s][m]表示还剩下状态为s的数,前面的余数为m时,满足要求的种数
//这样可以避免前面余数为m的情况的重复计算
ll n,m;
int cnt,va[20];

void init(ll cur)
{
    cnt=0;
    while(cur)
    {
        va[cnt++]=cur%10;
        cur/=10;
    }
}
ll dfs(ll cur,ll fi)
{
    if(!cur) //每一位已全部搞定
    {
        if(!fi)
            return 1;
        return 0;
    }
    if(dp[cur][fi]!=-1) //之前已经求,现在不需要求了
        return dp[cur][fi];

    int num[10];
    memset(num,0,sizeof(num));
    ll res=0;

    for(int i=0;i<cnt;i++)
    {
        if(cur&(1<<i)) //第i个数还在里面
        {
            if(num[va[i]])
                continue;
            num[va[i]]=1;
            res+=dfs(cur^(1<<i),(fi*10+va[i])%m); //把第i个数放进去
        }
    }
    return dp[cur][fi]=res; //保存
}

int main()
{
    //printf("%d\n",1<<18);
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   while(~scanf("%I64d%I64d",&n,&m))
   {
       init(n);
       memset(dp,-1,sizeof(dp));

       int num[10];
       memset(num,0,sizeof(num));
       int lim=(1<<cnt)-1;
       ll ans=0;

       for(int i=0;i<cnt;i++)
       {
           if(num[va[i]]||!va[i]) //注意首位不为零
                continue;
           num[va[i]]=1;
           ans+=dfs(lim^(1<<i),va[i]%m);
       }
       printf("%I64d\n",ans);

   }
   return 0;
}



### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值