codevs 1455 路徑

來源:互聯網
上載者:User

標籤:

題目連結

題目描述 Description

小明從A1到An+1,他知道從A1到A2,從A2到A3,......,從An到An+1都有m條路,且從A1到An+1都只有這些路。小明想知道,從A1地到An+1地共有多少種方法,由於答案可能會很大,小明只要你輸出總方案數mod k。

輸入描述 Input Description

輸入共1行,三個正整數m,n,k

輸出描述 Output Description

輸出共1行,表示答案

範例輸入 Sample Input

3 2 100

範例輸出 Sample Output

9

資料範圍及提示 Data Size & Hint

假設從A1到A2的所有路為W1,W2,W3,從A2到A3的所有路為W4,W5,W6

方案如下: 

W1>>W4
W2>>W4
W3>>W4
W1>>W5
W2>>W5
W3>>W5
W1>>W6
W2>>W6
W3>>W6
共9種方案 

對於100%的資料,m,k≤1,000,000,000,n≤101,000,000

  題意很明顯, 就是m^n%k, 但是n超級大, 所以我們用公式。 m^n%k = m^(n%phi(k)+phi(k))%k。 一下就算出來了好神奇...
#include <iostream>#include <vector>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <map>#include <set>#include <string>#include <queue>#include <stack>#include <bitset>using namespace std;#define pb(x) push_back(x)#define ll long long#define mk(x, y) make_pair(x, y)#define lson l, m, rt<<1#define mem(a) memset(a, 0, sizeof(a))#define rson m+1, r, rt<<1|1#define mem1(a) memset(a, -1, sizeof(a))#define mem2(a) memset(a, 0x3f, sizeof(a))#define rep(i, n, a) for(int i = a; i<n; i++)#define fi first#define se secondtypedef pair<int, int> pll;const double PI = acos(-1.0);const double eps = 1e-8;const int mod = 1e9+7;const int inf = 1061109567;const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };ll p;ll get_phi(ll n){    ll res = n,i,j;    for(i=2;i*i<=n;i++)    {        if(n%i==0)        {            n=n/i;            while(n%i==0)                n=n/i;            res=res/i*(i-1);        }        if(n<(i+1))            break;    }    if(n>1)        res = res/n*(n-1);    return res;}ll pow(ll a, ll b) {    ll ret = 1;    while(b) {        if(b&1)            ret = ret*a%p;        a = a*a%p;        b>>=1;    }    return ret;}int main(){    int m;    string n;    cin>>m>>n>>p;    ll phi = get_phi(p);    ll tmp = 0;    for(int i = 0; i<n.size(); i++) {        tmp = tmp*10+n[i]-‘0‘;        tmp %= phi;    }    tmp += phi;    ll ans = pow(1LL*m, tmp)%p;    cout<<ans<<endl;    return 0;}

 

codevs 1455 路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.