模拟
代码:
#include
#define ri register int
using namespace std;
inline int read(){
#define gc getchar
int ans=0;
char ch=gc();
while(!isdigit(ch))ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return ans;
#undef gc
}
const int mod=998244353;
typedef long long ll;
inline int add(int a,int b){return (a+=b)<mod?a:a-mod;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return (ll)a*b%mod;}
inline void Add(int&a,int b){(a+=b)<mod?a:a-=mod;}
inline void Dec(int&a,int b){(a-=b)<0?a+=mod:a;}
inline void Mul(int&a,int b){a=(ll)a*b%mod;}
inline int ksm(int a,int p){int ret=1;for(;p;p>>=1,Mul(a,a))if(p&1)Mul(ret,a);return ret;}
int n;
int main(){
ll a,b;
cin>>a>>b;
cout<<max(a-b,max(a+b,a*b));
return 0;
}
模拟
#include
#define ri register int
using namespace std;
inline int read(){
#define gc getchar
bool f=1;
int ans=0;
char ch=gc();
while(!isdigit(ch))f^=ch==gc(),ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return f?ans:-ans;
#undef gc
}
const int mod=998244353;
typedef long long ll;
inline int add(int a,int b){return (a+=b)<mod?a:a-mod;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return (ll)a*b%mod;}
inline void Add(int&a,int b){(a+=b)<mod?a:a-=mod;}
inline void Dec(int&a,int b){(a-=b)<0?a+=mod:a;}
inline void Mul(int&a,int b){a=(ll)a*b%mod;}
inline int ksm(int a,int p){int ret=1;for(;p;p>>=1,Mul(a,a))if(p&1)Mul(ret,a);return ret;}
int n;
int main(){
int k,x;
cin>>k>>x;
for(ri i=-k+1;i<=k-1;++i)cout<<i+x<<' ';
return 0;
}
模拟
#include
#define ri register int
using namespace std;
inline int read(){
#define gc getchar
bool f=1;
int ans=0;
char ch=gc();
while(!isdigit(ch))f^=ch==gc(),ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return f?ans:-ans;
#undef gc
}
const int mod=998244353;
typedef long long ll;
inline int add(int a,int b){return (a+=b)<mod?a:a-mod;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return (ll)a*b%mod;}
inline void Add(int&a,int b){(a+=b)<mod?a:a-=mod;}
inline void Dec(int&a,int b){(a-=b)<0?a+=mod:a;}
inline void Mul(int&a,int b){a=(ll)a*b%mod;}
inline int ksm(int a,int p){int ret=1;for(;p;p>>=1,Mul(a,a))if(p&1)Mul(ret,a);return ret;}
const int N=1e5+5;
int n;
string s[N];
int main(){
n=read();
for(ri i=1;i<=n;++i)cin>>s[i],sort(s[i].begin(),s[i].end());
sort(s+1,s+n+1);
ll ans=0,cnt=0;
for(ri i=1;i<=n;++i){
if(s[i]!=s[i-1]){
ans+=(ll)cnt*(cnt-1)/2;
cnt=1;
}
else ++cnt;
}
ans+=(ll)cnt*(cnt-1)/2;
cout<<ans;
return 0;
}
按时间和权值排序,时间倒流贪心即可。
#include
#define ri register int
#define fi first
#define se second
using namespace std;
inline int read(){
#define gc getchar
bool f=1;
int ans=0;
char ch=gc();
while(!isdigit(ch))f^=ch==gc(),ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return f?ans:-ans;
#undef gc
}
const int mod=998244353;
typedef long long ll;
inline int add(int a,int b){return (a+=b)<mod?a:a-mod;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return (ll)a*b%mod;}
inline void Add(int&a,int b){(a+=b)<mod?a:a-=mod;}
inline void Dec(int&a,int b){(a-=b)<0?a+=mod:a;}
inline void Mul(int&a,int b){a=(ll)a*b%mod;}
inline int ksm(int a,int p){int ret=1;for(;p;p>>=1,Mul(a,a))if(p&1)Mul(ret,a);return ret;}
const int N=1e5+5;
int n,m;
typedef pair<int,int> pii;
pii a[N];
inline bool cmp(pii a,pii b){return a.fi^b.fi?a.fi<b.fi:a.se>b.se;}
int main(){
n=read(),m=read();
for(ri i=1;i<=n;++i)a[i].fi=read(),a[i].se=read();
sort(a+1,a+n+1,cmp);
ll ans=0;
int cur=1;
priority_queue<int>q;
for(ri i=m;~i;--i){
while(cur<=n&&i+a[cur].fi<=m)q.push(a[cur].se),++cur;
if(q.size())ans+=q.top(),q.pop();
}
cout<<ans;
return 0;
}
边权全部减 P P P然后跑最长路
#include
#define ri register int
#define fi first
#define se second
using namespace std;
inline int read(){
#define gc getchar
bool f=1;
int ans=0;
char ch=gc();
while(!isdigit(ch))f^=ch==gc(),ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return f?ans:-ans;
#undef gc
}
const int mod=998244353;
typedef long long ll;
inline int add(int a,int b){return (a+=b)<mod?a:a-mod;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return (ll)a*b%mod;}
inline void Add(int&a,int b){(a+=b)<mod?a:a-=mod;}
inline void Dec(int&a,int b){(a-=b)<0?a+=mod:a;}
inline void Mul(int&a,int b){a=(ll)a*b%mod;}
inline int ksm(int a,int p){int ret=1;for(;p;p>>=1,Mul(a,a))if(p&1)Mul(ret,a);return ret;}
const int N=2505;
int n,m,P,cnt[N];
vector<int>e[N],val[N];
ll dis[N];
bool ban[N],in[N];
const double eps=1e-7;
inline bool check(double x){
queue<int>q;
for(ri i=1;i<=n;++i)in[i]=0,cnt[i]=0,dis[i]=-1e9;
dis[1]=0,q.push(1);
while(q.size()){
int x=q.front();
q.pop();
in[x]=0;
for(ri i=0,v;i<e[x].size();++i){
if((dis[v=e[x][i]])<dis[x]+val[x][i]){
if(ban[v])continue;
dis[v]=dis[x]+val[x][i];
if(!in[v]){
in[v]=1;
++cnt[v];
if(cnt[v]>=n){
puts("-1");
exit(0);
}
q.push(v);
}
}
}
}
cout<<max(0ll,dis[n]);
}
inline bool dfs(int p){
if(in[p])return 0;
if(p==n)return 1;
in[p]=1;
for(ri i=0;i<e[p].size();++i)if(dfs(e[p][i]))return 1;
return 0;
}
int main(){
n=read(),m=read(),P=read();
for(ri i=1,u,v,w;i<=m;++i){
u=read(),v=read(),w=read()-P;
e[u].push_back(v),val[u].push_back(w);
}
for(ri i=1;i<=n;++i){
memset(in,0,sizeof(in));
if(!dfs(i))ban[i]=1;
}
check(0);
return 0;
}
暴力多项式乘法/取模+多项式快速插值即可。
#include
#define ri register int
using namespace std;
const int rlen=1<<18|1;
inline char gc(){
static char buf[rlen],*ib,*ob;
(ib==ob)&&(ob=(ib=buf)+fread(buf,1,rlen,stdin));
return ib==ob?-1:*ib++;
}
inline int read(){
int ans=0;
char ch=gc();
while(!isdigit(ch))ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return ans;
}
typedef long long ll;
int mod;
inline int add(const int&a,const int&b){return a+b>=mod?a+b-mod:a+b;}
inline int dec(const int&a,const int&b){return a>=b?a-b:a-b+mod;}
inline int mul(const int&a,const int&b){return (ll)a*b%mod;}
inline void Add(int&a,const int&b){a=a+b>=mod?a+b-mod:a+b;}
inline void Dec(int&a,const int&b){a=a>=b?a-b:a-b+mod;}
inline void Mul(int&a,const int&b){a=(ll)a*b%mod;}
inline int ksm(int a,int p){int ret=1;for(;p;p>>=1,a=mul(a,a))if(p&1)Mul(ret,a);return ret;}
vector<int>pos[19],A,B,w[19];
int lim,tim,invv[19];
const int Lim=1<<19;
struct poly{
vector<int>a;
poly(int k=0,int x=0){a.resize(k+1),a[k]=x;}
inline int deg()const{return a.size()-1;}
inline int&operator[](const int&k){return a[k];}
inline const int&operator[](const int&k)const{return a[k];}
inline poly extend(const int&k){poly ret=*this;return ret.a.resize(k+1),ret;}
inline poly rev(){return reverse(a.begin(),a.end()),*this;}
inline int getval(const int&x){
int ret=0;
for(ri i=0,mult=1,up=deg();i<=up;++i,Mul(mult,x))Add(ret,mul(mult,a[i]));
return ret;
}
};
inline poly operator+(const poly&a,const poly&b){
int n=a.deg(),m=b.deg();
poly ret(max(n,m));
for(ri i=0;i<=n;++i)Add(ret[i],a[i]);
for(ri i=0;i<=m;++i)Add(ret[i],b[i]);
return ret;
}
inline poly operator-(const poly&a,const poly&b){
int n=a.deg(),m=b.deg();
poly ret(max(n,m));
for(ri i=0;i<=n;++i)Add(ret[i],a[i]);
for(ri i=0;i<=m;++i)Dec(ret[i],b[i]);
return ret;
}
inline poly operator*(const int&a,const poly&b){
poly ret=b;
for(ri i=0,n=ret.deg();i<=n;++i)Mul(ret[i],a);
return ret;
}
inline poly poly_direv(const poly&a){
poly ret(a.deg()-1);
for(ri i=1,up=a.deg();i<=up;++i)ret[i-1]=mul(a[i],i);
return ret;
}
inline poly operator*(poly a,poly b){
int n=a.deg(),m=b.deg(),t=n+m;
poly c(t);
for(ri i=0;i<=n;++i)for(ri j=0;j<=m;++j)Add(c[i+j],mul(a[i],b[j]));
return c;
}
inline poly operator%(poly a,poly b){
int n=a.deg(),m=b.deg();
if(n<m)return a;
for(ri t,i=n;i>=m;--i){
t=a[i];
for(ri j=i,tt=m;~tt;--tt,--j)Dec(a[j],mul(b[tt],t));
}
return a=a.extend(m),a;
}
const int N=1e5+5;
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (l+r>>1)
poly T[N<<2];
int n,fx[N],fy[N],val[N];
inline void build(int p,int l,int r){
if(l==r){T[p]=poly(1,1),T[p][0]=mod-fx[l];return;}
build(lc,l,mid),build(rc,mid+1,r);
T[p]=T[lc]*T[rc];
}
inline void getval(int p,int l,int r,poly t){
if(l==r){
val[l]=mul(ksm(t.getval(fx[l]),mod-2),fy[l]);
return;
}
getval(lc,l,mid,t%T[lc]),getval(rc,mid+1,r,t%T[rc]);
}
inline poly dc_ntt(int p,int l,int r){
if(l==r)return poly(0,val[l]);
poly L=dc_ntt(lc,l,mid),R=dc_ntt(rc,mid+1,r);
return L*T[rc]+T[lc]*R;
}
#undef lc
#undef rc
#undef mid
int main(){
mod=n=read();
for(ri i=1;i<=n;++i)fx[i]=i-1,fy[i]=read();
build(1,1,n);
getval(1,1,n,poly_direv(T[1]));
poly f=dc_ntt(1,1,n);
for(ri i=0;i<n;++i)cout<<f[i]<<' ';
return 0;
}