方格分割
注意结果除以4
#include <iostream>
using namespace std;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int ans = 0;
int map[7][7] = { 0 };
void DFS(int x, int y)
{
if (x == 0 || x == 6 || y == 0 || y == 6)
{
ans++;
return;
}
for (int i = 0; i < 4; i++)
{
int newx = x + dx[i];
int newy = y + dy[i];
if (map[newx][newy] == 0)
{
map[newx][newy] = 1;
map[6 - newx][6 - newy] = 1;
DFS(newx, newy);
map[newx][newy] = 0;
map[6 - newx][6 - newy] = 0;
}
}
}
int main()
{
map[3][3] = 1;
DFS(3, 3);
cout << ans / 4 << endl;
return 0;
}
包子凑数
对于不完全为0的整数a,b那么一定存在整数x,y使得gcd(x,y) = a * x + b * y。
所以我们可以假设a * x + b * y = d,要使该方程有解,那么d % gcd(a, b) = 0是必然的,如果gcd(a,b) = 1,那么d就能整除任何数,也就说a,b这2个数可以凑除任何数,反之不能。
#include <iostream>
#include <cmath>
using namespace std;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
const int N = 10010, M = 110;
int f[N];
int a[N];
int main()
{
int n;
cin >> n;
int g = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
g = gcd(g, a[i]);
}
if (g != 1)
{
puts("INF");
return 0;
}
for (int i = 1; i <= n; i++) f[a[i]] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j + a[i] <= 10000; j++)
{
if (f[j]) f[j + a[i]] = 1;
}
}
int ans = 0;
for (int i = 1; i <= 10000; i++) if (!f[i]) ans++;
cout << ans << endl;
return 0;
}
分巧克力
二分
#include <iostream>
#include <cmath>
#define LL long long int
using namespace std;
const int N = 100010;
int n, m;
int h[N], w[N];
bool check(int mid)
{
LL res = 0;
for (int i = 0; i < n; i++)
{
res += (LL)h[i] / mid * (w[i] / mid);
if (res >= m) return true;
}
return false;
}
int main()
{
int l = 1, r = 0;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> h[i] >> w[i];
r = max(r, max(h[i], w[i]));
}
while (l < r)
{
int mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << r;
return 0;
}
等差素数列
#include <iostream>
#include <cmath>
using namespace std;
const int N = 1e5 + 5;
int vis[N];
bool check(int x)
{
for (int i = 2; i * i <= x; i++)
{
if (x % i == 0)
{
return 0;
}
}
return 1;
}
int main()
{
memset(vis, 0, sizeof(vis));
for (int i = 2; i <= N; i++)
{
if (check(i))
{
vis[i] = 1;
}
}
for (int i = 1; i <= N; i++)
{
for (int j = 2; j <= N; j++)
{
int count;
for (count = 0; count < 10; count++)
{
if (vis[j + count * i] != 1)
{
break;
}
}
if (count == 10)
{
cout << i << endl;
return 0;
}
}
}
}
承压数计算
#include <iostream>
using namespace std;
double a[40][40] = { 0 };
int main()
{
double max = -1e18;
double min = 1e18;
for (int i = 1; i < 30; i++)
{
for (int j = 1; j <= i; j++)
{
cin >> a[i][j];
}
}
for (int i = 2; i <= 30; i++)
{
for (int j = 1; j <= i; j++)
{
a[i][j] += (a[i - 1][j - 1] / 2.0 + a[i - 1][j] / 2.0);
}
}
for (int i = 1; i <= 30; i++)
{
if (a[30][i] > max) max = a[30][i];
if (a[30][i] < min) min = a[30][i];
}
printf("%lf", 2086458231 / min * max);
return 0;
}
日期问题
#include <iostream>
using namespace std;
int m[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int x1, x2, x3;
bool judge(int n)
{
if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0)) return true;
else return false;
}
bool check(int a, int b, int c)
{
if (a == x1 && b == x2 && c == x3)
return true;
return false;
}
int main()
{
cin >> x1 >> x2 >> x3;
for (int i = 19600101; i <= 20591231; i++)
{
int year = i / 10000;
int month = i % 10000 / 100;
int day = i % 100;
if (judge(year)) m[2] = 29;
if (month >= 1 && month <= 12 && day >= 1 && day <= m[month])//日期和法
{
if (check(year % 100, month, day) || check(month, day, year % 100) || check(day, month, year % 100))//匹配
{
printf("%d-%02d-%02d\n", year, month, day);
}
}
m[2] = 28;
}
return 0;
}
k倍区间
前缀和
#include <iostream>
#define LL long long int
using namespace std;
LL a[100009];
LL sumn[100009];
LL mod[100009];
LL ans;
int main()
{
LL n, k;
cin >> n >> k;
for (LL i = 1; i <= n; i++)
{
cin >> a[i];
sumn[i] = sumn[i - 1] + a[i];
ans += mod[sumn[i] % k];
mod[sumn[i] % k]++;
if (sumn[i] % k == 0) ans++;
}
cout << ans;
}