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. 20-基于 DSP TMS320C6455的6U CPCI高速信号处理板卡

    基于 DSP TMS320C6455的6U CPCI高速信号处理板卡 1. 板卡概述 基于 DSP TMS320C6455的CPCI高速信号处理板卡是新一代高速DSP处理平台,广泛用于DSP性能验证, ...

  2. (解决某些疑难杂症)Ubuntu16.04 + NVIDIA显卡驱动 + cuda10 + cudnn 安装教程

    一.NVIDIA显卡驱动 打开终端,输入: sudo nautilus 在新打开的文件夹中,进入以下路径(不要用命令行): 左下角点计算机,lib,modules 这时会有几个文件夹,对每个文件夹都进 ...

  3. centos(6-7)安装openldap

    前言 参考资料: http://yhz61010.iteye.com/blog/2352672 https://www.cnblogs.com/lemon-le/p/6266921.html 实验环境 ...

  4. spring Boot 简单的登录功能,利用了jdbcTemplate.class完成sql语句的执行,无需service层、dao层和.xml文件

    1.搭建SpringBoot项目首先我们先在IDEA上创建一个SpringBoot的Web项目(1)file ——> new ——> project——> Spring Initia ...

  5. Python格式输出汇总

    print ('%10s'%('test')) print ('{:<10}'.format('test'))#left-aligned print ('{:>10}'.format('t ...

  6. nyoj 253:LK的旅行 【旋转卡壳入门】

    题目链接 求平面最大点对. 找凸包 -> 根据凸包运用旋转卡壳算法求最大点对(套用kuang巨模板) 关于旋转卡壳算法 #include<bits/stdc++.h> using n ...

  7. Angular JS - 8 - SeaJS与前端模块化

    一.前端模块化 关于前端模块化,参考以下链接 : https://github.com/seajs/seajs/issues/547 http://www.cnblogs.com/huiguo/cat ...

  8. Oracle 的trim,ltrim,rtrim函数的区别

    该函数共有两种作用:第一种,即大家都比较熟悉的去除空格.例子:--TRIM去除指定字符的前后空格SQL> SELECT TRIM(' dd df ') FROM dual;TRIM('DDDF' ...

  9. Python3解leetcode Rotate Array

    问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...

  10. APIO2019 练习赛 Wedding cake——思路+高精度

    题目大意: 给 n ( n<=1e5 ) 个数 \( a_i \) (\( a_i \) <=1e5),需要构造 n 个实数使得它们的和是 1 ,并且第 i 个实数必须小数点后恰好有 \( ...