#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
int x = 0,f = 1; char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int a, b;
bool ok(int y) {
return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline void solve() {
cin >> a >> b;
int Ans = 0;
for (int y = a + 1; y <= b - 1; y++) {
if (ok(y)) Ans += y;
}
printf("%d\n", Ans);
}
int main() {
int _ = 1;
while (_--) solve();
return 0;
}
2. 当天的第几秒
题目大意
输入一个时刻的时、分、秒,以及字符 A 或 P。A 表示上午,P 表示下午。求这个时刻是当天的第几秒。
题解
把时、分、秒统一转成秒。
如果是下午 P,小时数要加 12。
答案为:
h×3600+m×60+s
代码
#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
int x = 0,f = 1; char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int h, m, s;
char c;
inline void solve() {
cin >> h >> m >> s >> c;
if (c == 'P') h += 12;
printf("%d\n", (h * 3600 + m * 60 + s));
}
int main() {
int _ = 1;
while (_--) solve();
return 0;
}
3. 长方形面积
题目大意
给定长方形面积 A,求有多少种长和宽都是整数的长方形。要求长大于等于宽。
题解
如果 i 是面积 A 的因数,那么可以令宽为 i,长为 A/i。
由于要求长大于等于宽,只需要枚举 i 从 1 到 A。
如果 A%i==0,就找到一种。
代码
#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
int x = 0,f = 1; char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int A;
inline void solve() {
cin >> A;
int Ans = 0;
for (int i = 1; i * i <= A; i++) {
if (A % i == 0) Ans++;
}
printf("%d\n", Ans);
}
int main() {
int _ = 1;
while (_--) solve();
return 0;
}
4. 每月天数
题目大意
输入年份和月份,输出这个月有多少天。
题解
普通月份天数固定。
二月需要判断闰年:
闰年二月 29 天,平年二月 28 天。
代码
#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
int x = 0,f = 1; char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int y, m;
bool ok(int y) {
return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline void solve() {
cin >> y >> m;
if (m == 2) {
if (ok(y)) printf("29\n");
else printf("28\n");
} else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
printf("31\n");
} else {
printf("30\n");
}
}
int main() {
int _ = 1;
while (_--) solve();
return 0;
}
5. 时间规划
题目大意
输入开始时刻和结束时刻,求中间相差多少分钟。
题解
把两个时刻都转成 从当天 0 点开始经过的分钟数 。
然后用结束分钟数减去开始分钟数。
代码
#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
int x = 0,f = 1; char ch = getchar();
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
bool f[100005];
void mkk() {
fill(f + 2, f + 100005, true);
for (int i = 2; i * i <= 100000; i++) {
if (f[i]) {
for (int j = i * i; j <= 100000; j += i) f[j] = false;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
mkk();
int t;
cin >> t;
while (t--) {
int h;
cin >> h;
int ans = 1e9;
for (int k = 0; k <= 17; k++) {
int p2 = 1 << k;
if (h + 1 == p2) {
ans = min(ans, k);
}
int x = h + 1 - p2;
if (x >= 2 && x <= h && f[x]) {
ans = min(ans, k + 1);
}
}
if (ans > 1e8) cout << -1 << "\n";
else cout << ans << "\n";
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using i128 = __int128;
void put(i128 x) {
if(x == 0) {
cout << 0;
return;
}
if(x < 0) {
cout << '-';
x = -x;
}
string s;
while(x) {
s += char(x % 10 + '0');
x /= 10;
}
reverse(s.begin(), s.end());
cout << s;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long a, b;
cin >> a >> b;
i128 ans = (i128)a * b;
put(ans);
return 0;
}
如果输入:
1000000000000000000 1000000000
程序会输出:
1000000000000000000000000000
也就是:
1027
如果输入本身也可能超过 long long,就需要手写读入函数。
using i128 = __int128;
i128 read() {
string s;
cin >> s;
i128 x = 0;
int p = 0;
int f = 1;
if(s[0] == '-') {
f = -1;
p = 1;
}
for(int i = p; i < (int)s.size(); i++) {
x = x * 10 + s[i] - '0';
}
return x * f;
}
不过在大多数算法题里,通常只需要手写输出,不需要手写读入。
四、__int128 的常见使用场景
第一类场景是两个大数相乘。
比如:
a,b≤1018
如果要计算:
a×b
那么最大可能达到:
1036
这个结果一定会超过 long long。
即使最后要对某个数取模,中间乘法也可能先溢出。
例如:
long long a, b, mod;
cin >> a >> b >> mod;
long long ans = a * b % mod;
给定包含 N 个节点与 M 个选取配额的依赖网络。节点 i 具有权值 si 与唯一的直接前置依赖节点 ki(若 ki=0 则无依赖)。依赖关系整体构成森林拓扑。现要求在严格满足拓扑依赖律(即选取节点 i 的必要条件为节点 ki 已被选取)的前提下,选取恰好 M 个节点,最大化所选节点的权值总和。