Codeforces Round #514 (Div. 2) C.Sequence Transformation(找规律)

题意:给定一个数字,找从1到n这n个数字的最大公约数,然后从这n个数字中去掉一个数字,使得去掉之后剩下的n-1个数字的最大公约数能比之前的大,每次都去掉一个数字,使得最大公约数能够最快的上升。

思路:列出1到9的答案:1:1,2:12,3:113,4:1124,5:11124,6:111226,7:1111226,8:11112248,9:111112248。排除3,6,7,容易发现,1的个数是n/2向上取整,2的个数是n/2/2。所以可以每一次让n/2,如果最后是3,那么就

输出ans ans ans*3。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=200005;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    while(cin>>n)
    {
        int ans=1;
        while(n)
        {
            if(n==3)
            {
                cout<<ans<<" "<<ans<<" "<<ans*3<<endl;
                break;
            }
            else
            {
                for(int i=0;i<(n+1)/2;i++)
                {
                    cout<<ans<<" ";
                }
                if(n/2==0)
                    cout<<endl;
            }
            ans*=2;
            n/=2;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dilly__dally/article/details/82955262