B 特殊数字—预处理+二分
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
const int N = 100010;
vector<int>nums;
signed main()
{
for(int i=0;i<=30;i++)
for(int j=i+1;j<=30;j++)
nums.push_back((1<<i)+(1<<j));
sort(nums.begin(),nums.end());
int T;cin>>T;
while(T--)
{
int x;cin>>x;
int res=1e9;
int pos=lower_bound(nums.begin(),nums.end(),x)-nums.begin();
if(nums[pos]==x)res=0;
else
{
res=nums[pos]-x;
if(pos>0)res=min(res,x-nums[pos-1]);
}
cout<<res<<'\n';
}
}
C 质数拼图游戏—BFS
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define int long long
int dx[4]={0,1};
int dy[4]={1,0};
int vis[21]={0,
0,1,1,0,1,0,1,0,0,0,
1,0,1,0,0,0,1,0,1,0,
};
queue<string>q;
map<string,int>mp;
void bfs(string s)
{
q.push(s);
mp[s]=0;
while(q.size())
{
string t=q.front();
q.pop();
for(int i=0;i<3;i++)for(int j=0;j<3;j++)
{
int x1=i*3+j;
for(int k=0;k<2;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(x>=3||y>=3)continue;
int x2=x*3+y;
if(vis[t[x1]-'0'+t[x2]-'0'])
{
string ss=t;
swap(ss[x1],ss[x2]);
if(!mp.count(ss))
{
mp[ss]=mp[t]+1;
q.push(ss);
}
}
}
}
}
}
signed main()
{
string s="123456789";
bfs(s);
int T;cin>>T;
while(T--)
{
string ss="";
for(int i=0;i<9;i++)
{
char op;cin>>op;
ss+=op;
}
if(mp.count(ss))cout<<mp[ss]<<'\n';
else cout<<-1<<'\n';
}
}
D 推箱子—差分
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
const int N=1000010;
int s[N]={0};
signed main()
{
int n,h;cin>>n>>h;
for(int i=1,l,r;i<=n;i++)
{
cin>>l>>r;
s[l]++;
s[r+1]--;
}
for(int i=1;i<=n;i++)s[i]+=s[i-1];
for(int i=1;i<=n;i++)s[i]+=s[i-1];
int res=1e18;
for(int i=1;i+h-1<=n;i++)
{
int t=s[i+h-1]-s[i-1];
res=min(res,n*h-t);
}
cout<<res<<'\n';
}
E 最小公倍数—数学推导
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
#define int long long
const int N=1000010 , INF=1e18;
map<int,pair<int,int>>mp;
void init()
{
for(int l=1;l<=2000000;l++)
{
int sum=l*(l+1);
for(int r=l+2;;r++)
{
int g=__gcd(sum,r);
if(sum/g>INF/r)break;
sum=sum/g*r;
if(!mp.count(sum))
{
mp[sum]={l,r};
}
}
}
}
void solve()
{
pair<int,int>res;
int n;cin>>n;
int t=(sqrt(4*n+1)-1)/2;
if(t*(t+1)==n)
{
res={t,t+1};
if(mp.count(n))
{
if(mp[n].first<t)
res=mp[n];
}
}
else if(mp.count(n))res=mp[n];
else
{
cout<<-1<<'\n';
return ;
}
cout<<res.first<<" "<<res.second<<'\n';
}
signed main()
{
init();
int T;cin>>T;
while(T--)solve();
}