大意:
输入 a b k,范围 [1,1e6],a≤b。
请找到最短的 L,使得对于任意 a≤x≤b-L+1 都满足:在 x,x+1,...,x+L-1 中至少有 k 个质数。
输出 L。如果 L 不存在,输出 -1。
输入 2 4 2
输出 3
输入 6 13 1
输出 4
输入 1 4 3
输出 -1
思路1:根据数据范围可先通过欧拉筛预处理质数,同时使用前缀和sum[x]存[1,x]之间存在的质数个数。对于输入的a,b,若sum[b]-sum[a-1]< k ,显然无法存在L满足题意。若sum[b]-sum[a-1]>=k,则进行如下操作:
显然,对于L = b-a+1,是满足题意的,我们从x=a开始检索,显然,检索a的时候,L的值为b-x+1,这时先比较L和前面的所得的能到达的L的最小值minres,若小于minres,显然不能该L值及其往下不能作为结果,反之,则寻找sum[x'] - sum[x-1] = k的x',若无法找到,说明该L值及其往下不能作为结果,若找到了,需要判断x'-x+1即区间长度是否小于等于L,若小于等于L,则该L值能作为结果,反之,则无法作为结果。单词询问时间复杂度O(b-a)
思路2:二分答案。单词询问时间复杂度( (b-a)log(b-a))
思路一代码:
#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <deque>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 1e6+10 ;
int sum[N] ;
bool st[N] ;
int primes[N] ,cnt;
void init( )
{
st[0] = st[1] = true;
for(int i=2;i< N ; ++i){
if( !st[i]){
sum[i] = sum[i-1] +1;
primes[cnt++] = i ;
}else sum[i] =sum[i-1] ;
for(int j = 0 ; j <cnt && i*primes[j] < N; ++j){
st[ primes[j] * i ] = true;
if( 0 ==i % primes[j]) break;
}
}
//for(int i =0 ; i <cnt; ++i) cout<<primes[i] <<" " ;
}
int main()
{
init() ;
int T ;T=1;
while(T--){
int a,b,k;
cin >>a >>b >>k ;
if( sum[b] -sum[a-1] < k ) {
cout<<-1<<endl;continue;
}
int x = a, x1 = a , minres = 1;
int res = b-a+2;
while( x1 <=b ){
while( x1<=b && sum[x1]-sum[x-1] <k) ++x1;
if( x1 <=b && sum[x1] -sum[x-1] >= k ) {
int l = b -x +1 ;
if( l < minres ) break;
if( x1 -x > l ) break;
minres = max( minres, x1-x+1) ;
//cout<<l <<" " <<minres << " " <<res <<" " <<x<<" "<<x1<<endl;
res--;
++x;
}else break;
}
cout<< res<<endl;
}
return 0 ;
}
思路二代码:
#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <deque>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 1e6+10 ;
int sum[N] ;
bool st[N] ;
int primes[N] ,cnt;
int l ,r ;
int a,b,k;
void init( )
{
st[0] = st[1] = true;
for(int i=2;i< N ; ++i){
if( !st[i]){
sum[i] = sum[i-1] +1;
primes[cnt++] = i ;
}else sum[i] =sum[i-1] ;
for(int j = 0 ; j <cnt && i*primes[j] < N; ++j){
st[ primes[j] * i ] = true;
if( 0 ==i % primes[j]) break;
}
}
//for(int i =0 ; i <cnt; ++i) cout<<primes[i] <<" " ;
}
bool check( int x ){
for(int i =a; i+x-1 <= b ; ++i){
if( sum[i+x-1] - sum[i-1] < k ) return false;
}
return true;
}
int main()
{
init() ;
int T ;T=1;
while(T--){
cin >>a >>b >>k ;
if( sum[b] -sum[a-1] < k ) {
cout<<-1<<endl;continue;
}
l = 1 , r= b-a+1;
while( l < r ){
int mid = l+r>>1;
if( check(mid)) r = mid;
else l = mid +1;
}
cout<< r<<endl;
}
return 0 ;
}
大意:给定字符串n长度,只有+-两符号,q次询问,每次给出x,y问是否能为每个x,y分配完所有正负号使得和为0.
思路一:显然,当无论x,y为什么值,当+-的个数相同时,可以满足题意。当不同时,求x,y的最大公约数,进而求出x,y乘以某个系数能相等。然后分别对+-的个数进行减法,当某个时候出现+-的个数相同时,说明能完成,反之,当+-有个数<0时,则不满足题意。
思路二:设a个+,b个-。当只分配给x或y,则有 (a-b)*x = score 显然,只有a=b才能满足为0.
当分配给xy时,设x分配了k1个+,k2个-,则y分配了a-k1个+,b-k2个-。则有
x*k1 - x*k2 + y*(a-k1) - y*(a-k2) = score。
不妨设k= k1-k2,由于k1属于[0,a],k2属于[0,b],则k属于[-b,a]。
那么有 x*k + y*(a-k) = score;即k*(x-y) = score + y*a要使的score等于0,显然当x不等于y时, 需要k = y*a/(y-x) 。且需要 k >=-b && k <=a .
思路一代码:
#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <deque>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 1e6+10 ;
LL gcd( LL a , LL b){
if( b==0) return a;
return gcd( b, a% b) ;
}
bool check( LL a,LL b , LL x , LL y ){
if( !a || !b) return false;
if( x > y ) swap( x , y ) ;
LL lcm = x* y / gcd( x , y ) ;
x = lcm/x , y = lcm/y;
//cout<<x <<" " << y <<endl;
while( a>=0 && b>=0 && a>=b){
if( a ==b) return true;
a-=x , b-=y;
}
return false;
}
int main()
{
int T ;T =1;
while(T--){
int n ;string s;
cin >> n >> s;
LL a = 0, b= 0 ;
for(int i = 0; i < s.size() ; ++ i )
if( s[i] =='+') a++;
else b++ ;
if( a <b) swap( a,b) ;
int q; cin >> q;
while(q--){
LL x,y;
cin >>x >> y ;
if( check( a,b,x,y)) cout<<"YES"<<endl;
else cout<<"NO" <<endl;
}
}
return 0 ;
}
思路二代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
main(){
ios::sync_with_stdio(false);
int x; string s; cin>>x>>s;
int n=count(s.begin(),s.end(),'+'),m=x-n;
int t; cin>>t;
while(t--){
int a,b; cin>>a>>b;
if(a==b)cout<<(n==m?"YES\n":"NO\n");
else cout<<((m-n)*b%(a-b)||(m-n)*b/(a-b)<-m||(m-n)*b/(a-b)>n?"NO\n":"YES\n");
}
return 0;
}