@[暴搜, 找規律, 高精度]

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輪狀病毒 暴搜 + 找規律 + 高精度的更多相关文章

  1. 51nod1093(推公式&找規律)

    題目鏈接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1093 題意:中文題誒- 思路:xjb 一開始死活想不出怎麼將一 ...

  2. HDU - 6185 Covering(暴搜+递推+矩阵快速幂)

    Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...

  3. c++20701除法(刘汝佳1、2册第七章,暴搜解决)

    20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述     输入正整数n,按从小到大的顺序输出所有 ...

  4. 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 ...

  5. 子矩阵(暴搜(全排列)+DP)

    子矩阵(暴搜(全排列)+DP) 一.题目 子矩阵 时间限制: 1 Sec  内存限制: 128 MB 提交: 1  解决: 1 [提交][状态][讨论版] 题目描述 给出如下定义: 1. 子矩阵:从一 ...

  6. HDU5952 Counting Cliques 暴搜优化

    一.前言 这题看上去相当唬人(NPC问题),但是 因为限制了一些条件,所以实际上并没有太唬人. 二.题目 给你一个图,要求你找出数量为S的团的数量. 三.题解 暴搜,再加上一些玄学优化. 优化1:使用 ...

  7. hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜

    题目链接 题意 给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\). 思路 暴搜. 搜索的时候判断要加进 ...

  8. bzoj 1053 [ HAOI 2007 ] 反素数ant ——暴搜

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1053 试图打表找规律,但无果... 看TJ了,暴搜: 注意参数 w 是 long long. ...

  9. 紫书 习题7-14 UVa 307(暴搜+剪枝)

    这道题一开始我想的是在排序之后只在头和尾往中间靠近来找木块, 然后就WA, 事实证明这种方法是错误的. 然后参考了别人的博客.发现别人是直接暴搜, 但是加了很多剪枝, 所以不会超时. 我也想过这个做法 ...

随机推荐

  1. three.js为何如此奇妙

    WebGL是在浏览器中实现三维效果的一套规范,而最初使用WebGL原生的API来写3D程序是一件非常痛苦的事情,在辛苦的付出下WebGL开源框架出现了,其中three.js就是非常优秀的一个,它掩盖了 ...

  2. 绿色的银行类cms管理系统模板——后台

    链接:http://pan.baidu.com/s/1pK7Vu9X 密码:4cc5

  3. lspci 虚拟机网卡对应关系

    我这个办法有点笨: 到 /sys/devices/ 下去搜索网卡 eth*,找到网卡对应的PCI 总线位置,例如:05:00.0. 然后通过 "lspci -s 05:00.0" ...

  4. https、socket、http协议

    一.https https 其实是由两部分组成:http+ssl(Secure Sockets Layer 安全套接层)/tls(Transport Layer Security 继任者安全传输层), ...

  5. UVA题解一

    UVA 100 题目描述:经典3n+1问题在\(n \leq 10^6\)已经证明是可行的,现在记\(f[n]\)为从\(n\)开始需要多少步才能到\(1\),给出\(L, R\),问\(f[L], ...

  6. linux系统查找具体进程

    ps -ef | grep '查找内容' eg:ps -ef | grep '测试USB设备穿透'

  7. jquery ajax的再次封装,简化操作

    1.封装的ajax var funUrl=""   // 每个请求地址相同的部分 function queryData(url,params,success,error){ url ...

  8. vue 条件渲染与列表渲染

    本文是对官方文档的整理 因为 v-if 是一个指令,所以必须将它添加到一个元素上.但是如果想切换多个元素呢?此时可以把一个 <template> 元素当做不可见的包裹元素,并在上面使用 v ...

  9. [loj2116]「HNOI2015」开店 动态点分治

    4012: [HNOI2015]开店 Time Limit: 70 Sec  Memory Limit: 512 MBSubmit: 2452  Solved: 1089[Submit][Status ...

  10. electron写俄罗斯方块游戏(Tetris)

    背景 在折腾ES6,突然想起大学时用c语言写过俄罗斯方块,本项目中主要是利用ES6的Class特性进行面向对象编程.项目采用node.js v6.2.0 + electron v1.1.0 进行桌面开 ...