hdu-6237
A Simple Stone Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1247 Accepted Submission(s): 287
The game goes like this: one player starts the game with N
piles of stones. There is ai
stones on the i
th pile. On one turn, the player can move exactly one stone from one pile to another pile. After one turn, if there exits a number x(x>1)
such that for each pile bi
is the multiple of x
where bi
is the number of stone of the this pile now), the game will stop. Now you need to help Bob to calculate the minimum turns he need to stop this boring game. You can regard that 0
is the multiple of any positive number.
, indicating the number of piles of stones.
The second line contains N
positive number, the i
th number ai(1≤ai≤100000)
indicating the number of stones of the i
th pile.
The sum of N
of all test cases is not exceed 5∗105
.
initially, you just need to output 0
. It's guaranteed that there exists at least one solution.
5
1 2 3 4 5
2
5 7
1
1 #include<iostream>
2 #include<string.h>
3 #include<algorithm>
4 #include <cstdio>
5 using namespace std;
6 typedef long long ll;
7 const int maxn = 1e5+10;
8 int nu[maxn];
9 int prim[maxn];
10 bool isprim[maxn];
11 int res[maxn];
12 int cop[maxn];
13 int len;
14 void euler(int maxx)
15 {
16 memset(isprim,true,sizeof(isprim));
17 isprim[1]=false;
18 len=0;
19 for(int i=2;i<=maxx;++i)
20 {
21 if(isprim[i]) prim[++len]=i;
22 for(int j=1;j<=len && i*prim[j]<=maxx;++j)
23 {
24 isprim[i*prim[j]]=false;
25 if(i%prim[j]==0) break;
26 }
27 }
28 }
29 int main()
30 {
31 euler(1e5);
32 int t;
33 scanf("%d",&t);
34 while(t--)
35 {
36 int n;
37 scanf("%d",&n);
38 ll sum=0;
39 for(int i=0;i<n;++i)
40 {
41 scanf("%d",&nu[i]);
42 sum+=nu[i];
43 }
44 sort(nu,nu+n);
45 ll ans = sum-nu[n-1];
46 int le=0;
47 for(int i=1;i<=len;++i)
48 {
49 if(sum%prim[i]==0)
50 {
51 res[++le]=prim[i];
52 while(sum%prim[i]==0)
53 sum/=prim[i];
54 }
55 }
56
57 for(int i=1;i<=le;++i)
58 {
59 // printf("%di ",res[i]);
60 for(int j=0;j<n;++j)
61 {
62 cop[j]=nu[j]%res[i];
63 // printf("%d ",cop[j]);
64 }
65 sort(cop,cop+n);
66 ll cnt=0;
67 int l=0,r=n-1;
68 while(l<r)
69 {
70 // printf("@");
71 if(cop[l]==0)
72 {
73 l++;
74 continue;
75 }
76 if(cop[l]+cop[r]==res[i])
77 {
78 cnt+=cop[l];
79 // printf("\n%d##\n",cnt);
80 l++;
81 r--;
82 }
83 else if(cop[l]+cop[r]<res[i])
84 {
85 cop[r]+=cop[l];
86 cnt+=cop[l];
87 l++;
88 }
89 else if(cop[l]+cop[r]>res[i])
90 {
91 cnt+=res[i]-cop[r];
92 cop[l]-=(res[i]-cop[r]);
93 r--;
94 }
95 }
96 // printf("\n%d##\n",cnt);
97 ans=min(cnt,ans);
98 // printf("\n%d###\n",ans);
99 }
100 printf("%lld\n",ans);
101 }
102 return 0;
103 }
hdu-6237的更多相关文章
- HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- [Poi2005]Piggy Banks小猪存钱罐
题目描述 Byteazar有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar已经把每个存钱罐的钥匙放到了某些存钱罐里. Byteazar 现在想买一台汽车于是要把所有的钱都取 ...
- C#从入门到放弃治疗一:初探C#世界
C#是一款高级的面向对象语言,运行于.NET framework之上的高级程序设计语言.其语言规范和,语法和java有着惊人的类似之处.所以如果你在学习C#之前有着java的基础,你将快速地入门.当然 ...
- mysql事务测试
mysql事务测试 打开mysql的命令行,将自动提交事务给关闭 --查看是否是自动提交 1表示开启,0表示关闭 select @@autocommit; --设置关闭 set autocommit ...
- ChannelNets: 省力又讨好的channel-wise卷积,在channel维度进行卷积滑动 | NeurIPS 2018
Channel-wise卷积在channel维度上进行滑动,巧妙地解决卷积操作中输入输出的复杂全连接特性,但又不会像分组卷积那样死板,是个很不错的想法 来源:晓飞的算法工程笔记 公众号 论文: C ...
- java小技巧
String 转 Date String classCode = RequestHandler.getString(request, "classCode"); SimpleDat ...
- 无刷电调基础知识以及BLHeli固件烧录和参数调整
标题: 无刷电调基础知识以及BLHeli固件烧录和参数调整 作者: 梦幻之心星 sky-seeker@qq.com 标签: [#基础知识,#电调,#BLHeli,#固件,#烧录,#调参] 目录: [电 ...
- (G)I-DLE—화(火花) (HWAA)
闲来无事又来推歌了/cy 我这博客好像只能用来更日记+推歌了/kk 到今天(G)I-DLE已经获得九个一位啦~ 歌真的挺不错的 特别是,一个韩国女团出了这首歌的中文版 就觉得很有好感 music 韩文 ...
- 十一:SpringBoot-事务管理
SpringBoot-事务管理 1.事务管理简介 1.1 特性:ACID 1.2 隔离问题 1.3 隔离级别 2.Spring事务管理 2.1 顶级接口 2.2 事务状态 2.3 事务定义 1.事务管 ...
- windows提权常用系统漏洞与补丁编号速查对照表
#Security Bulletin #KB #Description #Operating System CVE-2020-0787 [Windows Background Intelligent ...
- 阿里云MQ
阿里云众多中间件服务中有一款非常强大的中间见服务,在企业互联网架构中起到不可替代的作用,相比较开源的RabbitMQ,阿里的消息队列MQ承受的住阿里内部1000+核心应用的使用,每天转几千条消息,稳定 ...