目录

1 问题描述

2 解决方案

 


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

  1. java实现 蓝桥杯 算法提高 Problem S4: Interesting Numbers 加强版

    1 问题描述 Problem Description We call a number interesting, if and only if: 1. Its digits consists of o ...

  2. 算法笔记_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- ...

  3. 算法笔记_056:蓝桥杯练习 未名湖边的烦恼(Java)

    目录 1 问题描述 2 解决方案 2.1 递归法 2.2 递推法   1 问题描述 问题描述 每年冬天,北大未名湖上都是滑冰的好地方.北大体育组准备了许多冰鞋,可是人太多了,每天下午收工后,常常一双冰 ...

  4. 算法笔记_055:蓝桥杯练习 Tricky and Clever Password (Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 在年轻的时候,我们故事中的英雄——国王 Copa——他的私人数据并不是完全安全地隐蔽.对他来说是,这不可接受的.因此,他发明了一种密码,好 ...

  5. 算法笔记_119:蓝桥杯第六届省赛(Java语言A组)试题解答

     目录 1 熊怪吃核桃 2 星系炸弹 3 九数分三组 4 循环节长度 5 打印菱形 6 加法变乘法 7 牌型种数 8 移动距离 9 垒骰子 10 灾后重建   前言:以下试题解答代码部分仅供参考,若有 ...

  6. 算法笔记_120:蓝桥杯第六届省赛(Java语言B组部分习题)试题解答

     目录 1 三角形面积 2 立方变自身 3 三羊献瑞 4 九数组分数 5 饮料换购 6 生命之树   前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~ 1 三角形面积 三角形 ...

  7. 算法笔记_121:蓝桥杯第六届省赛(Java语言C组部分习题)试题解答

     目录 1 隔行变色 2 立方尾不变 3 无穷分数 4 格子中输出 5 奇妙的数字 6 打印大X   前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~ 1 隔行变色 隔行变色 ...

  8. 算法笔记_122:蓝桥杯第七届省赛(Java语言A组)试题解答

     目录 1 煤球数目 2 生日蜡烛 3 搭积木 4 分小组 5 抽签 6 寒假作业 7 剪邮票 8 取球博弈 9 交换瓶子 10 压缩变换   前言:以下试题解答代码部分仅供参考,若有不当之处,还请路 ...

  9. 算法笔记_092:蓝桥杯练习 c++_ch04_02_修正版(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 [题目描述] 实现一个时间类Time.将小时,分钟和秒存储为int型成员变量.要求该类中包含一个构造函数,访问用的函数,一个推进当前时间的函数adv ...

随机推荐

  1. spark sql遇到的问题

    项目背景:计算平台架构hadoop spark   spark on yarn,支持多种sql引擎如hive.spark sql.impala等. 问题描述:SPARK SQL在做insert ove ...

  2. javascript中的this总结

    1.关于this 我们需要根据 "调用位置" 上函数的 "调用方式" 来确定函数中this使用的 "绑定规则" 2.绑定规则 非严格模式下: ...

  3. 《深入浅出Nodejs》笔记——模块机制(2)

    前言 书上还有很大一部分讲了C/C++模块的编译过程.核心模块编写和C/C++扩展模块的内容,不过我对C++一窍不通因此没有仔细看,如果以后需要再自习看吧. 包与NPM 第三方模块中,模块和模块之间是 ...

  4. 洛谷——P3909 异或之积

    P3909 异或之积 题目描述 对于A_1,A_2,A_3,\cdots,A_NA1​,A2​,A3​,⋯,AN​,求 (6\times \sum_{i=1}^N\sum_{j=i+1}^N\sum_ ...

  5. git "Could not read from remote repository.Please make sure you have the correct access rights."解决方案

    我们在使用git clone 或其他命令的时候,有时候会遇到这类问题,如图: fatal: Could not read from remote repository.Please make sure ...

  6. Flask实战第60天:帖子分页技术实现

    编辑manage.py,添加测试帖子 @manager.command def create_test_post(): for x in range(1, 100): title = '标题{}'.f ...

  7. shell脚本--字符串处理和动态数组

    Linux下的文本处理命令,以清晰的列分割数据为高效处理源: awk 的gsub函数可替换指定字符串 echo "<tr><td>col1</td>< ...

  8. ASP.NET总结——更改后

    这篇重新整理的总结,我做了很久,也在草稿箱中放了很久,一直感觉没有达到和老师谈话后的水平,感觉还是需要增加一些修改,希望读者能提出宝贵意见.既这篇博客之前,我发表了一篇ASP.net的总结,在结构上, ...

  9. 【Matrix-tree定理】【BEST Theorem】hdu6064 RXD and numbers

    题意:给你一张有向图,求从1出发,回到1的欧拉回路数量. 先特判掉欧拉回路不存在时的情况. 看这个吧:http://blog.csdn.net/yuanjunlai141/article/detail ...

  10. 【bfs+优先队列】POJ2049-Finding Nemo

    基本上算是普通但略有些繁琐的广搜.给出的墙面和门的坐标为点,而Nemo位于方格中. [思路] 首先思考一下如何存储下整个坐标系.我们预先约定,用一个方格的左下角顶点坐标来作为这个方格的坐标.map[i ...