Problem description
Xiao Ming first put the coin into a matrix of n rows M column.
Xiao Ming then performed a Q operation on each coin separately.
The definition of the Q operation for the coin in row y of line x: Turn all the coins in line i*x, section j*y.
Where I and J are arbitrary to make the operation feasible positive integer, line number and column number are starting from 1.
When Xiaoming performed a Q operation on all the coins, he found a miracle-all the coins were facing upwards.
Xiaoming wanted to know how many coins were on the opposite side of the coin at the beginning. So he sought help from his good friend, Little M.
The clever little m tells Xiaoming that it takes only one more Q operation for all the coins to return to the very first state. However, Xiao Ming is very lazy and unwilling to do it. So Xiaoming wants you to give him a better way. Help him figure out the answer.
Input format
The input data contains one row, two positive integers n m, meaning the topic description.
Output format
Outputs a positive integer that indicates how many coins are at the beginning of the opposite direction.
Sample input
2 3
Sample output
1
Data size and conventions
For 10% of data, N, M <= 10^3;
For 20% of data, N, M <= 10^7;
For 40% of data, N, M <= 10^15;
For 10% of data, N, M <= 10^1000 (10 of 1000).
Each change is a change: flip all the coins in i*x row, section j*y.
When the value changes to 10 of the 1000-square is not counted, so the introduction of the law:
Look at the case of n = 1: for (1, m), just look at the number of times it flips to determine its final state. Because x = 1, each time the first line is involved in flipping, when y can divide M, (1, m) will Flip, (1, m) the total process of flipping the number of times depends on the number of M, 1 of the number is 1, 3 is about 2, 5 is about 2, and 9 is about 3. When m = k^2 (k = 1, 2, 3) Its approximate number is an odd number, otherwise its approximate number is even. Because the average number of numbers is in pairs, and a number of squares, there are two of equal numbers.
So, finally (1, m) m = k^2 (k = 1, 2, 3) The final state is 0, the other is 1.
And the sum of the last 0 counts count = sqrt (m), rounding.
Then look at the general situation: (n, m) what the last state is. Now the change of line is also the factor that it flips. Easy to roll out from above, when M is determined, his number of flips is about N. and (n, m) the number of flips = (approximately several numbers of n * m). Just now, when the number of (n, m) flips is odd, its final state is 0. And only odd * odd = odd, so the approximate number of N, M must be an odd number, namely: N = k^2 (k = 1, 2, 3) and m = j^2 (j = 1, 2, 3).
Finally, the conclusion
is: for n-row m-column matrices, the number of tails after Q operation is count = sqrt (n) * sqrt (M), (rounded and multiplied).
finally found the formula, but there is a new problem, how to the 1000-digit number. The theorem is given here:
Suppose that the number of bits is Len's integer, and the root is rounded to a lensqrt bit.
when Len is an even number, lensqrt = LEN/2.
When Len is odd, lensqrt = (LEN/2) + 1.
The
proves to be simple, and there is no evidence of it. The
is now simple, with the number of bits determined from the high to the low one by one. For example: sqrt (1028), indicating that the 1028 root rounding
it to take the whole after the two digits. First:
Take 0, xx < 1028 so sqrt (1028) > XX
Take 1, ten * < 1 028 so sqrt (1028) >
Take 2, * < 1028 so sqrt (1028) >
3, * < 1028 so sqrt (1028) > 30
Take 4, sqrt, > 1028 so the first bit takes 3, so the 1028 < 40.
Second bit:
Take 0, * < 1028 so sqrt (1028) >
Take 1, * ~ ~ 1028 so sqrt (1028) >
Take 2, 32 * < 1028 so sqrt (1028) > 1028
Take 3, ~ ~ ~ 1028, so sqrt (1028) < 33, so sqrt (32) =.
#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> using namespace
Std
String Strmultiply (String str1, String str2)//large number multiplied by {string strresult = "";
int len1 = Str1.length ();
int len2 = Str2.length ();
int num[500] = {0};
int i = 0, j = 0; for (i = 0; i < len1; i++) {for (j = 0; J < Len2; J + +) {num[len1-1-i + len2-1-j]
+ = (str1[i]-' 0 ') * (Str2[j]-' 0 ');
}} for (i = 0; i < len1 + len2; i++) {num[i+1] + = num[i]/10;
Num[i] = num[i]% 10;
} for (i = len1 + len2-1, i >= 0; i--) {if (num[i]!=0) break;
} for (j = i; j >= 0; j--) {strresult + = Num[j] + ' 0 ';
} return strresult;
} int Compare (string str1, String str2, int pos)//Large number comparison, followed by 10 pos when {int len1 = Str1.length ();
int len2 = Str2.length ();
if (Len2 > Len1+pos) return 0;
if (Len2 < Len1+pos) return 1; int i = 0;
for (i = 0; i < len2; i++) {if (str1[i]-' 0 ' > str2[i]-' 0 ') return 1;
if (str1[i]-' 0 ' < str2[i]-' 0 ') return 0;
} return 0;
} string Sqrtbignum (string s)//For large number of roots {string strresult= "";
String s1= "";
int Len=s.length ();
int i,j; if (!len&1) for (i=0; i<len/2; i++) for (j=0; j<10; J + +) {S1=strre
Sult;
s1+=j+ ' 0 ';
if (Compare (Strmultiply (S1,S1), s,2* (len/2-i-1)) ==1) {strresult+=j-1+ ' 0 ';
Break
} if (j==9) strresult+= ' 9 '; } else {for (i=0; i<len/2+1; i++) for (j=0; j<10; J + +) {S1=s
Trresult;
s1+=j+ ' 0 ';
if (Compare (Strmultiply (S1,S1), s,2* (len/2-i)) ==1) {strresult+=j-1+ ' 0 '; BreaK
} if (j==9) strresult+= ' 9 ';
}} return strresult;
} int Main () {string s1,s2;
String strresult;
cin>>s1>>s2;
Cout<<strmultiply (Sqrtbignum (S1), Sqrtbignum (S2)) <<endl;
return 0;
}
Go from: http://blog.csdn.net/snailset/article/details/26752435