T1:
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #define MN 10000105 6 #define MP 665000 7 using namespace std; 8 bool vis[MN]; 9 int pri[MP]; 10 long long n,T; 11 int pos,t,cnt=0; 12 inline int in(){ 13 int x=0;bool f=0; char c; 14 for (;(c=getchar())<'0'||c>'9';f=c=='-'); 15 for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 16 return f?-x:x; 17 } 18 inline int getp(int n){ 19 cnt=0;pri[0]=1; 20 for (int i=2;i<=n;++i){ 21 if (!vis[i]) pri[++cnt]=i; 22 for (int j=1;i*pri[j]<=n&&j<=cnt;++j){ 23 vis[i*pri[j]]=1; 24 if (!(i%pri[j])) break; 25 } 26 } 27 } 28 int main() 29 { 30 getp(10000020);scanf("%lld",&T);while (T--){ 31 scanf("%lld",&n);if (n<=1) {printf("-1 ");continue;} 32 t=(int)sqrt(n);pos=lower_bound(pri+1,pri+cnt+1,t)-pri; 33 if (pri[pos]>t) --pos; 34 printf("%d ",pos+2); 35 }return 0; 36 }
T2:
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define inf 0x7fffffff 5 #define MN 1002 6 using namespace std; 7 inline int in(){ 8 int x=0;bool f=0; char c; 9 for (;(c=getchar())<'0'||c>'9';f=c=='-'); 10 for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 11 return f?-x:x; 12 } 13 struct que{ 14 int q[MN],id[MN]; 15 int head,tail; 16 inline void pushmax(int x,int y){ 17 while (head<=tail&&x>q[tail]) --tail; 18 ++tail;q[tail]=x,id[tail]=y; 19 } 20 inline void pushmin(int x,int y){ 21 while (head<=tail&&x<q[tail]) --tail; 22 ++tail;q[tail]=x,id[tail]=y; 23 } 24 inline void pop(int x){ 25 while (head<=tail&&id[head]<=x) ++head; 26 } 27 }mx[MN],mn[MN],mxc,mnc; 28 int c[MN][MN]; 29 int n,m,a,b,cnt=0,res,dif; 30 int main() 31 { 32 freopen("anchorman.in","r",stdin); 33 freopen("anchorman.out","w",stdout); 34 n=in();m=in();a=in();b=in();res=inf,cnt=0; 35 for (int i=1;i<=n;++i) 36 for (int j=1;j<=m;++j) c[i][j]=in(); 37 for (int i=1;i<=n;++i) 38 mx[i].head=mn[i].head=1,mx[i].tail=mn[i].tail=0; 39 for (int i=1;i<=n;++i) 40 for (int j=1;j<b;++j) 41 mx[i].pushmax(c[i][j],j),mn[i].pushmin(c[i][j],j); 42 for (int j=b;j<=m;++j){ 43 mxc.head=mnc.head=1;mxc.tail=mnc.tail=0; 44 for (int i=1;i<=n;++i){ 45 mx[i].pushmax(c[i][j],j);mx[i].pop(j-b); 46 mn[i].pushmin(c[i][j],j);mn[i].pop(j-b); 47 mxc.pushmax(mx[i].q[mx[i].head],i);mxc.pop(i-a); 48 mnc.pushmin(mn[i].q[mn[i].head],i);mnc.pop(i-a); 49 dif=mxc.q[mxc.head]-mnc.q[mnc.head]; 50 if (i>=a) if (dif==res) ++cnt; 51 else if (dif<res) res=dif,cnt=1; 52 } 53 }printf("%d %d",res,cnt);return 0; 54 }