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)的更多相关文章

  1. UVA.10192 Vacation (DP LCS)

    UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...

  2. UVA.10130 SuperSale (DP 01背包)

    UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...

  3. 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 ...

  4. UVa 10029 hash + dp

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. uva 10154 贪心+dp

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. 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 ...

  7. UVA 1358 - Generator(dp+高斯消元+KMP)

    UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524& ...

  8. uva 1534 - Taekwondo(dp+馋)

    题目连接:uva 1534 - Taekwondo 题目大意:有两组什么东西,题目背景有点忘记了,就是给出两组数,两组个数分别为n,m,要求找出min(n,m)对数.每一个数最多最多选一次,使得这mi ...

  9. uva 10118(DP)

    UVA 10118 题意: 有4堆糖果,每堆有n(最多40)个,有一个篮子,最多装5个糖果,我们每次只能从某一堆糖果里拿出一个糖果, 如果篮子里有两个相同的糖果,那么就可以把这两个(一对)糖果放进自己 ...

随机推荐

  1. shell 脚本规范

    shell 脚本规范 一.背景 1.使用哪一种shell? 必须使用bash shell 2.什么时候使用shell? 数量相对较少的操作 脚本文件少于100行 3.脚本文件扩展名是什么? shell ...

  2. spring(二):bean的生命周期

    bean的生命周期指的是bean的创建——>初始化——>销毁的过程,该过程是由spring容器进行管理的 我们可以自定义bean初始化和销毁的方法:容器在bean进行到当前生命周期时,调用 ...

  3. java 进销存 商户管理 系统 管理 库存管理 销售报表springmvc SSM项目

    统介绍: 1.系统采用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC浏览器使用) 2.springmvc +spring4.3.7+ mybaits3.3  SSM 普 ...

  4. PCA算法和实例

    PCA算法 算法步骤: 假设有m条n维数据. 1. 将原始数据按列组成n行m列矩阵X 2. 将X的每一行(代表一个属性字段)进行零均值化,即减去这一行的均值 3. 求出协方差矩阵C=1/mXXT 4. ...

  5. python 子类继承父类__init__(转载)

    转载: http://www.jb51.net/article/100195.htm 前言 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__方法 ...

  6. rdev - 查询/设置内核映像文件的根设备,RAM 磁盘大小或视频模式

    总览 SYNOPSIS rdev [ -rvh ] [ -o offset ] [ image [ value [ offset ] ] ] rdev [ -o offset ] [ image [ ...

  7. Nginx1.6.0+MySQL5.6.19+PHP5.5.14(centos)

    一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...

  8. CDH6.3 Centos7

    按照官方文档安装即可 CentOS7 上搭建 CDH(6.3.0) 官方文档:https://docs.cloudera.com/documentation/enterprise/6/6.3/topi ...

  9. JavaScript之BOM+DOM

    BOM 浏览器对象模型, 用于把浏览器相关的组件封装为对象进行操作. BOM是包含了DOM的. window对象 弹出框相关 确认: 取消: 与打开关闭window有关的方法 定时器相关 暂停选老婆 ...

  10. boost algorithm

    BOost Algorithm provides algorithms that complement the algorithms from the standard library. Unlike ...