国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

【Bzoj1008】越獄

2019-11-11 01:45:56
字體:
供稿:網(wǎng)友

1008: [HNOI2008]越獄

Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8512  Solved: 3655[Submit][Status][Discuss]

Description

  監(jiān)獄有連續(xù)編號(hào)為1...N的N個(gè)房間,每個(gè)房間關(guān)押一個(gè)犯人,有M種宗教,每個(gè)犯人可能信仰其中一種。如果相鄰房間的犯人的宗教相同,就可能發(fā)生越獄,求有多少種狀態(tài)可能發(fā)生越獄

Input

  輸入兩個(gè)整數(shù)M,N.1<=M<=10^8,1<=N<=10^12

Output

  可能越獄的狀態(tài)數(shù),模100003取余

Sample Input

2 3

Sample Output

6

HINT

  6種狀態(tài)為(000)(001)(011)(100)(110)(111)

這道題的話...思維比較強(qiáng),實(shí)現(xiàn)起來很簡單。我們這樣想,所有可能性顯然是m^n,但越獄的可能性我們也許不好考慮,那我們換一種思路,考慮不越獄的可能性。首先,第一個(gè)房間允許的宗教為m個(gè),第二個(gè)只要和第一個(gè)不同,后面的都和前一個(gè)不同,就能保證相鄰房間都不是同一宗教。即不越獄的可能性有m*((m-1)^(n-1))種,最后把答案相減即可,注意取模時(shí)的細(xì)節(jié),防止出現(xiàn)負(fù)數(shù)。

小學(xué)生版

#include<cstdio>#include<algorithm>using namespace std;typedef long long ll;ll k=100003;ll f(ll b,ll p){    if(p==0) return 1;    ll tmp=f(b,p/2)%k;    tmp=(tmp*tmp)%k;    if(p%2==1)    tmp=(b*tmp)%k;    return tmp;}int main(){    ll n,m,ans;    scanf("%lld%lld",&m,&n);    ans=f(m,n);    ans-=m*f(m-1,n-1)%k;     if(ans<0)    ans+=k;    PRintf("%lld",ans);    return 0;}

玄學(xué)版

#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;LL mod = 100003,tot=1,cnt=1;LL m,n,tot_m,tot_n,cnt_m,cnt_n;int main(){	scanf("%lld%lld",&m,&n);	tot_m=m;tot_n=n;cnt_m=m-1;cnt_n=n-1;	while(tot_n){		if(tot_n&1) tot = (tot*tot_m)%mod;		tot_m = (tot_m*tot_m)%mod;		tot_n >>= 1;	}	while(cnt_n){		if(cnt_n&1) cnt = (cnt*cnt_m)%mod;		cnt_m = (cnt_m*cnt_m)%mod;		cnt_n >>= 1;	}	tot = tot-((cnt*m)%mod);	printf("%lld",cnt>=0? cnt:cnt+mod);	return 0;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 麦盖提县| 宜昌市| 新乡县| 华坪县| 巧家县| 方城县| 富裕县| 桐柏县| 武定县| 泰宁县| 汕头市| 应城市| 江口县| 平凉市| 大方县| 永嘉县| 怀仁县| 哈巴河县| 阆中市| 东明县| 确山县| 磐石市| 佛山市| 瑞安市| 合水县| 宿松县| 富民县| 白城市| 宿迁市| 涡阳县| 米易县| 新泰市| 石渠县| 渝中区| 凤冈县| 砀山县| 宁南县| 平昌县| 桑植县| 息烽县| 广安市|