好久没打BC了,hack数据怎么输来着。。。真心手速和思维慢了好多。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 1;
int a[MAXN], p[MAXN];
bool vis[5001];
int main()
{
int t; scanf("%d", &t);
while(t--) {
int n, m; scanf("%d%d", &n, &m);
int sum = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sum += a[i];
p[i] = sum % m;
}
memset(vis, false, sizeof(vis));
bool flag = false;
for(int i = 1; i <= n; i++) {
if(p[i] == 0 || vis[p[i]]) {
flag = true; break;
}
vis[p[i]] = true;
}
printf(flag ? "YES\n" : "NO\n");
}
return 0;
}
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <queue>
using namespace std;
typedef __int64 LL;
const int MAXN = 1e5 + 1;
int main()
{
int t; scanf("%d", &t);
while(t--) {
int n, k; scanf("%d%d", &n, &k);
priority_queue<int, vector<int>, less<int> > Q;
for(int i = 1; i <= n - 1; i++) {
int d; scanf("%d", &d); Q.push(d);
}
k--;
while(!Q.empty() && k > 0) {
Q.pop(); k--;
}
LL ans = 0;
while(!Q.empty()) {
ans += Q.top(); Q.pop();
}
printf("%I64d\n", ans + n);
}
return 0;
}
显然是y个平方数,我们直接对x开根号。只要y是由若干个单个质数相乘得到即可。
由于1e9里面素数距离不超过300,4e4里面的素数不超过4500个.
这样时间复杂度最坏O(50 * 300 * 4500),1000ms足够了。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
typedef __int64 LL;
const int MAXN = 1e5 + 1;
int p[MAXN], k;
bool judge(LL n) {
for(int i = 0; i < k && 1LL * p[i] * p[i] <= n; i++) {
if(n % (1LL * p[i] * p[i]) == 0) return false;
}
return true;
}
bool Check(int n) {
for(int i = 2; i * i <= n; i++) {
if(n % i == 0) return false;
}
return true;
}
int main()
{
k = 0;
for(int i = 2; i <= 40000; i++) {
if(Check(i)) {
p[k++] = i;
}
}
//cout << k << endl;
int t; scanf("%d", &t);
while(t--) {
LL x; scanf("%I64d", &x);
LL xx = 1LL * sqrt(x * 1.0);
LL y1, y2;
for(y1 = xx; y1 >= 2; y1--) { // 300
if(judge(y1)) {
break;
}
}
for(y2 = xx + 1; ; y2++) { // 300
if(judge(y2)) {
break;
}
}
if(y1 >= 2) {
printf("%I64d\n", min(x - y1 * y1, y2 * y2 - x));
}
else {
printf("%I64d\n", y2 * y2 - x);
}
}
return 0;
}
抽空补。。。