题目相关
题目链接
AtCoder Beginner Contest 178 C 题,https://atcoder.jp/contests/abc178/tasks/abc178_c。
Problem Statement
How many integer sequences A1,A2,…,AN of length N satisfy all of the following conditions?
- 0 ≤ Ai ≤ 9
- There exists some i such that Ai=0 holds.
- There exists some i such that Ai=9 holds.
- The answer can be very large, so output it modulo 109+7
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer modulo 10^9+7.
Samples1
Sample Input 1
2
Sample Output 1
2
Explaination
Two sequences {0,9} and {9,0} satisfy all conditions.
Samples2
Sample Input 2
1
Sample Output 2
0
Samples3
Sample Input 3
869121
Sample Output 3
2511445
Constraints
- 1 ≤ N ≤ 10^6
- N is an integer.
题解报告
一个组合数学问题。
题目翻译
长度为 N 的序列 A1,A2,…,AN 中,有几个满足以下条件:
1、0 ≤ Ai ≤ 9;
2、序列中有一个数字为 0;
3、序列中有一个数字为 9;
求满足这样的序列个数,由于数据会比较大,答案对 取模。
题目分析
一个非常标准组合数学问题,需要使用的容斥原理,因为其中会有重复。从题目的意思可以看到,没有要求数字不能重复。
全排列
有长度为 N 的数组,每个位置的取值为 0 ~ 9,因此,数量为 种。
不包含 0 的排列
有长度为 N 的数组,每个位置的取值为 1 ~ 9,因此,数量为 种。
不包含 9 的排列
有长度为 N 的数组,每个位置的取值为 0 ~ 8,因此,数量为 种。
同时不包含 0 和 9 的排列
有长度为 N 的数组,每个位置的取值为 1 ~ 8,因此,数量为 种。
最终答案
这样,我们根据容斥原理可得,最终的答案为 。
难点
根据我们推导出的数学公式,你也许认为使用 pow() 函数就可以解决问题,请注意 N 的最大值为 ,也就意味这
这将是一个天文数字。无法直接使用 pow() 函数来实现。因此需要使用快速幂。
AC 参考代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MO=1e9+7;
LL qmi(LL a,LL b) {
LL res=1;
while(b) {
if (b&1) {
res=res*a%MO;
}
a=a*a%MO;
b>>=1;
}
return res;
}
int main() {
LL n;
cin>>n;
cout<<((qmi(10ll,n)-2*qmi(9ll,n)+qmi(8ll,n))%MO+MO)%MO<<"\n";
return 0;
}