a+b问题恶搞——【S神】苏嘉亿
这道题大家应该都不陌生了吧,真的是从小做到大
【a+b问题恶搞——【S神】苏嘉亿】题目描述:给你两个数,输入后,输出求和结果;easy水水水水题中的水水水水题;
为了不让新手们蒙圈,我决定还是先铺一个最简单的c++代码吧:
#includeint main(){
int a,b;
scanf("%d %d",&a,&b);
printf("%d",a+b);
return 0;
}
题解(难度恶搞):
算法一: 广度优先搜索(宽度优先搜索)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int oo_min=0xcfcfcfcf,oo_max=0x3f3f3f3f;
int n,m,t;
vector f;
vector e;
vector u;
vector pre;
vector vis;
vector > c;
vector > p;
vector > ce;
vector > cw;
deque q;
void add_edge_1(int x,int y,int c_v,int p_v){
cw[x].push_back(y);
c[x].push_back(c_v);
p[x].push_back(p_v);
ce[y].push_back(cw[x].size()-1);
cw[y].push_back(x);
c[y].push_back(0);
p[y].push_back(-p_v);
ce[x].push_back(cw[y].size()-1);
}int bfs_1(int s,int t,int *flow,int *cost){
f.resize(0);
f.resize(cw.size(),0);
f[s]=oo_max;
e.resize(0);
e.resize(cw.size(),-1);
u.resize(0);
u.resize(cw.size(),oo_max);
u[s]=0;
pre.resize(0);
pre.resize(cw.size(),-1);
pre[s]=s;
vis.resize(0);
vis.resize(cw.size(),0);
for (q.resize(0),vis[s]=1,q.push_back(s);
(!q.empty());
vis[q.front()]=0,q.pop_front())
{
int now=q.front();
for (int i=0;
i
算法二:深度优先搜索
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
int a[maxn],n=2,res;
int dfs(int sum,int ii){
if(ii>n)return -1;
if(res==sum)return sum;
int ans=dfs(sum+a[ii+1],ii+1);
return ans==-1?res:ans;
}int main(){
for(int i=1;
i<=n;
i++){
cin>>a[i];
res+=a[i];
}
cout<
算法三:Dijkstra求最短路
#include
using namespace std;
const int N=405;
struct Edge {
int v,w;
};
vector edge[N*N];
int n;
int dis[N*N];
bool vis[N*N];
struct cmp {
bool operator()(int a,int b) {
return dis[a]>dis[b];
}
};
int Dijkstra(int start,int end)
{
priority_queue,cmp> dijQue;
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
dijQue.push(start);
dis[start]=0;
while(!dijQue.empty()) {
int u=dijQue.top();
dijQue.pop();
vis[u]=0;
if(u==end)
break;
for(int i=0;
idis[u]+edge[u][i].w) {
dis[v]=dis[u]+edge[u][i].w;
if(!vis[v]) {
vis[v]=true;
dijQue.push(v);
}
}
}
}
return dis[end];
}
int main()
{
int a,b;
scanf("%d%d",&a,&b);
Edge Qpush;
Qpush.v=1;
Qpush.w=a;
edge[0].push_back(Qpush);
Qpush.v=2;
Qpush.w=b;
edge[1].push_back(Qpush);
printf("%d",Dijkstra(0,2));
return 0;
}
算法四:Floyd算法
#include
using namespace std;
long long n=3,a,b,dis[4][4];
int main()
{
cin>>a>>b;
for(int i=1;
i<=n;
i++)
for(int j=1;
j<=n;
j++)
dis[i][j]=2147483647;
dis[1][2]=a,dis[2][3]=b;
for(int k=1;
k<=n;
k++)
for(int i=1;
i<=n;
i++)
for(int j=1;
j<=n;
j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
cout<
算法五:LCT算法
#include
using namespace std;
struct node
{
int data,rev,sum;
node *son[2],*pre;
bool judge();
bool isroot();
void pushdown();
void update();
void setson(node *child,int lr);
}lct[233];
int top,a,b;
node *getnew(int x)
{
node *now=lct+ ++top;
now->data=https://www.it610.com/article/x;
now->pre=now->son[1]=now->son[0]=lct;
now->sum=0;
now->rev=0;
return now;
}
bool node::judge()
{
return pre->son[1]==this;
}
bool node::isroot()
{
if(pre==lct)return true;
return !(pre->son[1]==this||pre->son[0]==this);
}
void node::pushdown()
{
if(this==lct||!rev)return;
swap(son[0],son[1]);
son[0]->rev^=1;
son[1]->rev^=1;
rev=0;
}
void node::update()
{
sum=son[1]->sum+son[0]->sum+data;
}
void node::setson(node *child,int lr)
{
this->pushdown();
child->pre=this;
son[lr]=child;
this->update();
}
void rotate(node *now)
{
node *father=now->pre,*grandfa=father->pre;
if(!father->isroot()) grandfa->pushdown();
father->pushdown();
now->pushdown();
int lr=now->judge();
father->setson(now->son[lr^1],lr);
if(father->isroot()) now->pre=grandfa;
else grandfa->setson(now,father->judge());
now->setson(father,lr^1);
father->update();
now->update();
if(grandfa!=lct) grandfa->update();
}
void splay(node *now)
{
if(now->isroot())return;
for(;
!now->isroot();
rotate(now))
if(!now->pre->isroot())
now->judge()==now->pre->judge()?rotate(now->pre):rotate(now);
}
node *access(node *now)
{
node *last=lct;
for(;
now!=lct;
last=now,now=now->pre) {
splay(now);
now->setson(last,1);
}
return last;
}
void changeroot(node *now)
{
access(now)->rev^=1;
splay(now);
}
void connect(node *x,node *y)
{
changeroot(x);
x->pre=y;
access(x);
}
void cut(node *x,node *y)
{
changeroot(x);
access(y);
splay(x);
x->pushdown();
x->son[1]=y->pre=lct;
x->update();
}
int query(node *x,node *y)
{
changeroot(x);
node *now=access(y);
return now->sum;
}
int main()
{
scanf("%d%d",&a,&b);
node *A=getnew(a);
node *B=getnew(b);
connect(A,B);
cut(A,B);
connect(A,B);
printf("%d",query(A,B));
return 0;
}
算法六:main自递归和位运算
#include
int main(int a,int b,int k)
{
if(k)scanf("%d%d",&a,&b);
printf("%d",b==0?a:main(a^b,(a&b)<<1,0));
exit(0);
}
算法七:SPFA算法
#include
using namespace std;
int n,m,a,b,op,l,r,u,v1,e;
int head[200009],ne_xt[200009],dis[200009];
int len[200009],v[200009],team[200009],pd[100009];
int lt(int x,int y,int z)
{
op++;
v[op]=y;
ne_xt[op]=head[x];
head[x]=op;
len[op]=z;
}
int SPFA(int s,int f)
{
for(int i=1;
i<=200009;
i++){dis[i]=999999999;
}
l=0,r=1,team[1]=s,pd[s]=1,dis[s]=0;
while(l!=r){
l=(l+1)%90000,u=team[l],pd[u]=0,e=head[u];
while(e!=0){
v1=v[e];
if(dis[v1]>dis[u]+len[e]){
dis[v1]=dis[u]+len[e];
if(!pd[v1]){
r=(r+1)%90000,
team[r]=v1,
pd[v1]=1;
}
}
e=ne_xt[e];
}
}
return dis[f];
}
int main(){
scanf("%d%d",&a,&b);
lt(1,2,a);
lt(2,3,b);
printf("%d",SPFA(1,3));
return 0;
}
算法八:递归算法
#include
using namespace std;
long long a,b,c;
long long dg(long long a)
{
if(a<=5)return a;
return (dg(a/2)+dg(a-a/2));
}
int main()
{
cin>>a>>b;
cout<
算法九:二分查找算法
#include
using namespace std;
long long a,b,c;
long long dg(long long a)
{
if(a<=5)return a;
return (dg(a/2)+dg(a-a/2));
}
int main()
{
cin>>a>>b;
cout<
算法十:压位高精度算法
#include
#define p 8
#define carry 100000000
using namespace std;
const int Maxn=50001;
char s1[Maxn],s2[Maxn];
int a[Maxn],b[Maxn],ans[Maxn];
int change(char s[],int n[])
{
char temp[Maxn];
int len=strlen(s+1),cur=0;
while(len/p){
strncpy(temp,s+len-p+1,p);
n[++cur]=atoi(temp);
len-=p;
}
if(len){
memset(temp,0,sizeof(temp));
strncpy(temp,s+1,len);
n[++cur]=atoi(temp);
}
return cur;
}
int add(int a[],int b[],int c[],int l1,int l2)
{
int x=0,l3=max(l1,l2);
for(int i=1;
i<=l3;
i++){
c[i]=a[i]+b[i]+x;
x=c[i]/carry;
c[i]%=carry;
}
while(x>0){c[++l3]=x%10;
x/=10;
}
return l3;
}
void print(int a[],int len)
{
printf("%d",a[len]);
for(int i=len-1;
i>=1;
i--)printf("%0*d",p,a[i]);
printf("\n");
}
int main()
{
scanf("%s%s",s1+1,s2+1);
int la=change(s1,a);
int lb=change(s2,b);
int len=add(a,b,ans,la,lb);
print(ans,len);
}
算法十一:网络流
#include
using namespace std;
#define set(x) Set(x)
#define REP(i,j,k) for (int i=(j),_end_=(k);
i<=_end_;
++i)
#define DREP(i,j,k) for (int i=(j),_start_=(k);
i>=_start_;
--i)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()-1))
#define ALL(x) ((x).begin()+1),(x).end()
template inline bool chkmin(T &a,const T &b){ return a > b ? a = b, 1 : 0;
}
template inline bool chkmax(T &a,const T &b){ return a < b ? a = b, 1 : 0;
}
typedef long long LL;
typedef pair node;
const int dmax=1010,oo=0x3f3f3f3f;
int n,m;
int a[dmax][dmax] , ans;
int d[dmax],e[dmax];
priority_queue q;
inline bool operator >(node a,node b){ return a.y>b.y;
}
bool p[dmax];
void Set(int x){ p[x]=1;
}
void unset(int x){ p[x]=0;
}
bool check(int x){ return x!=1 && x!=n && !p[x] && e[x]>0;
}
void preflow(){
e[1]=oo;
d[1]=n-1;
q.push(mp(1,n-1));
set(1);
while (!q.empty()){
bool flag=1;
int k=q.top().x;
q.pop(),unset(k);
DREP(i,n,1)
if ((d[k]==d[i]+1 || k==1) && a[k][i]>0){
flag=0;
int t=min(a[k][i],e[k]);
e[k]-=t;
a[k][i]-=t;
e[i]+=t;
a[i][k]+=t;
if (check(i)){
q.push(mp(i,d[i]));
set(i);
}
if (e[k]==0) break;
}
if (flag){
d[k]=oo;
REP(i,1,n)
if (a[k][i]>0)chkmin(d[k],d[i]+1);
}
if (check(k)){
q.push(mp(k,d[k]));
set(k);
}
}
ans=e[n];
}
int main(){
n = 2, m = 2;
int x, y;
scanf("%d%d", &x, &y);
a[1][2] += x + y;
preflow();
printf("%d\n",ans);
return 0;
}
算法十二:线段树算法
#include
using namespace std;
const int MAXN = 5000 + 10;
struct Tree
{
int l, r, val;
}t[MAXN * 4];
int N = 2, a[MAXN];
inline void init()
{
for(int i=1;
i<=N;
i++)
scanf("%d", &a[i]);
}
void Build(int Root, int L, int R)
{
t[Root].l = L;
t[Root].r = R;
t[Root].val = 0;
if(L == R) {
t[Root].val = a[L];
return ;
}
int m = (L + R) >> 1;
int Next = Root * 2;
Build(Next, L, m);
Build(Next+1, m+1, R);
t[Root].val = t[Next].val + t[Next+1].val;
}
void Updata(int Root, int pos, int _val)
{
if(t[Root].l == t[Root].r) {
t[Root].val += _val;
return ;
}
int Next = Root * 2;
int m = (t[Root].l + t[Root].r) >> 1;
if(pos <= m) Updata(Next, pos, _val);
else Updata(Next+1, pos, _val);
t[Root].val = t[Next].val + t[Next+1].val;
}
int Doit(int Root, int L, int R)
{
if(t[Root].l>=L && t[Root].r<=R)return t[Root].val;
int Ans = 0;
int Next = Root * 2;
int m=(t[Root].l + t[Root].r) >> 1;
if(L<=m)Ans+=Doit(Next,L,R);
if(R>m)Ans+=Doit(Next+1,L,R);
return Ans;
}
int main()
{
init();
Build(1,1,N);
int Ans=Doit(1,1,N);
printf("%d", Ans);
return 0;
}
算法十三:动态规划算法
#include
#include
using namesppace std;
int n=2,a[0x3f],f[0x3f];
int main(){
for(int i=1;
i<=n;
i++){
cin>>a[i];
f[i]=f[i-1]+a[i];
}
cout<
算法十四:最小生成树算法
#include
#include
#define INF 2140000000
using namespace std;
struct tree{int x,y,t;
}a[10];
bool cmp(const tree&a,const tree&b)
{
return a.t
算法十五:main自动计算
#include
#include
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<
算法十六:前缀和算法
#include
#include
using namesppace std;
int n=2,a[0x3f],f[0x3f];
int main(){
for(int i=1;
i<=n;
i++){
cin>>a[i];
f[i]=f[i-1]+a[i];
}
cout<
算法十七:后缀和算法
#include
#include
using namesppace std;
int n=2,a[0x3f],f[0x3f];
int main(){
for(int i=n;
i>=1;
i--){
cin>>a[i];
f[i]=f[i-1]+a[i];
}
cout<
本次恶搞到此结束!
不过这些题解都是正确的,可以提交!
谢谢!(勿喷)
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- jhipster|jhipster 升级无效问题
- “精神病患者”的角度问题
- 解决SpringBoot引用别的模块无法注入的问题
- Hive常见问题汇总
- 姚老师互动问答会|姚老师互动问答会 # 问题001(如何更有智慧的和身边人分享金刚智慧())
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- 【教育故事】|【教育故事】 一个“问题学生”的蜕变
- 蓝桥杯试题
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片