D - Distribution in Metagonia -数论.

本文介绍了一种解决Metagonia中特殊立方体分配问题的算法。该算法确保每个家庭获得的立方体数量能被2或3的幂次整除,并且任何两个家庭的立方体数量不构成倍数关系。

D - Distribution in Metagonia

 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.
题目大意 :
拆分数字;
然后每个数字由2^x * 3^y 的形式,所以如果 n 可以直接被 2^x * 3^y 整除的话,即 n % (2^x * 3^y) == 0,那么就可以直接输出 n 了。如果不能被直接整除的话,我们可以先将 n 拆解成不能被 3 和 2 整除的形式,这里的话用一个 mul 记录 n 除以多少。
#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
long long  a[maxn],cnt,ans[maxn];
void judge(long long x,long long temp)
{
    long long y;
    while(x%3==0)
    {
        x/=3;
        temp*=3;
    }
    while(x%2==0)
    {
        x/=2;
        temp*=2;
    }
    if(x==1)
        ans[cnt++]=temp;
    else
    {
        y=3;
        while(y*3<x)
        {
            y*=3;
        }
        ans[cnt++]=temp*y;;
        judge(x-y,temp);
    }
}
int main()
{

    ios::sync_with_stdio(false);
    freopen("distribution.in", "r", stdin);
    freopen("distribution.out", "w", stdout);
    int t;
    cin>>t;
    for(int i=0; i<t; i++)
        cin>>a[i];
    for(int i=0; i<t; i++)
    {
        cnt=0;
        judge(a[i],1);
        cout<<cnt<<endl;
        for(int j=0; j<cnt; j++)
        {
            if(j==0)
                cout<<ans[j];
            else
                cout<<" "<<ans[j];
        }
        cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值