#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int a[111],b[111];
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);
int t=read();
while(t--) {
int n=read();
For(i,n) a[i]=read();
For(i,n) b[i]=read();
For(i,n) if(a[i]>b[i]) swap(a[i],b[i]);
int p=*max_element(a+1,a+1+n);
int q=*max_element(b+1,b+1+n);
if(p==a[n]&&q==b[n]) puts("Yes");else puts("No");
}
return 0;
}
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (50100)
vi a[MAXN];
bool b[MAXN];
int m;
bool ck() {
vi ans;
ForD(i,m) {
bool fl=0;
for(int p:a[i]) {
if(!b[p]) {
b[p]=1;
if(!fl) ans.pb(p);
fl=1;
}
}
if(!fl) return 0;
}
reverse(ALL(ans));
for(int i:ans) cout<<i<<' ';
cout<<endl;return 1;
}
int main()
{
// freopen("B.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
while(T--) {
m=read();
For(i,m) {
int p=read();
while(p--) {
int c=read();
a[i].pb(c);
b[c]=0;
}
}
if(!ck()) puts("-1");
For(i,m) a[i].resize(0);
}
return 0;
}
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (202020)
int n;
ll a[MAXN],b[MAXN];
ll __gcd(ll a,ll b){
if(!b) return a;return __gcd(b,a%b);
}
ll __lcm(ll a,ll b){
return a/__gcd(a,b)*b;
}
int main()
{
// freopen("C.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
while(T--) {
n=read();
For(i,n) a[i]=read(),b[i]=read();
ll p=0,q=0,ans=0;
For(i,n) {
ll mi=b[i];
ll ma=b[i]*a[i];
if(!p && !q) {
p=mi,q=ma;
}
else {
p=__lcm(p,mi);
q=__gcd(q,ma);
if(q%p!=0) {
++ans;
p=mi,q=ma;
}
}
}
cout<<ans+1<<endl;
}
return 0;
}
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll a[301000],ans[301000];
int main()
{
// freopen("D.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
while(T--) {
int n=read();
For(i,n) a[i]=read();
int p=*max_element(a+1,a+1+n);
int q=*min_element(a+1,a+1+n);
if(p<=0||q>=0) puts("No");
else {
vi v[2];
For(i,n) {
if(a[i]>0) v[0].pb(a[i]);
else v[1].pb(a[i]);
}
puts("Yes");
ll s=0;
int p0=0,p1=0;
For(i,n) {
int c;
if(s<0 || (s==0 &&p0<SI(v[0])) ) {
c=v[0][p0++];
} else {
c=v[1][p1++];
}
s+=c;
ans[i]=c;
}
PRi(ans,n)
}
}
return 0;
}
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (301000)
ll a[MAXN];
ll f[MAXN];
ll ans[MAXN];
ll ma[MAXN],sfa[MAXN];
ll la[MAXN];
int main()
{
freopen("E.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
while(T--) {
int n=read();
For(i,n) a[i]=read();
For(i,n) f[i]=-1; f[n+1]=0;
ForD(i,n) {
if(i+a[i]+1<=n+1 && f[i+a[i]+1]!=-1)
f[i]=f[i+a[i]+1]+1;
}
ma[n+1]=0;
ForD(i,n) ma[i]=max(ma[i+1],f[i]);
// PRi(f,n)
// PRi(ma,n)
int fma=1;
ForD(i,n) if(f[i]==fma) la[fma]=i,++fma;
ForD(i,n) {
sfa[i]=ma[i+1]+1;
if(i+a[i]+1<=n+1)
gmax(sfa[i],1+sfa[i+a[i]+1])
}
For(i,n) ans[i]=2;
ForD(i,n-1) {
if(a[i]==f[i+1]) ans[i]=0;
}
ForD(i,n-1) if(ans[i]==2) {
if(f[i+1]!=-1) ans[i]=1;
else if(a[i]==1) ans[i]=1;
else if(a[i]<=sfa[i+1] ) ans[i]=1;
}
PRi(ans,(n-1))
}
return 0;
}
from any 2 n − 1 2n−1 2n−1 integers, you can choose n n n with a sum divisible by n n n (Erdős–Ginzburg–Ziv theorem)
所以直接暴力对最小的班级暴力背包,最后最大的那个加一个元素
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (301)
ll a[MAXN];
ll p[MAXN],s[MAXN];
vi v;
vi ans[1000];
int main()
{
// freopen("F.in","r",stdin);
// freopen(".out","w",stdout);
int T=1;
while(T--) {
int n=read(),k=read();
For(i,n) a[i]=read(),v.pb(a[i]);
ll pans=0;
For(i,k) s[i]=read();
iota(p+1,p+1+k,1);
sort(p+1,p+1+k,[&](int a,int b) {return s[a]<s[b]; } );
For(tt,k) {
int i=p[tt];
if(tt<k) {
vector<vector<vector<int> > > f(SI(v)+1, vector<vector<int> >(s[i]+1, vector<int>(s[i],0)));
vector<vector<vector<int> > > pr(SI(v)+1, vector<vector<int> >(s[i]+1, vector<int>(s[i],0)));
f[0][0][0]=1;
int sz=s[i];
Rep(i,SI(v)) Rep(j,min(i,sz)+1 ) Rep(k,sz) {
if(f[i][j][k]) {
f[i+1][j][k]=1;
if(j<sz)f[i+1][j+1][(k+v[i])%sz]=1;
}
}
int x=SI(v),j=sz,k=0;
vi id;
assert(f[x][j][k]);
while(j>0) {
if(x>j&f[x-1][j][k]) --x;
else {
x--,j--,k=((k-v[x])%sz+sz)%sz;
ans[i].pb(v[x]);
v.erase(v.begin()+x);
}
}
}
else {
ll t=0;
for(auto p:v) t+=p,ans[i].pb(p);
pans=s[i]-t%s[i];
ans[i].pb(pans);
}
}
cout<<pans<<endl;
For(i,k) {
for(auto j:ans[i]) cout<<j<<' ';
cout<<endl;
}
For(i,k) ans[i].resize(0);
}
return 0;
}