Numerical Circulation

The R&D center  formulated a  sequence from a spectrum according to electron orbital changes. In order to create the sequence, the initial number, N & the square number, P are required. When N & P are fixed, the first element of the sequence is N, and the second element is the sum of raising each cipher of the first element to P. And also for the third element, you can do the sum of raising each cipher of the second element to P.

For example, let’s think about the case, N=57 & P=2. The first element of the sequence is 57, and the second element is 5×5+7×7=74. Like this, the third element is 7×7+4×4=65, & the forth element is 6×6+5×5=61. After that, you can find the following sequence: 57, 74, 65, 61, 37, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89 etc.

However, our R&D center found that the elements of sequence are circulated and wanted to find out the length of the sequence of the front section which is not circulated. Let’s satisfy their curiosity of the R&D center.

Time limit: 1 second (java: 2 seconds)

Input format


Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 30) are given in a row.
N, the initial number and P, the square number of the sequence in the first row of each test case are given as being separated. (2 ≤ N ≤ 9999, 2 ≤ P ≤ 5)

Output format

Output the length of the sequence of the front section which is not circulated from the sequence given in the first row of each test case.

Example of Input

2
57 2
638 2

Example of Output

4

5


Strategy:

1. Generate Next Number.

2. Try to add that in a Set (data structure that does not allow duplicate entry)

    - if < successful >

        - Then add it to a LIST also (ArrayList perhaps) GOTO step-1

    - if < NOT successful > (it means we got the number before and it is the first number where the repetition started)

        - DO NOT add this to the LIST.

        - find the lastIndex of that number in the LIST

        - count the number of emenemts before that >>>> this is your result.


#include <iostream>
#include <set>
#include <math.h>

using namespace std;

struct data_t
{
	data_t(int i, int v, int p, int first = false) : idx(i), val(0)
	{
		if (first)
			val = v;
		else
		{
			int tmp = v, nr, pw;
			for(int i = 5; i >= 0; --i)
			{
				pw = pow((double)10, (double)i);
				nr = tmp / pw;
				val += pow((double)nr, (double)p); 
				tmp %= pw;
			}
		}
	}

	bool operator<(const data_t& rhs) const
	{
		return val < rhs.val;
	}

	int idx;
	int val;
};

set<data_t> results;


int main(int argc, char** argv)
{			  
	ios_base::sync_with_stdio(false);
	pair<set<data_t>::iterator, bool> ret;

	int t, Tc, idx, N, P, val;
	
	cin >> Tc;
	for (t = 1; t <= Tc; ++t)
	{
		results.clear();
		idx = 0;
		cin >> N >> P;

		ret = results.insert(data_t(idx++, N, P, true));
		val = ret.first->val;

		do
		{
			ret = results.insert(data_t(idx++, val, P));
			val = ret.first->val;
		}
		while (ret.second);

		cout << ret.first->idx << endl;
	}
	

	return 0;
}


/*
设置一个数组,下标代表可能出现的值,内容存的是第一次出现这个数字的count数。
按照题意循环跑,不出现重复则记录count值,出现重复则返回其count,最后的answer
为长度,即count-1即可。
*/


#include <string.h>
#include <stdio.h>

int number[250000];		
int N, P;
int count, Answer;

int power(int n, int p)
{
	int res=1;
	while(p!=0)
	{
		res*=n;
		p--;
	}
	return res;
}

int func(int n, int p)
{
	int res=0;
	while(n!=0)
	{
		res+=power(n%10, p);
		n/=10;
	}
	
	return res;
}



int main(void)
{
	int tc, T;
	
	setbuf(stdout, NULL);

	scanf("%d", &T);
	for(tc = 0; tc < T; tc++)
	{
		int tmp;
		scanf("%d %d", &N, &P);
		
		//initial
		memset(number, 0, sizeof(number));
		number[N]=1;
		tmp = N;
		count=1;
		while(1)
		{
			tmp = func(tmp, P);
			if(number[tmp]!=0)
			{
				Answer = number[tmp];
				break;
			}
			count++;
			number[tmp]=count;
		}

		printf("%d\n", Answer-1);
		
	}

	return 0;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值