Algorithmic note _093: Blue Bridge cup exercise problem s4:interesting Numbers enhanced (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description

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 We definition is 2013. There is both 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 is very large, you have need to output the answer modulo 1000000007.
Input Format
The input has a 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 1000 000007.
Input Sample
4
Output Sample
3

2 Solutions

This paper mainly examines the mathematics combination number inference simplification, the concrete thought process is as follows:

references from the end of the article 1 :

The derivation process, deduced on the draft paper:

The following code results run as 90 points, the code is for reference only, at the end of the article 1 of the code runs the result of 100 points, you can refer to OH.

The specific code is as follows:

ImportJava.util.Scanner; Public classMain { Public Final Static Longp = 1000000007L; //To find the value of the B-order residual p of a     Public LongGetpowmod (LongALongb) {Longtemp = A, result = 1;  while(b! = 0) {            if((b & 1) = = 1) Result= result * Temp%p; Temp= Temp * Temp%p; b>>= 1; }        returnresult; }         Public voidPrintresult (LongN) {if(n = = 4) {System.out.println (3); return; }        Longm = n-1; Longresult = 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 voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); Longn =In.nextlong ();            Test.printresult (n); }}

Resources:

1. algorithm to improve problem s4:interesting Numbers enhanced version of the problem solving report

Algorithmic note _093: Blue Bridge cup exercise problem s4:interesting Numbers enhanced (Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.