C.Ubiquity
Sample Input 1
2
Sample Output 1
2
Sample Input 2
1
Sample Output 2
0
Sample Input 3
869121
Sample Output 3
2511445
题解:输入一个N,0<=Ai<=9,所以一共10N 种情况,序列中元素个数为N,序列中一定存在 0 和 9,要得到至少有一个0和一个9的所有情况,思路使用总共的情况减去只有一个 0 、只有 一个 9 、或者 0 和 9 都没有的情况。
用 10N 减去序列种没有 0 的 9N 种情况,再减去序列中没有 1 的 9N种情况,这些都不符合有 0 有 9 的条件。
但是没有 0 和没有 1 的 9N 的两个情况中肯定都包括既没有 0 也没有 1 的 8N 情况,所以多减了一次,要加回来。
所以计算 10N - 9N - 9N + 8N 再取余得到答案
注意最后一定要:
ans = (ans + mod)%mod; // 因为取余后,各数的大小发生变化,这里防止ans减为负数!!!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<time.h>
#include<vector>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
ll po(ll a,ll b) //快速幂算法
{
ll ans = 1;
while(b)
{
if(b & 1)
{
ans = ans * a % mod;
}
b >>= 1;
a = a * a % mod;
}
return ans;
}
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
ll n;
cin>>n;
ll ans = (po(10,n)-po(9,n)-po(9,n)+po(8,n))%mod;
ans = (ans + mod)%mod; // 因为取余后,各数的大小发生变化,这里防止ans减为负数!!!
cout<<ans<<endl;
return 0;
}