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
  • 相关阅读:
    第5章 JDBC/ODBC服务器
    第4章 SparkSQL数据源
    第3章 SparkSQL解析
    第2章 执行SparkSQL查询
    第1章 Spark SQL概述
    Ubutun重启网卡
    Java面试通关要点汇总整理
    40道Java基础常见面试题及详细答案
    ListView
    数据库表及字段命名规范
  • 原文地址:https://www.cnblogs.com/JDFZ-ZZ/p/14087535.html
Copyright © 2011-2022 走看看