BZOJ1002輪狀病毒 暴搜 + 找規律 + 高精度
@[暴搜, 找規律, 高精度]
Description
轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的。一个\(n\)轮状基由圆环上\(n\)个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道。如下图所示

\(n\)轮状病毒的产生规律是在一个\(n\)轮状基中删去若干条边,使得各原子之间有唯一的信息通道,例如共有\(16\)个不
同的3轮状病毒,如下图所示

现给定\(n\)(\(n <= 100\)),编程计算有多少个不同的n轮状病毒
Input
1个正整数n
Output
计算出的不同的n轮状病毒数输出
Sample Input
3
Sample Output
16
Solution
先暴搜找規律(結果爆搜就打了好久\(QAQ\))
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
inline int read()
{
int x = 0, flag = 1;;
char c;
while(! isdigit(c = getchar()))
if(c == '-')
flag *= - 1;
while(isdigit(c))
x = x * 10 + c - '0', c = getchar();
return x * flag;
}
void println(int x)
{
if(x < 0)
putchar('-'), x *= - 1;
if(x == 0)
putchar('0');
int ans[10], top = 0;
while(x)
ans[top ++] = x % 10, x /= 10;
for(; top; top --)
putchar(ans[top - 1] + '0');
putchar('\n');
}
int n;
struct Edge
{
int u, v;
}G[100];
int ans;
int fa[16];
int find(int x)
{
if(fa[x] == x)
return x;
return fa[x] = find(fa[x]);
}
void search(int x, int top, int cnt)
{
if(cnt == n)
{
ans ++;
return;
}
if(x == top)
return;
search(x + 1, top, cnt);
int _fa[16]; //并查集很執行難撤回操作, 因此只能用這種比較笨的辦法來搞
for(int i = 0; i <= n; i ++)
_fa[i] = fa[i];
int fu = find(G[x].u), fv = find(G[x].v);
if(fu == fv)
return;
fa[fu] = fv;
search(x + 1, top, cnt + 1);
for(int i = 0; i <= n; i ++)
fa[i] = _fa[i];
}
int main()
{
for(int i = 2; i < 16; i ++)
{
n = i;
for(int j = 0; j < n; j ++)
G[j].u = 0, G[j].v = j + 1;
for(int j = n; j < (n << 1); j ++)
G[j].u = j - n + 1, G[j].v = j - n + 2;
G[(n << 1) - 1].v = 1;
for(int i = 0; i <= n; i ++)
fa[i] = i;
ans = 0;
search(0, n << 1, 0);
println(ans);
}
}
得到輸出數據
5
16
45
121
320
841
2205
5776
15125
39601
103680
271441
710645
1860496
--------------------------------
Process exited after 70.46 seconds with return value 0
请按任意键继续. . .
通過待定係數法可得, 對於\(n\)輪狀病毒有種類數
\(f(n) = f(n - 1) * 3 - f(n - 2) + 2\)
注意到當 \(n = 100\) 時\(f(n)\) 會變得很大, 所以要寫高精度...
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
inline int read()
{
int x = 0, flag = 1;;
char c;
while(! isgraph(c = getchar()))
if(c == '-')
flag *= - 1;
while(isgraph(c))
x = x * 10 + c - '0', c = getchar();
return x * flag;
}
void println(int x)
{
if(x < 0)
putchar('-'), x *= - 1;
if(x == 0)
putchar('0');
int ans[10], top = 0;
while(x)
ans[top ++] = x % 10, x /= 10;
for(; top; top --)
putchar(ans[top - 1] + '0');
putchar('\n');
}
const int N = 1 << 7;
struct Giant
{
int dig[1 << 10];
int top;
}f[N];
Giant operator *(Giant x, int y)
{
for(int i = 0; i < x.top; i ++)
x.dig[i] *= y;
for(int i = 0; i < x.top; i ++)
x.dig[i + 1] += x.dig[i] / 10, x.dig[i] %= 10;
if(x.dig[x.top])
x.top ++;
return x;
}
Giant operator -(Giant x, Giant y)
{
for(int i = 0; i < x.top; i ++)
{
x.dig[i] -= y.dig[i];
if(x.dig[i] < 0)
x.dig[i] += 10, x.dig[i + 1] --;
}
if(! x.dig[x.top - 1])
x.top --;
return x;
}
Giant operator +(Giant x, int y)
{
x.dig[0] += y;
for(int i = 0; i < x.top; i ++)
x.dig[i + 1] += x.dig[i] / 10, x.dig[i] = x.dig[i] % 10;
if(x.dig[x.top])
x.top ++;
return x;
}
void println(Giant &x)
{
for(int i = x.top; i; i --)
putchar(x.dig[i - 1] + '0');
putchar('\n');
}
int main()
{
int n = read();
memset(f, 0, sizeof(f));
f[2].dig[0] = 5, f[2].top = 1;
f[3].dig[0] = 6, f[3].dig[1] = 1, f[3].top = 2;
for(int i = 4; i <= n; i ++)
f[i] = f[i - 1] * 3 - f[i - 2] + 2;
println(f[n]);
}
BZOJ1002輪狀病毒 暴搜 + 找規律 + 高精度的更多相关文章
- 51nod1093(推公式&找規律)
題目鏈接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1093 題意:中文題誒- 思路:xjb 一開始死活想不出怎麼將一 ...
- HDU - 6185 Covering(暴搜+递推+矩阵快速幂)
Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...
- c++20701除法(刘汝佳1、2册第七章,暴搜解决)
20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入正整数n,按从小到大的顺序输出所有 ...
- Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜
题目链接: 题目 D. Toy Sum time limit per test:1 second memory limit per test:256 megabytes 问题描述 Little Chr ...
- 子矩阵(暴搜(全排列)+DP)
子矩阵(暴搜(全排列)+DP) 一.题目 子矩阵 时间限制: 1 Sec 内存限制: 128 MB 提交: 1 解决: 1 [提交][状态][讨论版] 题目描述 给出如下定义: 1. 子矩阵:从一 ...
- HDU5952 Counting Cliques 暴搜优化
一.前言 这题看上去相当唬人(NPC问题),但是 因为限制了一些条件,所以实际上并没有太唬人. 二.题目 给你一个图,要求你找出数量为S的团的数量. 三.题解 暴搜,再加上一些玄学优化. 优化1:使用 ...
- hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜
题目链接 题意 给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\). 思路 暴搜. 搜索的时候判断要加进 ...
- bzoj 1053 [ HAOI 2007 ] 反素数ant ——暴搜
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1053 试图打表找规律,但无果... 看TJ了,暴搜: 注意参数 w 是 long long. ...
- 紫书 习题7-14 UVa 307(暴搜+剪枝)
这道题一开始我想的是在排序之后只在头和尾往中间靠近来找木块, 然后就WA, 事实证明这种方法是错误的. 然后参考了别人的博客.发现别人是直接暴搜, 但是加了很多剪枝, 所以不会超时. 我也想过这个做法 ...
随机推荐
- Django之组合搜索组件(一)
什么是组合搜索呢? 比如你想买车,但手里只有10万块!所以你只能在10万块的车里挑选,但你喜欢黑色,因为觉得很高端大气上档次,说白了就是装逼杠杠的!之后售车姐给你拿了个表表,你看到了低于10万块且颜色 ...
- Go语言 6 结构体、方法和接口
文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ Go学习群:415660935 结构体(struct)是由一系列具有相同类型或不同类 ...
- [Gym-100625J] 搜索
题目链接:https://cn.vjudge.net/problem/Gym-100625J 具体思路:首先,具体思路是两个人一起走到一个点,然后两个人按照同样的道路走出去,听了别人的思路,还有一种特 ...
- 怎么让IIS7第一次访问相应速度加快
当我们把网站部署在IIS7或IIS6S的时候,每当IIS或是Application Pool重启后,第一次请求网站反应总是很慢,原因大家都知道(不知道可以参考这个动画说明ASP.NET网页第一个Req ...
- DAY1-GO初识(概述)
一.概述 1.特征 1.1.语法简单:规则严谨.保留指针.但默认阻止指针运算.将切片和字典作为内置类型.更好的维护性: 1.2.并发模型:运行时用GOroutine,一个关键字.简单而自然:搭配cha ...
- INIT_WORK
借助runtime pm,在需要使用模块时,增加引用计数(可调用pm_runtime_get),不需要使用时,减少引用计数(可调用pm_runtime_put). 1.INIT_WORK(struct ...
- 关于DataTable.Select不到数据的一种解决方案
网上有很多说的,试过,都没用.自己研究了一下,解决方案如下: 建立dataview,用dv.rowfilter,就可以取到了,然后TOTABLE即可. 代码如下:(只看中间那几句即可) private ...
- 生成器(generator)和迭代(iterable , iterator, iteration)
在搞清楚Generator之前,我们先讨论一下 iterable , iterator, iteration 1.Iterable 我们知道,在Python中所有东西都是object, 比如说变量,容 ...
- 访问公网WebService服务
接下来,我们演示如何访问公网webservice服务. 我们以访问 http://www.webxml.com.cn/zh_cn/index.aspx 为例,主要演示手机号码归属地查询服务(使用说明书 ...
- NTP算法
网络时间协议 由特拉华大学的David L. Mills热心提供.http://www.eecis.udel.edu/~mills mills@udel.edu 由Reinhard v. Hanxle ...