zoukankan      html  css  js  c++  java
  • 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 imes 3^y imes a$的形式

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

    设$b=a-3^m$

    那么$n=2^x imes 3^{y+m} + 2^x imes 3^y imes 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
    ",tot);
            for(int i=1;i<tot;i++) printf("%lld ",ans[i]);
            printf("%lld
    ",ans[tot]);
        }
        return 0;
    }
    [NWRRC2015]Distribution in Metagonia
  • 相关阅读:
    GDOI 2020 赛前两周模拟总结
    猫树模板
    LOJ#2023. 「AHOI / HNOI2017」抛硬币(OGF+ExLucas+Crt)
    扩展Lucas定理及其优化
    LOJ#2018. 「AHOI / HNOI2017」单旋(平衡树模拟+set+线段树)
    LOJ #2008. 「SCOI2015」小凸想跑步(半平面交)
    [TJOI2018]游园会
    [Ynoi2018]未来日记
    「雅礼集训 2018 Day7」B
    「雅礼集训 2018 Day7」A
  • 原文地址:https://www.cnblogs.com/JDFZ-ZZ/p/14087535.html
Copyright © 2011-2022 走看看