目录

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. CodeForces 734E Anton and Tree

    $dfs$缩点,树形$dp$. 首先将连通块缩点,缩点后形成一个黑白节点相间的树.接下来的任务就是寻找一个$root$,使这棵树以$root$为根,树的高度是最小的(也就是一层一层染色).树形$dp$ ...

  2. Memory Allocation with COBOL

    Generally, the use of a table/array (Static Memory) is most common in COBOL modules in an applicatio ...

  3. VB查询数据库之写入数据库——机房收费系统总结(三)

    在机房收费系统中,新注册的用户,更改的密码,上机下级记录,上机收费记录等等都要写入数据库,这样,后面的查询才能生效.像数据库中写入数据,首先,找到你要写入数据的数据库中的表,在表中建立新的行,让后再把 ...

  4. poj 3464(Trie)Approximations

    Approximations Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 419   Accepted: 23 Desc ...

  5. 【暴力】hdu6121 Build a tree

    给你n,K,让你构造出一颗n个结点的完全K叉树,求所有结点子树大小的异或和. 先把n号结点到根的路径提取出来单独计算.然后这条路径把每一层分成了左右两部分,每一层的左侧和其上一层的右侧的结点的子树大小 ...

  6. 【带权并查集】poj1182 食物链

    带权并查集,或者叫做种类并查集,经典题. http://blog.csdn.net/shuangde800/article/details/7974668 这份代码感觉是坠吼的. 我的代码是暴力分类讨 ...

  7. JVM入门——JVM内存结构

    一.java代码编译执行过程 1.源码编译:通过Java源码编译器将Java代码编译成JVM字节码(.class文件) 2.类加载:通过ClassLoader及其子类来完成JVM的类加载 3.类执行: ...

  8. Problem H: 零起点学算法109——单数变复数

    #include <stdio.h> #include<string.h> int main(void) { int n; ]; scanf("%d",&a ...

  9. Problem D: 调用函数,输出Fibonacci数列的m项至n项

    #include<stdio.h> int fib(int n)//定义FIbonacci函数 { int s,i; ||n==) { s=; } else { int s1,s2; s1 ...

  10. java中interface的完整表述

    我用一个工具:java Decompiler反编译工具查看jar包源码的时候,出现了以下代码: public abstract interface AbsITest{} 在网上搜索了一下: 我对这种情 ...