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

Problem Description
After he has learned how to play Nim game, Bob begins to try another stone game which seems much easier.

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.

 
Input
The first line is the number of test cases. For each test case, the first line contains one positive number N(1≤N≤100000)

, 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

.

 
Output
For each test case, output a integer donating the answer as described above. If there exist a satisfied number x

initially, you just need to output 0

. It's guaranteed that there exists at least one solution.

 
Sample Input
2
5
1 2 3 4 5
2
5 7
 
Sample Output
2
1
 
Source
 
铜牌题坑就是多呀,注意读懂题意,sum是会爆int的,还有sum本身就有可能是个素数,其他的就没啥坑了。
 
解题思路:求出石子总数sum,能整除sum的质数肯定满足x,遍历1-1e5的所有质数,统计满足x的有多少个。然后遍历满足x的质数,求出最小的移动次数,与sum减去最大堆(sum为质数的情况)的值作比较,取小的便是最终答案。具体操作看代码,代码比文字好理解(●'◡'●)
 
  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的更多相关文章

  1. 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 ...

  2. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  4. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  5. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  7. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  10. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. floating point

    记录浮点数的单精度和双精度(IEEE754) 1.单精度(float) ​ 1.定义:单精度占4字节/32位,其中1号位符号位,其次是8位阶码/指数(阶符+阶数),23位尾数(小数). 2.双精度(d ...

  2. 转 Fiddler4 手机抓包

    Fiddler4 手机抓包  文章转自:https://www.cnblogs.com/zhengna/p/10876954.html 1.要对计算机Fiddler进行配置,允许远程计算机连接. 2. ...

  3. Jmeter5.1.1 把默认语言调整为中文

    进入安装目录:apache-jmeter-5.1.1\bin\ 找到 jmeter.properties文件 搜索" language=en ",前面带有"#" ...

  4. centos系统磁盘扩容

    1.查看磁盘空间大小,使用df -h 命令. 2. 增加磁盘空间,例如下图使用VM虚拟机增加的方式.物理机直接安装挂载上去. 3. 使用fdisk /dev/sda, 创建新分区. 4.重启Linux ...

  5. Go 如何实现热重启

    https://mp.weixin.qq.com/s/UVZKFmv8p4ghm8ICdz85wQ Go 如何实现热重启 原创 zhijiezhang 腾讯技术工程 2020-09-09  

  6. It is thread-safe and idempotent, but not reentrant.

    https://github.com/django/django/blob/master/django/apps/registry.py

  7. __init__ raises an exception, then __del__ will still be called

    issue 808164: socket.close() doesn't play well with __del__ - Python tracker https://bugs.python.org ...

  8. GDB查看内存命令(x命令) 用gdb查看指定地址的内存内容

    GDB查看内存命令(x命令) - super119 - 博客园 https://www.cnblogs.com/super119/archive/2011/11/18/2254382.html 可以使 ...

  9. Linux centos7编译源码安装redis

    1.安装准备 ① 由于redis底层用c语言编写的,安装redis需要先将官网下载的源码进行编译,编译依赖make和gcc环境,如果没有则需要安装(一般系统中已经装了了make和gcc,无须再装) 安 ...

  10. JavaScript——事件

    事件:是由访问Web页面的用户引起一系列操作. 事件的作用:用于浏览器和用户的交互 以下代码为相关试验代码: HTML事件: <script type="text/javascript ...