ZOJ HDU 3538 Arrange the Schedule

來源:互聯網
上載者:User

 打表找規律,推出了公式,但是各種調試還是不過

最後發現在公式裡有一個除法,而對相應的模數操作忘處理了,導致一直WA到比賽結束都沒調出來

以後要注意這個問題了。。。

這題類似高中的排列組合裡的傳球問題, 甲乙丙丁4人傳球,從甲傳n次到甲 和從甲傳n次到乙丙丁的個數即為所要求的分段的個數。

能夠推出公式分別自己到自己和自己到其他人2種 , 分奇偶2種公式

            if ( Node[i].s == Node[i-1].s )            {                temp = ((3*(cal_3( t ) +(t&1?1:(-1))) )%mod)/4;                //這裡的除法一開始沒注意,凡涉及和除法有關的mod都要處理下,要記住這次教訓                ans = ans * temp%mod_;            }            else if ( Node[i].s != Node[i-1].s)            {                temp = (((cal_3( t+1 ) -(t&1?1:(-1))) )%mod)/4;                ans = ans * temp%mod_;            }

 

 

#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;typedef unsigned long long ll;const ll mod=4*1000000007ll;const ll mod_=1000000007ll;ll pow3[100];//3^(2^i);long long cal_3( int n ){    //cout << n << endl;    ll res=1;    for (int t=0 ; n ; n>>=1 ,t++ )        if(n&1)res=res*pow3[t]%mod;    //cout << res << endl;    return res;}struct node {char s ;int loc;}Node[15];bool cmp ( node a, node b ){    return a.loc < b. loc;}int main(){    int n , m;    char c;    ///    pow3[0]=3;    for (int i=0 ; i<50 ;++i)    {        pow3[i+1]=pow3[i]*pow3[i]%mod;        //printf("%lld  %lld\n",pow3[i] * pow3[i] ,pow3[i+1]);    }    ///    //freopen ("in.txt" , "r" , stdin);    //freopen ("out.txt" , "w" ,stdout );    while (scanf("%d%d",&n , &m ) != EOF )    {        if(m==0){cout <<(4*cal_3(n-1)%mod_) <<endl;continue;}        for ( int i = 0 ; i < m ; i ++ )        {            scanf("%d%c%c" ,&Node[i].loc, &c ,&Node[i].s );        }        long long ans = 1;        sort ( Node , Node + m , cmp );        ans = ans*cal_3(Node[0].loc-1 )%mod_;        //cout << ans << endl;        for ( int i = 1 ; i < m ; i ++ )        {            int t = (Node[i].loc - Node[i-1].loc -1 );            long long temp;            if ( Node[i].s == Node[i-1].s )            {                temp = ((3*(cal_3( t ) +(t&1?1:(-1))) )%mod)/4;                //這裡的除法一開始沒注意,凡涉及和除法有關的mod都要處理下,要記住這次教訓                ans = ans * temp%mod_;            }            else if ( Node[i].s != Node[i-1].s)            {                temp = (((cal_3( t+1 ) -(t&1?1:(-1))) )%mod)/4;                ans = ans * temp%mod_;            }        }        ans = ans*cal_3( n - Node[m-1].loc )%mod_;        //printf("%d\n", ans%mod );        cout << (ans % mod_ ) << endl;    }    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.