HDU4291A Short problem

來源:互聯網
上載者:User

隨意選了一題,就作為部落格生涯的開端吧^^

#include<stdio.h>typedef __int64 LL;/*g(n) % MOD1 = g(n % MOD2) % MOD1;//MOD2為迴圈節,以下如上g(n) % MOD2 = g(n % MOD3) % MOD2;   g(g(g(n))) % MOD1= g(g(g(n)) % MOD2) % MOD1= g(g(g(n) % MOD3) % MOD2) % MOD1;n = g(n) % MOD3;n = g(n) % MOD2;n = g(n) % MOD1;*/const LL MOD1 = 1000000007;const LL MOD2 = 222222224;const LL MOD3 = 183120;const LL base[2][2] = {3, 1, 1, 0};LL ans[2][2], tmp[2][2];/*int main()//求迴圈節{    LL n0 = 0, n1 = 1, tt;    LL mod = MOD2;//改變該值    for(int i = 1; ;i++)    {        tt = (3 * n1 + n0) % mod;        n0 = n1;        n1 = tt;        if(n0 == 0 && n1 == 1)        {            printf("%d\n", i);            break;        }    }}*/inline void Mult(LL pro[][2], LL mul1[][2], LL mul2[][2], LL MOD)//ans = mul1 * mul2;{    LL m1[2][2], m2[2][2];    for(int i = 0; i < 2; i++)        for(int j = 0; j < 2; j++)            m1[i][j] = mul1[i][j], m2[i][j] = mul2[i][j];    for(int i = 0; i < 2; i++)        for(int j = 0; j < 2; j++)        {            pro[i][j] = 0;            for(int k = 0; k < 2; k++)                pro[i][j] += m1[i][k] * m2[k][j];            pro[i][j] %= MOD;        }}inline void FastPow(LL n, LL MOD)//矩陣的base^n{    ans[0][0] = 1, ans[0][1] = 0;    ans[1][0] = 0, ans[1][1] = 1;//單位矩陣    for(int i = 0; i < 2; i++)        for(int j = 0; j < 2; j++)            tmp[i][j] = base[i][j];    while(n)    {        if(n & 1)        {            Mult(ans, ans, tmp, MOD);        }        Mult(tmp, tmp, tmp, MOD);        n >>= 1;    }}int main(){    LL m;    while(scanf("%I64d", &m) != EOF)    {        FastPow(m, MOD3);        m = ans[1][0];        FastPow(m, MOD2);        m = ans[1][0];        FastPow(m, MOD1);        m = ans[1][0];        printf("%I64d\n", m);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.