[Codeforces - Gym10081D (NEERC)] Distribution in Metagonia (构造+数的拆分)

Codeforces - Gym10081D (NEERC)

给定一个数,将其变成若干个数的和
如果这些数有因子,那么只能是 2或 3
并且这些数两两不能除尽


构造题
如果这个数为 1,那么答案就为 1
若不为 1,那么如果他是奇数
那么我减去最大的一个 3次幂转化为一个偶数,
再不断地提出 2,变成一个奇数,循环直到其变为 1
所以这个数 N=3a1+2b1(3a2+2b2(...))
其中 3的次数 a1>a2>a3...
而展开后 2的次数 0<b1×b2×b3...<b2×b3...<b3...
所以这些数按顺序排开,他们 3这个因子的次数是递减的
而 2这个因子的次数是递增的,所以相互不能整除,即为答案

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define SQR(a) ((a)*(a))
#define PCUT puts("\n----------")

int siz;
LL outp[110], pow3[110];

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    #endif

    #ifndef LOCAL
    freopen("distribution.in", "r", stdin);
    freopen("distribution.out", "w", stdout);
    #endif

    pow3[0]=1;
    for(int i=1; i<=39; i++) pow3[i]=pow3[i-1]*3;

    int T;
    scanf("%d", &T);
    for(int ck=1; ck<=T; ck++)
    {
        LL N, mul=1;
        siz = 0;
        cin >> N;
        while(N)
        {
            while(!(N&1)) {mul<<=1; N>>=1;}
            for(int i=39; i>=0; i--) if(pow3[i]<=N)
            {
                outp[siz++] = mul*pow3[i];
                N -= pow3[i];
                break;
            }
        }
        printf("%d\n", siz);
        for(int i=0; i<siz; i++) cout << outp[i] << ' ';
        puts("");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值