1.D守恒(主要通过数份数来和n作比较,一定要特判1这个特殊情况)
#include<bits/stdc++.h>
using namespace std;
long long t[1000000];
void solve()
{
int n;
cin>>n;
long long sum=0,ans=0;
for(int i=0;i<n;i++)
{
cin>>t[i];
sum+=t[i];
}
if(n==1){cout<<1<<endl;
return;
}
for(int i=1;i<=ceil((double)sum/n);i++)
{
if(sum/i>=n&&sum%i==0)ans++;
}
cout<<ans<<endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
int T = 1;
// cin >> T ;
while(T--) solve();
return 0;
}
2.E漂亮数组(使用map存图,记录余数情况,如果某余数再次出现,说明中间有一段数字和为k,ans计数并清图重新计算)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void solve()
{
ll n,k,t;cin >> n >> k;
map<ll,int> mp;
mp[0]=1;
ll sum=0,ans=0;
for(int i=1;i<=n;i++){
cin >> t;
sum+=t;
if(mp[sum%k]){
ans++;
sum=0;
mp.clear();
mp[0]=1;
}else{
mp[sum%k]=1;
}
}cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
int T = 1;
// cin >> T ;
while(T--) solve();
return 0;
}
3.G数三角形(easy)(枚举三角形的最上面那个点,然后用一个前缀和来看下面是否有一条线)
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 510 , M = 1e6+10 ;
int ans=0;
char t[N][N];
int h[N][N];
void solve() {
int n,m;
cin >> n >> m;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
cin >> t[i][j];
if(t[i][j]=='*') h[i][j]=h[i][j-1]+1;
else h[i][j]=h[i][j-1];
}
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(t[i][j]=='*') {
int k=i+1 , x=j-1 , y=j+1 , cnt=1;
while(k<=n && x>=1 && y<=m && t[k][y]=='*' && t[k][x]=='*') {
if(h[k][y]-h[k][x-1]==2*cnt+1) ans++;
k++ , x-- , y++ , cnt++;
}
}
}
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
int T = 1;
// cin >> T ;
while(T--) solve();
return 0;
}