NYOJ 52 boring James [simulation], nyoj52 Simulation
Boring James time limit: 3000 MS | memory limit: 65535 KB difficulty: 3
-
Description
-
On this day, James was very bored and had nothing to do, but he was unwilling to be bored. James cleverly thought of a way to solve the boring problem, because he suddenly became interested in the positive integer power of the logarithm.
As we all know, the last digit of the positive integer power of 2 is always repeating 2, 4, 8, 6, 2, 4, 8, 6 ...... Let's say that the Loop Length of the last digit of the positive integer power of 2 is 4 (in fact, all multiples of 4 can be called the loop length, but we only consider the minimum loop length ). Similarly, the last digit of the positive integer power of other numbers has a similar cycle.
Then James's question came out: is there such a loop only in the last place? For the positive integer power of an integer n, does the last k bit of it have a loop? What is the cycle length if it is a loop?
Note:
1. If the number of digits in a positive integer of n is less than k, the high value of n is regarded as 0.
2. If the Loop Length is L, it means that for any positive integer a, the power of n is the same as the last k bits of the + L power.
-
Input
-
Enter an integer N (0 <n <10) in the first row. Then, each group of test data has only one row, containing two integers n (1 <= n <100000) and k (1 <= k <= 5), n and k are separated by a space, indicating the cycle length of the last k digits of the positive integer power of n.
-
Output
-
The output of each group of test data includes one row. This row contains only one integer, indicating the Loop Length. If the loop does not exist, output-1.
-
Sample Input
-
132 2
-
Sample output
-
4
This is to simulate multiplication, and the knowledge of multiplication of large numbers and struct is required. Up to 1e4;
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int M =1e4;struct node{int s[15];};int main(){int n;cin >> n;while(n --){int m, k;cin >> m >> k;/*if(k == 1){printf()}*/node a;memset(a.s, 0, sizeof(a.s));int i = 1;string st;while(m){a.s[i++] = m%10;//st += (a.s[i-1]+'0');m /= 10;}for(int i = 1; i <= k; ++ i) st += (a.s[i]+'0');//cout << st;int res = 0;node b = a, c;//memset(c.s, 0, sizeof(c.s));string str;while(res < M){memset(c.s, 0, sizeof(c.s));for(int i = 1; i <= k; ++ i){for(int j = 1; j <= k; ++ j){c.s[i+j-1] += a.s[i]*b.s[j];c.s[i+j] += c.s[i+j-1]/10;c.s[i+j-1] %= 10;}/*str += (c.s[i]+'0');cout << str;*/}for(int i = 1; i <= k; ++ i) str += (c.s[i]+'0');++res;if(st == str) break;str.clear();b = c;}if(res == M) cout << -1 <<endl;else cout <<res << endl;}return 0;}