饥饿的小易

题目:

https://www.nowcoder.com/practice/5ee8df898312465a95553d82ad8898c3?tpId=85&tqId=29843&tPage=1&rp=1&ru=/ta/2017test&qru=/ta/2017test/question-ranking

参考博客

https://blog.youkuaiyun.com/waltonhuang/article/details/52143201

https://blog.youkuaiyun.com/waltonhuang/article/details/52142214

主要用到取模的操作。要用到long long 和bfs

如果a%c=a,那么 
(a*x+b)%c = (a*(x%c)+b)%c 

代码:

#include<iostream>
#include<queue>
#include<map>
using namespace std;
typedef long long llint;
const llint base = 1000000007LL;
 


int solve(int x0){
	//存储模
	queue<int> q;
	//存储{模: 数量}
	map<int, int>m;
	q.push(x0);
	m[x0] = 0;
	while (!q.empty()){
		llint now = q.front();
		q.pop();
		if (now == 0) //说明整除了 
			return m[now];
		//如果大于他了
		if (m[now] > 100000)
			continue;
		int next0 = (now * 4 + 3) % base;
		if (!m[next0]){
			m[next0] = m[now] + 1;
			q.push(next0);
		}
		int next1 = (now * 8 + 7) % base;
		if (!m[next1]){
			m[next1] = m[now] + 1;
			q.push(next1);
		}
	}
	return -1;
}
int main() {
	int x0;
	while (cin >> x0)
	{
		cout << solve(x0) << endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值