算法笔记_093:蓝桥杯练习 Problem S4: Interesting Numbers 加强版(Java)
目录
1 问题描述
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
2 解决方案
本题主要考查数学组合数推理化简,具体思考过程如下:
引用自文末参考资料:

推导过程,在草稿纸上推导了一下:

本文下面代码结果运行为90分,代码仅供参考,文末参考资料1中代码运行结果为100分,可以参考一下哦。

具体代码如下:
import java.util.Scanner;
public class Main {
public final static long p = 1000000007L;
//求取a的b次方取余p的值
public long getPowMod(long a, long b) {
long temp = a, result = 1;
while(b != 0) {
if((b & 1) == 1)
result = result * temp % p;
temp = temp * temp % p;
b >>= 1;
}
return result;
}
public void printResult(long n) {
if(n == 4) {
System.out.println(3);
return;
}
long m = n - 1;
long result = getPowMod(2, m - 2);
m = m % p;
result = result * (m * m % p - 3 * m % p) % p + m;
result %= p;
System.out.println(result);
return;
}
public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
long n = in.nextLong();
test.printResult(n);
}
}
参考资料:
1. 算法提高 Problem S4: Interesting Numbers 加强版 解题报告
算法笔记_093:蓝桥杯练习 Problem S4: Interesting Numbers 加强版(Java)的更多相关文章
- java实现 蓝桥杯 算法提高 Problem S4: Interesting Numbers 加强版
1 问题描述 Problem Description We call a number interesting, if and only if: 1. Its digits consists of o ...
- 算法笔记_091:蓝桥杯练习 递推求值(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 已知递推公式: F(n, 1)=F(n-1, 2) + 2F(n-3, 1) + 5, F(n, 2)=F(n-1, 1) + 3F(n- ...
- 算法笔记_056:蓝桥杯练习 未名湖边的烦恼(Java)
目录 1 问题描述 2 解决方案 2.1 递归法 2.2 递推法 1 问题描述 问题描述 每年冬天,北大未名湖上都是滑冰的好地方.北大体育组准备了许多冰鞋,可是人太多了,每天下午收工后,常常一双冰 ...
- 算法笔记_055:蓝桥杯练习 Tricky and Clever Password (Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 在年轻的时候,我们故事中的英雄——国王 Copa——他的私人数据并不是完全安全地隐蔽.对他来说是,这不可接受的.因此,他发明了一种密码,好 ...
- 算法笔记_119:蓝桥杯第六届省赛(Java语言A组)试题解答
目录 1 熊怪吃核桃 2 星系炸弹 3 九数分三组 4 循环节长度 5 打印菱形 6 加法变乘法 7 牌型种数 8 移动距离 9 垒骰子 10 灾后重建 前言:以下试题解答代码部分仅供参考,若有 ...
- 算法笔记_120:蓝桥杯第六届省赛(Java语言B组部分习题)试题解答
目录 1 三角形面积 2 立方变自身 3 三羊献瑞 4 九数组分数 5 饮料换购 6 生命之树 前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~ 1 三角形面积 三角形 ...
- 算法笔记_121:蓝桥杯第六届省赛(Java语言C组部分习题)试题解答
目录 1 隔行变色 2 立方尾不变 3 无穷分数 4 格子中输出 5 奇妙的数字 6 打印大X 前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~ 1 隔行变色 隔行变色 ...
- 算法笔记_122:蓝桥杯第七届省赛(Java语言A组)试题解答
目录 1 煤球数目 2 生日蜡烛 3 搭积木 4 分小组 5 抽签 6 寒假作业 7 剪邮票 8 取球博弈 9 交换瓶子 10 压缩变换 前言:以下试题解答代码部分仅供参考,若有不当之处,还请路 ...
- 算法笔记_092:蓝桥杯练习 c++_ch04_02_修正版(Java)
目录 1 问题描述 2 解决方案 1 问题描述 [题目描述] 实现一个时间类Time.将小时,分钟和秒存储为int型成员变量.要求该类中包含一个构造函数,访问用的函数,一个推进当前时间的函数adv ...
随机推荐
- maven 打包可运行jar包(转)
目录 1.前提 2.方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包 3.方法二:使用maven-assembly-plugin插件打包 4.方法三 ...
- FFT实现高精度乘法
你应该知道$FFT$是用来处理多项式乘法的吧. 那么高精度乘法和多项式乘法有什么关系呢? 观察这样一个$20$位高精度整数$11111111111111111111$ 我们可以把它处理成这样的形式:$ ...
- Flask实战第57天:UEditor编辑器集成以及配置上传文件到七牛
相关链接 UEditor:http://ueditor.baidu.com/website/ 下载地址:http://ueditor.baidu.com/website/download.html# ...
- CUPOJ6242: LHC的二进制升级版
6242: LHC的二进制升级版 时间限制:1秒 内存限制:128MB Special Judge 提交:6 正确:3 题目描述 在发现了3的二进制特殊性质后,LHC认为形如1.3.7.15..... ...
- uva 10648(简单dp)
Recently one of my friend Tarik became a member of the food committee of an ACM regional competition ...
- JZYZOJ1530 [haoi2013]开关控制 状压 dfs 折半搜索
http://172.20.6.3/Problem_Show.asp?id=1530 元宵节快要到了,某城市人民公园将举办一次灯展.Dr.Kong准备设计出一个奇妙的展品,他计划将编号为1到N的N(1 ...
- BZOJ 1257 [CQOI2007]余数之和sum(分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1257 [题目大意] 给出正整数n和k,计算j(n,k)=k mod 1 + k mod ...
- 【动态规划】【滚动数组】【搜索】Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) D. Field expansion
显然将扩张按从大到小排序之后,只有不超过前34个有效. d[i][j]表示使用前i个扩张,当length为j时,所能得到的最大的width是多少. 然后用二重循环更新即可, d[i][j*A[i]]= ...
- 【FFT】hdu1402 A * B Problem Plus
FFT板子. 将大整数看作多项式,它们的乘积即多项式的乘积在x=10处的取值. #include<cstdio> #include<cmath> #include<cst ...
- java面试笔试总结(一)--亲生经历的面试题
说明:本文只是自己的一些心得体会,答案也是自己写的,正确与否,还需考证 java笔试题 1java笔试题1 启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7 ...