题目:计算以-2为基数的数的表示。

分析:数论。写出不同位数能表示的数字区间就能够找到规律。

长度为1:[1,1]; 长度为2:[-2,-1]; 长度为3:[2,5];

观察发现,区间长度增长为1,2,4,8,..,2^k,而且奇偶间隔开;

这样能够按顺序找到相应的1的位置,每次减去相应的基底(-2^k)寻找下一个1的位置就可以。

说明:又是数论。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio> using namespace std; long long l[40],r[40],b[40];
int ans[40]; int main()
{
l[0] = 1; r[0] = 1; b[0] = 1;
l[1] = -2;r[1] = -1;b[1] = -2;
long long base = 4;
for (int i = 2 ; i < 33 ; ++ i) {
if (i%2) {
r[i] = l[i-2]-1;
l[i] = l[i-2]-base;
}else {
l[i] = r[i-2]+1;
r[i] = r[i-2]+base;
}
base <<= 1;
b[i] = b[i-1]*-2;
} int n,m,s,e;
while (~scanf("%d",&n))
for (int i = 1 ; i <= n ; ++ i) {
scanf("%d",&m);
printf("Case #%d: ",i); for (int i = 0 ; i < 33 ; ++ i)
ans[i] = 0;
s = 32;
while (s >= 0) {
if (m >= l[s] && m <= r[s]) {
ans[s] = 1;
m -= b[s];
}
s --;
} e = 32;
while (e > 0 && !ans[e]) e --;
while (e >= 0) printf("%d",ans[e --]);
printf("\n");
}
return 0;
}

UVa 11121 - Base -2的更多相关文章

  1. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  2. UVa 10473 - Simple Base Conversion

    题目大意:十进制与十六进制之间的相互转换. #include <cstdio> int main() { #ifdef LOCAL freopen("in", &quo ...

  3. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  4. UVA 437 十九 The Tower of Babylon

    The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  5. UVA 1456 六 Cellular Network

    Cellular Network Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  6. Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)

     Count the Trees  Another common social inability is known as ACM (Abnormally Compulsive Meditation) ...

  7. UVa 10969 (圆与圆之间的覆盖问题) Sweet Dream

    题意: 有n个按先后顺序放置的不同大小不同位置的圆,求所有可见圆弧的长度. 分析: 这道题应该是大白书上例题 LA 2572 (求可见圆盘的数量) Kanazawa 的加强版,整体框架都差不多. 对于 ...

  8. POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】

    典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...

  9. UVA 11774 - Doom&#39;s Day(规律)

    UVA 11774 - Doom's Day 题目链接 题意:给定一个3^n*3^m的矩阵,要求每次按行优先取出,按列优先放回,问几次能回复原状 思路:没想到怎么推理,找规律答案是(n + m) / ...

随机推荐

  1. webBrower控件实现winform和webpage交互

    添加WebBrowser控件 private WebBrowser webBrowser1; 引用页面的document对象 HtmlDocument doc = webBrowser1.Docume ...

  2. uav 11258 String Partition (DP)

    Problem F - String Partition                                                                         ...

  3. the apple tree

    the apple tree A long time ago, there was a huge apple tree. A little boy loved to come and lay arou ...

  4. 举例说,Linux核心名单(两)

    使用列表 我认为最好的方式,成为熟悉的核心列表功能是看一些简单的例子,素材去更好的理解链表. 以下是一个样例.包括创建.加入.删除和遍历链表. <span style="font-si ...

  5. SplashScreenDemo

    对Java应用最常见的抱怨就是启动时间太长.这是因为Java虚拟机花费一段时间去加载所有必需的类,特别是对Swing应用,它们需要从Swing和AWT类库代码中去抽取大量的内容. 用户并不喜欢应用程序 ...

  6. Java 启动线程的方式

    面试题:JAVA启动线程的方式有哪些? 1.继承Thread [java] view plaincopy public class java_thread extends Thread{ public ...

  7. uva10954 - Add All(multiset功能)

    题目:10954 - Add All 题目大意:求n个数的和,可是有点不一样的是题目要求计算最少花费.每次两个数相加,得到的那个数就是每次计算的cost. 解题思路:之前没有想到用multiset,自 ...

  8. nodejs的安装和使用

    一 下载 下载地址: https://nodejs.org/download/ 二 安装 1 win7系统直接双击,就能够执行了: 2 win8须要使用管理员权限执行,否则会报错Error 2502, ...

  9. Catalan数总结

    财产: 前20条目:1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, ...

  10. 【git学习五】git基础之git分支

    1.背景                最早用github的时候,我傻傻的问舍友大神,git里面的branch是干什么的,他用了非常直白的解释,我至今还记得."branch就是你能够自己建立 ...