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

本文介绍了一种通过数学组合数推理化简的方法来解决特定数字组合的问题,即寻找由0、1、2、3组成的有趣数字,这些数字需满足特定排列规则,并计算n位数下有趣数字的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 解决方案
本题主要考查数学组合数推理化简,具体思考过程如下:

在这里插入图片描述
推导过程,在草稿纸上推导了一下:

在这里插入图片描述

import java.util.Scanner;
 
public class Main {
	static long n;
	static long mul, ans, res;
	static long monum = 1000000007;
 
	private static long cal(long n) {
		if (n == 1)
			return 2;
		long num = cal(n / 2);
		num = num * num % monum;
		if (n % 2 == 1)
			num = num * 2 % monum;
		return num;
	}
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		n = reader.nextLong();
		if (n == 4)
			System.out.println(3);
		else {
			n = n - 1;
			res = (n % monum) * (n % monum);
			res = ((res - 3 * n) % monum + monum) % monum;
			n = n - 2;
			mul = cal(n);
			res = res * mul % monum;
			n = n + 2;
			res = (res + n) % monum;
			System.out.println(res);
		}
	}
 
}
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值