【BZOJ 1008】[HNOI2008]越狱

【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1008

【题意】

【题解】

相邻就会犯罪的话;
可以考虑它的反面;
即让所有相同信仰的人都不相邻;
第一个位置的人的信仰任意,第二个人在第一个人的基础上少一个;
即m-1,第三个人只要不和左边的第二个人一样就好也是m-1,再往后就都是m-1了..
所以不会犯罪的方案数是
m*(m-1)^(n-1)
然后总的次数为m^n->即每个位置的人的信仰任意;
用这个m^n去减m*(m-1)^(n-1)就好
写个快速幂就好

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
const LL mod = 100003;

LL m,n,temp;

void ksm(LL x,LL y)
{
    if (y==0) return;
    ksm(x,y>>1);
    temp = (temp*temp)%mod;
    if (y&1)
        temp = (temp*x)%mod;
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    //m^n-m*(m-1)^(n-1)
    rel(m),rel(n);
    temp = 1;
    ksm(m,n);
    LL ans = temp;
    temp = 1;
    ksm(m-1,n-1);
    temp = (m*temp)%mod;
    ans = (ans-temp+mod)%mod;
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626614.html