【入门6】函数与结构体 - 题单 - 洛谷
P5461 赦免战俘
赦免战俘 - 洛谷
1:分治
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1 << 11;
int N, M, MAP[maxn][maxn];
void Init(int len, int x, int y)
{
if (len == 2)
{
MAP[x][y] = 0;
return;
}
for (int i = x; i <= x + len / 2 - 1; i++)
for (int j = y; j <= y + len / 2 - 1; j++)
MAP[i][j] = 0;
Init(len / 2, x + len / 2, y);
Init(len / 2, x + len / 2, y + len / 2);
Init(len / 2, x ,y + len / 2);
}
int main()
{
cin >> N;
M = 1 << N;
fill(MAP[0], MAP[0] + sizeof(MAP) / sizeof(int), 1);
Init(M, 1, 1);
for (int i = 1; i <= M; i++)
{
for (int j = 1; j <= M; j++)
cout << MAP[i][j] << " ";
cout << endl;
}
return 0;
}
2:杨辉递推
3:位运算
P2415 集合求和
集合求和 - 洛谷
找规律,n个数,对于每一个数出现 n - 1次
#include<bits/stdc++.h>
using namespace std;
long long num, sum, n;
int main()
{
while (cin >> num)sum += num, n++;
while (--n)sum *= 2;
printf("%lld",sum);
return 0;
}