AtCoder Beginner Contest 178 C

题意: 构造一个长度为 n n n的序列 a a a,要求至少包括 0 0 0 9 9 9,求总共方案数
数据范围: 0 ≤ a i ≤ 9 , 1 ≤ n ≤ 1 0 6 0\leq a_i\leq 9,1\leq n\leq 10^6 0ai9,1n106

题解: 正难则反。
所有方案数: p o w ( 10 , n ) pow(10,n) pow(10,n)
只包括 0 0 0或只包括 9 9 9的方案数: p o w ( 9 , n ) pow(9,n) pow(9,n)
不包括 0 0 0 9 9 9的方案数: p o w ( 8 , n ) pow(8,n) pow(8,n)
r e s = p o w ( 10 , n ) − 2 × p o w ( 9 , n ) + p o w ( 8 , n ) res=pow(10,n)-2\times pow(9,n)+pow(8,n) res=pow(10,n)2×pow(9,n)+pow(8,n)

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; 
const int mod = 1e9 + 7;
int qp(int a, int b) {
    
    
	int ans = 1;
	while(b) {
    
    
		if(b & 1) ans = 1ll * ans * a % mod;
		a = 1ll * a * a % mod;
		b >>= 1; 
	}
	return ans;
}

int main()
{
    
    
	int n; scanf("%d", &n); 
	if(n == 1) puts("0");
	else if(n == 2) puts("2");
	else printf("%d\n", (((ll)qp(10, n) - 2 * qp(9, n) + qp(8, n)) % mod + mod) % mod);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43900869/article/details/108577224