http://uoj.ac/problem/216

题意:给定A,B,C,如果集合中有数i,j(i>j),那么集合就会增加i-j这个数,问有没有在初始集合为{A,B}400步内生成C的方案。

思路:我们用辗转相除法得到gcd(A,B),然后我们用A去减这个GCD,减出"二进制"数,然后就可以组成C了。

由于是log级别的,因此不会有超过400的方案。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
#define ll long long
ll c[][],A,B,C,Gcd;
int tot;
ll read(){
ll t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
ll gcd(ll a,ll b){
if (b==) return a;
else return gcd(b,a%b);
}
bool superjudge(){
if (C>A) {puts("-1");return ;}
if (A==C||B==C){puts("");return ;}
ll Gcd=gcd(A,B);
if (C%Gcd!=){puts("-1");return ;}
return ;
}
void work(ll A,ll B,ll C){
ll t=(A-C)/B,i;
if (A==C) return;
c[++tot][]=A;c[tot][]=B;
for (i=;i*<=t;i*=){
c[++tot][]=A-i*B;c[tot][]=i*B;
c[++tot][]=A;c[tot][]=A-*i*B;
}
A-=B*i;
while (A>C){
if (A-i*B>=C){
c[++tot][]=A;
c[tot][]=i*B;
A-=i*B;
}
i/=;
}
}
void get_gcd(ll A,ll B){
if (A==Gcd||B==Gcd) return;
work(A,B,A%B);
get_gcd(B,A%B);
}
int main(){
A=read();B=read();C=read();
if (A<B)std::swap(A,B);
if (superjudge()) return ;
Gcd=gcd(A,B);
get_gcd(A,B);
work(A,Gcd,C);
printf("%d\n",tot);
for (int i=;i<=tot;i++)
for (i=;i<=tot;++i)
printf("%lld ",c[i][]),printf("%lld\n",c[i][]);
}

最新文章

  1. 扩展方法(C#)
  2. .NET 基础 一步步 一幕幕 [注释、命名规则、访问修饰符、数据类型、常量、变量]
  3. Elasticsearch 5.0 —— Head插件部署指南
  4. testng+reportng,运行xml
  5. Servlet API遍程常用接口和类
  6. android的m、mm、mmm编译命令
  7. hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1
  8. 9.30 noip模拟试题
  9. Django新手图文教程
  10. Asp.net文件缓存依赖
  11. 这些年,我收集的JavaScript代码(一)
  12. python基础 --02
  13. 洛谷 P2587 解题报告
  14. 安装window下的redis,redis可视化管理工具(Redis Desktop Manager)安装,基础使用,实例化项目
  15. Sprint会议计划
  16. Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)
  17. JavaScript资源收集分享,持续更新中。。。
  18. stdlib.h
  19. 操作系统-移动操作系统-百科: iOS(苹果公司的移动操作系统)
  20. PS改变图像颜色

热门文章

  1. Java里的IO流里的FileInputStream 的读取并在前打印行数!
  2. VS2012 无法启动IIS Express Web服务器的解决方案
  3. MYSQL 体系结构图-LRU FREELIST FLUSH LIST
  4. &lt;c:if&gt;判断参数是否同时为空
  5. My.Ioc 代码示例——Lifetime 和 ILifetimeScope
  6. 抓取锁的sql语句-第三次修改
  7. iOS9中将图片保存到照片中的某个相册的方法说明
  8. Xcode7 通过 Single View Application 得到一个 Empty Application 工程
  9. 卸载mysql时,如何卸载干净!
  10. Hive学习之二 《Hive的安装之自定义mysql数据库》