算法提高 Problem S4: Interesting Numbers 加强版 解题报告
2017-03-14 19:10
316 查看
Problem Description
We call a number interesting, if and only if:
1. Its digits consists of only 0, 1, 2 and 3, and all these digits occurred at least once.
2. Inside this number, all 0s occur before any 1s, and all 2s occur before any 3s.
Therefore, the smallest interesting number according to our definition is 2013. There are two more interseting number of 4 digits: 2031 and 2301.
Your task is to calculate the number of interesting numbers of exactly n digits. As the answer might be very large, you only need to output the answer modulo 1000000007.
Input Format
The input has one line consisting of one positive integer n (4 ≤ n ≤ 10^15).
Output Format
The output has just one line, containing the number of interesting numbers of exactly n digits, modulo 1000000007.
Input Sample
4
Output Sample
3
这道是蓝桥杯练习系统上为数不多的英文题,难度似乎也相应比其他题目更高一筹。一开始这道我是往动态规划方面想,希望能够借助递推式,再转化转化,矩阵快速幂用一用(类似斐波那契数列第n项求解)。后来自己倒是算捣鼓除了递推式,可是无论如何也化简不了能套用快速幂就求出f
的形式。所以,灵机一动,联想到之前做的公式求值,便往组合计数方面考虑。
这道题目虽是英文,但题意应该好理解,就是求满足0、1、2、3这几个数码都出现过且所有0在1之前,所有2在3之前出现的n位数的数目。很显然,第一位是2(首先第一位不可能是0,而如果第一位是1或3的话,则那两个“之前出现”的条件必定不能同时满足)。所以,问题就变成了求后面(n-1)位的事情了,不妨记m=n-1。
不妨将0、1放到第一组中,2、3放到第二组中。设第一组在剩余m位中占用x个数字,则第二组占用(m-x)个数字
而具体到各组中,第一组有(x-1)种数码安排方法(考虑0出现的次数),第二组则有(m-x)种数码安排方法。综上,
对于固定的x,数码安排方法有C(m,x)*(x-1)*(m-x)种。总的这样n位数的数目,即x从2到(m-1)下前式的和,即为
C(m,2)*1*(m-2)+C(m,3)*2*(m-3)+~~~+C(m,m-1)*(m-2)*1,又知x=1及x等于m时C(m,x)*(x-1)*(m-x)=0,而x=0时
C(m,x)*(x-1)*(m-x)=-m,所以再转化上式,最后加上m,即分离出一个x从0到m的组合式子的和,即组合数乘以x的二次多项式的和。后面的数学处理,可以看看http://tieba.baidu.com/p/2832505865的思想(其实相当于k=0,1,2的应用了已经)
最后结果即(m*m-3*m)*2^(m-2)+m,其中m=n-1。然后就可以用快速幂算了。注意能取模就取模,不然会溢出
代码如下(其实不用n=4特殊情况判断的,写烦了点):
We call a number interesting, if and only if:
1. Its digits consists of only 0, 1, 2 and 3, and all these digits occurred at least once.
2. Inside this number, all 0s occur before any 1s, and all 2s occur before any 3s.
Therefore, the smallest interesting number according to our definition is 2013. There are two more interseting number of 4 digits: 2031 and 2301.
Your task is to calculate the number of interesting numbers of exactly n digits. As the answer might be very large, you only need to output the answer modulo 1000000007.
Input Format
The input has one line consisting of one positive integer n (4 ≤ n ≤ 10^15).
Output Format
The output has just one line, containing the number of interesting numbers of exactly n digits, modulo 1000000007.
Input Sample
4
Output Sample
3
这道是蓝桥杯练习系统上为数不多的英文题,难度似乎也相应比其他题目更高一筹。一开始这道我是往动态规划方面想,希望能够借助递推式,再转化转化,矩阵快速幂用一用(类似斐波那契数列第n项求解)。后来自己倒是算捣鼓除了递推式,可是无论如何也化简不了能套用快速幂就求出f
的形式。所以,灵机一动,联想到之前做的公式求值,便往组合计数方面考虑。
这道题目虽是英文,但题意应该好理解,就是求满足0、1、2、3这几个数码都出现过且所有0在1之前,所有2在3之前出现的n位数的数目。很显然,第一位是2(首先第一位不可能是0,而如果第一位是1或3的话,则那两个“之前出现”的条件必定不能同时满足)。所以,问题就变成了求后面(n-1)位的事情了,不妨记m=n-1。
不妨将0、1放到第一组中,2、3放到第二组中。设第一组在剩余m位中占用x个数字,则第二组占用(m-x)个数字
而具体到各组中,第一组有(x-1)种数码安排方法(考虑0出现的次数),第二组则有(m-x)种数码安排方法。综上,
对于固定的x,数码安排方法有C(m,x)*(x-1)*(m-x)种。总的这样n位数的数目,即x从2到(m-1)下前式的和,即为
C(m,2)*1*(m-2)+C(m,3)*2*(m-3)+~~~+C(m,m-1)*(m-2)*1,又知x=1及x等于m时C(m,x)*(x-1)*(m-x)=0,而x=0时
C(m,x)*(x-1)*(m-x)=-m,所以再转化上式,最后加上m,即分离出一个x从0到m的组合式子的和,即组合数乘以x的二次多项式的和。后面的数学处理,可以看看http://tieba.baidu.com/p/2832505865的思想(其实相当于k=0,1,2的应用了已经)
最后结果即(m*m-3*m)*2^(m-2)+m,其中m=n-1。然后就可以用快速幂算了。注意能取模就取模,不然会溢出
代码如下(其实不用n=4特殊情况判断的,写烦了点):
import java.util.Scanner; public class Main { static long n; static long mul, ans, res; static long monum = 1000000007; private static long cal(long n) { if (n == 1) return 2; long num = cal(n / 2); num = num * num % monum; if (n % 2 == 1) num = num * 2 % monum; return num; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner reader = new Scanner(System.in); n = reader.nextLong(); if (n == 4) System.out.println(3); else { n = n - 1; res = (n % monum) * (n % monum); res = ((res - 3 * n) % monum + monum) % monum; n = n - 2; mul = cal(n); res = res * mul % monum; n = n + 2; res = (res + n) % monum; System.out.println(res); } } }
相关文章推荐
- 算法笔记_093:蓝桥杯练习 Problem S4: Interesting Numbers 加强版(Java)
- Problem S4: Interesting Numbers 加强版 (数论)
- 解题报告 之 HDU5323 Solve this interesting problem
- 【算法】轰炸(BOMB)解题报告(模拟提高组)
- BUAA-SCSE 暑期算法提高班 Final Contest 解题报告
- Codeforces Round #365 (Div. 2) Problem D.Mishka and Interesting sum 解题报告
- 【算法】NOIP2010提高组解题报告
- 第四届吉林省大学生程序设计大赛个人解题报告 Problem J: Buying and Selling
- Pku acm 3041 Asteroids 数据结构题目解题报告(十六)---- 匈牙利算法求二分图的最大匹配
- Pku acm 1466 Girls and Boys数据结构题目解题报告(十七)---- 匈牙利算法求二分图的最大匹配
- 第四届吉林省大学生程序设计大赛个人解题报告 Problem A: Harshad Number
- poj1459最高标号预流推进算法解题报告
- pku 匈牙利算法 1469 COURSES 解题报告
- Pku acm 1469 COURSES 数据结构题目解题报告(十一)---- 匈牙利算法求二分图的最大匹配
- 珠海邀请赛个人解题报告 Problem B: Magic Matrix
- 珠海邀请赛个人解题报告 Problem E: Cinema
- The sum problem(hdu 2058)解题报告
- Pku acm 2771 Guardian of Decency 数据结构题目解题报告(十五)---- 匈牙利算法求二分图的最大匹配
- USACO :Runaround Numbers 解题报告
- 贪心算法解汽车加油问题——算法解题报告