2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D:Distribution in Metagonia(构造)
http://codeforces.com/gym/100801/attachments
题意:给出一个数n(1 <= n <= 1e18),将 n 拆成 m 个整数,其中 m 必须是 2^x * 3^y 的形式,并且 x 和 y 不能被彼此整除, 输出 m 并将这些整数输出。
思路:Inspired by http://blog.csdn.net/snowy_smile/article/details/49852091 。
第一步:因为要求的 m 是 2^x * 3^y 的形式,所以如果 n 可以直接被 2^x * 3^y 整除的话,即 n % (2^x * 3^y) == 0,那么就可以直接输出 n 了。如果不能被直接整除的话,我们可以先将 n 拆解成不能被 3 和 2 整除的形式,这里的话用一个 mul 记录 n 除以多少。
while(n % == ) {
n /= ;
mul *= ;
}
while(n % == ) {
n /= ;
mul *= ;
}
if(n == ) {
num[m++] = mul;
return ;
}
第二步:那么更重要的问题是如果 n 不能被 2 或 3 整除,同时也不为 1 时应该怎么做。因为不能被 2 整除,所以这时的 n 必定是一个奇数。那么我们可以将 n 减去 w,w = 3^y && w < n,我们所做的是将 n 拆解出一个 w(w必定为奇数),那么剩下的 n 就是一个偶数了,这个时候我们又能回到第一步进行递归求解了。因为我们拆出的 w 也是组成 n 的一部分,因此要记录下来,记录的数值是 w * mul,因为我们前面 n 除以了 一些 2 和 3 ,我们用 mul 记录下来了,因此这个时候要乘回去,所以是 w * mul。
else {
long long x = ;
while(x * < n) {
x *= ;
}
n -= x;
num[m++] = x * mul;
solve(n, mul);
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <cstdlib>
using namespace std;
typedef long long LL;
LL num[];
int m;
/*
题意:将n拆成m个数,这m个数的表示是
*/
void solve(LL n, LL mul)
{
while(n % == ) {
n /= ;
mul *= ;
}
while(n % == ) {
n /= ;
mul *= ;
}
if(n == ) {
num[m++] = mul;
return ;
} else {
long long x = ;
while(x * < n) {
x *= ;
}
n -= x;
num[m++] = x * mul;
solve(n, mul);
}
} int main()
{
freopen("distribution.in", "r", stdin);
freopen("distribution.out", "w", stdout);
int t;
cin >> t;
while(t--) {
LL n;
cin >> n;
m = ;
solve(n, );
cout << m << endl;
for(int i = ; i < m; i++) {
cout << num[i];
if(i != m-) cout << " ";
else cout << endl;
}
}
return ;
}
2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D:Distribution in Metagonia(构造)的更多相关文章
- 2018-2019 ICPC, NEERC, Southern Subregional Contest
目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...
- Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest
2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D】---暑假三校训练
2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D Problem D. Distribution in Metagonia Input ...
- 模拟赛小结:2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest
2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest 2019年10月11日 15:35-20:35(Solved 8,Penalty 675 ...
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest (9/12)
$$2015-2016\ ACM-ICPC,\ NEERC,\ Northern\ Subregional\ Contest$$ \(A.Alex\ Origami\ Squares\) 签到 //# ...
- ACM ICPC 2016–2017, NEERC, Northern Subregional Contest Problem J. Java2016
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个数字c 用 " ...
- 2016 NEERC, Northern Subregional Contest G.Gangsters in Central City(LCA)
G.Gangsters in Central City 题意:一棵树,节点1为根,是水源.水顺着边流至叶子.该树的每个叶子上有房子.有q个询问,一种为房子u被强盗入侵,另一种为强盗撤离房子u.对于每个 ...
- 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror) Solution
从这里开始 题目列表 瞎扯 Problem A Find a Number Problem B Berkomnadzor Problem C Cloud Computing Problem D Gar ...
- 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest Problem F. Format
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:1s 空间限制:512MB 题目大意: 给定一个字符串,使用%[...] ...
- 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest Problem I. Integral Polygons
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个凸多边形,有一种连接两个 ...
随机推荐
- wpf C# 操作DirectUI窗口 SendMessage+MSAA
原文:wpf C# 操作DirectUI窗口 SendMessage+MSAA 最近做一个抓取qq用户资料的工具,需要获取qq窗口上的消息,以前这种任务是用句柄获取窗口中的信息,现在qq的窗口用的是D ...
- MySQL 执行原生sql
public class MySqlHelper { private YourContext _context; public MySqlHelper(YourContext context) { _ ...
- WPF 4 DataGrid 控件(自定义样式篇)
原文:WPF 4 DataGrid 控件(自定义样式篇) 在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...
- Mysql事务,并发问题,锁机制-- 幻读、不可重复读--专题
1.什么是事务 事务是一条或多条数据库操作语句的组合,具备ACID,4个特点. 原子性:要不全部成功,要不全部撤销 隔离性:事务之间相互独立,互不干扰 一致性:数据库正确地改变状态后,数据库的一致性约 ...
- jquery 访问cookie
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- Genymotion常见问题解决方案
最近更新了Genymotion之后, 出了一大堆问题, 花了差不多两天的时间, 把一系列问题基本遇到遍了, 终于修成正果, 分享出来希望可以让大家少走一点弯路. 无法启动Genymotion;关键字: ...
- wpf 事件参数 绑定到viewmdoel
public sealed class EventCommand : TriggerAction<DependencyObject> { public static readonly De ...
- C++得到当前进程所占用的内存
原文地址:C++得到当前进程所占用的内存作者:雪碧狗 使用SDK的PSAPI (Process Status Helper)中的BOOL GetProcessMemoryInfo( HANDLE P ...
- 探究操作系统的内存分配(malloc)对齐策略
问题: 我们在写程序的时候经常发现程序使用的内存往往比我们申请的多,为了优化程序的内存占用,搅尽脑汁想要优化内存占用,可是发现自己的代码也无从优化了,怎么办?现在我们把我们的焦点放到malloc上,毕 ...
- vs2010 编译release没问题debug编译不通过
------ 已启动全部重新生成: 项目: VM661JTcpDLL, 配置: Debug Win32 ------生成启动时间为 2018-12-29 14:07:20.项目文件包含 ToolsVe ...