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

2024-06-21 19:38

本文主要是介绍[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 LOCALfreopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);#endif#ifndef LOCALfreopen("distribution.in", "r", stdin);freopen("distribution.out", "w", stdout);#endifpow3[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;
}

这篇关于[Codeforces - Gym10081D (NEERC)] Distribution in Metagonia (构造+数的拆分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1082144

相关文章

将一维机械振动信号构造为训练集和测试集(Python)

从如下链接中下载轴承数据集。 https://www.sciencedirect.com/science/article/pii/S2352340918314124 import numpy as npimport scipy.io as sioimport matplotlib.pyplot as pltimport statistics as statsimport pandas

PHP序列化用到的构造:__sleep() __wakeup()

串行化serialize可以把变量包括对象,转化成连续bytes数据. 你可以将串行化后的变量存在一个文件里或在网络上传输. 然后再反串行化还原为原来的数据. 你在反串行化类的对象之前定义的类,PHP可以成功地存储其对象的属性和方法. 有时你可能需要一个对象在反串行化后立即执行. 为了这样的目的,PHP会自动寻找__sleep和__wakeup方法.   当一个对象被串行化,PHP会

代码随想录算法训练营第三十九天|62.不同路径 63. 不同路径 II 343.整数拆分 96.不同的二叉搜索树

LeetCode 62.不同路径 题目链接:62.不同路径 踩坑:二维的vector数组需要初始化,否则会报错访问空指针 思路: 确定动态数组的含义:dp[i][j]:到达(i,j)有多少条路经递推公式:dp[i][j] = dp[i-1][j] + dp[i][j-1]初始化动态数组:dp[0][0] = 1遍历顺序:从左到右,从上到下 代码: class Solution {pu

leetcode刷题(97)——106. 从中序与后序遍历序列构造二叉树

根据一棵树的中序遍历与后序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 中序遍历 inorder = [9,3,15,20,7]后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树: 3/ \9 20/ \15 7 看下后序和中序遍历的框架: void traverse(TreeNode root) {trave

leetcode刷题(97)——105. 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7 1.先回顾前序遍历和中序遍历的框架: void traverse(TreeNode root) {//

Codeforces 158B

很久没有刷题了,刚刚有小学弟问了这道题,AC之后贴上来吧。水~~~ #include <cstdio>#include <cmath>int main() {int n;while(scanf("%d", &n) != EOF) {int a = 0, b = 0, c = 0, d = 0;int arr[100001];for (int i = 0; i < n; ++

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin

「动态规划」如何解决单词拆分问题?

139. 单词拆分https://leetcode.cn/problems/word-break/description/ 给你一个字符串s和一个字符串列表wordDict作为字典。如果可以利用字典中出现的一个或多个单词拼接出s则返回true。注意:不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。 输入:s = "leetcode",wordDict = ["leet", "c

day16--513.找树左下角的值+112. 路径总和+106.从中序与后序遍历序列构造二叉树

一、513.找树左下角的值 题目链接:https://leetcode.cn/problems/find-bottom-left-tree-value/ 文章讲解:https://programmercarl.com/0513.%E6%89%BE%E6%A0%91%E5%B7%A6%E4%B8%8B%E8%A7%92%E7%9A%84%E5%80%BC.html 视频讲解:https://www