#include <bits/stdc++.h>
#include <iostream>
#define X 10005
#define inf 0x3f3f3f3f
#define PI 3.141592653589793238462643383
#define IO ios::sync_with_stdio(false),cin.tie(0), cout.tie(0);
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e6+10;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
ll L, v, l, r;
cin>>L>>v>>l>>r;
ll ans = (l - 1) / v + L / v - r / v;
printf("%lld\n", ans);
}
}
B Heaters
给出每个位置是否有热水器,然后给出热水器的作用距离(每个作用距离都一样)现在每个热水器都处于关闭状态,问最少要开几个热水器使得所有的位置都可以被覆盖
#include <bits/stdc++.h>
#include <iostream>
#define X 10005
#define inf 0x3f3f3f3f
#define PI 3.141592653589793238462643383
#define IO ios::sync_with_stdio(false),cin.tie(0), cout.tie(0);
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=1e6+10;
int a[maxn];
int n,r,cnt=0,l= 0,i=1,pos,ans;
int tag=0;
int main()
{
cin>>n>>r;
for (int i = 1; i <= n;i++) cin>>a[i];
for(int i=1;i<=n;)
{
pos=i+r-1,ans=0;
while (pos>l)
{
if (a[pos])
{
ans=pos;
break;
}
else --pos;
}
if (ans==0)
{
cout<<-1<<endl;
tag=1;
break;
}
else
{
l=ans;
i=ans+r;
cnt++;
}
}
if(!tag)
cout<<cnt<<endl;
return 0;
}
C Books Queries
给q次操作,可以往书架最右面或者往书架最左面放书或者查询一本书离边缘的最短距离
用两个数组记录一本书是第几本放在左面或者右面的书,这样就可以保证如果这本书当时是通过放在左面进入书架,
则距离是min(L+b[id]-1,R-b[id]) 如果是通过右面则是min(R+a[id]-1,L-a[id])
谢大佬@@
#include <bits/stdc++.h>
#include <iostream>
#define X 10005
#define inf 0x3f3f3f3f
#define PI 3.141592653589793238462643383
#define IO ios::sync_with_stdio(false),cin.tie(0), cout.tie(0);
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e6+10;
int main()
{
int n,id;
char str;
while(cin>>n)
{
int _l=1,_r=0,l=0,r=1;
for(int i=0;i<n;++i)
{
cin>>str>>id;
if(str=='L') a[id]=++l,b[id]=--_l;//a[id]左面所在的位置 b[id]第几本从左面放的书
if(str=='R') a[id]=--r,b[id]=++_r;//a[id]右面的位置 b[id]第几本从右面放的书
if(str=='?') cout<<min(l-a[id],_r-b[id])<<endl;
}
}
}
E Binary Numbers AND Sum
题意:给出两个01串ab 当b最后一位为1的时候就a&b的结果进行计入,为0不做处理,然后b右移一位,最后统计累加计入的和
统计b中1的个数 从a的低位开始,当为1时,快速幂进行计算 累加?
#include <bits/stdc++.h>
#include <iostream>
#define X 10005
#define inf 0x3f3f3f3f
#define PI 3.141592653589793238462643383
#define IO ios::sync_with_stdio(false),cin.tie(0), cout.tie(0);
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e6+10;
char a[maxn],b[maxn];
ll Pow(ll x,ll n)
{
ll ans=1;
while(n)
{
if(n&1)
ans=ans*x%mod;
n>>=1;
x=x*x%mod;
}
return ans;
}
int main()
{
int n,m,cnt=0;
cin>>n>>m;
for(int i=n-1;i>=0;--i)cin>>a[i];
for(int i=m-1;i>=0;--i){cin>>b[i];if(b[i]=='1')cnt++;}
ll ans=0;
for(int i=0,j=0;i<n&&cnt;++i,++j)
{
if(a[i]=='1')
ans=(ans+Pow(2,i)*ll(cnt)%mod)%mod;
if(b[i]=='1')cnt--;
}
cout<<ans<<endl;
return 0;
}