BZOJ 1008 - HNOI 2008 越狱 乘法原理 快速幂

36 篇文章 0 订阅

1008: [HNOI2008]越狱

Time Limit: 1 Sec   Memory Limit: 162 MB
Submit: 4119   Solved: 1749
[ Submit][ Status]

Description

监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱

Input

输入两个整数M,N.1<=M<=10^8,1<=N<=10^12

Output

可能越狱的状态数,模100003取余

Sample Input

2 3

Sample Output

6

HINT

6种状态为(000)(001)(011)(100)(110)(111)

Source

[ Submit][ Status]


水题

从反面来求,总的方法数减去不越狱的方法数就是答案

根据乘法原理可以很简单的写出

然后用快速幂来求即可


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));

const int MOD = 100003;

LL powmod(LL a, LL p, LL C) {
    if(p == 0) return 1;
    LL ret = powmod(a, p>>1, C);
    ret = (ret * ret) % C;
    if(p&1) ret = (ret * a) % C;
    return ret;
}

int main() {
    LL m, n;

    scanf("%lld%lld", &m, &n);
    LL all = powmod(m, n, MOD);
    LL no = powmod(m-1, n-1, MOD);
    no = (no * m) % MOD;
    LL ans = (all + MOD - no) % MOD;
    printf("%lld\n", ans);

    return 0;
}











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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值