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

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。。。

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

代码:

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 typedef long long ll;
 8 const int N=1e5+10;
 9 ll a[N],b[N];
10 ll phi[N];
11 ll sum,cnt;
12 bool cmp(int a,int b){
13     return a>b;
14 }
15
16 void sushu(){   //找出来素因子
17     cnt=0;
18     for(ll i=2;i<=sqrt(sum);i++){
19         if(sum%i==0)
20             phi[cnt++]=i;
21         while(sum%i==0)
22             sum/=i;
23     }
24     phi[cnt++]=sum;   //找出来的素因子存在这个数组里
25 }
26
27 int main(){
28     int t,n;
29     ll h,num,ret,minn;
30     scanf("%d",&t);
31     while(t--){
32         scanf("%d",&n);
33         sum=0;
34       for(int i=0;i<n;i++){
35         scanf("%lld",&a[i]);
36         sum+=a[i];
37       }
38     sushu();
39     minn=1e10+10;
40     for(int j=0;j<cnt;j++){   //跑一遍所有的素因子,本来素因子就很少,不会超时
41         num=0;
42         for(int i=0;i<n;i++)
43             b[i]=a[i];
44         for(int i=0;i<n;i++){
45             b[i]=b[i]%phi[j]; //取余操作
46             num+=b[i];
47         }
48         ret=0;
49         sort(b,b+n,cmp);
50         int i=0;
51         while(num>0&&i<n){
52             ret+=phi[j]-b[i];//将补的次数(移动次数)加到ret中
53             num-=phi[j];//总数减去
54             i++;
55         }
56         minn=min(minn,ret);//找出移动次数最小的
57     }
58     printf("%lld\n",minn);
59     }
60     return 0;
61 }

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

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

菜哭(?_?)

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

时间: 2024-08-17 14:37:49

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

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

A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1526    Accepted Submission(s): 346 Problem Description After he has learned how to play Nim game, Bob begins to try another

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha&#39;s staff(几何找规律)

Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in CaoHaha's hand.As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.But now,hi

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B. Goldbach-米勒拉宾素数判定(大素数)

若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目传送门 自己看题意吧,直接贴代码了. 代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdlib> 6 #i

找新朋友(欧拉函数)

找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8976    Accepted Submission(s): 4736传送门 Problem Description 新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大于1的

HDU 1286 找新朋友(欧拉函数模板)

HDU 1286 找新朋友 题意:中文题. 思路:欧拉函数的纯模板题,没什么好说的,主要是理解欧拉函数的意义. 在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等. 例如φ(8)=4,因为1,3,5,7均和8互质.   ----by度娘. #include <stdio.h> int eular(int n){ int ret = 1; for(int i = 2; i*

Help Tomisu UVA - 11440 难推导+欧拉函数,给定正整数N和M, 统计2和N!之间有多少个整数x满足,x的所有素因子都大于M (2&lt;=N&lt;=1e7, 1&lt;=M&lt;=N, N-M&lt;=1E5) 输出答案除以1e8+7的余数。

/** 题目:Help Tomisu UVA - 11440 链接:https://vjudge.net/problem/UVA-11440 题意:给定正整数N和M, 统计2和N!之间有多少个整数x满足,x的所有素因子都大于M (2<=N<=1e7, 1<=M<=N, N-M<=1E5) 输出答案除以1e8+7的余数. 思路: lrjP338 由于x的所有素因子都>M:那么x与M!互质. 根据最大公约数的性质,对于x>y,x与y互质,那么x%y与y也互质. 由于N

poj 3090 (欧拉函数,找规律)

poj 3090 (欧拉函数,找规律) 题目: 给出一个n*n的点阵,求从(0,0)出发斜率不相等的直线有多少条. 限制: 1 <= n <= 1000 思路: 先定义sum[i] sum[i] = 0, if(i == 1) sum[i] = sum[i-1] + phi[i], if(i >= 2) ans = sum[n] * 2 + 3 /*poj 3090 题目: 给出一个n*n的点阵,求从(0,0)出发斜率不相等的直线有多少条. 限制: 1 <= n <= 100

(hdu step 2.1.6)找新朋友(欧拉函数的简单使用:求与n互质的元素的个数)

题目: 找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2788 Accepted Submission(s): 1307   Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大

HDU - 1286 找新朋友(欧拉函数)解题

找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13024    Accepted Submission(s): 6941 Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有