T1:
Subject description:
NYG a magic backpack, into each article to a volume of the pack becomes large.
That is, each placed an item, a backpack will be occupied by a certain volume, but then the total volume of the backpack and
will increase a certain value (note that the total volume of goods placed in a backpack after only increase).
NYG found this backpack is very easy to use, so can not help but think of a problem.
Backpack now given initial volume V of the article and n, a value of each of the two articles; B, respectively, showing the volume occupied by the article
and after the increase in volume of the backpack into the backpack.
NYG want to know whether all the items loaded into?
Because NYG more honest, such a simple question naturally do not like to pretend.
So he came to ask you.
Sample input:
3
7 9269
21366 1233
7178 23155
16679 23729
15062 28427
939 6782
24224 9306
22778 13606
5 22367
17444 5442
16452 30236
14893 24220
31511 13634
4380 29422
7 18700
25935 4589
24962 9571
26897 14892
20822 2380
21103 12648
32006 22912
23367 20674
Sample output:
Yes
Yes
No
Resolution:
Consider greedy. For b> = a, apparently a weight can be in ascending order. For b <a, b according to descending order. Proof is given below:
The whole process look backwards, with a volume equivalent to the volume increase of a, b increase the volume of the volume corresponds with the b. Analogous to b> = a process, and we definitely want to make up a small backpack that is b small volume value takes precedence. But because it is look backwards, so being there a time when you press b descending order.
The time complexity of O (nlogn)
Code:
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; typedef long long LL; const int MAXN=100010; int T; struct Backpack{ int a,b; }s1[MAXN],s2[MAXN]; int n; LL h; int cnt1=0,cnt2=0; inline int read(){ int ret=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-') f=-f;c=getchar();} while(c>='0'&&c<='9'){ret=ret*10+c-'0';c=getchar();} return ret*f; } bool cmp1(Backpack A,Backpack B){ return A.a<B.a; } bool cmp2(Backpack A,Backpack B){ return A.b>B.b; } int main(){ freopen("backpack.in","r",stdin); freopen("backpack.out","w",stdout); T=read(); while(T--){ cnt1=0;cnt2=0; n=read();h=1LL*read(); for(int i=1;i<=n;++i){ int a=read(),b=read(); if(b-a>=0){ s1[++cnt1].a=a;s1[cnt1].b=b; }else{ s2[++cnt2].a=a;s2[cnt2].b=b; } } sort(s1+1,s1+cnt1+1,cmp1); bool flag=false; for(int i=1;i<=cnt1;++i){ h-=1LL*s1[i].a; if(h<0){ printf("No\n"); flag=true; break; } h+=1LL*s1[i].b; } if(flag) continue; sort(s2+1,s2+cnt2+1,cmp2); for(int i=1;i<=cnt2;++i){ h-=1LL*s2[i].a; if(h<0){ printf("No\n"); flag=true; break; } h+=1LL*s2[i].b; } if(flag) continue; printf("Yes\n"); } return 0; }
T2:
Subject description:
Sample input:
30
15 15 3 30 9 30 27 11 5 15 20 10 25 20 30 15 30 15 25 5 10 20 7 7 16 2 7 7 28 7
Sample output:
1 13
9
Resolution:
Ask to see the title of the maximum conditions to meet, you know half the answer. The question is how check. We found that for an interval [l, r], ak value must satisfy the condition gcd this interval is, and this is the minimum interval. ST table can be used to maintain.
Pretreatment complexity of O (nlogn²), check complexity of O (nlogn), the total time complexity of O (nlogn²).
Code:
#include<cstdio> #include<iostream> using namespace std; typedef long long LL; const int MAXN=500004; LL a[MAXN]; LL Min[MAXN][25],gcd[MAXN][25]; int n; int lg[MAXN]; int ans[MAXN],top=0; inline LL read(){ LL ret=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(f=='-') f=-f;c=getchar();} while(c>='0'&&c<='9'){ret=ret*10+c-'0';c=getchar();} return ret*f; } inline LL min(LL a,LL b){ return a<b?a:b; } inline LL Gcd(LL a,LL b){ return (!b)?a:Gcd(b,a%b); } inline void prework(){ lg[0]=-1; for(register int i=1;i<=n;++i){ lg[i]=lg[(i>>1)]+1; Min[i][0]=a[i]; gcd[i][0]=a[i]; } for(register int k=1;k<=20;++k){ for(register int i=1;i<=n-(1<<k)+1;++i){ Min[i][k]=min(Min[i][k-1], Min [i + ( 1 << (k- 1 ))] [k- 1 ]); gcd [i] [k] = Gcd (gcd [i] [k- 1 ], gcd [i + ( 1 << (k- 1 ))] [k- 1 ]); } } } Inline bool check ( int len) { for (register int i = 1 ; i <= n-len + 1 ; ++ i) { if (Gcd (gcd [i] [lg [len]], gcd [i + len- ( 1 << lg [len])] [ lg [len]]) == min (Min [i] [lg [len]], Min [i + len- ( 1 << lg [len]) ] [lg [len]]) )return true ; } Return false ; } Inline void calc ( int len) { for (register int i = 1 ; i <= n-len + 1 ; ++ i) { if (Gcd (gcd [i] [lg [len]], gcd [i + len - ( 1 << lg [len])] [ lg [len]]) == min (Min [i] [lg [len]], Min [i + len- ( 1 << lg [len])] [ lg [len]])) ans [ ++ top] = i; } } Inline void solve () { int l = 0 , r = n; while(l<r){ int mid=(l+r+1)>>1; if(check(mid)) l=mid; else r=mid-1; } calc(l); printf("%d %d\n",top,l-1); for(register int i=1;i<=top;++i) printf("%d ",ans[i]); } int main(){ freopen("point.in","r",stdin); freopen("point.out","w",stdout); scanf("%d",&n); for(register int i=1;i<=n;++i) a[i]=read(); prework(); solve(); return 0; }
T3
Subject description:
Sample input:
4
1 1 2
10 6 6
3 1 4
100 1000 100000
Sample output:
2
512
198
540522901
Resolution:
First consider the equation dp: set CNT [i] is the number of i in geometric progression as the length of the column section, dp [i] is the total number of fill has i bits resolution.
Obviously dp [i] = dp [i-1] * cnt [1] + dp [i-2] * cnt [2] + ... + dp [0] * cnt [n];
This formula obviously can quickly optimize power matrix. The question then becomes how to quickly deal with an array cnt.
Code:
#include<cstdio> #include<iostream> #include<cmath> using namespace std; typedef long long LL; const int mod=1e9+7; LL n; int l,r,T; LL cnt[35]; struct Matrix{ LL m[35][35]; Matrix(){ for(int i=1;i<=27;++i) for(int j=1;j<=27;++j) m[i][j]=0LL; } }dw; int gcd(int x,int y){ return (!y)?x:gcd(y,x%y); } void prework(){ for(int i=0;i<=25;++i) cnt[i]=0; cnt[1]=r-l+1; cnt[2]=1LL*(r-l+1)*(r-l)%mod; int lim=sqrt(r)+1; for(int x=1;x<=lim;++x) for(int y=1;y<x;++y) if(gcd(x,y)==1){ int xx=x,yy=y; for(int k=2;1LL*xx*x<=r;++k) xx*=x,yy*=y,cnt[k+1]+=1LL*max(0,r/xx-(l-1)/yy); } for(int i=3;i<=25;++i) cnt[i]=(cnt[i]<<1)%mod; } Matrix mul(Matrix A,Matrix B){ Matrix C; for(int i=1;i<=27;++i) for(int j=1;j<=27;++j) for(int k=1;k<=27;++k) C.m[i][j]=(C.m[i][j]+A.m[i][k]*B.m[k][j]%mod)%mod; return C; } Matrix qpow(Matrix x,LL y){ Matrix res=dw; while(y){ if(y&1) res=mul(res,x); x=mul(x,x); y>>=1; } return res; } void work(){ Matrix A; A.m[1][1]=1; Matrix B; for(int i=1;i<=25;++i) B.m[i][1]=cnt[i]; B.m[27][1]=1LL*(r-l+1); for(int i=1;i<=25;++i) B.m[i][i+1]=1LL; B.m[1][27]=1LL; B.m[27][27]=1LL; B=qpow(B,n); printf("%lld\n",mul(A,B).m[1][1]%mod); } int main(){ freopen("excellent.in","r",stdin); freopen("excellent.out","w",stdout); scanf("%d",&T); for(int i=1;i<=27;++i) dw.m[i][i]=1; while(T--){ scanf("%lld%d%d",&n,&l,&r); prework(); work(); } return 0; }