AtCoder Beginner Contest 178 C - Ubiquity题解

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葛济维的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值