HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
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
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.
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.
题意就是任意的一堆石头一次只能移动到其他堆石头一次,比如一开始为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中国大学生程序设计竞赛-哈尔滨站-重现赛)的更多相关文章
- HDU6237-A Simple Stone Game-找素因子(欧拉函数)-2017中国大学生程序设计竞赛-哈尔滨站-重现赛
A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU 6235.Permutation (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
- HDU 6273.Master of GCD-差分数组 (2017中国大学生程序设计竞赛-杭州站-重现赛(感谢浙江理工))
Super-palindrome 题面地址:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf 这道题是差分数组的题目,线 ...
- 2017中国大学生程序设计竞赛-哈尔滨站 H - A Simple Stone Game
A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛
/* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...
- HDU 6150 - Vertex Cover | 2017 中国大学生程序设计竞赛 - 网络选拔赛
思路来自 ICPCCamp /* HDU 6150 - Vertex Cover [ 构造 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 给了你一个贪心法找最小覆盖的算法,构造一组 ...
- HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛
普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...
- HDU 6154 CaoHaha's staff(2017中国大学生程序设计竞赛 - 网络选拔赛)
题目代号:HDU 6154 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 CaoHaha's staff Time Limit: 2000/1 ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...
随机推荐
- 批量上传图片(jQuery-File-Upload使用)
jQuery-File-Upload jQuery-File-Upload是一个jquery下的ajax文件上传插件,支持批量上传,github地址:https://github.com/blueim ...
- 【Binary Tree Right Side View 】cpp
题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the ...
- jvm可视化工具jvisualvm插件——Visual GC
转自:http://blog.csdn.net/xuelinmei_happy/article/details/51090115 Visual GC是一个Java 内存使用分析与GC收集的可视化工具插 ...
- python3知识点之---------列表的介绍
1.列表是什么? 它是由一系列特定顺序排序的元素组成.元素可以表示一切任何的事物,元素之间可以没有任何关系.列表用方括号[ ] 表示,元素之间由逗号隔开. 例如表示一系列数字的列表: numbe ...
- Box布局管理
创建wx.BoxSizer对象时可以指定布局方向: hbox = wx.BoxSizer(wx.HORIZONTAL) 设置为水平方向 hbox = wx.BoxSizer() 默认就是就是水平方向的 ...
- 数据结构与算法之顺序表C语言实现
顺序表等相关概念请自行查阅资料,这里主要是实现. 注: 1.顺序表C语言实现: 2.按较简单的方式实现,主要帮助理解,可在此基础上修改,更加完善: 3.提供几个简单函数,可自行添加功能: 4.可用C+ ...
- ZOJ 3717 Balloon ( TLE )
正解2-SAT. 我用DLX想搜一搜的,结果TLE了…… 没什么遗憾,最起码我尝试过了. 扔个代码留作纪念. #include <cstdio> #include <cstring& ...
- HDU 4671 Backup Plan 构造
负载是否平衡只与前两列有关,剩下的只要与前两列不重复就随便放. 第一列我们按1-n这样循环放,第二列每次找个数最少的那个服务器放. #include <cstdio> #include & ...
- hadoop-eclipse环境搭建(二)
Eclipse插件配置 第一步:把我们的"hadoop-eclipse-plugin-1.0.0.jar"放到Eclipse的目录的"plugins"中,然后重 ...
- Ubuntu安装完之后需要做的事情
字体推荐思源 lantern可以设置全局代理 安装好了ubuntu之后,安装gnome主题 安装Gnome之前,升级系统: $ sudo apt update $ sudo apt upgrade 1 ...