UVALive - 6529 找规律+dp
题目链接:
http://acm.hust.edu.cn/vjudge/problem/47664
Eleven
Time Limit: 5000MS
#### 问题描述
> In this problem, we refer to the digits of a positive integer as the sequence of digits required to write
> it in base 10 without leading zeros. For instance, the digits of N = 2090 are of course 2, 0, 9 and 0.
> Let N be a positive integer. We call a positive integer M an eleven-multiple-anagram of N if and
> only if (1) the digits of M are a permutation of the digits of N, and (2) M is a multiple of 11. You are
> required to write a program that given N, calculates the number of its eleven-multiple-anagrams.
> As an example, consider again N = 2090. The values that meet the first condition above are 2009,
> 2090, 2900, 9002, 9020 and 9200. Among those, only 2090 and 9020 satisfy the second condition, so
> the answer for N = 2090 is 2.
输入
The input file contains several test cases, each of them as described below.
A single line that contains an integer N (1 ≤ N ≤ 10100).
输出
For each test case, output a line with an integer representing the number of eleven-multiple-anagrams
of N . Because this number can be very large, you are required to output the remainder of dividing it
by 109 + 7.
样例
sample input
2090
16510
201400000000000000000000000000sample output
2
12
0
题意
给你一串数,求由这些数排列组合成的能被11整除的数的个数。
题解
能被11整除的数有一个特点,(偶数位的和-奇数位的和)%11=0;
dp[i][j][k] (0<=i-1<=9) 表示统计到i-1的时候,偶数位已经有j个数时,(y-x)%11==k(y代表偶数位和,x代表奇数位和)的情况数。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn = 111;
const int maxm = 15;
const int mod = 1e9 + 7;
char str[maxn];
int cntv[maxm];
int n;
LL C[maxn][maxn];
LL dp[maxm][maxn][maxm];
LL solve(int n) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
int sum = 0,need=n/2;
for (int i = 1; i <= 10; i++) {
for (int j = 0; j <= sum&&j <= need; j++) {
for (int dj = 0; dj <= cntv[i - 1] && (j + dj) <= need; dj++) {
int dx = (dj - (cntv[i - 1] - dj))*(i-1)%11;
dx = (dx%11 + 11) % 11;
for (int k = 0; k < 11; k++) {
int nk = (k + dx) % 11;
dp[i][j + dj][nk] = (dp[i][j + dj][nk]+dp[i - 1][j][k]*C[need-j][dj]%mod*C[n-need-(sum-j)][cntv[i-1]-dj]%mod)%mod;
}
}
}
sum += cntv[i - 1];
}
return dp[10][need][0];
}
void pre() {
memset(C, 0, sizeof(C));
C[0][0] = 1;
for (int i = 1; i < maxn; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) {
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
}
}
void init() {
memset(cntv, 0, sizeof(cntv));
}
int main() {
pre();
while (scanf("%s", str) == 1) {
init();
n = strlen(str);
for (int i = 0; i < n; i++) {
cntv[str[i] - '0']++;
}
LL ans = solve(n);
if (cntv[0]) {
//扣掉第一位为0的情况
cntv[0]--;
ans -= solve(n - 1);
}
ans = (ans%mod + mod) % mod;
printf("%lld\n", ans);
}
return 0;
}
Notes
确定阶段的划分很重要!比如这题,用不同的数字作为阶段性,非常典型。
往往找到了阶段性,对状态转移方程的设计也会更明确。
UVALive - 6529 找规律+dp的更多相关文章
- Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)
题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...
- UVALive - 3722 找规律
题意:找规律 题解:找规律 结论是\(a^n(x-1)-\sum_{i=1}^{n-1}a^i \mod\ c\) #include<iostream> #include<algor ...
- 2015 acm taipei L-Reward the Troop(uvalive 7465)(找规律)
原题链接 就大概说的是一个将军要给部下发勋章,他的部下以和别人不一样的勋章为荣,但是他没这么多钱,所以问你最少要多少钱 要求是每个人的上司是他的上两级,他的下两级是他的部下,每个人的勋章不能和他的上司 ...
- hdu 4455 Substrings(找规律&DP)
Substrings Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- LOJ 2550 「JSOI2018」机器人——找规律+DP
题目:https://loj.ac/problem/2550 只会写20分的搜索…… #include<cstdio> #include<cstring> #include&l ...
- SPOJ GNYR09F 数字上的找规律DP
Problem C SPOJ GNYR09F dp题,dp可能刚刚开始对大家来说比较难,但是静下心来分析还是比较简单的: dp(i ,j ,k)表示前i个数中,当前累积和为j,末尾数字为k的 ...
- UVALive 6529 Eleven 区间dp
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4540">点击打开链接 题意: ...
- 找规律 UVALive 6506 Padovan Sequence
题目传送门 /* 找规律:看看前10项就能看出规律,打个表就行了.被lld坑了一次:( */ #include <cstdio> #include <algorithm> #i ...
- 找规律/数位DP HDOJ 4722 Good Numbers
题目传送门 /* 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () http://www.cnblogs.com/crazyapple/p/3315436.html 数 ...
随机推荐
- Cassandra 有限分页策略
瀑布式分页 如果你的应用只需要瀑布式的分页,那么,Cassandra可以很好的支持,不过记得要指定好排序顺序. CLUSTERING ORDER BY (add_time DESC); 常见的分页,跳 ...
- 安装mysql 5.7 最完整版教程
Step1: 检测系统是否自带安装mysql #yum list installed | grep mysql Step2: 删除系统自带的mysql及其依赖 命令: yum remove mysql ...
- 射击比赛 (POJ 1719) 题解
[问题描述] 我们假设射击的目标是一个由R*C(2≤R≤C≤ 1000)个小方格组成的矩形网格.网格中每一列恰有2个白色的小方格和R-2个黑色的小方格.定义网格的行从顶至底编号为1~R,列从左至右编号 ...
- Python之mongodb操作
1.安装驱动pymongo 输入命令:pip install pymongo 2.直接使用驱动 #encoding=utf-8 from pymongo import MongoClient clie ...
- SQL Server 锁表、查询被锁表、解锁相关语句
SQL Server 锁表.查询被锁表.解锁相关语句,供参考. --锁表(其它事务不能读.更新.删除) BEGIN TRAN SELECT * FROM <表名> WITH(TABLOCK ...
- django-url调度器-中级篇
在初级篇中,我们接触了: 1.url 的简单编写 2.两种传参的方式 3.捕获的参数总是字符串 4.为视图设置默认参数 …… 在中级篇中将更进一步. 包含其它的URLconfs 当网站非常大的时候,将 ...
- CMD怎样建立文件?
一.建立空文件的几种方法1.cd.>a.txtcd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出.>表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.txt. ...
- Ztack学习笔记(4)-系统网络分析
协调器的组网,终端设备和路由设备发现网络以及加入网络 //第一步:Z-Stack 由 main()函数开始执行,main()函数共做了 2 件事:一是系统初始化,另外一件是开始执行轮转查询式操作系统 ...
- 【微信平台&后台管理】第一个外包项目:XX科技城微信平台项目总结
苍天有眼啊,学了半年的网站开发终于派上用处,终于能赚钱了啊. 这个项目是和学长一起做的,项目的甲方是大庆某房地产土豪,项目要求就是搭建一整套的微信平台和微信平台管理系统,具体要求就是:回复关键字能拿到 ...
- WPF工作笔记:本地化支持、主进程通知、两种最常用异步编程方式
1.本地化支持 (1)重写控件默认的依赖属性LanguageProperty FrameworkElement.LanguageProperty.OverrideMetadata( typeof(Fr ...