LG P7051 [NWRRC2015]Distribution in Metagonia

Description

There are one hundred noble families in the country of Metagonia, and each year some of these families receive several ritual cubes from the Seer of the One. The One has several rules about cube distribution: if a family receives at least one cube, every prime divisor of the number of cubes received should be either $2$ or $3$ , moreover if one family receives $a > 0$ cubes and another family in the same year receives $b < 0$ cubes then a should not be divisible by $b$ and vice versa.

You are the Seer of the One. You know in advance how many cubes would be available for distribution for the next $t$ years. You want to find any valid distribution of cubes for each of these years. Each year you have to distribute all cubes available for that year.

Solution

对于每个$n$可以拆分成$2^x \times 3^y \times a$的形式

找到一个$m$使得$3^m < a < 3^{m+1}$

设$b=a-3^m$

那么$n=2^x \times 3^{y+m} + 2^x \times 3^y \times b$

对于右边递归处理,对于每个子问题,$x$递增,$y$递减,所以一定不互为倍数

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int t,tot;
long long n,temp,ans[100005],base;
inline long long read()
{
    long long f=1,w=0;
    char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') w=(w<<1)+(w<<3)+ch-'0',ch=getchar();
    return f*w;
}
int main()
{
    t=read();
    for(;t;t--)
    {
        tot=0,temp=1ll;
        n=read();
        while(n)
        {
            base=1ll;
            while(!(n%2)) temp*=2,n/=2;
            while(!(n%3)) temp*=3,n/=3;
            while(base*3<=n) base*=3;
            ans[++tot]=temp*base,n-=base;
        }
        printf("%d\n",tot);
        for(int i=1;i<tot;i++) printf("%lld ",ans[i]);
        printf("%lld\n",ans[tot]);
    }
    return 0;
}
[NWRRC2015]Distribution in Metagonia

 

posted @ 2020-12-04 20:01  QDK_Storm  阅读(181)  评论(0编辑  收藏  举报