传送门

一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”。今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且用的原题。


Description

time limit 5s

In binary, the square root of 2, denoted by sqrt(2), is an infinite number 1.0110101000001001111... Given an integer n and a binary string (i.e. a string consisting of 0 and 1) S, your task is to find the first occurrence of S in the fraction part (i.e. the part after the decimal point) of sqrt(n). In case sqrt(n) is an integer, the fraction part is an infinite sequence of zeros.

Input

The first line contains T (T ≤ 100), the number of test cases. Each of the following lines contains an integer n (2 ≤ n ≤ 1, 000, 000) and a binary string S with at most 20 characters.

Output

For each case, print the position of the first character in the first occurrence of S. The first digit after the dot is at position 0. The answer is guaranteed to be no greater than 100.

Sample Input

2

2 101

1202 110011

Sample Output

2

58

Solution

高精度

复杂度 $O(n^3)$

#include <bits/stdc++.h>
using namespace std;
const int N(), M();
int get_len(int n){
if(!n) return ;
int res=;
for(;n;res++,n>>=);
return res;
}
void trans(int n, int *bit, int &len){
int l=get_len(n);
for(int i=len+l-; i>=len; i--){
bit[i]=n&, n>>=;
}
len+=l;
}
int tmp[N], a[N], b[N], len, _len, LEN;
void sqr(){ //error-prone
memset(tmp, , sizeof(tmp));
for(int i=; i<len; i++) for(int j=; j<len; j++) tmp[i+j]+=a[i]*a[j];
for(int i=*len-; i; i--){
tmp[i-]+=tmp[i]/;
tmp[i]%=;
}
trans(tmp[], b, LEN=);
for(int i=; i<=*len-; i++) b[LEN++]=tmp[i];
}
int get_int(){
int res=, l=LEN-*_len;
for(int i=l-, _pow=; i>=; res+=b[i]*_pow, i--, _pow<<=); //error-prone
return res;
}
int solve(){
int n; string s, t; cin>>n>>t;
int m=sqrt(n);
if(m*m==n) return ;
len=_len=;
trans(m, a, len);
int tmp=len;
while(_len<M){
a[len++]=, _len++;
sqr();
if(get_int()>=n) a[len-]=;
}
for(int i=tmp; i<len; i++) s+='0'+a[i];
return s.find(t);
}
int main(){
//freopen("in", "r", stdin);
int T;
for(cin>>T; T--; cout<<solve()<<endl);
}

有意写得短一些,但还是有50行,太长了都不想看~

总结

总结一下高精度度乘法的写法,虽说也没什么可总结的。

无论二进制还是十进制,写法都一样,以下代码结合题目按二进制写。

1. 大整数的存法:int[]数组,左边高位(左高右低即书写格式,这样处理有一定优越性(真的有优越性吗?)),0-indexed

2. 将长为n的大整数的a[]和长为m的大整数的b[]相乘

先按如下方式将长为n+m-1中间结果存在数组tmp[]

memset(tmp, , sizeof(tmp));
for(int i=; i<n; i++)
for(int j=; j<m; j++)
tmp[i+j]+=a[i]*b[j];

再处理出最终结果存到数组c[]

for(int k=i+j-; k; k--)
tmp[k-]+=tmp[k]/, tmp[k]%=; int get_len(int n){
if(!n) return ;
int res=;
for(; n; res++, n>>=);
return res;
} len=get_len(tmp[]);
for(int i=len-; i>=; i--)
c[i]=tmp[]%, tmp[]/=; for(int k=; k<i+j-; k++)
c[len++]=tmp[k];

UVa 12505 Searching in sqrt(n)的更多相关文章

  1. 湖南省第八届大学生程序设计大赛原题 D - 平方根大搜索 UVA 12505 - Searching in sqrt(n)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30746#problem/D D - 平方根大搜索 UVA12505 - Searchin ...

  2. STL --- UVA 123 Searching Quickly

    UVA - 123 Searching Quickly Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...

  3. uva 123 Searching Quickly

     Searching Quickly  Background Searching and sorting are part of the theory and practice of computer ...

  4. uva 1597 Searching the Web

    The word "search engine" may not be strange to you. Generally speaking, a search engine se ...

  5. Lucas定理初探

    1.1 问题引入 已知\(p\)是一质数,求\(\dbinom{n}{m}\pmod{p}\). 关于组合数,它和排列数都是组合数学中的重要概念.这里会张贴有关这两个数的部分内容. 由于Lucas定理 ...

  6. 基于STL的字典生成模块-模拟搜索引擎算法的尝试

    该课题来源于UVA中Searching the Web的题目:https://vjudge.net/problem/UVA-1597 按照题目的说法,我对按照特定格式输入的文章中的词语合成字典,以满足 ...

  7. Volume 1. Sorting/Searching(uva)

    340 - Master-Mind Hints /*读了老半天才把题读懂,读懂了题输出格式没注意,结果re了两次. 题意:先给一串数字S,然后每次给出对应相同数目的的一串数字Si,然后优先统计Si和S ...

  8. Searching the Web UVA - 1597

      The word "search engine" may not be strange to you. Generally speaking, a search engine ...

  9. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 3(Sorting/Searching)

    第一题:340 - Master-Mind Hints UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...

随机推荐

  1. Android Studio中提示:Project SDK is not defined

    Android Studio中提示:Project SDK is not defined 2015 年 4 月 1 日 下午 9:04crifan已有2209人围观我来说几句 [背景] 之前用Andr ...

  2. Xcode7创建的项目添加启动图有问题?

    在Xcode7下创建的项目,由于某个原因,Xcode7添加启动图有点不一样.Xcode7与Xcode6不一样的地方在于:Xcode6的LaunchScreen.xib改成了LaunchScreen.s ...

  3. How to get date from OAMessageDateFieldBean

    OAMessageDateFieldBean dateFromBean = (OAMessageDateFieldBean)webBean.findChildRecursive("pRece ...

  4. jQuery上传插件Uploadify使用帮助

    Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.它的功能特色总结如下: 支持单文件或多文件上传,可控制并发上传的文件数 在服务器端支持各种语言与之配合使用,诸如PHP, ...

  5. 20135316王剑桥 linux第十二周课实验笔记

    第十二章并发编程 1.如果逻辑控制流在时间上重叠,那么它们就是并发的.这种现象,称为并发(concurrency). 2.为了允许服务器同时为大量客户端服务,比较好的方法是:创建并发服务器,为每个客户 ...

  6. Javascript的字面量对象以及如何拆解字面量对象

    简单的说,字面量对象提供了一直非常方便构建新对象的方式,它的格式非常简单且容易阅读.是大多数前端程序员在构建对象时比较推崇的一种方式. 格式: var person = { name : " ...

  7. 关于那些难改的bug

    多年的测试经验中,经常发现有这么一种现象:总有些提了的bug不能顺利的被修复.这些bug往往有4个走向: 1.在被发现的版本中最终被解决,但中途花费较多周折. 2.有计划的在后续的版本中被解决. 3. ...

  8. 傻傻分不清楚的php脚本路径

     闲话就不说了,还是直接提出今天的问题,准确的说,对多个相似的 有关当前脚本信息的全局变量常量的区分. 先贴上代码: <?php echo $_SERVER['PHP_SELF']; echo ...

  9. Linq动态查询简易解决之道(原创)

    因为项目需要使用Linq来查询数据,但是在多条件查询时,需要使用一大堆if(...!=string.empty)等判断条件感觉不是很优雅.网上搜索以下,大概找到了两种办法,一种是老外写的一个类,感觉用 ...

  10. 使用Nginx解决IIS绑定域名导致应用程序重启的问题

    在将多个站点迁移到一个站点(Tenant Feature)的时候碰到了一个棘手的问题,用户需要绑定自定义域名,但IIS绑定域名的时候会导致这个站点重启,那么只要一个用户绑定了一个域名则会导致这个应用上 ...