UVA12627 Erratic Expansion

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4352

题思路:

找规律题。然后用递归求解。

AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
ll num[35],l[35];
int n;

ll fun(int k,int x)
{
    if(x==0)
        return 0;
    if(x==l[k])
        return num[k];
    else
    {
        if(x>l[k-1])
            return fun(k-1,x%l[k-1])+2*num[k-1];
        else
            return 2*fun(k-1,x);
    }
}

int main()
{
    int i,j;
    num[0]=1;l[0]=1;
    for(i=1;i<=30;i++)
    {
        num[i]=3*num[i-1];
        l[i]=2*l[i-1];
    }
    int T,tt=1;
    scanf("%d",&T);
    while(T--)
    {
        int a,b;
        scanf("%d%d%d",&n,&a,&b);
        printf("Case %d: %lld\n",tt++,fun(n,b)-fun(n,a-1));
    }
    return 0;
}


你可能感兴趣的:(找规律)