UVA 12627 Erratic Expansion

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=1<<29;

ll k,a,b;
ll s[maxn];

ll f(ll i,ll k)
{
    if(i==0) return 0;
    if(i==(1LL<<k)) return s[k];
    if(i>=(1LL<<(k-1))) return 2*s[k-1]+f(i-(1LL<<(k-1)),k-1);
    else return 2*f(i,k-1);
}

int main()
{
    freopen("in.txt","r",stdin);
    int T;cin>>T;int casen=1;
    s[0]=1;REP(i,1,32) s[i]=s[i-1]*3;
    while(T--){
        scanf("%lld%lld%lld",&k,&a,&b);
        printf("Case %d: %lld\n",casen++,f(b,k)-f(a-1,k));
    }
    return 0;
}
/**
这种找规律应该优先找递推式,而不应该先去找通项。

*/
View Code

 

posted @ 2015-12-12 17:39  __560  阅读(192)  评论(0编辑  收藏  举报