UVA 12672 Eleven(DP)
12672 - Eleven
Time limit: 5.000 seconds
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.
Input
The input file contains several test cases, each of them as described below.
A single line that contains an integer N (1 ≤ N ≤ 10^100).
Output
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
201400000000000000000000000000
Sample Output
2
12
0
一条很好的DP题。
问一个数重排后(不包括前序0) , 有多少个数能够被整除11。
对于11的倍数,可以发现一个规律就是:
( 奇数位数字的总和 - 偶数为数字的总和 )% 11 == 0的数能够被11整除
因为作为11的倍数,都符合:
77000 85481是可以被11整除的。
7700 因为它各个相邻位都有一个相等的性质。
770 对于奇数位要进位的话,相当于少加了10(即-10) , 同时偶数为多了1(即-1) ,还是符合被11整除
11 偶数为亦然 , 偶数为进位, 相当于少减10 ,(即+10) , 同时奇数位多了1(即+1)。
------------
85481
那么,设一个 dp[i][j][k] 表示用了i位(0~9)数字,奇数位有j个数,余数是k的组合成的数有多少个。
#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int mod = 1e9+;
const int N = ;
const int M = ; LL cnt[M] , dp[M][N][M] , C[N][N];
string s; void Init() {
C[][] = ;
for( int i = ; i < N ; ++i ){
for( int j = ; j <= i ; ++j ){
C[i][j]=(j==)?:(C[i-][j]+C[i-][j-])%mod ;
}
}
}
void Run() {
memset( cnt , ,sizeof cnt ) ;
memset( dp , ,sizeof dp ) ;
int n = s.size() , n2 = n / , n1 = n - n2 ;
for( int i = ; i < n ; ++i ) cnt[ -(s[i]-'') ]++ ;
dp[][][] = ; LL sum = ;
for( int i = ; i < ; ++i ) { // digit
for( int j = ; j <= n1 ; ++j ) { // odd used
for( int k = ; k < ; ++k ) { // remainder
if( !dp[i][j][k] || j > sum ) continue ;
int j1 = j , j2 = sum - j;
for( int z = ; z <= cnt[i] ; ++z ){
int z1 = z , z2 = cnt[i] - z ;
if( j1 + z1 > n1 || j2 + z2 > n2 ) continue ;
LL tmp = dp[i][j][k];
if( (n&) && i== ) tmp = tmp * C[j1+z1-][z1] % mod ;
else tmp = tmp * C[j1+z1][z1] % mod ;
if(!(n&) && i== ) tmp = tmp * C[j2+z2-][z2] % mod ;
else tmp = tmp * C[j2+z2][z2] % mod;
int _i = i + , _j = j1 + z1 , _k = ( k + z1*(-i)-z2*(-i)+*)%;
dp[_i][_j][_k] = ( dp[_i][_j][_k] + tmp ) % mod ;
}
}
}
sum += cnt[i];
}
cout << dp[][n1][] << endl ;
} int main(){
Init(); while( cin >> s ) Run();
}
UVA 12672 Eleven(DP)的更多相关文章
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- UVa 10029 hash + dp
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 10154 贪心+dp
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 674 (入门DP, 14.07.09)
Coin Change Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We ...
- UVA 1358 - Generator(dp+高斯消元+KMP)
UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524& ...
- uva 1534 - Taekwondo(dp+馋)
题目连接:uva 1534 - Taekwondo 题目大意:有两组什么东西,题目背景有点忘记了,就是给出两组数,两组个数分别为n,m,要求找出min(n,m)对数.每一个数最多最多选一次,使得这mi ...
- uva 10118(DP)
UVA 10118 题意: 有4堆糖果,每堆有n(最多40)个,有一个篮子,最多装5个糖果,我们每次只能从某一堆糖果里拿出一个糖果, 如果篮子里有两个相同的糖果,那么就可以把这两个(一对)糖果放进自己 ...
随机推荐
- 常见前端面试题JS部分
1.闭包 2.JS操作和获取cookie //创建cookie function setCookie(name, value, expires, path, domain, secure) { var ...
- 录制rtsp音视频
1.使用ffmpeg来录制rtsp视频 视频 ffmpeg -y -i rtsp://172.16.23.66:554/h264major -vcodec copy -f mp4 record.mp4 ...
- 微信小程序(13)--页面滚动到某个位置添加类效果
微信小程序页面滚动到某个位置添加类,盒子置顶效果. <!-- vh,是指CSS中相对长度单位,表示相对视口高度(Viewport Height),1vh = % * 视口高度 --> &l ...
- kali Linux 入门(一)
一.描述 1.基于Debian Linux 发行版 2013年3月13日 2.包含约600个安全工具 3.定制 安全稳定的内核 4.前身是BackTrack(2013年停止维护) 5.官方机构:Off ...
- .bat 文件调用python脚本
1.将clearlog.py 脚本放在指定目录 比如 我放在 C:\Users\Administrator\Desktop 上 也就是桌面上 2.创建一个.bat 位后缀名的脚本 3.写入如下脚本 @ ...
- 【python实例】判断是否是回文数
""" 输入一个数,判断一个这个数是否是回文数.例如:121,这个数反过来还是121,所以这个是回文数: 再如:134,这个数反过来是431,所以这不是一个回文数: 12 ...
- Center os6.5 mysql
1 # yum -y install mysql-server mysql mysql-dev 2 启动mysql # service mysqld start 3 为root用户配置一个密码 ...
- Flutter-showBottomSheet底部彈出框
onPressed: () { showModalBottomSheet( context: context, builder: (BuildContext context) { return new ...
- 人生苦短_我用Python_Try_Exception异常捕捉_007
# coding=utf-8 ''' request+try__异常处理 ''' import requests class HttpRequests: def __init__(self, url, ...
- Ubuntu14.04安装Ruby2.2方法
直接使用系统的sudo apt-get install ruby2.0安装后,ruby -v显示ruby的版本依然是ruby 1.9. 以下方法可以顺序地在Ubuntu14.04安装Ruby2.2 s ...