ZOJ3538Arrange the Schedule

比赛被虐了,一个都不会,这题只要是找出规律,然后要注意的的mod,MOD要用long long 这坑了好久

/*
** 时间:2014-9-29
** 知识点:
** 题意:F(n) = 3/4 * (3^L - (-1)^L) ;   D(n) = (3^(L+1) + (-1)^L) / 4 ;
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const ll mod=4*1000000007ll;
const ll MOD=1000000007ll;
int n,m;
struct node{
    int pos;
    char c;
}g[20];
bool cmp(node a,node b)
{
    return a.pos<b.pos;
}
long long pow_3(int a)
{
    long long res=1;
    while(a)
    {
        //cout<<a<<endl;
        a--;
        res*=3;
        res%=mod;
        int  b=1;
        long long s=3;
        while(a-b>=0)
        {
            res*=s;
            res%=mod;
            s*=s;
            s%=mod;
            a-=b;
            b<<=1;
        }
    }
    return res;
}
void solve(){
    long long ans=1,num;
    if(m==0)
    {
         ans=4*pow_3(n-1)%mod;
        cout<<ans%MOD<<endl;return;
    }else{
        sort(g,g+m,cmp);
        ans=pow_3(g[0].pos-1)%mod;
        ans=(ans*pow_3(n-g[m-1].pos))%mod;
        for(int i=1;i<m;i++)
        {
            int l=g[i].pos-g[i-1].pos-1;
            if(g[i].c!=g[i-1].c){
                    //(3^(L+1) + (-1)^L) / 4
                num = (((pow_3( l+1 ) -(l&1?1:(-1))) )%mod)/4;
                ans = ans * num % mod;
            }else if(g[i].c==g[i-1].c){
                // 3/4 * (3^L - (-1)^L)
                num = ((3*(pow_3( l ) +(l&1?1:(-1))) )%mod)/4;  ;

                ans = ans * num % mod;
            }
        }
       cout<<ans%MOD<<endl;
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){

        for(int i=0; i<m; i++)
        {
            cin>>g[i].pos>>g[i].c;
        }
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值