A. Multiplication Table
题意: 水题 - -
//
// Created by TaoSama on 2015-09-11
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, m;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &m) == 2) {
int ans = 0;
for(int i = 1; i <= n; ++i) {
if(m % i == 0 && m / i <= n) ++ans;
}
printf("%d\n", ans);
}
return 0;
}
B. Modulo Sum
题意: 求一个序列中选出一些数的和能被m整除
分析: 根据鸽笼原理 n > m一定有解
We have a1, a2, ... an.
Make array Si = sum ak from 1 to i (S1 = a1; S2 = a1 + a2; ... ).
If n > m, there are Si and Sj which Si % m = Sj % m and j > i.
So (Sj — Si) % m = 0. Since Sj — Si = a1+a2+...+aj — (a1+a2+...+ai) = a(i+1) + ... + aj.
So we've got answer.
蓝后对于 n<=m dp就可以了
//
// Created by TaoSama on 2015-09-11
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e6 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, m, a[N];
bool dp[2][1005];
bool judge() {
if(n > m) return true;
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(!dp[i & 1][j]) continue;
dp[(i + 1) & 1][(j + a[i + 1]) % m] = dp[(i + 1) & 1][j] = 1;
if((j + a[i + 1]) % m == 0) return true;
}
}
return false;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &m) == 2) {
for(int i = 1; i <= n; ++i) scanf("%d", a + i);
puts(judge() ? "YES" : "NO");
}
return 0;
}
C. Vasya and Petya's Game
题意: 选出一些数 每次可以问1~n能不能整除这些数 求最少的数 并构造一组解
分析: 首先质数一定选, 只能被自己整除不能被其它所有数整除
然后对于每个1~n的数 当前数如果已经有能整数的数被选择了.
如果这些数的最小公倍数也被选了, 那么当前这个数一定要选 不然无法区分
//
// Created by TaoSama on 2015-09-11
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
bool vis[1005];
void gao() {
for(int i = 2; i <= 1000; ++i) {
if(!vis[i]) {
for(int j = i + i; j <= 1000; j += i) vis[j] = true;
}
}
}
long long __lcm(long long a, long long b) {
return a / __gcd(a, b) * b;
}
int n;
bool have[1005];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
gao();
while(scanf("%d", &n) == 1) {
if(n == 1) {
puts("0\n");
continue;
}
vector<int> ans;
memset(have, false, sizeof have);
for(int i = 2; i <= n; ++i) {
if(!vis[i]) ans.push_back(i), have[i] = true;
else {
long long lcm = 1;
for(int j = 0; j < ans.size(); ++j)
if(i % ans[j] == 0) lcm = __lcm(lcm, ans[j]);
if(have[lcm]) {
ans.push_back(i);
have[i] = true;
}
}
}
printf("%d\n", ans.size());
for(int i = 0; i < ans.size(); ++i)
printf("%d%c", ans[i], " \n"[i == ans.size() - 1]);
}
return 0;
}
E. Points on Plane
题意: 平面上有n个点 构造一个哈密顿路 并且使得曼哈顿距离和<= 2.5e9
分析: 显然应该波浪形构造, 尝试验证正确性, 假设选出x距离最近的, 那么对于y以波浪形排序的话
分成sqrt(n)大小的矩形的话 x的最坏差距是sqrt(n) y最坏是10^6
一共有sqrt(n)个 那么加起来就是1000 * (1000 + 10^6) = 1e9 + 1e6 左右 估算的话
构造是正确的 然后就可以用分块乱搞了
//
// Created by TaoSama on 2015-09-11
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e6 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n;
inline int read() {
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = 0;
while(isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
const int B = 1000;
vector<pair<int, int> > a[B + 5];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
n = read();
for(int i = 0; i < n; ++i) {
int x, y;
x = read(); y = read();
a[x / B].push_back({y, i + 1});
}
for(int i = 0; i <= B; ++i) {
sort(a[i].begin(), a[i].end());
if(i & 1) reverse(a[i].begin(), a[i].end());
for(int j = 0; j < a[i].size(); ++j)
printf("%d ", a[i][j].second);
}
puts("");
return 0;
}