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 ...
随机推荐
- Windows下nginx设置开机自启动
第一步:下载 WinSW https://github.com/winsw/winsw/releases/download/v2.10.3/WinSW.NET4.exe 64位系统 https://g ...
- CSS响应式布局学习笔记(多种方法解决响应式问题)
在做web开发的工作中,会遇到需要我给页面根据设计的要求,进行响应式布局,这里跟大家分享下我对于响应式布局的解决方法: 我主要利用的是CSS3 媒体查询,即media queries,可以针对不同的媒 ...
- Python+Selenium+Unittest实现PO模式web自动化框架(4)
1.PageLocators目录下的具体模块 2.PageLocators目录下主要放置个页面的元素定位.用于统一管理个页面的定位元素. 例如:登录页面的元素定位login_page_locator. ...
- java.io.IOException: Could not find resource com/xxx/xxxMapper.xml
java.io.IOException: Could not find resource com/xxx/xxxMapper.xml 报错内容: org.apache.ibatis.exception ...
- Flutter环境搭建遇坑小结(二)
在上一节中,已经对Flutter运行中始终卡在Running Gradle task 'assembleDebug'...,做出了解决方案,继续往下运行,但是新的问题又出现了: Failed to i ...
- 31-1.解决service iptables save出错
CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替.service命令只保留下了极少部分使用 ...
- (28)Vim 4
1.Vim多窗口编辑 在编辑文件时,有时需要参考另一个文件,如果在两个文件之间进行切换则比较麻烦.可以使用 Vim 同时打开两个文件,每个文件分别占用一个窗口. 例如,在査看 /etc/passwd ...
- docker学习一
由于本人学习精力有限,不能像大学时那样,每个笔记都认真的手敲记录,一些地方会截图展示,并会添加自己的理解和备注,建议大家也做自己的笔记,看别人的可以,但是自己动手并思考记忆更深刻. 1.什么是虚拟化 ...
- 【LAMP】搭建Web网站过程中的记录【Ubuntu18.04+Apache2.4+PHP7.2+MySQL5.7】
全文使用的环境如题,主机使用的是腾讯云主机. 内容应该会是linux和apache这些所有部分都有一点,因为是遇见一个问题就记录一个. 配置LAMP环境 这部分可以参考这篇文章:https://www ...
- Kwp2000协议的应用(程序原理篇)
作者:良知犹存 转载授权以及围观:欢迎添加微信:becom_me 总述 接上篇文章Kwp2000协议的应用(硬件原理使用篇),本篇针对kwp2000协议标准的服务ID详细介绍,以及针对程序实现 ...