P1037 产生数

P1037

DFS+高精度乘低精度

一道简单题,对每一位数字进行dfs,看最多能变换几次,最后利用乘法原理相乘就是结果

话不多说,直接干代码:

#define  _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cmath>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <stack>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <fstream>
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LNF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
const int maxn = 1002;
int k, temp, ans[maxn];
string n;
bool vis[maxn];
pair<int, int> q[maxn];

LL read() {
	LL x = 0, f = 1; char c = getchar();
	while (!isdigit(c)) { if (c == '-') f *= -1; c = getchar(); }
	while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
	return x * f;
}

void put(int x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) put(x / 10);
	putchar(x % 10 + '0');
}

void dfs(int x) {
	if (vis[x]) return;
	vis[x] = 1;
	for (int i = 1; i <= k; i++) {
		if (q[i].first == x && !vis[q[i].second]) {
			dfs(q[i].second);
			temp++;
		}
	}
}

void mul(int ans[], int x) { //高精度乘低精度
	int jinwei = 0;
	for (int i = 1; i <= 50; i++) {
		ans[i] = ans[i] * x + jinwei;
		jinwei = ans[i] / 10;
		ans[i] %= 10;
	}
}

//二者都可

//void mul(int ans[], int x) { //高精度乘低精度
//	for (int i = 1; i <= ans[0]; i++) ans[i] *= x;
//	for (int i = 1; i <= ans[0]; i++) {
//		if (ans[i] >= 10) {
//			ans[i + 1] += ans[i] / 10;
//			ans[i] %= 10;
//			if (i == ans[0]) ans[0]++;
//		}
//	}
//}

int main() {
	cin >> n;
	k = read();
	ans[0] = 1; //ans[0]保存当前位数
	ans[1] = 1;
	for (int i = 1; i <= k; i++) q[i].first = read(), q[i].second = read();
	for (int i = 0; i < n.length(); i++) {
		temp = 1;
		dfs(n[i] - '0');
		mul(ans, temp);
		memset(vis, 0, sizeof(vis));
	}
	int flag = 0;
	for (int i = 50; i >= 1; i--) {
		if (ans[i]) flag = 1;
		if (flag) put(ans[i]);
	}
	/*for (int i = ans[0]; i >= 1; i--)
		put(ans[i]);*/
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值