本文涉及知识点
P10136 USACO24JAN Cowlendar S
题目描述
Bessie 在一个陌生的星球上醒来。这个星球上有 N N N( 1 ≤ N ≤ 10 4 1\le N\le 10^4 1≤N≤104)个月,分别有 a 1 , ... , a N a_1,\ldots,a_N a1,...,aN 天( 1 ≤ a i ≤ 4 ⋅ 10 9 1\le a_i\le 4\cdot 10^9 1≤ai≤4⋅109,所有 a i a_i ai 均为整数)。此外,这个星球上还存在周,一周为 L L L 天,其中 L L L 是一个正整数。有趣的是,Bessie 知道以下事情:
- 对于正确的 L L L,每个月至少有 4 4 4 周。
- 对于正确的 L L L, a i m o d L a_i\bmod L aimodL 至多有 3 3 3 个不同值。
不幸的是,Bessie 忘记了 L L L 是多少!请通过输出 L L L 的所有可能值之和来帮助她。
注意这个问题涉及到的整数可能需要使用 64 位整数型(例如,C/C++ 中的 "long long")。
输入格式
输入的第一行包含一个整数 N N N。第二行包含 N N N 个空格分隔的整数 a 1 , ... , a N a_1,\ldots,a_N a1,...,aN。
输出格式
输出一个整数,为 L L L 的所有可能值之和。
输入输出样例 #1
输入 #1
12
31 28 31 30 31 30 31 31 30 31 30 31
输出 #1
28
输入输出样例 #2
输入 #2
4
31 35 28 29
输出 #2
23
说明/提示
样例解释 1
L L L 的可能值为 1 1 1, 2 2 2, 3 3 3, 4 4 4, 5 5 5, 6 6 6 和 7 7 7。例如, L = 7 L=7 L=7 是合法的,因为每个月的至少有 4 ⋅ 7 = 28 4\cdot 7=28 4⋅7=28 天,且每个月的天数模 7 7 7 的余数均为 0 0 0, 2 2 2 或 3 3 3。
样例解释 2
L L L 的可能值为 1 1 1, 2 2 2, 3 3 3, 4 4 4, 6 6 6 和 7 7 7。例如, L = 6 L=6 L=6 是合法的,因为每个月的至少有 4 ⋅ 6 = 24 4\cdot 6=24 4⋅6=24 天,且每个月的天数模 6 6 6 的余数均为 1 1 1, 4 4 4 或 5 5 5。
测试点性质
- 测试点 3 − 4 3-4 3−4: 1 ≤ a i ≤ 10 6 1\le a_i\le 10^6 1≤ai≤106。
- 测试点 5 − 14 5-14 5−14:没有额外限制。
数论
对a排序并去重。
至少四周 ⟺ L ≤ L 1 = ⌊ a 0 4 ⌋ \iff L \le L1=\lfloor \frac{a0}4 \rfloor ⟺L≤L1=⌊4a0⌋
N = a . s i z e ( ) N=a.size() N=a.size()如果N<4,则一定符合。返回1到L1之和。
下面讨论 N ≥ 4 N \ge 4 N≥4
0 ≤ i < j < 4 , a i ≡ a j m o d L 0 \le i < j < 4,ai \equiv aj \mod L 0≤i<j<4,ai≡ajmodL一定有解,即:
a j − a i ≡ 0 m o d L aj-ai \equiv 0 \mod L aj−ai≡0modL
即:L可能的解一定再是aj-ai的约数。
枚举 0 ≤ i < j < 4 , a j − a i 的约数 0 \le i < j < 4,aj-ai的约数 0≤i<j<4,aj−ai的约数,这4个数的约数不超过M(2000)个。
代码
核心代码
cpp
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include<array>
#include <bitset>
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T1, class T2, class T3, class T4, class T5, class T6, class T7 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4,T5,T6,T7>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<int N = 1'000'000>
class COutBuff
{
public:
COutBuff() {
m_p = puffer;
}
template<class T>
void write(T x) {
int num[28], sp = 0;
if (x < 0)
*m_p++ = '-', x = -x;
if (!x)
*m_p++ = 48;
while (x)
num[++sp] = x % 10, x /= 10;
while (sp)
*m_p++ = num[sp--] + 48;
AuotToFile();
}
void writestr(const char* sz) {
strcpy(m_p, sz);
m_p += strlen(sz);
AuotToFile();
}
inline void write(char ch)
{
*m_p++ = ch;
AuotToFile();
}
inline void ToFile() {
fwrite(puffer, 1, m_p - puffer, stdout);
m_p = puffer;
}
~COutBuff() {
ToFile();
}
private:
inline void AuotToFile() {
if (m_p - puffer > N - 100) {
ToFile();
}
}
char puffer[N], * m_p;
};
template<int N = 1'000'000>
class CInBuff
{
public:
inline CInBuff() {}
inline CInBuff<N>& operator>>(char& ch) {
FileToBuf();
while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车
ch = *S++;
return *this;
}
inline CInBuff<N>& operator>>(int& val) {
FileToBuf();
int x(0), f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
inline CInBuff& operator>>(long long& val) {
FileToBuf();
long long x(0); int f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
template<class T1, class T2>
inline CInBuff& operator>>(pair<T1, T2>& val) {
*this >> val.first >> val.second;
return *this;
}
template<class T1, class T2, class T3>
inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
return *this;
}
template<class T1, class T2, class T3, class T4>
inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
return *this;
}
template<class T = int>
inline CInBuff& operator>>(vector<T>& val) {
int n;
*this >> n;
val.resize(n);
for (int i = 0; i < n; i++) {
*this >> val[i];
}
return *this;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
*this >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
return ret;
}
private:
inline void FileToBuf() {
const int canRead = m_iWritePos - (S - buffer);
if (canRead >= 100) { return; }
if (m_bFinish) { return; }
for (int i = 0; i < canRead; i++)
{
buffer[i] = S[i];//memcpy出错
}
m_iWritePos = canRead;
buffer[m_iWritePos] = 0;
S = buffer;
int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
if (readCnt <= 0) { m_bFinish = true; return; }
m_iWritePos += readCnt;
buffer[m_iWritePos] = 0;
S = buffer;
}
int m_iWritePos = 0; bool m_bFinish = false;
char buffer[N + 10], * S = buffer;
};
class Solution {
public:
long long Ans(vector<long long>& a) {
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
const int N = a.size();
const long long L1 = a[0] / 4;
if (N < 4) { return (1 + L1) * L1 / 2; }
unordered_set<int> ls;
auto Split = [&](int x) {
for (int i = 2; i * i <= x; i++) {
if (0 == x % i) {
ls.emplace(i); ls.emplace(x / i);
}
ls.emplace(x);
}
};
long long ans = 0;
auto Check = [&](int x) {
unordered_set<int> has;
for (const auto& i : a) {
has.emplace(i % x);
if (has.size() > 3) { return; }
}
ans += x;
};
ls.emplace(1);
Split(a[1] - a[0]); Split(a[2] - a[0]); Split(a[3] - a[0]); Split(a[2] - a[1]); Split(a[3] - a[1]); Split(a[3] - a[2]);
for (auto& x : ls) {if(x <= L1) Check(x); }
return ans;
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
ios::sync_with_stdio(0); cin.tie(nullptr);
//CInBuff<> in; COutBuff<10'000'000> ob;
auto a = Read<long long>();
#ifdef _DEBUG
//printf("K=%d", K);
//Out(a, ",a=");
//Out(com, ",com=");
//Out(str2, ",str2=");
//Out(que, ",ope=");
#endif // DEBUG
auto res = Solution().Ans(a);
cout << res << "\n";
return 0;
};
单元测试
cpp
vector<long long> a;
TEST_METHOD(TestMethod11)
{
a = { 31,28,31,30,31,30,31,31,30,31,30,31 };
auto res = Solution().Ans(a);
AssertEx(28LL, res);
}
TEST_METHOD(TestMethod12)
{
a = { 31,35,28,29 };
auto res = Solution().Ans(a);
AssertEx(23LL, res);
}
TEST_METHOD(TestMethod13)
{
a = { 3 };
auto res = Solution().Ans(a);
AssertEx(0LL, res);
}

扩展阅读
| 计算几何为骨,排样优化为魂 |
|---|
| 作品一:亲士套料工具箱 作品二: 亲士CAD工具箱 |
| 经典文章推荐:二维排样 |
| 牛人博主:可视化、物联网、工业互联网与AI 大佬 作品 零代码可视化工具 |
| 万物皆数学 |
| 查阅鄙人的博文,请点击博文下载学院导航 |
| 活到老,学到老。明朝中后期,大约50%的进士能当上堂官(副部及更高);能当上堂官的举人只有十余人。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。
