[NOIP模拟] (二) T1-操作
阅读本文推荐BGM :바다에 띄워 보내는 이야기 (寄给大海的故事) - Yulia
border="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=5362105&auto=1&height=66">问题描述 :
Mstdream有n个数: a1, a2, a3, a4 …. an 每一次,他选出最小的数 x 和最大的数 y ,并且将它们都修改为 y - x,直到所有数都一样。如果所有数据已经都一样了,则不用修改了。
Mstdream想知道,最后剩下的数是什么?
如果无法让所有数一样,输出“No!!!!!!!!!!” (不包括引号)
输入格式
第一行 N ,
接下来一行 N 个数 ai
输出格式
输出最后的数
如果无法让所有数一样,输出“No!!!!!!!!!!”(不包括引号)
Sample Input
3
1 2 3
Sample Output
2
Sample Input
2
5 5
Sample Output
5
数据规模
30% 数据保证有解
60% 数据保证 n <= 5
100% 数据保证n <= 10, 1 <= ai <= 50000
谈一谈想法 :
这道题看了一眼, n <= 10 太小了随便怎么搞都可以,但这个无解的情况不好考虑, 于是在考场上直接打了一个 hash 后来发现可以不用 hash 的, 因为就没有无解的情况, 还是太 naive 了。
题解 :
直接排序找到最大最小, 更新, 到最大最小值相同时退出, 由于最大值一定一直减小一定有解。
下面贴出代码 :
(我这里用了 hash 其实是不用的)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <map>
#include <vector>
using namespace std;
inline int read() {
int i = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') {
if(ch == '-') f = -1; ch = getchar();
}
while(ch >= '0' && ch <= '9') {
i = (i << 3) + (i << 1) + ch - '0'; ch = getchar();
}
return i * f;
}
const int p = 57503;
const int mod = 1e9 + 7;
int a[20];
map<long long, int> b;
int main() {
int n = read();
for(int i = 1; i <= n; ++i) a[i] = read();
sort(a + 1, a + n + 1);
int flag = 1;
while(true) {
if(a[1] == a[n]) break;
long long now = 0;
for(int i = 1; i <= n; ++i)
now = (long long)(now * p + a[i] * 1ll) % mod;
if(b[now]) {
flag = 0;
break;
}
else b[now] = 1;
int x = a[1], y = a[n];
a[1] = a[n] = y - x;
sort(a + 1, a + n + 1);
}
if(flag)
printf("%d\n", a[1]);
else
printf("No!!!!!!!!!!\n");
}
本题结束
本题结束
border="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=30431015&auto=0&height=66">
是不是要犒劳自己一曲音乐呢:The Curse Of The Sad Mummy感谢阅读本篇文章,喜欢的话,点个赞吧,你的鼓励就是我最大的动力
有什么意见,尽情发表吧。