博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法笔记_093:蓝桥杯练习 Problem S4: Interesting Numbers 加强版(Java)
阅读量:7051 次
发布时间:2019-06-28

本文共 1863 字,大约阅读时间需要 6 分钟。

目录

 


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. 

 

转载地址:http://uypol.baihongyu.com/

你可能感兴趣的文章
jQuery.extend 函数详解
查看>>
phpstorm 安装 及 使用 去掉名称类型提示
查看>>
Xp sp3 创建进程的堆栈
查看>>
Log4j的扩展-支持设置最大日志数量的DailyRollingFileAppender
查看>>
IT行业¬——Linux
查看>>
linkerd ab部署测试
查看>>
#日常杂记#Informatica 910 常见问题及可能的解决方法
查看>>
Spring Cloud Gateway 之 Only one connection receive subscriber allowed
查看>>
VoltDB 简介
查看>>
编译日志
查看>>
FieldType in Lucene
查看>>
为面试准备的知识点
查看>>
使用 CXF 做 webservice 简单例子
查看>>
Spring MVC之@RequestMapping 详解
查看>>
使用STS和Gradle创建Restful服务-Hello World
查看>>
网络服务器开发总结
查看>>
关于redis的主从、哨兵、集群
查看>>
Extjs Form用法详解
查看>>
ExecutorService线程池
查看>>
OD使用及快捷键
查看>>