链接:https://ac.nowcoder.com/acm/contest/11746/B
来源:牛客网
题目描述
对于小宝来说,如果一个数组的总和能够整除他的幸运数字k,就是他的幸运数组,而其他数组小宝都很讨厌。现在有一个长度为n的数组,小宝想知道这个数组的子数组中,最长的幸运子数组有多长。
对于子数组的定义,如果可以通过从开头和从结束分别删除若干个(可以为零或全部,前后删除个数不必相同)元素来从数组b获得数组a,则称数组a是数组b的子数组。(子数组包含原数组,但不包含空串)
输入描述:
多组输入。第一行包含一个整数T(1≤T≤10),表示有T组测试数据。
每组测试数据包含两行,第一行包含两个整数n和k(1≤n≤105,1≤k≤105),分别表示数组长度和小宝的幸运数字。第二行包含n个空格分隔的整数a1,a2,.,an(0≤ai≤10^9),为数组的元素。
输出描述:
对于每组数据,输出和能被k整除的最长子数组的长度。如果没有这样的子数组,则输出−1。
示例1
输入
复制
4
3 3
1 2 3
3 5
1 2 3
3 7
1 2 3
1 6
5
输出
复制
3
2
-1
-1
涉及区间的和的问题一般要想到前缀和,如果两个前缀和对一个数的余数相等,那么这两个区间的差的和一定是那个余数的倍数,特殊判断一下,如果一个数的前缀和对那个数的余数是0的话,那么这个·前缀和是那个数的倍数
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 1e5 + 10;
const int M = 4 * N;
const int mod = 1e4 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int a[N], s[N];
int n, k;
vector<int> v[N];
signed main(){
int T;
cin >> T;
while(T --){
for (int i = 0; i < N;i ++) v[i].clear();
memset(a, 0, sizeof a);
memset(s, 0, sizeof s);
scanf("%lld%lld", &n, &k);
for (int i = 1; i <= n; i ++) scanf("%lld", &a[i]);
for (int i = 1; i <= n; i ++) s[i] = s[i - 1] + a[i];
for (int i = 1; i <= n; i ++){
int dd = s[i] % k;
//cout << s[i] << "---" << dd << endl;
v[dd].push_back(i);
}
int ans = 0;
if (v[0].size() >= 1) ans = max(ans, v[0][v[0].size() - 1]);
for (int i = 1; i < k; i ++){
if (v[i].size() >= 1) ans = max(ans, v[i][v[i].size() - 1] - v[i][0]);
}
if (ans == 0) ans = -1;
printf("%lld\n", ans);
}
return 0;
}
上进的凡凡
链接:https://ac.nowcoder.com/acm/contest/11746/C
来源:牛客网
题目描述
凡凡是一个上进的人,他的人生没有下坡路,他也讨厌带有”下坡路“的东西。
所以,对于凡凡来说,只有非降序的数组才是nice的(如:1,2,2,3,4,5,5);若数组元素个数为1,也满足非降序,也是nice的。
现在有一个长度为n的数组,凡凡想知道它的子数组中有多少个数组是nice的。
你能帮帮他吗?
对于子数组的定义,如果可以通过从开头和从结束分别删除若干个(可以为零或全部,前后删除个数不必相同)元素来从数组b获得数组a,则称数组a是数组b的子数组。(子数组包含原数组,但不包含空串)
输入描述:
第一行输入一个整数n(1≤n≤10^5),表示数组的长度。
第二行包含n个空格分隔的整数a1,a2,.,an(0≤ai≤10^9),为数组的元素。
输出描述:
输出给定数组的子数组中是nice数组的个数。(注意使用long long)
示例1
输入
复制
5
1 2 3 4 5
输出
复制
15
别人的做法是找到连续单调递增的区间,然后在这个区间选任意多得数,公式为n*(n + 1) / 2, 我没有想到,我的做法是表示出来以每个数为首祥的可能的情况数,直接处理的话会超时,就发现性质,防止对一个东西重复计算,当前一个数的可能数大于2的时候,这个数的可能情况一定是前一个数-1.可以优化时间
#include<iostream>
#define int long long
using namespace std;
const int N=1e5+10;
int a[N];
signed main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
int ans=0;
for(int i=1;i<=n;i++)
{
int j=i+1,tmp=1;
while(j<=n && a[j]>=a[j-1])
{
tmp++;
j++;
}
ans+=(1+tmp)*tmp/2;
i=j-1;
}
cout<< ans <<endl;
return 0;
}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 1e5 + 10;
const int M = 4 * N;
const int mod = 1e4 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int a[N];
int n;
int cnt[N];
signed main(){
scanf("%lld", &n);
for (int i = 1; i <= n; i ++) scanf("%lld", &a[i]);
for (int i = 1; i <= n; i ++){
if (cnt[i - 1] >= 2){
cnt[i] = cnt[i - 1] - 1;
}
else{
int j = i + 1;
cnt[i] = 1;
while(a[j] >= a[j - 1] && j <= n){
cnt[i] ++;
j ++;
}
}
}
int ans = 0;
for (int i = 1; i <= n; i ++){
ans += cnt[i];
}
cout << ans << endl;
return 0;
}
Seek the Joker I
链接:https://ac.nowcoder.com/acm/contest/11746/D
来源:牛客网
长达数日的春日祭终于告一段落,作为巫女的朝野芳乃在打扫完神社本决定好好享受一下久违的宁静。然而守护了神刀数百年的丛雨难耐寂寞,希望芳乃能陪她一起玩扑克消解愁闷。
芳乃并不擅长市井的游戏,所以总是输多赢少。而昨日被芳乃的神乐舞深深吸引,以致一早就前来建实神社希望能再睹芳华的你碰巧听见了此事。尽管不知道为什么美丽的巫女要自言自语地为玩扑克而苦恼,但你毅然决然地毛遂自荐,希望能为芳乃一解眉间愁。
芳乃告诉了你丛雨准备了n张扑克牌作为牌堆,每人每次至多从牌堆顶部抽k张牌,至少抽1张牌。牌堆底部的最后一张牌作为乌龟,抽中的将输掉这一轮比赛。芳乃想知道在你的帮助下,她和丛雨都采取积极策略时,她自己是否一定能获胜。作为被丛雨邀请的一方,每轮游戏都是芳乃先抽。
因为看不见丛雨而误认芳乃罹患精神分裂的你在不由感叹红颜薄命的同时,决定尽全力帮助芳乃完成她的委托。
声明:本题中的所有角色在剧情发生时均已超过18岁。
输入描述:
第一行包含一个整数T,表示共有T组测试数据。
每组测试数据共一行,包含两个正整数n和k,分别表示牌堆中有n张牌和每次抽取最多抽取k张。
数据保证T,n,k≤1000000。
输出描述:
对于每组测试数据给出一行结果。
如果芳乃必胜,则输出“yo xi no forever!”,
否则输出 ”ma la se mi no.1!“。
示例1
输入
复制
4
1 1
23 2
6 4
114 514
输出
复制
ma la se mi no.1!
yo xi no forever!
ma la se mi no.1!
yo xi no forever!
暴力超时做法
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 2e5 + 10;
const int M = 4 * N;
const int mod = 1e4 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int n, k;
int f[N];
int sg(int x){
if (f[x] != -1) return f[x];
unordered_set<int> S;
for (int i = 1; i <= k; i ++){
if (x >= i) S.insert(sg(x - i));
}
for (int i = 0; ; i ++)
if (!S.count(i))
return f[x] = i;
}
signed main(){
int T;
scanf("%lld", &T);
while(T --){
scanf("%lld%lld", &n, &k);
memset(f, -1, sizeof f);
int res = sg(n - 1);
if (res) cout << "yo xi no forever!" << endl;
else cout << "ma la se mi no.1!" << endl;
}
return 0;
}
从而找到公式,发现是书上的结论
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 2e5 + 10;
const int M = 4 * N;
const int mod = 1e4 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int n, k;
int f[N];
int sg(int x){
if (f[x] != -1) return f[x];
unordered_set<int> S;
for (int i = 1; i <= k; i ++){
if (x >= i) S.insert(sg(x - i));
}
for (int i = 0; ; i ++)
if (!S.count(i))
return f[x] = i;
}
signed main(){
int T;
scanf("%lld", &T);
while(T --){
scanf("%lld%lld", &n, &k);
memset(f, -1, sizeof f);
int res = sg(n - 1);
if (res) cout << "yo xi no forever!" << endl;
else cout << "ma la se mi no.1!" << endl;
}
return 0;
}
Seek the Joker II
链接:https://ac.nowcoder.com/acm/contest/11746/D
来源:牛客网
题目描述
长达数日的春日祭终于告一段落,作为巫女的朝野芳乃在打扫完神社本决定好好享受一下久违的宁静。然而守护了神刀数百年的丛雨难耐寂寞,希望芳乃能陪她一起玩扑克消解愁闷。
芳乃并不擅长市井的游戏,所以总是输多赢少。而昨日被芳乃的神乐舞深深吸引,以致一早就前来建实神社希望能再睹芳华的你碰巧听见了此事。尽管不知道为什么美丽的巫女要自言自语地为玩扑克而苦恼,但你毅然决然地毛遂自荐,希望能为芳乃一解眉间愁。
芳乃告诉了你丛雨准备了n张扑克牌作为牌堆,每人每次至多从牌堆顶部抽k张牌,至少抽1张牌。牌堆底部的最后一张牌作为乌龟,抽中的将输掉这一轮比赛。芳乃想知道在你的帮助下,她和丛雨都采取积极策略时,她自己是否一定能获胜。作为被丛雨邀请的一方,每轮游戏都是芳乃先抽。
因为看不见丛雨而误认芳乃罹患精神分裂的你在不由感叹红颜薄命的同时,决定尽全力帮助芳乃完成她的委托。
声明:本题中的所有角色在剧情发生时均已超过18岁。
输入描述:
第一行包含一个整数T,表示共有T组测试数据。
每组测试数据共一行,包含两个正整数n和k,分别表示牌堆中有n张牌和每次抽取最多抽取k张。
数据保证T,n,k≤1000000。
输出描述:
对于每组测试数据给出一行结果。
如果芳乃必胜,则输出“yo xi no forever!”,
否则输出 ”ma la se mi no.1!“。
示例1
输入
复制
4
1 1
23 2
6 4
114 514
输出
复制
ma la se mi no.1!
yo xi no forever!
ma la se mi no.1!
yo xi no forever!
暴力超时做法
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 1e3 + 10;
const int M = 4 * N;
const int mod = 1e4 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int n, k;
int f[N][N];
int sg(int x, int y){
if (f[x][y] != -1) return f[x][y];
unordered_set<int> S;
for (int i = 1; i <= max(x, y); i ++){
if (x >= i) S.insert(sg(x - i, y));
if (y >= i) S.insert(sg(x, y - i));
}
for (int i = 1; i <= min(x, y); i ++){
S.insert(sg(x - i, y - i));
}
for (int i = 0; ; i ++)
if (!S.count(i))
return f[x][y] = i;
}
signed main(){
int T;
scanf("%lld", &T);
while(T --){
scanf("%lld%lld", &n, &k);
memset(f, -1, sizeof f);
int res = sg(k - 1,n - k);
if (res) cout << "yo xi no forever!" << endl;
else cout << "ma la se mi no.1!" << endl;
}
return 0;
}
成绩查询ing
链接:https://ac.nowcoder.com/acm/contest/11746/F
来源:牛客网
题目描述
去年的新冠疫情爆发让众多大学生只能只能在家里上学,老师为了方便自己录入成绩和方便大家成绩查询,建立了一个录入和查询成绩的系统,能完成M次两种不同的查询,输入查询次数M,查询M次,每次首先输入查询的模式T,T为1时,输入同学的姓名Name,并依次输出同学的成绩Grade(0<=Grade<=100), 学号(0~1000000},性别(1/2),T为2时,输入成绩,输出有具体有哪些同学考到了这个分数,输出同学的Name,并要求按字典序输出,当没有同学为此分数时,则不输出。字典序,对于字符串,先按首字符排序,如果首字符相同,再按第二个字符排序,以此类推。
输入描述:
第一行包含一个整数N,表示系统中共有N个人(1<=N<=100000)。 下面N行分别输入N个人的姓名Name,成绩Grade(成绩在0~100之间),性别(1或2分别表示男性、女性),学号。表示系统中成员的信息 输入查询次数M(M<=10000000),接下来M行完成M次查询任务
输出描述:
输出M次查询的结果,当T为1时,输入同学的姓名Name,并在一行中依次输出同学的成绩Grade(0<=Grade<=100), 学号(0~1000000},性别(1/2),用空格间隔(注意行末无空格),T为2时,输入成绩,输出有具体有哪些同学考到了这个分数,输出同学的Name(每个Name输出一行,无空格),并要求按字典序输出,当没有同学为此分数时,则不输出。
示例1
输入
复制
5
N 28 2 7475
UN 83 2 27550
EXF 5 2 17298
OVYNH 51 2 14827
XNV 53 1 7591
2
1
XNV
2
27
输出
复制
53 7591 1
stl
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define int long long
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
const int N = 1e3 + 10;
const int M = 4 * N;
const int mod = 1e4 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
struct Node{
int grade;
int sex;
int number;
};
int n, m;
map<string, Node> mp;
vector<string> v[11110];
signed main(){
//ios::sync_with_stdio(false);
scanf("%lld", &n);
for (int i = 1; i <= n; i ++){
string str;
cin >> str;
int a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
mp[str] = {a, b, c};
v[a].push_back(str);
}
for (int i = 0; i <= 100; i ++){
if (v[i].size() >= 1){
sort(v[i].begin(), v[i].end());
}
}
scanf("%lld", &m);
for (int i = 1; i <= m; i ++){
int type;
scanf("%lld", &type);
if (type == 1){
string str1;
cin >> str1;
cout << mp[str1].grade << " " << mp[str1].number << " " << mp[str1].sex << "\n";
}
else{
int x;
scanf("%lld", &x);
if (v[x].size() >= 1){
for (int j = 0; j < v[x].size(); j ++){
cout << v[x][j] << "\n" ;
}
}
}
}
return 0;
}
/*
5
N 28 2 7475
UN 83 2 27550
EXF 5 2 17298
OVYNH 51 2 14827
XNV 53 1 7591
2
1
XNV
2
53*/
数羊
链接:https://ac.nowcoder.com/acm/contest/11746/H
来源:牛客网
题目描述
憨憨小杨晚上睡不着觉,就开始数羊,她觉得一只一只数太慢了,突发奇想出了一种新的数羊方式,羊羊数量A(n,m)由两个整形变量n和m决定,计算方式如下:
现在给出n和m的值,请你帮小杨数数一共有多少只羊。
输入描述:
多组输入。
第一行包含一个整数T(1≤T≤1000),表示有T组测试数据。
每组测试数据包含一行,包含两个整数n(1≤n≤10^9)和m(0≤m≤2).
输出描述:
对每一组输入,在一行中输出A(n,m)的值,由于输出的结果可能会很大,答案对998244353取模
示例1
输入
复制
3
3 0
3 1
3 2
输出
复制
5
6
8
m的可能很少,先暴力后分类找规律
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 1e5 + 10;
const int M = 4 * N;
const int mod = 998244353;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int n, m;
int qmi(int a, int b){
int res = 1 % mod;
while(b){
if (b & 1) res = res * a % mod;
a = a % mod * a % mod;
b >>= 1;
}
return res % mod;
}
signed main(){
int T;
scanf("%lld", &T);
// cout << qmi(2, 4) << endl;
while(T --){
scanf("%lld%lld", &n, &m);
if (m == 1) cout << n * 2 % mod << endl;
else if (m == 0) cout << (n + 2) % mod << endl;
else cout << qmi(2, n) % mod << endl;
}
return 0;
}
买花
链接:https://ac.nowcoder.com/acm/contest/11746/I
来源:牛客网
题目描述
情人节马上要到了,阳阳想送出n朵花给喜欢的妹妹,他打算提前开始买。但是,因为他有强迫症,所有的花要分k天买(k>1,即不能一天全买完),第一天他可以买任意朵花,之后每一天买花的数量为前一天的两倍,(如若第一天买4朵,第二天就要买8朵,以此类推)。
现在离情人节还有15天(k≤15),请你告诉阳阳,他能不能刚好买到n朵花。
输入描述:
多组输入。第一行一个正整数T(1<=T<=10^5),表示数据组数。
接下来T行,每行一个正整数n(1<=n<=10^9),表示预计买花的数量。
输出描述:
每组数据输出一行,共T行。
判断能否刚好买到n朵花,可以则输出"YE5",否则输出"N0"。
示例1
输入
复制
2
21
20
输出
复制
YE5
N0
想这个过程,发现一定是某些数的倍数
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 1e5 + 10;
const int M = 4 * N;
const int mod = 1e4 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int a[20];
signed main(){
int T;
a[0] = 1;
for (int i = 1; i <= 14; i ++){
a[i] = a[i - 1] * 2 + 1;
}
scanf("%lld", &T);
while(T --){
int n;
scanf("%lld", &n);
bool flag = false;
for (int i = 1; i <= 14; i ++){
if (n % a[i] == 0){
flag = true;
}
}
if (flag) cout << "YE5" << endl;
else cout << "N0" << endl;
}
return 0;
}
这是一题简单的模拟
链接:https://ac.nowcoder.com/acm/contest/11746/J
来源:牛客网
题目描述
财务计划要从家里出发,去N个城市出差,然后再回到家中,但N个出差地点之间不一定都能通车,现在他要筛选出花费最少的路径,你能帮帮他吗?
输入描述:
第一行为两个正整数N和M(1≤N≤300,1≤M≤N(N+1)/2),分别表示有N个出差地点和这些地点之间的M条通路,其中出差地点用1到N编号,而财务的家所在城市用编号0表示。
随后的M行,每行给出通路连接的两个城市和这条通路上的花费,格式为:
城市A 城市B 花费
通路是双向的,且两个城市间最多有一条通路,不存在自环。保证所有花费大于0。
再下一行给出一个正整数K(K<=20000),表示现在有K条推荐路径(注意:给出的路径不一定能通过或可能不满足财务的要求)。
接下来K行每一行表示一个推荐路径,第一个整数n表示途径的城市数,后面有n(n<=2*N)个整数xi(1≤xi≤N)(表示途经的城市(不包括财务的家),如:
3 1 2 3
表示实际路径为0→1→2→3→0。
输出描述:
请你检验给出的K条推荐路径,当它满足:
1.给出的路径能实际通车,即路径中相邻城市存在通路;
2.给出的路径恰好能都到达N个出差城市一次,即不能漏掉某个城市,也不能重复到达。
则称这条路径是可行的。
对于给出的K条推荐路径,请输出其中可行路径中最少的花费,若不存在可行路径,请输出"-1"。(题目保证花费和不超过int范围)
示例1
输入
复制
5 10
0 1 5
0 5 12
1 2 2
2 3 8
3 4 13
1 3 11
0 2 5
0 4 9
4 5 6
3 5 7
5
5 1 3 2 3 1
5 3 2 1 4 5
5 2 1 3 5 4
6 1 2 3 4 5 1
5 1 2 3 5 4
输出
复制
37
真就模拟,找到题意
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 320;
const int M = 4 * N;
const int mod = 998244353;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int n, m;
int g[320][320];
signed main(){
for (int i = 1; i <= N; i ++)
for (int j = 1; j <= N; j ++)
g[i][j] = 0x3f3f3f3f;
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= m; i ++){
int a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
g[a][b] = c, g[b][a] = c;
}
int k;
scanf("%lld", &k);
int ans = 0x3f3f3f3f;
for (int i = 1; i <= k; i ++){
int cnt;
scanf("%lld", &cnt);
int res = 0;
int st[N];
memset(st, 0, sizeof st);
int u = 0;
while(cnt --){
int x;
scanf("%lld", &x);
res += g[u][x];
st[x] ++;
u = x;
}
res += g[u][0];
if (res < 0x3f3f3f3f){
bool flag = false;
for (int j = 1; j <= n; j ++){
if (st[j] != 1) flag = true;
}
if (!flag) ans = min(res, ans);
}
}
if (ans == 0x3f3f3f3f) cout << "-1" << endl;
else cout << ans << endl;
return 0;
}
黑洞密码
链接:https://ac.nowcoder.com/acm/contest/11746/K
来源:牛客网
题目描述
近些日子,某科学家接受到了来自外太空的神秘讯息,在经过了一段时间的研究后,科学家发现讯息是一个由字母和数字组成的字符串str,想要破译,需要通过一定的规则将字符串进行转换。规则如下:
1.确定讯息的长度为32;
2.字符串中第4n+1\sim∼4n+4的字母和第4n+14n+4的字母和第4n+1\sim∼4n+4(4n+4(0 \leq n \leq 30≤n≤3)的数字为一组,共4组;
3.每组的第1,2,3,4个字符分别往后推每组第1,2,3,4个数字个数 例:如第一个字母为a,第一个数字为3,转换后变为d,‘z’之后是’B’,‘Z’之后是’b’;
4.将每组内部字母的顺序颠倒;
5.将四组字符合并就是最后的讯息。
输入描述:
输入一个长度为32的字符串
输出描述:
输出转换后的密码
示例1
输入
复制
Zzc6Ltw2OD4yR640263W7G8G30HW9C71
输出
复制
RgCgJQwxJfYCDeQG
特殊的字母变化方式,以函数来实现
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 320;
const int M = 4 * N;
const int mod = 998244353;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
char change(char a, int x){
while(x --){
if (a == 'Z'){
a = 'a';
}
if (a == 'z'){
a = 'A';
}
a ++;
}
return a;
}
signed main(){
string str;
cin >> str;
string str1[6];
string str2[6];
//cout << str << endl;
int idx = 1, edx = 1;
for (int i = 0; i < str.size(); i ++){
if (str[i] >= 'a' && str[i] <= 'z') str1[idx] += str[i];
if (str[i] >= 'A' && str[i] <= 'Z') str1[idx] += str[i];
if (str[i] >= '0' && str[i] <= '9') str2[edx] += str[i];
// cout << str1[idx] << "---" << str2[edx] << endl;
if (str1[idx].size() >= 4) idx ++;
if (str2[edx].size() >= 4) edx ++;
//cout << i << "---" << endl;
}
//cout << str << endl;
for (int i = 1; i <= 4; i ++){
// cout << str1[i] << "-----" << str2[i] << endl;
for (int j = 0; j < 4; j ++){
int x;
if (str2[i][j] == '0') x = 0;
else x = (str2[i][j] - '0');
// cout << x << endl;
str1[i][j] = change(str1[i][j], x);
}
//cout << str1[i] << endl;
reverse(str1[i].begin(), str1[i].end());
cout << str1[i];
}
cout << endl;
return 0;
}
建立火车站
链接:https://ac.nowcoder.com/acm/contest/11746/L
来源:牛客网
题目描述
新冠疫情,导致了各个城市之间物资输送的障碍。假设有N个城市在一条直线上,为了物资能顺利抵达各个城市,可以在路线上建立最多个数为K个暂时停靠站,由于火车在两个站台(城市也算站台)之间的距离越近,需要的总花费越少,因此我们需要让火车相邻两个站台之间的最大距离最小,求出距离L,2 ≤N ≤100000, 0 ≤K ≤100000,所有城市坐标小于等于10^12,且不存在负值。提醒: 城市坐标均为正整数,且停靠站只能建在整数坐标点上。
输入描述:
第一行输入城市个数N,可建立停靠站个数K,
第二行输入N个城市的坐标(不保证前一个城市坐标比后一个城市小)。
输出描述:
输出L
示例1
输入
复制
2 2
4 106
输出
复制
34
最大最小,二分实锤,check函数,关键要模拟实际,上取整是关键
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 1e5 + 10;
const int M = 4 * N;
const int mod = 998244353;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int n, k;
int a[N];
int lmax, rmax;
bool check(int mid){
int cnt = 0;
for (int i = 2; i <= n; i ++){
if (a[i] - a[i - 1] > mid){
cnt += ((a[i] - a[i - 1] + mid - 1) / mid - 1);
}
}
return cnt <= k;
}
signed main(){
scanf("%lld%lld", &n, &k);
for (int i = 1; i <= n; i ++){
scanf("%lld", &a[i]);
}
sort(a + 1, a + n + 1);
lmax = a[1], rmax = a[n];
int l = 1, r = a[n];
while(l < r){
int mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l << endl;
return 0;
}