hihoCoder #1106 : Koch Snowflake 微软苏州校招笔试(1月17日)
描述
Koch Snowflake is one of the most famous factal. It is built by starting with an equilateral triangle, removing the inner third of each side, building another equilateral triangle at the location where the side was removed, and then repeating the process indefinitely.
Let Kn be the Koch Snowflake after n-th iteration. It is obvious that the number of sides of Kn, Nn, is 3*4n. Let's number the sides clockwisely from the top of Koch Snowflake.
Let si,n be the i-th side of Kn. The generation of si,n is defined as the smallest m satifying si,n is a part of the sides of Km. For example, in the above picture, the yellow sides are of generation 0; the blue sides are of generation 1; the red sides are of generation 2.
Given a side si,n, your task is to calculate its generation.
输入
The input contains several test cases.
The first line contains T(T <= 1000), the number of the test cases.
The following T lines each contain two numbers, i(1 <= i <= 10^9) and n(0 <= n <= 1000). Your task is to calculate the generation of side si,n.
输出
For each test case output the generation of the side.
样例输入
5
1 0
1 1
2 1
10 2
16 3
样例输出
0
0
1
2
0
题目大意
给定一个三角形,每经过一次迭代,每一条边变成4条小一点的边。告诉你迭代的次数,以及边的编号,求该条边是第几次迭代出现的边。
解题思路
本题是一道数学题,用找规律的方法即可解决。
我们先来观察一下一条边在两次迭代之前的变化情况:
其中黄线为我们选中的一条边,我们并不知道它是第几次迭代产生的边。蓝线是第n次迭代后产生的新边,因此其代数为n。
假设迭代前的边为原图形中第i条边,那么迭代后对应的新图形中第4(i-1)+1 .. 4(i-1)+4这四条边。
在新的四条边中,我们可以知道4(i-1)+2和_4(i-1)+3_这两条边一定是当前迭代产生新边。同时我们可以根据其序号,算出原来的i。
由此我们可以得到结论,对于第n
次迭代后的第k
条边,若:
k mod 4 == 2或3
:则该边一定是第n次迭代产生的新边;k mod 4 == 0或1
:则该边不是第n次迭代产生的新边,且该边在第n-1次迭代中对应的是第ceil(k/4)
条边。(ceil
为上取整函数)
利用这个性质,对于给定的k
和n
,我们先判定第k
条边是否是第n
次迭代产生的新边。若是,则直接返回n
;否则我们根据第二条性质,计算出的新的k' = ceil(k/4)
,n' = n - 1
,再次代入进行计算。
我们可以写出这个递归的程序:
calc(k, n):
If (n == 0) Then
Return 0; // 若n = 0时,该边一定属于初始图形
End If
If (k mod 4 == 2 or k mod 4 == 3) Then
Return n;
End If
Return calc(ceil(k/4), n - 1);
得到了该函数,本题也就迎刃而解了。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
const int N=;
const int mod=1e9+;
int t,i,n;
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d %d",&i,&n);
while(n!=){
if(i%==||i%==) break;
i=i/+i%;
n--;
}
printf("%d\n",n);
}
return ;
}
hihoCoder #1106 : Koch Snowflake 微软苏州校招笔试(1月17日)的更多相关文章
- hihoCoder #1094 : Lost in the City(枚举,微软苏州校招笔试 12月27日 )
#1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He ...
- hihocoder #1094 : Lost in the City微软苏州校招笔试 12月27日 (建图不大【暴力枚举】 子图的4种形态 1Y )
#1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He ...
- hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)
#1103 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorit ...
- Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)
#1095 : HIHO Drinking Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playin ...
- SQL点滴6—“微软不认识闰年2月29日”&字符"N"的作用
原文:SQL点滴6-"微软不认识闰年2月29日"&字符"N"的作用 http://www.cnbeta.com/articles/50580.htm这个 ...
- 苏州Uber优步司机奖励政策(1月11日~1月17日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- [Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)
传送门 #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Someti ...
- 【hiho一下第77周】递归-减而治之 (MS面试题:Koch Snowflake)
本题是一道微软面试题,看起来复杂,解出来会发现其实是一个很简单的递归问题,但是这道题的递归思路是很值得我们反复推敲的. 原题为hihocoder第77周的题目. 描述 Koch Snowflake i ...
- 【内推】2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐
2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐 大家好,目前微软Office365核心团队在美丽宜居的苏州有150多的社招职位虚位以待,欢迎大家自荐,推荐,转发!除以下列 ...
随机推荐
- 20181223 python 使用Beautiful Soup
(这篇,没什么营养价值) 怎么说呢! 爬虫吧!把html页面进行解析得到有效数据,而beautiful soup 能快速格式化页面再进行方法对数进行提取,存入想要存入的DB中. from bs4 im ...
- Python识别字符型图片验证码
前言 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越来越严峻.本文介绍了一套字符验证码识别的完整流程,对于验 ...
- RestFramework——API基本实现及dispatch基本源码剖析
基于Django实现 在使用RestFramework之前我们先用Django自己实现以下API. API完全可以有我们基于Django自己开发,原理是给出一个接口(URL),前端向URL发送请求以获 ...
- 并发编程---线程---开启方式---进程线程的区别----Thread的其他属性
线程 进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合)而线程才是cpu上的执行单位 1.同一个进程内的多个线程共享该进程内的地址资源 2.创建线程的开销远小于创建进程的开销(创建一 ...
- JetBrains 2017/2018全系列产品激活工具
可谓是工欲善其事,必先利其器,相信作为优秀开发工程师的你都想拥有一套快捷高效的编码工具,而JetBrains这家公司的产品,不管是那种编程语言,其开发工具确实让开发者们着迷,JetBrains的产品博 ...
- vue-preview的使用
使用vue-preview做图片缩率图1.安装 npm i vue-preview -S2.如果使用vue-cli生成的项目,需要修改webpack.base.conf.js文件中的loaders,添 ...
- 万恶之源 - Python模块二
shelve 我们之前学了json和pickle模块 这些都是序列化的模块,咱们进行在讲一个序列化的东西 叫做shelve 你们肯定有个疑问,这个东西和那个类似为什么要讲.是因为这个模块比较简单的,并 ...
- mybatis级联
mybatis中有时候表不能都分成单表进行查询,表之间会有联系,这时候需要将表进行级联 下面讲一下如何将mybatis中 的表进行级联.映射表关系如下 1:创建数据表 DROP TABLE IF EX ...
- http协议下载文件
通过在 URL 上调用 openConnection 方法创建连接对象.(HttpURLConnection conn = (HttpURLConnection)new URL("网址&qu ...
- c# 判断文件是否发生了变化
你这个是想文件发生改变时,自动调用一个函数,做出一些操作呢. 还是有一个按钮(或者别的什么),你去点击一下,然后检测下一个文件,是否发生了变化? 下面的代码,监控d盘下的所有.txt文件的修改 1 2 ...