2017"百度之星"程序设计大赛 - 初赛(A)

时间:2022-10-01 09:48:56

1001

统计p - 1的因子个数

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 2e5 + 10;
const int INF = 1e9 + 10;

int main(){
int t; scanf("%d", &t);
while(t--) {
int n; scanf("%d", &n);
n = n - 1;
int ans = 1;
for(int i = 2; i * i <= n; ++i) {
if(n % i == 0) {
int cnt = 1;
while(n % i == 0) {
n /= i;
cnt++;
}
ans *= cnt;
}
}
if(n > 1) ans *= 2;
printf("%d\n", ans);
}
return 0;
}


1005

暴力计算即可,要注意的就是2月29以及2月前后的分析

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 2e5 + 10;
const int INF = 1e9 + 10;

int main(){
int t; scanf("%d", &t);
while(t--) {
int d, m, y;
scanf("%d-%d-%d", &y, &m, &d);
int cnt = 0;
if(m == 2 && d == 29) {
for(int i = y + 1; ; ++i) {
if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
cnt += 2;
if(cnt > 2 && cnt % 7 == 0) {
printf("%d\n", i);
break;
}
} else {
cnt += 1;
}
}
} else if(m <= 2) {
for(int i = y; ; ++i) {
if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
cnt += 2;
} else {
cnt += 1;
}
if(cnt > 0 && cnt % 7 == 0) {
printf("%d\n", i + 1);
break;
}
}
} else if(m > 2) {
for(int i = y + 1 ; ; ++i) {
if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
cnt += 2;
} else {
cnt += 1;
}
if(cnt > 0 && cnt % 7 == 0) {
printf("%d\n", i);
break;
}
}
}
}
return 0;
}

1006

直接数1和0的联通块数就好了,但在此之前要先在外围涂上一层0

刘汝佳的紫书上有类似的题 160+页吧、

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 105 + 10;
const int INF = 1e9 + 10;
char st[qq][qq];
int vis[qq][qq];
int n, m;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void Dfs(int x, int y, char ch) {
vis[x][y] = 1;
for(int i = 0; i < 4; ++i) {
int a = x + dx[i];
int b = y + dy[i];
if(a < 0 || b < 0 || a > n + 1 || b > m + 1) continue;
if(st[a][b] != ch) continue;
if(vis[a][b]) continue;
Dfs(a, b, ch);
}
}

int main(){
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = 1; i <= n; ++i) {
scanf("%s", st[i] + 1);
}
for(int i = 0; i <= m + 1; ++i) {
st[0][i] = st[n + 1][i] = '0';
}
for(int i = 0; i <= n + 1; ++i) {
st[i][0] = st[i][m + 1] = '0';
}
mst(vis, 0);
int cnt0, cnt1;
cnt0 = cnt1 = 0;
for(int i = 0; i <= n + 1; ++i) {
for(int j = 0; j <= m + 1; ++j) {
if(vis[i][j]) continue;
Dfs(i, j, st[i][j]);
if(st[i][j] == '0') cnt0++;
else cnt1++;
}
}
if(cnt1 != 1) {
puts("-1");
} else {
if(cnt0 == 1) puts("1");
else if(cnt0 == 2) puts("0");
else puts("-1");
}
}
return 0;
}