cpp
#include<iostream>
using namespace std;
int n;
int k;
int const N = 1e5 + 10;
int arr[N];
long long s[N];
int main()
{
cin >> n >> k;
for (int i = 1;i <= n;i++)
{
cin >> arr[i];
s[i] = s[i - 1] + arr[i];
}
long long ans = 0;
for (int i = 1;i <= n;i++)
{
for (int j = i;j <= n;j++)
{
if ((s[j] - s[i - 1]) % k == 0)
{
ans++;
}
}
}
cout << ans << endl;
return 0;
}
cpp
#include<iostream>
using namespace std;
int n;
int k;
int const N = 1e5 + 10;
int arr[N];
long long s[N];
int x[N];
int main()
{
cin >> n >> k;
for (int i = 1;i <= n;i++)
{
cin >> arr[i];
s[i] = s[i - 1] + arr[i];
}
long long ans = 0;
for (int i = 0;i <= n;i++)
{
ans += x[s[i] % k];
x[s[i] % k]++;
}
cout << ans << endl;
return 0;
}
cpp
#include<iostream>
using namespace std;
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<string>
bool use[4];
int arr[4];
int s[4];
bool dp[2200][40][40];
void p(int x)
{
if (x < 10)
{
cout << 0 << x;
}
else
{
cout << x;
}
return;
}
bool run(int x)
{
if (x % 100 == 0)
{
if (x % 400 == 0)
{
return true;
}
else
{
return false;
}
}
else
{
if (x % 4 == 0)
{
return true;
}
else
{
return false;
}
}
}
void f(int num1,int num2,int num3)
{
//月
if (num2 > 12 || num2 < 1)
{
return;
}
//日
{
if (num2 == 1 || num2 == 3 || num2 == 5 || num2 == 7 || num2 == 8 || num2 == 10 || num2 == 12)
{
if (num3 <= 31)
{
cout << num1 << "-";
p(num2);
cout << "-";
p(num3);
cout<< endl;
return;
}
else
{
return;
}
}
else if (num2 == 2)
{
if (run(num1))
{
if (num3 <= 29)
{
cout << num1 << "-";
p(num2);
cout << "-";
p(num3);
cout << endl;
return;
}
else
{
return;
}
}
else
{
if(num3<=28)
{
cout << num1 << "-";
p(num2);
cout << "-";
p(num3);
cout << endl;
return;
}
else
{
return;
}
}
}
else
{
if (num3 <= 30)
{
cout << num1 << "-";
p(num2);
cout << "-";
p(num3);
cout << endl;
return;
}
else
{
return;
}
}
}
}
//void dfs(int u)
//{
// if (u == 4)
// {
// f();
// return;
// }
// for (int i = 1;i <= 3;i++)
// {
// if (use[i])
// {
// s[u] = arr[i];
// use[i] = false;
// dfs(u+1);
// use[i] = true;
// }
// }
//}
int fx(int num1)
{
if (num1 >= 60)
{
num1 = num1 + 1900;
}
else
{
num1 += 2000;
}
return num1;
}
int main()
{
memset(use, 1, sizeof(use));
string str;
cin >> str;
/*arr[1] = int(str[0] - 48) + int(str[1] - 48);
arr[2] = int(str[3] - 48) + int(str[4] - 48);
arr[3] = int(str[6] - 48) + int(str[7] - 48);*/
//dfs(1);
int num1, num2, num3;
num1 = int(str[0] - 48) + int(str[1] - 48);
num2 = int(str[3] - 48) + int(str[4] - 48);
num3 = int(str[6] - 48) + int(str[7] - 48);
dp[fx(num1)][num2][num3] = 1;
dp[fx(num3)][num1][num2] = 1;
dp[fx(num3)][num2][num1] = 1;
for (int i = 1960;i <= 2059;i++)
{
for (int j = 1;j <=12;j++)
{
for (int z = 1;z <= 31;z++)
{
if (dp[i][j][z])
{
f(i, j, z);
}
}
}
}
return 0;
}
cpp
#include<iostream>
using namespace std;
int const N = 1e3 + 10;
int m, n;
int k;
int fa[N * N];
int res[N * N];
int find(int x)
{
if (fa[x] != x)
{
fa[x] = find(fa[x]);
}
return fa[x];
}
void unity(int x, int y)
{
int f1 = find(x);
int f2 = find(y);
fa[f1] = f2;
}
int main()
{
cin >> m>> n;
cin >> k;
for (int i = 1;i <= m;i++)
{
for (int j = 1;j <= n;j++)
{
fa[(i - 1) * n + j] = (i - 1) * n + j;
//cout << (i - 1) * n + j << " ";
}
// << endl;
}
for (int i = 1;i <= k;i++)
{
int num1;
int num2;
cin >> num1 >> num2;
int x = find(num1);
int y = find(num2);
unity(x, y);
}
int ans = 0;
//验证
for (int i = 1;i <= m;i++)
{
for (int j = 1;j <= n;j++)
{
res[find((i - 1) * n + j)]++;
//cout << find((i - 1) * n + j) << " ";
}
//cout << endl;
}
for (int i = 1;i <= n * m;i++)
{
if (res[i] >= 1)ans++;
}
cout << ans << endl;
return 0;
}
cpp
#include<iostream>
using namespace std;
int const N = 1e3 + 10;
char arr[N][N];
char cop[N][N];
int n;
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void yuan(int x, int y)
{
cop[x][y] = '.';
for (int i = 0;i < 4;i++)
{
int r1 = x + dx[i];
int r2 = y + dy[i];
if (r1 > 0 && r1 <= n && r2 > 0 && r2 <= n&&cop[r1][r2]=='#')
{
yuan(r1, r2);
}
}
}
void hou(int x,int y)
{
arr[x][y] = '.';
for (int i = 0;i < 4;i++)
{
int r1 = x + dx[i];
int r2 = y + dy[i];
if (r1 > 0 && r1 <= n && r2 > 0 && r2 <= n && arr[r1][r2] == '#')
{
hou(r1, r2);
}
}
}
int main()
{
cin >> n;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
cin >> arr[i][j];
cop[i][j] = arr[i][j];
}
}
int ans = 0;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
if (cop[i][j] == '#')
{
ans++;
yuan(i, j);
}
}
}
int anss = 0;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
if (arr[i][j] == '#')
{
int flag = 1;
for (int z = 0;z < 4;z++)
{
int r1 = i + dx[z];
int r2 = j + dy[z];
if (r1 > 0 && r1 <= n && r2 > 0 && r2 <= n && arr[r1][r2] == '.')
{
flag = 0;
}
}
if (flag == 1)
{
anss++;
hou(i, j);
}
}
}
}
cout << ans-anss << endl;
return 0;
}