【Foreign】Number [状压DP]

Number

Time Limit: 10 Sec  Memory Limit: 256 MB

Description

  

Input

  

Output

  

Sample Input

  12345 5

Sample Output

  24

HINT

  

Solution

  我们运用状压DP,令 f[j][opt] 表示当前余数为 j,状态为opt的方案

  状态记录的是:各个数字被用了几次。

  那么我们就可以状压了。先DFS出每个状态,记sum[k]表示后缀积,那么显然 从 opt 转移到 第k个数字多用一次的状态 就是 opt + sum[k + 1]

  注意判断一下首位不能为0

Code

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cmath>
 8 #include<vector>
 9 #include<queue>
10 using namespace std;
11 typedef long long s64;
12
13 const int ONE = 500005;
14 const int MOD = 998244353;
15
16 int n, m;
17 int num;
18 int vis[ONE], Num[20], sum[20];
19 int f[105][200005];
20 int Sta[ONE][10];
21 char ch[ONE];
22
23 int get()
24 {
25         int res=1,Q=1;char c;
26         while( (c=getchar())<48 || c>57 )
27         if(c==‘-‘)Q=-1;
28         res=c-48;
29         while( (c=getchar())>=48 && c<=57 )
30         res=res*10+c-48;
31         return res*Q;
32 }
33
34 void Dfs(int T)
35 {
36         if(T > 10)
37         {
38             num++;
39             for(int i = 0; i <= 9; i++)
40                 Sta[num][i] = vis[i];
41             return;
42         }
43
44         for(int i = 0; i <= Num[T]; i++)
45             vis[T] = i, Dfs(T + 1);
46 }
47
48 int main()
49 {
50         scanf("%s", ch + 1);    m = get();
51         n = strlen(ch + 1);
52
53         for(int i = 1; i <= n; i++) Num[ch[i] - ‘0‘]++;
54         sum[10] = 1; for(int i = 9; i >= 0; i--) sum[i] = sum[i + 1] * (Num[i] + 1);
55
56         Dfs(0);
57
58         f[0][1] = 1;
59         for(int opt = 1; opt <= num; opt++)
60             for(int j = 0; j < m; j++)
61                 if(f[j][opt])
62                     for(int k = 0; k <= 9; k++)
63                     {
64                         if(opt == 1 && k == 0) continue;
65                         if(Sta[opt][k] >= Num[k]) continue;
66                         int to = opt + sum[k + 1];
67                         f[(j * 10 + k) % m][to] += f[j][opt];
68                         f[(j * 10 + k) % m][to] %= MOD;
69                     }
70
71         printf("%d", f[0][num]);
72 }

时间: 2024-08-27 05:10:29

【Foreign】Number [状压DP]的相关文章

poj 2411 Mondriaan&#39;s Dream(状压DP)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12232   Accepted: 7142 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

HDU5816 Hearthstone(状压DP)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5816 Description Hearthstone is an online collectible card game from Blizzard Entertainment. Strategies and luck are the most important factors in this game. When you suffer a desperate situation an

COdeforces#417D Cunning Gena(状压DP)

A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him. The participants

UVA 10817 Headmaster&#39;s Headache 状压DP

记录两个状态S1,S2分别记录哪些课程被1个人教过或2个人教过,然后记忆化搜索 UVA - 10817 Headmaster's Headache Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem D: Headmaster's Headache Time limit: 2 seconds The headmaster of Spr

状压DP [Uva 11825] Hackers’ Crackdown

Hackers’ Crackdown  Input: Standard Input Output: Standard Output   Miracle Corporations has a number of system services running in a distributed computer system which is a prime target for hackers. The system is basically a set of N computer nodes w

POJ 题目2411 Mondriaan&#39;s Dream(状压DP)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13519   Accepted: 7876 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

【NOI2015】寿司晚会 题解(状压DP)

[问题描述] 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n?1 种不同的寿司,编号 1,2,3,?, n?1, 其中第 i 种寿司的美味度为 i+1  (即寿司的美味度为从 2 到 n ). 现在小 G 和小 W 希望每人选一些寿司种类来品尝,他们规定一种品尝方案 为不和谐的当且仅当:小 G 品尝的寿司种类中存在一种美味度为 x 的寿司,小 W 品尝的寿司中存在一种美味度为 y

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

状压DP HDU3538 A sample Hamilton path

A sample Hamilton path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 527    Accepted Submission(s): 213 Problem Description Give you a Graph,you have to start at the city with ID zero. Input T