1. 程式人生 > >CSU 1162: Balls in the Boxes(快速冪、快速乘法)

CSU 1162: Balls in the Boxes(快速冪、快速乘法)

題目:

Description

Mr. Mindless has many balls and many boxes,he wants to put all the balls into some of the boxes.Now, he wants to know how many different solutions he can have.you know,he could put all the balls in one box,and there could be no balls in some of the boxes.Now,he tells you the number of balls and the numbers of boxes, can you to tell him the number of different solutions? Because the number is so large, you can just tell him the solutions mod by a given number C.Both of boxes and balls are all different.

Input

There are multiple testcases. In each test case, there is one line cantains three integers:the number of boxes ,the number of balls,and the given number C separated by a single space.All the numbers in the input are bigger than 0 and less than 2^63.

Output

 For each testcase,output an integer,denotes the number you will tell Mr. Mindless

Sample Input

3 2 4
4 3 5

Sample Output

1
4

注意點:乘法會溢位,要用二分法實現乘法

程式碼:

#include<iostream>
using namespace std;
#define ll long long

llg(lla, llb, llp)//a*b
{
	if (b == 0)return 0;
	llr = g(a, b / 2, p);
	r = (r + r) % p;
	if (b % 2)r = (r + a) % p;
	return r;
}

llf(lla, llb, llp)//a^b
{
	if (b == 0)return 1;
llr = f(a, b / 2, p); r = g(r, r, p); if (b % 2)r = g(r, a, p); return r; } int main() { lla, b, p; while (cin >> a >> b >> p)cout << f(a, b, p) << endl; return 0; }

相關推薦

CSU 1162: Balls in the Boxes快速快速乘法

題目:DescriptionMr. Mindless has many balls and many boxes,he wants to put all the balls into some of t

Gym 101246D Fire in the Countrydfs求SG函數

tor 我們 style sizeof std clas mem class == http://codeforces.com/gym/101246/problem/D 題意: 給定一個無向有環圖,大火從1點開始,每個時間點與它相鄰的點也將會著火,現在有兩個人輪流操作機

2631】Roads in the North 樹的直徑,模板

題幹: Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build su

【板子】gcdexgcd乘法逆元快速快速篩素數快速求逆元組合數

1.gcd int gcd(int a,int b){return b?gcd(b,a%b):a;} 2.擴充套件gcd )extend great common divisor ll exgcd(ll l,ll r,ll &x,ll &

[模板] gcdexgcd乘法逆元快速快速篩素數快速求逆元組合數

1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.擴充套件gcd )extend great common divisor ll exgcd(ll l,ll r,ll &x,ll &

poj2631 ?Roads in the North求樹的直徑

cst num ted style positive con inpu eache memset Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Su

Codeforces 835F Roads in the Kingdom 環套樹DP

for com ads 題目 現在 targe 都是 題意 ont 題目鏈接 Roads in the Kingdom 題意 給出一個環套樹的結構,現在要刪去這個結構中的一條邊,滿足所有點依然連通。 刪邊之後的這個結構是一棵樹,求所有刪邊情況中樹的直徑的最小值。

題解報告:poj 2631 Roads in the North最長鏈

ilo ger amp som and mon unit ace iostream Description Building and maintaining roads among communities in the far North is an expensive b

DensePose: Dense Human Pose Estimation In The Wild理解

0 - 背景   Facebook AI Research(FAIR)開源了一項將2D的RGB影象的所有人體畫素實時對映到3D模型的技術(DensePose)。支援戶外和穿著寬鬆衣服的物件識別,支援多人同時識別,並且實時性良好。 1 - 思路 1.1 - 標註資料集   對於一般的姿態識別(骨骼追蹤)

「日常訓練」Duff in the Army Codeforces Round #326 Div.2 E

題意(CodeForces 588E) 給定一棵\(n\)個點的樹,給定\(m\)個人(\(m\le n\))在哪個點上的資訊,每個點可以有任意個人;然後給\(q\)個詢問,每次問\(u\)到\(v\)上的路徑有的點上編號最小的\(k(k \le 10)\)個人(沒有那麼多人就該有多少人輸出多少人)。 分

【跟我學oracle18c】第十七天:Multitenant Architecture多租戶框架:2.2 Overview of Commonality in the CDB藍色感悟

在CDB中,每個使用者、角色或物件都是通用的或本地的。類似地,通常或區域性授予特權. This section contains the following topics: About Commonality in a CDB A common phenomenon defined i

A Spy in the Metro dp

A Spy in the Metro   Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling eve

第十二課:Running in the Background基於AndroidStudio3.2

現在我們對UI元素和螢幕有了一些瞭解,我們需要讓它們具有響應性。響應力並不僅僅與速度有關 - 你可以在一段時間內完成多少工作。更重要的是應用程式的速度有多快。當人們說應用程式響應時,通常他們的意思是應用程式不會阻止他們做他們正在嘗試的事情去做。它不會妨礙他們。如果你曾經使用過

ZOJ 3674 Search in the Wiki字典樹 + map + vector

題意:每個單詞都一些tips單詞。先輸入n個單詞和他們的tips。然後m組查詢,每次查詢一些單詞,按字典序輸出這些單詞的公有tips。(每個單詞都都只包含小寫大寫字母) 思路:對第i個單詞,用vector陣列g,g[i]來存這個單詞的所有tips。對於所有單詞建立字典

Codeforces1099D.Sum in the tree貪心

題目連結:傳送門 思路:   一個節點放的數越大,那麼以它為根的子樹的節點權值之和就越小。   所以我們要在合法的範圍內,使偶數層節點的權值儘可能地大。也就是說,令它的權值是子節點的最小值,這樣保證了它的子節點權值為正。   因為奇數層的節點的s已知,所以修改偶數層的節點僅影響,向下一層的節點。(因為

Who's in the Middle水題,誰在中間

Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

A project with that name already exists in the workspace小常識

我們在使用Eclipse時會出現如下問題 I'm new to Eclipse/Java/Android I have already created this project, but I wanted to start over. deleted the Hell

A Spy in the MetroDP

【題意】        某城市裡的地鐵是線性的,有n個車站(2<=n<=50),有M1輛列車從第1站從左往右開,有M2輛列車從第n站從右往左開,在0時刻間諜從第一站出發,目的是要在時刻T(0<=T<=200)與另一個人在第n個車站碰

poj 3070 Fibonacci矩陣快速求Fibonacci數列

代碼 include cnblogs inf stream exp class set names 題目鏈接: http://poj.org/problem?id=3070 題意: 我們知道斐波那契數列0 1 1 2 3 5 8 13…… 數列中的第i位為第i-1位

【POJ 2482】 Stars in Your Window線段樹+離散化+掃描線

d+ opera algorithm ans som lov ble word wait 【POJ 2482】 Stars in Your Window(線段樹+離散化+掃描線) Time Limit: 1000MS M