题意:较大的容量减较小的容量,较小的容量翻倍。问操作几回其中一个空。

开始用set判重,重复就不可行。不过状态最多有2e18种。不仅爆内存,还超时。然后找规律。发现只有比例为1:1,1:3,1:7,3:5,1:15,3:13,5:11,7:9......这样才行。也就是化简以后相加是2^k。

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <list>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
const int SZ=1e2+,INF=0x7FFFFFFF;
const long long mod=;
typedef long long lon; int gcd(int x,int y)
{
if(x==)return y;
if(y==)return x;
if(x<y)swap(x,y);
int res=;
for(;;)
{
int rem=x%y;
if(rem==)return y;
x=y;
y=rem;
}
} int cnt(int x)
{
int res=;
for(;x;x-=x&-x,++res);
return res;
} int chk(int x)
{
int num=cnt(x);
if(num!=)return -;
else
{
for(int i=;i<;++i)
{
if((<<i)&x)return i;
}
}
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
int casenum;
//cin>>casenum;
//scanf("%d",&casenum);
//for(int time=1;time<=casenum;++time)
//for(int time=1;cin>>n;++time)
{
int n,m;
cin>>n>>m;
int d=gcd(n,m);
//cout<<d<<endl;
n/=d,m/=d;
if(n==||m==)cout<<<<endl;
else
{
cout<<chk(n+m)<<endl;
}
}
return ;
}

超内存的:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <list>
using namespace std;
const double EPS=1e-;
const int SZ=1e2+,INF=0x7FFFFFFF;
const long long mod=;
typedef long long lon;
int n,sum[],arr[]; bool chk(int x,int t)
{
int res=;
if(x/n&&arr[x]<arr[x-n])++res;
if(x%n&&arr[x]<arr[x-])++res;
if(x/n!=n-)
{
if(t)
{
if(arr[x]<arr[x+n])++res;
}
else ++res;
}
if(x%n!=n-)
{
if(t)
{
if(arr[x]<arr[x+])++res;
}
else ++res;
}
return t?res==sum[x]:res>=sum[x];
} bool dfs(int x)
{
//cout<<x<<endl;
if(x==n*n)
{
// for(int i=0;i<n*n;++i)
// {
// cout<<arr[i]<<" ";
// }cout<<endl;
return ;
}
for(int i=;i<;++i)
{
arr[x]=i;
if(x%n&&chk(x-,||((x-)/n==n-))==)continue;
if(x/n&&chk(x-n,)==)continue;
if(x==n*n-&&chk(x,)==)continue;
//cout<<x<<" "<<i<<endl;
if(dfs(x+))return ;
//else cout<<x<<" "<<i<<" fail"<<endl;
}
arr[x]=;
return ;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
int casenum;
//cin>>casenum;
//scanf("%d",&casenum);
//for(int time=1;time<=casenum;++time)
//for(int time=1;cin>>n;++time)
{
cin>>n;
for(int i=;i<n*n;++i)
{
cin>>sum[i];
}
if(n==)
{
if(sum[]==)cout<<<<endl;
else cout<<"NO SOLUTION"<<endl;
}
else if(dfs())
{
for(int i=;i<n*n;++i)
{
if(i%n)cout<<" ";
cout<<arr[i];
if(i%n==n-)cout<<endl;
}
}
else
{
cout<<"NO SOLUTION"<<endl;
}
}
return ;
}

超时的:

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <list>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
const int SZ=1e2+,INF=0x7FFFFFFF;
const long long mod=;
typedef long long lon; int work(int x,int y,int x1,int y1,int s)
{
if(x<y)swap(x,y);
if(x1<y1)swap(x1,y1);
//cout<<x<<" "<<y<<" "<<x1<<" "<<y1<<endl;
if(s!=&&x==x1&&y==y1)return -;
x-=y;
y*=;
x1-=y1;
y1*=;
if(x<y)swap(x,y);
if(x1<y1)swap(x1,y1);
//if(s!=1&&x==x1&&y==y1)return -1;
x1-=y1;
y1*=;
if(x==||y==)return s;
return work(x,y,x1,y1,s+);
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
int casenum;
//cin>>casenum;
//scanf("%d",&casenum);
//for(int time=1;time<=casenum;++time)
//for(int time=1;cin>>n;++time)
{
int n,m;
cin>>n>>m;
if(n==||m==)cout<<<<endl;
else if((n&)^(m&))cout<<-<<endl;
else cout<<work(n,m,n,m,)<<endl;
}
return ;
}

sgu 126 Boxes的更多相关文章

  1. 找规律 SGU 126 Boxes

    题目地址:http://acm.sgu.ru/problem.php?contest=0&problem=126 /* 找规律,智商不够,看了题解 详细解释:http://blog.csdn. ...

  2. SGU 126 Boxes(模拟题|二进制)

    Translate:Sgu/126 126. 盒子 time limit per test: 0.5 sec. memory limit per test: 4096 KB 有两个盒子. 第一个盒子里 ...

  3. SGU 126. Boxes --- 模拟

    <传送门> 126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two b ...

  4. Boxes - SGU 126(找规律)

    题目大意:有两个箱子,一个箱子装了A个球,一个箱子装了B个球,可以从球多的那个箱子拿出来球少的箱子里面球的总数放在少的那个箱子里面,问能否把球全部放在一个箱子里面? 分析:很容易求出来最后放的拿一下一 ...

  5. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  6. 快速切题sgu126. Boxes

    126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two boxes. There ...

  7. 今日SGU 5.4

    SGU 127 题意:给你n个数字,和m,k,问你有多少个数字的m次幂可以被k整除 收获:快速幂 #include<bits/stdc++.h> #define de(x) cout< ...

  8. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  9. SGU Volume 1

    SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...

随机推荐

  1. Asp.net MVC 通过自定义ControllerFactory实现构造器注入

    一.重写ControllerFactory的GetControllerInstance ControllerFactory是asp.net中用于在运行时构造Controller的工厂 ,默认使用的工厂 ...

  2. mysql count group by统计条数方法

    mysql count group by统计条数方法 mysql 分组之后如何统计记录条数? gourp by 之后的 count,把group by查询结果当成一个表再count一次select c ...

  3. mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题

    提示意思是不能用grant创建用户,mysql8.0以前的版本可以使用grant在授权的时候隐式的创建用户,8.0以后已经不支持,所以必须先创建用户,然后再授权,命令如下: mysql> CRE ...

  4. 计算概论(A)/基础编程练习2(8题)/4:骑车与走路

    #include<stdio.h> int main() { // 待处理的数据数量n ; scanf("%d", &n); float meters[n]; ...

  5. Kafka学习之(五)搭建kafka集群之Zookeeper集群搭建

    Zookeeper是一种在分布式系统中被广泛用来作为:分布式状态管理.分布式协调管理.分布式配置管理.和分布式锁服务的集群.kafka增加和减少服务器都会在Zookeeper节点上触发相应的事件kaf ...

  6. Mysql错误:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

    昨晚添加完索引之后, 查询整表的时候抛出Lock wait timeout exceeded; try restarting transaction, 吓死小白的我, 为什么条件查询可以, 整表查不了 ...

  7. 20145301赵嘉鑫《网络对抗》Exp8 Web基础

    20145301赵嘉鑫<网络对抗>Exp8 Web基础 基础问题回答 什么是表单? 表单是一个包含表单元素的区域,主要负责数据采集部分.表单元素允许用户在表单中输入信息.一个表单有三个基本 ...

  8. STM32唯一的ID

    请看如下程序: /*------------------------------------------------------------------------------------------ ...

  9. openwrt如何单独编译uboot

    答:make package/boot/uboot-<chip series>/compile

  10. POJ 1845 Sumdiv(求因数和 + 逆元)题解

    题意:给你a,b,要求给出a^b的因子和取模9901的结果. 思路:求因子和的方法:任意A = p1^a1 * p2^a2 ....pn^an,则因子和为sum =(1 + p1 + p1^2 + . ...