从今天起,cf开刷,先从难度1600写起试试,每个难度的题目放在一个博客里面。看看最后可以写多少个博客。每一道AC的题目的代码都放在这里
1600打算写25道题后晋级1700.
#include
#include
using namespace std;
const int maxn = 110;
int f[maxn][maxn], N, K, D;
const int mod = 1e9 + 7;
int main(){
scanf("%d%d%d", &N, &K, &D);
f[0][0] = 1;
for(int i = 0; i <= N; i++){
for(int j = 1; j <= min(D - 1, i); j++){
f[i][0] = (f[i][0] + f[i - j][0]) % mod;
}
}
for(int i = 0; i <= N; i++){
for(int j = 1; j <= min(i, K); j++){
f[i][1] = (f[i][1] + f[i - j][1]) % mod;
}
for(int j = D; j <= min(i, K); j++){
f[i][1] = (f[i][1] + f[i - j][0]) % mod;
}
}
printf("%d\n", f[N][1]);
return 0;
}
#include
#include
#include
#include
using namespace std;
const int maxn = 200010, maxm = maxn * 2;
typedef long long ll;
int h[maxn], e[maxm], ne[maxm], idx;
int N, K;
int d[maxn], happiness[maxn];
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int dfs(int u, int fa){
int res = 1;
for(int i = h[u]; i != -1; i = ne[i]){
int v = e[i];
if(v == fa) continue;
d[v] = d[u] + 1;
res += dfs(v, u);
}
happiness[u] = res - d[u] - 1;
return res;
}
int main(){
scanf("%d%d", &N, &K);
memset(h, -1, sizeof h);
for(int i = 1; i < N; i++){
int a, b;
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
dfs(1, -1);
sort(happiness + 1, happiness + N + 1, greater<int>());
ll ans = 0;
for(int i = 1; i <= N - K; i++) ans += happiness[i];
printf("%lld\n", ans);
return 0;
}
这道题,我想这是什么玩意儿,想半天不知道怎么写。然后看一下题解…
#include
#include
#include
using namespace std;
int N, M, K;
const int maxn = 510;
char maze[maxn][maxn];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int cnt, blank;
void dfs(int x, int y){
maze[x][y] = '*';
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if(nx < 0 || nx >= N || ny < 0 || ny >= M || maze[nx][ny] != '.') continue;
if(++blank <= cnt - K) dfs(nx, ny);
}
}
int main(){
scanf("%d%d%d", &N, &M, &K);
for(int i = 0; i < N; i++){
scanf("%s", maze[i]);
}
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(maze[i][j] == '.') cnt++;
}
}
bool flag = false;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(maze[i][j] == '.' && ++blank <= cnt - K){
dfs(i, j);
flag = true;
break;
}
}
if(flag) break;
}
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(maze[i][j] == '*') maze[i][j] = '.';
else if(maze[i][j] == '.') maze[i][j] = 'X';
}
}
for(int i = 0; i < N; i++) printf("%s\n", maze[i]);
return 0;
}
dic = {0:'A', 1:'B', 2:'C', 3:'D', 4:'E', 5:'F', 6:'G', 7:'H'};
def get(x):
if(x > 0):
get((x - 1) // 8);
print(dic[(x - 1) % 8], end = '');
x = int(input());
get(x);
#include
#include
#include
using namespace std;
struct P {
int l, r;
P(int l_ = 0, int r_ = 0): l(l_), r(r_){}
bool operator < (const P& rhp)const {
return r - l < rhp.r - rhp.l || r - l == rhp.r - rhp.l && l > rhp.l;
}
};
const int maxn = 200010;
int a[maxn], N;
priority_queue<P> que;
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
que.push(P(0, N - 1));
int cnt = 0;
while(que.size()){
auto p = que.top(); que.pop();
int mid = (p.r + p.l) / 2;
a[mid] = ++cnt;
if (mid - 1 >= p.l) {
que.push(P(p.l, mid - 1));
}
if (p.r >= mid + 1) {
que.push(P(mid + 1, p.r));
}
}
for(int i = 0; i < N; i++){
printf("%d%c", a[i], i + 1 == N ? '\n' : ' ');
}
}
return 0;
}
#include
#include
#include
using namespace std;
const int maxn = 1010, maxm = 2010;
int N, X, deg[maxn];
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &N, &X);
memset(deg, 0, sizeof deg);
for(int i = 1; i < N; i++){
int a, b;
scanf("%d%d", &a, &b);
deg[a]++, deg[b]++;
}
if(deg[X] <= 1 || (N - 1) & 1) printf("Ayush\n");
else printf("Ashish\n");
}
return 0;
}
#include
#include
#include
using namespace std;
const int maxn = 300010;
typedef long long ll;
struct P{
ll a, b;
}G[maxn];
int N;
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
for(int i = 0; i < N; i++){
scanf("%lld%lld", &G[i].a, &G[i].b);
}
ll A = 0, B = 0, max_B = 1e18;
for(int i = 0; i < N; i++){
A += G[i].a, B += min(G[i].b, G[(i + 1) % N].a);
max_B = min(min(G[i].b, G[(i + 1) % N].a), max_B);
}
printf("%lld\n", A - B + max_B);
}
return 0;
}
#include
#include
using namespace std;
const int maxn = 1010;
int a[maxn], N;
int dx[5][10] = { {1, 3, 5, 2, 4}, {1, 3, 6, 4, 2, 5}, {1, 3, 7, 4, 6, 2, 5},
{1, 3, 5, 7, 4, 8, 6, 2}, { 1, 3, 5, 7, 9, 6, 8, 4, 2 } };
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
int m1 = N / 5, m2 = N % 5;
bool flag = true;
if(N == 2 || N == 3){
flag = false;
}
else if(N == 4){
a[0] = 2, a[1] = 4, a[2] = 1, a[3] = 3;
}
else{
for (int i = 0; i < (m1 - 1) * 5; i++) {
int k1 = i / 5, k2 = i % 5;
a[i] = k1 * 5 + dx[0][k2];
}
for(int i = (m1 - 1) * 5; i < N; i++){
a[i] = dx[m2][i - (m1 - 1) * 5] + (m1 - 1) * 5;
}
}
if(!flag){
printf("-1\n");
continue;
}
for(int i = 0; i < N; i++){
printf("%d%c", a[i], i + 1 == N ? '\n' : ' ');
}
}
return 0;
}
你需要找到有多少个区间使得其区间内每个元素的和为区间长度。
#include
#include
#include
#include
using namespace std;
const int maxn = 100010;
int N, a[maxn];
unordered_map<int, int> cnt;
typedef long long ll;
int main() {
int T;
cin >> T;
while (T--) {
cnt.clear();
cnt[0] = 1;
char c;
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> c;
a[i] = c - '0';
a[i] += a[i - 1];
cnt[a[i] - i]++;
}
ll ans = 0;
for (auto p : cnt) {
ll x = p.second;
ans += (ll)x * (x - 1) / 2;
}
cout << ans << endl;
}
return 0;
}
#include
#include
#include
#include
using namespace std;
const int maxn = 100010;
char s[maxn];
int N, K;
bool all_the_same(const char* s) {
for (int i = 0; s[i]; i++) {
if (s[i] != s[0]) return false;
}
return true;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
unordered_map<char, int> cnt;
char minc = 'z';
scanf("%d%d%s", &N, &K, &s);
s[N] = 0;
for (int i = 0; i < N; i++) {
cnt[s[i]]++;
if (minc > s[i]) minc = s[i];
}
sort(s, s + N);
if (cnt[minc] < K) {
printf("%c\n", s[K - 1]);
continue;
}
else {
printf("%c", minc);
if (!all_the_same(s + K)) printf("%s\n", s + K);
else {
int left = strlen(s + K);
for (int i = 0; i < (left + K - 1) / K; i++) {
printf("%c", s[K]);
}
printf("\n");
}
}
}
return 0;
}
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 400010;
int res[maxn];
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll A, B, lcm;
ll cal(ll x) {
return x / lcm * res[lcm] + res[x % lcm];
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
memset(res, 0, sizeof res);
scanf("%lld%lld", &A, &B);
lcm = A * B / gcd(A, B);
for (int i = 1; i <= lcm; i++) {
if (i % A % B != i % B % A) res[i] = 1;
res[i] += res[i - 1];
}
int Q;
scanf("%d", &Q);
for(int i = 0; i < Q; i++){
ll l, r;
scanf("%lld%lld", &l, &r);
printf("%lld%c", cal(r) - cal(l - 1), i + 1 == Q ? '\n' : ' ');
}
}
return 0;
}
#include
#include
#include
using namespace std;
const int maxn = 100010;
string nor[maxn], rev[maxn];
typedef long long ll;
ll c[maxn], f[maxn][2];
int N;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int main() {
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> c[i];
}
for (int i = 1; i <= N; i++) {
cin >> nor[i];
rev[i] = nor[i];
reverse(rev[i].begin(), rev[i].end());
}
memset(f, 0x3f, sizeof f);
f[0][0] = f[0][1] = 0;
for (int i = 1; i <= N; i++) {
if (nor[i] >= nor[i - 1]) f[i][0] = min(f[i][0], f[i - 1][0]);
if (nor[i] >= rev[i - 1]) f[i][0] = min(f[i][0], f[i - 1][1]);
if (rev[i] >= nor[i - 1]) f[i][1] = min(f[i][1], f[i - 1][0] + c[i]);
if (rev[i] >= rev[i - 1]) f[i][1] = min(f[i][1], f[i - 1][1] + c[i]);
}
ll ans = min(f[N][0], f[N][1]);
if (ans == INF) cout << -1 << endl;
else cout << ans << endl;
return 0;
}
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 100010;
int N;
ll a[maxn], GCD[maxn], res[maxn];
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b, ll g) {
return a * b / g;
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
for (int i = N; i >= 1; i--) {
GCD[i] = gcd(a[i], GCD[i + 1]);
}
for (int i = 1; i < N; i++) {
res[i] = lcm(a[i], GCD[i + 1], gcd(a[i], GCD[i + 1]));
}
ll ans = res[1];
for (int i = 2; i < N; i++) {
ans = gcd(ans, res[i]);
}
cout << ans << endl;
}
#include
#include
#include
#include
using namespace std;
const int maxk = 210;
vector<char> ans;
int N, M, K;
int main() {
scanf("%d%d%d", &N, &M, &K);
for (int i = 0; i < 2 * K; i++) {
int x, y;
scanf("%d%d", &x, &y);
}
for (int i = 1; i < N; i++) ans.push_back('U');
for (int i = 1; i < M; i++) ans.push_back('L');
for (int i = 1; i <= N; i++) {
if (i & 1) {
for (int j = 1; j < M; j++) {
ans.push_back('R');
}
}
else {
for (int j = 1; j < M; j++) {
ans.push_back('L');
}
}
ans.push_back('D');
}
ans.pop_back();
cout << ans.size() << endl;
for (auto u : ans) cout << u;
cout << endl;
return 0;
}
题意:有1-n个点,每个点赋值为a[i],现在可以移动k次,不过只能向左移动z次,并且不能连续向左移动,问你最后走过所有点的和最大是多少。
#include
#include
#include
#include
using namespace std;
const int maxn = 100010;
int a[maxn], res[maxn];
int N, K, Z;
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &N, &K, &Z);
for (int i = 1; i <= N; i++) scanf("%d", &a[i]);
int max_now = 0;
for (int i = 1; i <= N; i++) {
if (a[i] + a[i - 1] > max_now) max_now = a[i] + a[i - 1];
res[i] = max_now;
}
for (int i = 1; i <= N; i++) {
a[i] += a[i - 1];
}
int ans = 0;
for (int t = 0; t <= Z; t++) {
if (K - 2 * t + 1 < 1) break;
int tmp = a[K - 2 * t + 1] + t * res[K - 2 * t + 2];
ans = max(ans, tmp);
}
cout << ans << endl;
}
return 0;
}
#include
#include
using namespace std;
typedef long long ll;
ll N;
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
cin >> N;
if (N == 1) cout << 1 << endl;
else if (N == 2) cout << 2 << endl;
else if (N & 1) cout << N * (N - 1) * (N - 2) << endl;
else {
ll ans = max(N * (N - 1) * (N - 2) / 2, N * (N - 1) * (N - 3) / gcd(N, N - 3));
ans = max((N - 1) * (N - 2) * (N - 3), ans);
cout << ans << endl;
}
return 0;
}
//int main() {
// for (ll i = 6; i <= 1000; i+=2) {
// printf("%lld %lld %lld\n", i, i - 3, gcd(i, i - 3));
// }
//}
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 100010;
int a[maxn], N;
int main() {
cin >> N;
for (int i = 1; i <= N; i++) cin >> a[i];
if (N == 1) {
printf("1 1\n0\n1 1\n0\n1 1\n%d\n", -a[1]);
}
else {
printf("1 1\n%d\n", -a[1]);
printf("1 %d\n", N);
a[1] = 0;
for (int i = 1; i <= N; i++) {
printf("%lld%c", -(ll)N * (ll)a[i], i == N ? '\n' : ' ');
}
printf("2 %d\n", N);
for (int i = 2; i <= N; i++) {
printf("%lld%c", (ll)(N - 1) * (ll)a[i], i == N ? '\n' : ' ');
}
}
return 0;
}
#include
#include
#include
using namespace std;
const int maxn = 200010;
typedef long long ll;
int N;
ll a[maxn], M;
int main() {
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
if (N > M) {
cout << 0 << endl;
}
else {
ll ans = 1;
for (int i = 1; i <= N; i++) {
for (int j = i + 1; j <= N; j++) {
ans = ans * abs(a[i] - a[j]) % M;
}
}
cout << ans << endl;
}
return 0;
}
#include
#include
#include
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
P R, G, B;
vector<P> v;
int main() {
cin >> R.second >> G.second >> B.second;
R.first = R.second % 3, G.first = G.second % 3, B.first = B.second % 3;
v.push_back(R), v.push_back(G), v.push_back(B);
sort(v.begin(), v.end());
ll res = R.second + G.second + B.second;
while (v[0].first >= 1 && v[1].first >= 1 && v[2].first >= 1) {
v[0].first--;
v[1].first--;
v[2].first--;
}
if (v[0].first == 0 && v[1].first == 2 && v[2].first == 2 && v[0].second >= 3) {
cout << ((res - 1) / 3) << endl;
}
else {
cout << ((res - v[0].first - v[1].first - v[2].first) / 3) << endl;
}
return 0;
}
#include
#include
#include
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll N, M;
ll mod_pow(ll x, ll n) {
ll res = 1;
while (n) {
if(n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
int main() {
cin >> N >> M;
ll ans = 1;
for (ll i = N + 2 * M - 1; i >= N; i--) {
ans = ans * i % mod;
}
for (ll i = 1; i <= 2 * M; i++) {
ans = ans * mod_pow(i, mod - 2) % mod;
}
cout << ans << endl;
}
#include
#include
#include
using namespace std;
const int maxn = 10010;
int a[maxn], N;
int main() {
int max_id = 1;
scanf("%d", &N);
for (int i = 2; i <= N; i++) {
printf("? %d %d\n", i, max_id);
fflush(stdout);
int x1, x2;
scanf("%d", &x1);
printf("? %d %d\n", max_id, i);
fflush(stdout);
scanf("%d", &x2);
if (x1 > x2) {
a[i] = x1;
}
else if(x1 < x2){
a[max_id] = x2;
max_id = i;
}
}
a[max_id] = N;
printf("! ");
fflush(stdout);
for (int i = 1; i <= N; i++) {
printf("%d%c", a[i], i == N ? '\n' : ' ');
fflush(stdout);
}
return 0;
}
#include
#include
#include
using namespace std;
const int maxn = 200010;
vector<int> v[maxn];
int N, K;
int main() {
scanf("%d%d", &N, &K);
for (int i = 0; i < N; i++) {
int x;
scanf("%d", &x);
int cnt = 0;
while (x > 0) {
v[x].push_back(cnt);
x >>= 1;
cnt++;
}
v[0].push_back(cnt);
}
int ans = 1e9;
for (int i = 0; i <= 200000; i++) {
int res = 0;
if (v[i].size() >= K) {
sort(v[i].begin(), v[i].end());
for (int j = 0; j < K; j++) res += v[i][j];
ans = min(ans, res);
}
}
printf("%d\n", ans);
}
#include
#include
#include
#include
using namespace std;
const int maxn = 310;
int g[maxn][maxn], N, a[maxn], ans[maxn];
void floyd() {
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
g[i][j] |= (g[i][k] & g[k][j]);
}
}
}
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
char c;
cin >> c;
g[i][j] = c - '0';
}
}
floyd();
for (int u = 1; u <= N; u++) {
int id;
for (id = 1; id <= N; id++) {
if (a[id] == u) break;
}
for (int i = 1; i <= N && i <= id; i++) {
if (g[i][id] == 1 && !ans[i]) {
ans[i] = u;
swap(a[i], a[id]);
break;
}
if (i == id) {
ans[i] = u;
}
}
}
for (int i = 1; i <= N; i++) {
printf("%d%c", ans[i], i == N ? '\n' : ' ');
}
return 0;
}
#include
#include
#include
using namespace std;
double qpow(double x, int n) {
double res = 1;
while (n) {
if(n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
int N, M;
int main() {
cin >> M >> N;
double ans = 0;
for (int i = 1; i <= M; i++) {
ans += i * (qpow((double)i / (double)M, N) - qpow(((double)(i - 1) / (double)M), N));
}
printf("%.15f\n", ans);
return 0;
}
#include
#include
using namespace std;
typedef long long ll;
ll a, b;
int main() {
cin >> a >> b;
ll ans = 0;
while (b) {
ans += a / b;
a %= b;
swap(a, b);
}
cout << ans << endl;
}