2018hdu个人排位赛:Travel

给n个点,求出同等长度路径的最多条数。

实际上题目求的就是,拿掉头尾(特判两个点的情况)求剩下的n-2个点分解成若干整数(可相同)的乘积最大和,快速幂求积(直觉。。。用pow应该会失精度而wa)

#include<bits/stdc++.h>
using namespace std;
//#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
int n;
int poww(int x,int y)
{
    int s = 1;
    while(y > 0)
    {
        if(y%2!=0)
        {
            s = s * x;
        }
        x = x * x;
        y/=2;
    }
    return s;
}
int f(int N)
{
    int k = N / 3;
    if (N == 1)
       return 1;
    if (N % 3 == 0)
        return poww(3, k);
    if (N % 3 == 1)
        return 4 * poww(3, k - 1);
    else
        return 2 * poww(3, k);
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {

        scanf("%d",&n);
        if(n<=3)
        {
            cout<<1<<endl;
            continue;
        }
        if(n==5)
        {
            cout<<3<<endl;
            continue;
        }
        n-=2;
        cout<<f(n)<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41058467/article/details/81051955
今日推荐