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, 事实证明这种方法是错误的. 然后参考了别人的博客.发现别人是直接暴搜, 但是加了很多剪枝, 所以不会超时. 我也想过这个做法 ...
随机推荐
- css3旋转、过渡、动画属性
1.transform 该属性对元素进行旋转.缩放.移动和倾斜 translate元素从当前位置移动 rotate元素顺时针旋转 scale元素的尺寸增大或减小 skew元素翻转 2.transiti ...
- mysql数据库单表增删改查命令
数据库DB-database-mysql 课程安排 第一天: 1.数据库定义以及设计 2.mysql服务端的安装 3.mysql-dos操作 库的操作 表的操作 4.mysql客户端navicate工 ...
- 配置tomcat多域名访问
C:\Windows\System32\drivers\etc下的hosts文件改成:127.0.0.1 localhost 127.0.0.1 www.greenmood.net 127.0.0.1 ...
- Struts结果跳转方式(四种result配置)
1.转发(默认转发)
- tar解压与压缩
1.解压 tar -zxvf 压缩文件名 -C 指定的目录 (制定的目录必须存在) 2.压缩 tar -czvf 压缩后的文件名 要压缩的文件夹
- Python3 断言
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:CarsonLi ''' 断言一般用于后面有非常重要的操作,需要使用前面的数据,而且不容许出 ...
- 75.VS2013和opencv3.1.0开发环境配置
首先要做的就是 开发环境配置,具体过程如下: Step 1:OpenCV环境变量配置 我的电脑--->属性--->高级系统设置--->高级--->环境变量--->系统变量 ...
- HDU 5936 朋友
题意为给出一棵n个节点的树,这棵树的每条边有一个权值,这个权值只可能是0或1. 在一局游戏开始时,会确定一个节点作为根. 当一方操作时,他们需要先选择一个不为根的点,满足该点到其父亲的边权为1; 然后 ...
- MVC – 7.Razor 语法
7.1 Razor视图引擎语法 Razor通过理解标记的结构来实现代码和标记之间的顺畅切换. @核心转换字符,用来 标记-代码 的转换字符串. 语境A: @{ string rootName=&quo ...
- Dell服务器iDrac口默认账号密码和IP
https://blog.csdn.net/artdao1987/article/details/79875528