A Simple Stone Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

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 ith 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 ith number ai(1≤ai≤100000) indicating the number of stones of the ith 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
2017 ACM/ICPC 哈尔滨赛区网络赛——测试专用
 

题意就是任意的一堆石头一次只能移动到其他堆石头一次,比如一开始为8,移动一次就为7啦,问最少移动几次使得移动后的数都能够被某个数整除(就是他们有一个因子相同)

思路:石头数求和,求出的值进行欧拉函数找出来素因子,然后通过对石头数进行取余操作,再对剩下的余数进行操作。

只需要将余数求和,然后将余数按从大到小的顺序,将大的数补满(因为不确定是移动哪个小的数,所以直接补大的数),补成素因子,然后减去加上去的数就可以。

思路很好写,但是智障wa了好几次,初始化初始错了,改好之后,又发现for循环是<cnt,不是<=cnt。。。

谢谢我的队友,愿意给我这个猪队友查错。。。

代码:

 #include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+;
ll a[N],b[N];
ll phi[N];
ll sum,cnt;
bool cmp(int a,int b){
return a>b;
} void findprime(){ //找出来素因子
cnt=;
for(ll i=;i<=sqrt(sum);i++){
if(sum%i==)
phi[cnt++]=i;
while(sum%i==)
sum/=i;
}
phi[cnt++]=sum; //找出来的素因子存在这个数组里
} int main(){
int t,n;
ll h,num,ret,minn;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
sum=;
for(int i=;i<n;i++){
scanf("%lld",&a[i]);
sum+=a[i];
}
findprime();
minn=1e10+;
for(int j=;j<cnt;j++){ //跑一遍所有的素因子,本来素因子就很少,不会超时
num=;
for(int i=;i<n;i++)
b[i]=a[i];
for(int i=;i<n;i++){
b[i]=b[i]%phi[j]; //取余操作
num+=b[i];
}
ret=;
sort(b,b+n,cmp);
int i=;
while(num>&&i<n){
ret+=phi[j]-b[i];//将补的次数(移动次数)加到ret中
num-=phi[j];//总数减去
i++;
}
minn=min(minn,ret);//找出移动次数最小的
}
printf("%lld\n",minn);
}
return ;
}

太菜啦,每次都是错在一些很智障的地方上。

本来会写的题目就不多,还经常出一些智障的错误。没写对和写不出来结果是一样的。

菜哭(ಥ_ಥ)

今天双十一,单身狗节快乐_(:з」∠)_

 

HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)的更多相关文章

  1. HDU6237-A Simple Stone Game-找素因子(欧拉函数)-2017中国大学生程序设计竞赛-哈尔滨站-重现赛

    A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  2. HDU 6235.Permutation (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)

    Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tot ...

  3. HDU 6273.Master of GCD-差分数组 (2017中国大学生程序设计竞赛-杭州站-重现赛(感谢浙江理工))

    Super-palindrome 题面地址:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf 这道题是差分数组的题目,线 ...

  4. 2017中国大学生程序设计竞赛-哈尔滨站 H - A Simple Stone Game

    A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  5. HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    /* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...

  6. HDU 6150 - Vertex Cover | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    思路来自 ICPCCamp /* HDU 6150 - Vertex Cover [ 构造 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 给了你一个贪心法找最小覆盖的算法,构造一组 ...

  7. HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  8. HDU 6154 CaoHaha's staff(2017中国大学生程序设计竞赛 - 网络选拔赛)

    题目代号:HDU 6154 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 CaoHaha's staff Time Limit: 2000/1 ...

  9. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...

随机推荐

  1. 【Neural Network】林轩田机器学习技法

    首先从单层神经网络开始介绍 最简单的单层神经网络可以看成是多个Perception的线性组合,这种简单的组合可以达到一些复杂的boundary. 比如,最简单的逻辑运算AND  OR NOT都可以由多 ...

  2. php数组循环的三种方式

    PHP 的遍历数组的三种方式:for循环.foreach循环.while.list().each()组合循环 PHP当中数组分为:索引数组[转换成json是数组]和关联数组[转换成json是对象] f ...

  3. Linux 查看当前日期和时间

    一.查看和修改Linux的时区 1. 查看当前时区 命令 : "date -R" 2. 修改设置Linux服务器时区 方法 A 命令 : "tzselect" ...

  4. 【转】通过制作Flappy Bird了解Native 2D中的Sprite,Animation

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 上一次我们讲了MonoBehaviou ...

  5. 使用IDEA新建Maven项目没有完整的项目结构(src文件夹等等)

    maven还没加载好,右下角还有进度条在从中央仓库读,所以在创建maven项目的时候,需要加archetypeCatalog=internal 右边新增一项 如下图: 此时就可以快速的建立maven项 ...

  6. Spring 对属性文件的加密与解密

    一般用于配置密码等敏感信息 解密/加密工具类 package com.baobaotao.placeholder; import sun.misc.BASE64Decoder; import sun. ...

  7. div样式

    DIV样式汇总 一.常用属性: 1.Height:设置DIV的高度. 2.Width:设置DIV的宽度. 例: <div style="width:200px;height:200px ...

  8. 【BZOJ 5000 OI树】

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 107  Solved: 64[Submit][Status][Discuss] Description ...

  9. 《c程序设计语言》读书笔记-4.12-递归整数转字符串

    #include <stdio.h> #include <math.h> #include <stdlib.h> void itoa_num(int n, char ...

  10. [03] react 测试

    测试是开发周期中的一个重要组成部分.没有测试的代码被称为:遗留代码.对于我而言,第一次学习 React 和 JavaScript 的时候,感到很有压力.如果你也是刚开始学习 JS/React,并加入他 ...