POJ Round and Round We Go(核心大数相乘算法)

136 阅读3分钟

1047:Round and Round We Go

    • 总时间限制: 
    • 1000ms 
    • 内存限制: 
    • 65536kB
    • 描述

    • A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a"cycle"of the digits of the original number. That is, if you consider the number after the last digit to "wrap around"back to the first digit, the sequence of digits in both numbers will be the same, though they may start at different positions.For example, the number 142857 is cyclic, as illustrated by the following table:
      142857 *1 = 142857
      142857 *2 = 285714
      142857 *3 = 428571
      142857 *4 = 571428
      142857 *5 = 714285
      142857 *6 = 857142\

    • 输入

    • Write a program which will determine whether or not numbers are cyclic. The input file is a list of integers from 2 to 60 digits in length. (Note that preceding zeros should not be removed, they are considered part of the number and count in determining n. Thus, "01"is a two-digit number, distinct from "1" which is a one-digit number.)

    • 输出

    • For each input integer, write a line in the output indicating whether or not it is cyclic.

    • 样例输入

    • 142857
      142856
      142858
      01
      0588235294117647
      
    • 样例输出

    • 142857 is cyclic
      142856 is not cyclic
      142858 is not cyclic
      01 is not cyclic
      0588235294117647 is cyclic
      
    • 来源

    • Greater New York 2001

    • \

    • \

    • \

    • #include<iostream>
      #include<iomanip>
      #include<string>
      #include<vector>
      #include<algorithm>
      using namespace std;
      
      
      vector<int> bigMultiplies(vector<int> a, vector<int> b)//大数相乘算法,模拟手工乘法
      {
      	vector<int> rt;   //保存返回结果
      	
      	vector<vector<int>> temp(a.size());  //二维数组存储临时结果 temp[i][j]表示a[i]*b的临时结果
      
      	reverse(a.begin(), a.end());   //低位放到前面
      	reverse(b.begin(), b.end());  //低位放到前面
      
      
      	for (int i = 0; i < a.size(); i++)    //依次计算a中的每个元素和b的乘积
      	{
      		for (int x = 0; x < i; x++) //插入0保证移位
      		{
      			temp[i].push_back(0);  
      		}
      
      		int jinwei = 0;
      		int cura = a[i];
      		
      		for (int j = 0; j < b.size(); j++)   //依次乘以b中的每一位
      		{
      			int curb = b[j];
      			int currs = cura*curb+jinwei;
      			jinwei = currs / 10;
      			currs = currs - 10 * jinwei;
      			temp[i].push_back(currs);
      
      		}
      		if(jinwei!=0)
      		temp[i].push_back(jinwei);
      	}
      
      
      	for (int i = 0; i < temp[0].size(); i++) //rt初始化为temp的第一行
      	{
      		rt.push_back(temp[0][i]);
      	}
      
      	for (int i = 1; i < temp.size(); i++)  //汇总结果
      	{
      		int jinwei = 0;
      		int j = 0;
      		for (j=0; j < temp[i].size(); j++)
      		{
      			int cur = temp[i][j];
      			if (j >= rt.size())
      			{
      				rt.push_back((cur + jinwei) % 10);
      				jinwei = (cur + jinwei) / 10;
      			}
      			else
      			{
      				int t = rt[j] + cur + jinwei;
      				jinwei= t / 10;
      				rt[j] = t - jinwei * 10;
      			}
      		}
      		while (jinwei != 0)   //处理最后的进位
      		{
      			if (j < rt.size())
      			{
      				int t = jinwei + rt[j];
      				jinwei = t / 10;
      				rt[j] = t % 10;
      				j++ ;
      			}
      			else
      			{
      				rt.push_back(jinwei);
      				jinwei = 0;
      			}
      		}
      		
      	}
      
      	reverse(rt.begin(), rt.end());  //逆序得到真实结果
      	
      	while(rt[0]==0)   //除去可能开头出现的0
      	{
      		rt.erase(rt.begin());
      	}
      	return rt;
      
      }
      
      
      int main()
      {
      
      	string in;
      	while (cin >> in)
      	{
      		int length = in.size();
      		int now = 2;         //now表示当前in*now 看结果是不是包含相同的所有变量
      		bool endflag = true;
      
      		while (endflag&&now <= length) {  //依次判断in*(2~length)的结果是不是合格的
      
      			vector<int> mul_a;      //将in变成数组存储
      			vector<int> mul_b;     //将now变成mul_b存储
      			vector<int> result;    //存储一次乘法的结果
      
      			vector<int> temp(10, 0);   //辅助数组来判断in和result是不是有相同的元素以及对应数量是不是相同
      
      			for (int i = 0; i < length; i++)
      			{
      				mul_a.push_back(in[i] - '0');     //将in变成数组
      				temp[in[i] - '0']++;             //对应元素个数加一
      			}
      
      			string temp_b = to_string(now); 
      			for (int i = 0; i < temp_b.size(); i++)    //将now变成数组
      			{
      				mul_b.push_back(temp_b[i] - '0');
      			}
      
      
      			while (mul_a[0] == 0)
      			{
      				mul_a.erase(mul_a.begin());        //除去in中的开头的0
      			}
      
      			result = bigMultiplies(mul_a, mul_b);  //调用大数相乘算法得到结果
      
      			
      			for (int i = 0; i < result.size(); i++)  //对应元素数量减一
      			{
      				temp[result[i]]--;
      			}
      
      			for (int i = 0; i < temp.size(); i++)  //看最后辅助数组所有数字是不是0,是0表示in和 result的对应元素个数相同
      			{
      				if (temp[i] != 0)
      				{
      					endflag = false;
      					break;
      				}
      			}
      			now++;
      		}
      		if (endflag)
      			cout << in << " is cyclic" << endl;
      		else
      			cout << in << " is not cyclic" << endl;
      	}
      
      
      	system("pause");
      	return 0;
      }
      


      \