[luoguP1037] 产生数(floyd + 高精度)
先用 floyd 求出每一个数可以变成那些数。
然后利用乘法原理求解,需要高精度。
代码
#include <cstdio>
#include <cstring>
#include <iostream> using namespace std; const int MAXN = 101; char s[MAXN];
char c[MAXN];
int map[10][10]; inline char *read()
{
scanf("%s", c);
return c;
} struct Big_int
{
int s[MAXN], idx;
Big_int()
{
idx = 0;
memset(s, 0, sizeof(s));
}
inline void operator = (char *c)
{
idx = strlen(c);
for(int i = 0; i < idx; i++) s[i] = c[idx - i - 1] - '0';
}
inline void operator = (int x)
{
idx = 0;
if(!x) idx++, s[0] = 0;
while(x)
{
s[idx] = x % 10;
x /= 10;
idx++;
}
}
inline void print()
{
if(!idx) printf("0");
else for(int i = idx - 1; i >= 0; i--) printf("%d", s[i]);
puts("");
}
}; inline Big_int operator + (const Big_int x, const Big_int y)
{
Big_int ret;
ret.idx = max(x.idx, y.idx) + 1;
for(int i = 0; i < ret.idx; i++)
{
ret.s[i] += x.s[i] + y.s[i];
if(ret.s[i] >= 10)
ret.s[i + 1] += 1, ret.s[i] -= 10;
}
while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
return ret;
} inline bool operator < (const Big_int x, const Big_int y)
{
if(x.idx < y.idx) return 1;
if(x.idx > y.idx) return 0;
for(int i = x.idx - 1; i >= 0; i--)
if(x.s[i] ^ y.s[i])
return x.s[i] < y.s[i];
return 0;
} inline Big_int operator - (Big_int x, Big_int y)
{
Big_int ret;
if(x < y) swap(x, y);
ret.idx = x.idx;
for(int i = 0; i < ret.idx; i++)
{
if(x.s[i] < y.s[i])
{
x.s[i] += 10;
x.s[i + 1]--;
}
ret.s[i] = x.s[i] - y.s[i];
}
while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
return ret;
} inline Big_int operator * (const Big_int x, const Big_int y)
{
Big_int ret;
ret.idx = x.idx + y.idx;
for(int i = 0; i < x.idx; i++)
for(int j = 0; j < y.idx; j++)
{
ret.s[i + j] += x.s[i] * y.s[j];
ret.s[i + j + 1] += ret.s[i + j] / 10;
ret.s[i + j] %= 10;
}
while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
return ret;
} Big_int a, ans; int main()
{
int i, j, k, x, y;
scanf("%s", s);
scanf("%d", &k);
for(i = 1; i <= k; i++)
{
scanf("%d %d", &x, &y);
map[x][y] = 1;
}
for(i = 0; i <= 9; i++) map[i][i] = 1;
for(k = 0; k <= 9; k++)
for(i = 0; i <= 9; i++)
for(j = 0; j <= 9; j++)
map[i][j] = map[i][j] || (map[i][k] && map[k][j]);
ans = 1;
k = strlen(s);
for(i = 0; i < k; i++)
{
x = 0;
for(j = 0; j <= 9; j++)
if(map[s[i] - '0'][j]) x++;
a = x;
ans = ans * a;
}
ans.print();
return 0;
}
[luoguP1037] 产生数(floyd + 高精度)的更多相关文章
- NOIP2002pj产生数[floyd 高精度]
背景 给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15). 规则:一位数可变换成另一个一位数:规则的右部不能为零. 例如:n=234.有规则(k=2):2-> 53 ...
- [NOIP 2002普及组]产生数(floyd+高精度)
https://www.luogu.org/problem/P1037 题目描述 给出一个整数 n(n<1030) 和 k 个变换规则(k<=15). 规则: 一位数可变换成另一个一位数: ...
- TYVJ 矩阵取数 Label:高精度+dp
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...
- [JZYZOJ 1288][洛谷 1005] NOIP2007 矩阵取数 dp 高精度
https://www.luogu.org/problem/show?pid=1005 dp好想,高精度练手题,有点不舒服的是前后取数位置的计算,代码量太少才会写题这么慢,noip之前虽然重点放在 ...
- UVA 125 统计路径条数 FLOYD
这道题目折腾了我一个下午,本来我的初步打算是用SPFA(),进行搜索,枚举出发点,看看能到达某个点多少次,就是出发点到该点的路径数,如果出现环,则置为-1,关键在于这个判环过程,如果简单只找到某个点是 ...
- 【BZOJ 2822】2822: [AHOI2012]树屋阶梯(卡特兰数+高精度)
2822: [AHOI2012]树屋阶梯 Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处 ...
- [noip2002] 产生数
题目描述 给出一个整数 n (n<1030)和 k 个变换规则 (k < 15) . 规则: 一位数可变换成另一个一位数: 规则的右部不能为零. 例如:n = 234 .有规则( k=2 ...
- DP+高精度 URAL 1036 Lucky Tickets
题目传送门 /* 题意:转换就是求n位数字,总和为s/2的方案数 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 高精度直接拿J ...
- BZOJ 2822: [AHOI2012]树屋阶梯
Description 求拼成阶梯状的方案数. Sol 高精度+Catalan数. 我们可以把最后一行无线延伸,所有就很容易看出Catalan数了. \(f_n=f_0f_{n-1}+f_1f_{n- ...
随机推荐
- Person p = new Person("zhangsan",20);该句话都做了什么事情?
1,因为new用到了Person.class.所以会先找到Person.class文件并加载到内存中.2,执行该类中的static代码块,如果有的话,给Person.class类进行初始化.3,在堆内 ...
- Activity状态图、生命周期图(超详细),onSaveInstanceState只保存、恢复基本ui数据,持久数据不在这里保存。
1.Activity状态图 2.Activity生命周期简图 启动Activity: onCreate()—>onStart()—>onResume(),Activity进入running ...
- JAVA Android王牌教程
Java基础 在Java基础系列文章中,我将说明Java的基础内容,特别是面向对象的相关概念. Java基础01 从HelloWorld到面向对象 Java基础02 方法与数据成员 Java基础03 ...
- Android开发学习--RecycleView入门
该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我们并不陌生,例如:ListView.GridView 通过设置它提供的不同LayoutManager,ItemDecoration , It ...
- jq判断上下滚动
$(document).ready(function(){ var p=0,t=0; $(window).scroll(function(e){ p = $(this).scrollTop(); if ...
- Actionscript,AS3,MXML,Flex,Flex Builder,Flash Builder,Flash,AIR,Flash Player之关系
转自zrong's blog:http://zengrong.net/post/1295.htm ActionScript ActionScript通常简称为AS,它是Flash平台的语言.AS编写的 ...
- git push失败the remote end hung up unexpectedly
Git Push是老是失败,提示: fatal: the remote end hung up unexpectedly git did not exit cleanly (exit code 1) ...
- JavaScript——responseType
https://www.cnblogs.com/cdemo/p/5225848.html https://blog.csdn.net/wkyseo/article/details/78232485 异 ...
- MySql 基础知识-常用命令及sql语句
一.常用mysql命令行命令 1,启动mysql服务 net start mysql. 停止mysql服务 net stop mysql 2,netstart -na|findstr 330 ...
- C++ 继承/派生、访问属性、构造函数
1.子类继承父类的继承方式:public,private,protected,不写则默认为private: 2.子类会继承父类的全部成员(除了构造函数.析构函数,虽然析构函数有virtual,但是不是 ...