#include<iostream>#include<cstdio>
using namespace std;constint N =1e5+10, MOD =10000;int n, a[N], f[N], b[N];//f:前缀乘积, b:后缀积 intmain(){scanf("%d",&n);for(int i =1; i <= n; i ++)scanf("%d",&a[i]);
f[0]=1;for(int i =1; i <= n; i ++){//预处理
f[i]= f[i -1]* a[i]% MOD;}
b[n +1]=1;for(int i = n; i >=1; i --){
b[i]= b[i +1]* a[i]% MOD;}for(int i =1; i <= n; i ++){int ans = f[i -1]* b[i +1]% MOD;printf("%d\n", ans);}return0;}
倍数区间 https://iai.sh.cn/problem/86
#include<iostream>#include<cstdio>
using namespace std;constint N =2e5+10;int n, k, a[N], s[N], cnt[N]={1};//s[0]%k = 0, 所以一开始,余数为0的情况就是1,cnt[0] = 1intmain(){scanf("%d%d",&n,&k);for(int i =1; i <= n; i ++){scanf("%d",&a[i]);
s[i]=(s[i -1]+ a[i])% k;
cnt[s[i]]++;}//任何整数除以k的余数范围:[0, k) int ans =0;for(int i =0; i < k; i ++){
ans += cnt[i]*(cnt[i]-1)/2;}printf("%d\n", ans);return0;}
平衡点 https://iai.sh.cn/problem/442
#include<iostream>#include<cstdio>#include<cmath>#defineLLlonglong
using namespace std;constint N =3e5+10;int n, x;
LL s1, s2, minn =1e18;intmain(){scanf("%d",&n);for(int i =1; i <= n; i ++){scanf("%d",&x);
s1 += x;
s2 += x * i;}//某个点的引力值等于 i * s1 - s2for(int i =1; i <= n; i ++){if(abs(i * s1 - s2)< minn){
minn =abs(i * s1 - s2);}}printf("%d\n", minn);return0;}
差分相关
最高的牛
注:p这个变量没有用处
差分,set
#include<iostream>#include<cstdio>#include<cstring>#include<set>
using namespace std;constint N =1e4+10;int n, p, h, m;//n头牛,a[p] = h, m对关系int a[N];//初始时设所有牛高度为h,则差分数组初始值为0
set < pair<int,int>> st;intmain(){
cin >> n >> p >> h >> m;
a[1]= h;//差分数组的第1项为h while(m --){int x, y;
cin >> x >> y;//a[x+1...y-1]的高度减1 if(x > y)swap(x, y);if(st.count({x, y}))continue;
st.insert({x, y});
a[x +1]-=1;
a[y]+=1;}for(int i =1; i <= n; i ++){
a[i]+= a[i -1];
cout << a[i]<< endl;}return0;}
差分,map
#include<iostream>#include<cstdio>#include<cstring>#include<map>
using namespace std;constint N =1e4+10;int n, p, h, m;//n头牛,a[p] = h, m对关系int a[N];//初始时设所有牛高度为h,则差分数组初始值为0
map < pair<int,int>, bool > mp;intmain(){
cin >> n >> p >> h >> m;
a[1]= h;//差分数组的第1项为h while(m --){int x, y;
cin >> x >> y;//a[x+1...y-1]的高度减1 if(x > y)swap(x, y);if(mp[{x,y}])continue;
mp[{x, y}]= true;
a[x +1]-=1;
a[y]+=1;}for(int i =1; i <= n; i ++){
a[i]+= a[i -1];
cout << a[i]<< endl;}return0;}
拓扑排序
#include<bits/stdc++.h>
using namespace std;constint N =1e4+10;int n, p, h, m, d[N], ans[N];
vector <int> g[N];
queue <int> q;intmain(){
cin >> n >> p >> h >> m;for(int i =1; i <= m; i ++){int a, b;
cin >> a >> b;if(a > b)swap(a, b);for(int j = a +1; j < b; j ++){
d[j]+=2;//建立a->j, b->j的两条边
g[a].push_back(j);
g[b].push_back(j);}}for(int i =1; i <= n; i ++){if(!d[i]){//入度为0的点,表示最高的牛
q.push(i);
ans[i]= h;}}while(q.size()){int ft = q.front(); q.pop();for(int i =0; i < g[ft].size(); i ++){int t = g[ft][i];if(-- d[t]==0){
q.push(t);
ans[t]= ans[ft]-1;}}}for(int i =1; i <= n; i ++) cout << ans[i]<< endl;return0;}
增减序列
#include<iostream>#include<cstdio>#include<algorithm>#defineLLlonglong
using namespace std;constint N =1e5+10;int n, a[N], b[N];
LL pos, neg;intmain(){scanf("%d",&n);for(int i =1; i <= n; i ++)scanf("%d",&a[i]);for(int i =2; i <= n; i ++){
b[i]= a[i]- a[i -1];if(b[i]>0) pos += b[i];else neg -= b[i];}
cout <<max(pos, neg)<< endl;
cout <<abs(pos - neg)+1<< endl;return0;}