目录

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. go chapter 3 - defer

    https://www.cnblogs.com/bonelee/p/6861777.html 函数返回之前(或任意位置执行 return 语句之后)一刻才执行某个语句或函数.用法类似于面向对象编程语言 ...

  2. Loj10222 佳佳的Fibonacci(矩阵乘法)

    题面 给定\(n,m\),求: \[ T(n)=\sum_{i=1}^ni\times f_i \] 其中\(f_i\)为斐波那契数列的第\(i\)项 题解 不妨设: \[ S(n)=\sum_{i= ...

  3. shell 倒引号

    `command` 倒引号 (backticks) 在前面的单双引号,括住的是字串,但如果该字串是一列命令列,会怎样?答案是不会执行.要处理这种情况,我们得用倒单引号来做. fdv=`date +%F ...

  4. Java多线程-run方法与start方法的区别

    package com.interview; /** * java多线程的两种实现方式以及run.start方法的区别 * @author MEI.LIU * */ public class Thre ...

  5. AtCoder - 3939 Strange Nim

    Problem Statement Takahashi and Aoki are playing a stone-taking game. Initially, there are N piles o ...

  6. django2与1的差别和视图

    django1与2路由的差别 在django1中的url在django2中为re_path django2中新增了path 1.from django.urls import path 2.不支持正则 ...

  7. Java高级架构师(一)第36节:Nginx的反向代理模块

    理解Http正向代理和Http反向代理的区别 Proxy模块,最常用的proxy_pass, Proxy_pass 可以转发请求到其他的浏览器.  # Nginx强项在于负载.反向.动静分离 #

  8. 8VC Venture Cup 2016 - Final Round C. Package Delivery 优先队列

    C. Package Delivery 题目连接: http://www.codeforces.com/contest/627/problem/C Description Johnny drives ...

  9. 兼容IE8

    由于IE8不支持HTML5,而它又是Win7的默认浏览器,我们即使讨厌它,在这几年却也拿它没办法. 最近做了个需要兼容IE8的项目,不可避免地用了HTML5+CSS3,甚至canvas和svg,做兼容 ...

  10. CentOS下OpenVPN实现公网IP映射到内网(iptables转发功能)(转)

    说明:这种方案的实现前提是必须要有一台拥有公网IP的电脑,OpenVPN搭建过程很普通,关键技术在于iptables的转发.搭建教程可能有点旧了,可以只看iptables的关键点技术. 方案背景: 公 ...