题目链接:

hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5167

题意:

给你一个x,判断x能不能由斐波那契数列中的数相乘得到(一个数可以重复使用)

题解:

1、筛法

首先,小于10^9的斐波那契数很少,就42个(不包括0,1),对于给定的x能同时成为它的因子的斐波那契数更少,所以可以暴力考虑所有能整除x的斐波那契数组成的集合的所有子集。

但是,对于每个子集做筛法的时候必须从大的数开始筛(质因数分解可以随便筛是因为一个数的质因数分解具有唯一性,但这里并不具有唯一性),因为大的数比小的数更特别,它可能具有小的数不具备的组成x的质因子,如果它具备独特质因子,那它就要筛到不能筛为止,如果它不具备独特质因子,它多筛了也不影响的,所以从大到小的做保证了正确的分解不会丢失。

68=2*34=2*2*17,如果从小到大筛,会把2全筛了,剩下个17是没办法筛掉的,而如从后往前则会先筛掉34,再筛掉一个2,这样就可以了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL; const int maxn = ; int fib[maxn]; int tot;
void get_fib() {
fib[] = , fib[] = ;
for (tot = ;; tot++) {
fib[tot] = fib[tot - ] + fib[tot - ];
if (fib[tot] >) break;
}
} int di[maxn], n; void init() {
n = ;
} int main() {
get_fib();
int tc;
scanf("%d", &tc);
while (tc--) {
init();
int x;
scanf("%d", &x);
if (x == ||x==) {
puts("Yes"); continue;
}
for (int i = ; i < tot; i++) {
if (x%fib[i] == ) {
di[n++] = fib[i];
}
}
bool su = false;
for (int stat = ; stat < ( << n); stat++) {
int tmp = x;
for (int i = n-; i >=; i--) {
if (stat&( << i)) {
while (tmp%di[i] == ) tmp /= di[i];
}
}
if (tmp == ) {
su = true; break;
}
}
if (su) puts("Yes");
else puts("No");
}
return ;
}
/*
68
*/

2、直接暴搜

看完题解哭晕。。

而且跑的溜溜的orz

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL; const int maxn = ; int fib[maxn]; int tot;
void get_fib() {
fib[] = , fib[] = ;
for (tot = ;; tot++) {
fib[tot] = fib[tot - ] + fib[tot - ];
if (fib[tot] >) break;
}
} int di[maxn], n; bool dfs(int cur, int x) {
if (x == ) return true;
if (cur == n) return false;
if (dfs(cur + , x)) return true; if (x%di[cur]==) {
int tmp = x;
while (tmp%di[cur] == ) {
tmp /= di[cur];
//printf("tmp:%d\n", tmp);
if (dfs(cur + , tmp)) return true;
}
}
return false;
} void init() {
n = ;
} int main() {
get_fib();
int tc;
scanf("%d", &tc);
while (tc--) {
init();
int x;
scanf("%d", &x);
if (x == ||x==) {
puts("Yes"); continue;
}
for (int i = ; i < tot; i++) {
if (x%fib[i] == ) {
di[n++] = fib[i];
}
}
//printf("n:%d\n", n);
if (dfs(,x)) puts("Yes");
else puts("No");
}
return ;
}

HDU 5167 Fibonacci 筛法+乱搞的更多相关文章

  1. hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树

    首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...

  2. hdu 5167 Fibonacci 打表

    Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Proble ...

  3. hdu 5167 Fibonacci(预处理)

    Problem Description Following is the recursive definition of Fibonacci sequence: Fi=⎧⎩⎨01Fi−1+Fi−2i ...

  4. BZOJ-1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法+乱搞

    1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 1383 Solved: 7 ...

  5. HDU 4614 Vases and Flowers(线段树+记录区间始末点或乱搞)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题目大意:有n个空花瓶,有两种操作: 操作①:给出两个数字A,B,表示从第A个花瓶开始插花,插B ...

  6. hdu 4506 小明系列故事——师兄帮帮忙【幂取模乱搞】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4506 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. hdu 5246 乱搞

    题意:题目太长直接看链接 链接:点我 乱搞题 显然,一个人要想成功,必须大于等于最强的人的战斗力,所以我们从后往前看 这里直接拿例1解释,首先递减排个序 15,13,10,9,8 作差得2,3,1,1 ...

  8. URAL 1827 Indigenous Wars(排序、乱搞)

    题意:给一个长度为n数组{a[i]}.有m个操作Ti,Si,Li表示找以Ti值结束,以Si值开始,长度为Li的连续子串.找到后,将区间的答案值设为1.一开始答案值全部为0.最后输出n个答案值. 好久没 ...

  9. UVA 11853 [dfs乱搞]

    /* 大连热身E题 不要低头,不要放弃,不要气馁,不要慌张 题意: 在1000×1000的格子内有很多个炮弹中心,半径给定. 为某人能否从西部边界出发,从东部边界走出. 不能输出不能,能的话输出最北边 ...

随机推荐

  1. FreeRTOS内存管理

    简介 Freertos的内存管理分别在heap_1.c,heap_2.c,heap_3.c,heap_4.c,heap_5.c个文件中,选择合适的一种应用于嵌入式项目中即可. 本文的图片中 红色部分B ...

  2. usb驱动之打印usb设备信息(二)

    以下是打印鼠标左右键及其他输入的源代码,详细说明见https://www.cnblogs.com/zhu-g5may/p/9309381.html /*参考/drivers/hid/usbhid/us ...

  3. Java使用POI导出excel(上)——基本操作

    相关的介绍参考自:http://zc985552943.iteye.com/blog/1491546 一.概述 1.概念 受上文博文博主的启发,有必要先对excel的各个概念先做了解! //上述基本都 ...

  4. 20155202 20155222 信息安全技术概论实验一 PGP的使用

    20155202 信息安全技术概论实验一 PGP的使用 实验原理 一.PGP简介 在现代社会里,电子邮件和网络上的文件传输已经成为生活的一部分.邮件的安全问题也就突出了,大家都知道在互联网上传输的数据 ...

  5. 20155233 2006-2007-2 《Java程序设计》第2周学习总结

    20155233 2006-2007-2 <Java程序设计>第2周学习总结 教材学习内容总结 本章主要学习Java的基础语法,这些语法在C语言的学习中基本上都涉及到过,基本上,Java可 ...

  6. 20155321 实验四 Android程序设计

    20155321 实验四 Android程序设计 安装Android studio成功 任务一:Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)( ...

  7. 【LG4585】[FJOI2015]火星商店问题

    [LG4585][FJOI2015]火星商店问题 题面 bzoj权限题 洛谷 \(Notice:\) 关于题面的几个比较坑的地方: "一天"不是一个操作,而是有0操作就相当于一天开 ...

  8. 在azure windows虚拟机上安装iis

    在 dashboard-添加角色和功能-一直往下点就好了,后”选择安装类型“页面 中选择[基于角色或基于功能的安装],安装完成后 在浏览器输入 http://localhost/ 就可以正常访问网站了 ...

  9. STM32L431仿真卡在HAL_InitTick(TICK_INT_PRIORITY);

    1. 使用IAR 8.20版本,STM32L431RBT芯片,JLINK V9仿真器,实际仿真测试的时候卡在如下的函数 /* Use SysTick as time base source and c ...

  10. idea 单元测试 mybatis spring-test 异常: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    因为在idea中必须在test下才能进行单元测试,所以进行单元测试时,ssm的项目会因为找不到resourece中的配置文件而报错 这里 org.apache.ibatis.binding.Bindi ...