牛客算法周周练14 BCircle D 绝地求生(gcd,思维) E
B
知识点:
任意相邻的两个数互质
代码:
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
cout<<n<<endl;
return 0;
}
D
思路:
求最小公倍数,x,y的最小公倍数=x*y/Gcd(x,y).
这里直接算会爆炸,所以变一下形,x/Gcd(x,y)*y
可我比赛用的__int128…,赛后看大佬的代码才知道
大佬AC代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+10;
const int mod=1e9+7;
ll gcd(ll a,ll b)
{
if(b==0) return a;
else return gcd(b,a%b);
}
ll Lcm(ll a,ll b)
{
return a/gcd(a,b)*b;
}
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
ll x,y;
cin>>x>>y;
cout<<"Case "<<i<<": "<<Lcm(x,y)<<'\n';
}
}
我的AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
__int128 Gcd(__int128 a,__int128 b)
{
if(b==0) return a;
else return Gcd(b,a%b);
}
inline __int128 read(){
__int128 x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void print(__int128 x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9)
print(x/10);
putchar(x%10+'0');
}
int main()
{
int t;
cin>>t;
int n = 0;
while(t--)
{
n++;
__int128 x,y;
// scanf("%llu %llu",&x,&y);
x=read();
y=read();
__int128 g = Gcd(x,y);
__int128 ans = x*y/g;
printf("Case %d: ",n);
print(ans);
printf("\n");
}
return 0;
}
E
思路:
暴力
枚举前缀和后缀
如果前后缀相等,就去字符串里(除开前后缀的位置)找还有没有和前后缀相等的字串
如果某一次找不到了,直接break
好像有bug,本蒟弱只能写成这样,望大佬指教
代码:
#include <bits/stdc++.h>
using namespace std;
//判断b的转置是否和a相等
int check(string a,string b)
{
int n1 = a.size();
int n2 = b.size();
if(n1!=n2) return 0;
for(int i=0;i<n1;i++)
{
if(a[i]!=b[n1-i-1]) return 0;
}
return 1;
}
int main()
{
std::ios::sync_with_stdio(false);
string s;
cin>>s;
int n = s.size();
string x = "";
string ans = "";
string x2 = "";
for(int i=0;i<n;i++)
{
x+=s[i];
x2+=s[n-i-1];
//如果相等就去找
if(check(x,x2))
{
//cout<<"x="<<x<<endl;
string y="";
int flag = 0;
for(int j=i+1;j<n-(int)x.size()+1;j++)
{
if(s[j]==x[0])
{
int t = 0;
//注意范围
while(j+t<n-(int)x.size()+1)
{
y+=s[j+t];
if(y==x)
{
ans = x;
flag = 1;
break;
}
else if(((int)y.size())>=((int)x.size()))
{
y = "";
break;
}
t++;
}
}
if(flag) break;
}
//这个剪枝很重要,不写会超时
if(!flag)
{
break;
}
}
}
cout<<ans;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话