HPU1288 matrix Flip Coin "large number"

Source: Internet
Author: User

1288: Matrix coin time limit: ten Sec memory limit: MB
Submissions: 1 Resolution: 1
Submitted State [Discussion Version] [Edit] Title Description

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

Multiple sets of inputs, two positive integers per line n m, where n, M <= 10^1000 (10 of 1000), meaning see topic description.

Output

Outputs a positive integer that indicates how many coins are at the beginning of the opposite direction.

Sample input2 3Sample output1first consider a one-dimensional situation, if the number of x is reversed odd number of times, then its approximate number must be odd, and only the number of squares is an odd number, then that is, for one-dimensional case only X is the square number when it is flipped odd number of times, for the two-dimensional case (x, y) point is flipped is an approximate The approximate number of Y for this point is flipped, only when this number is odd, the point is flipped odd times, and odd * odd = = odd, that is, X and y must all be squared, then the answer comes out, an n-row m-column matrix has sqrt (n) squared on the row and a sqrt (m) squared number on the column , the combined square point pair is altogether sqrt (n) *sqrt (m). Because it is a large number, it needs to be stored with a string, large number multiplication and large number of direct use template.

#include <iostream> #include <string> using namespace std;     /* Large number multiplied by */string operator * (String str1, String str2) {string result = "";     int len1 = Str1.length ();     int len2 = Str2.length (); int num[1002] = {0};       This is used to temporarily save the result of two-number multiplication int I, J; if (!len1 | |!len2) return "0"; Handle NULL pointer 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]%= 10;     } for (i = len1 + len2-1;!num[i];-I.);           for (; I >= 0; i.) result + = Num[i] + ' 0 ';     if (result.length () = = 0) result = "0"; return result;     } bool Str_bigger (string str1, string str2, int zeros) {int len1 = Str1.length ();     int len2 = Str2.length ();       int I, J;     if (len1 + zeros < LEN2) return false;       else if (len1 + zeros > Len2) return true; For (i = 0; i < len1; ++i)         {if (Str1[i] > Str2[i]) return true;     else if (Str1[i] < Str2[i]) return false; } return false;     }/* Returns the rounding result of sqrt (n) */string sqrt_large (string str) {int len = str.length ();     int I, j, len1 = Len >> 1;       String str1 = ""; if (Len & 1) ++len1;         The length is odd, for example 121=11*11 for (i = 0; i < len1; ++i) {str1 + = ' 0 '; for (j = 0; J < ++j) {Str1[i] = j + ' 0 ';//attention//The third parameter is the end of the first string, 0 numbers if (s             Tr_bigger (STR1 * str1, str, (len1-(i+1)) << 1)) {--str1[i]; }}} return str1;     } int main () {//Freopen ("Stdin.txt", "R", stdin);     Freopen ("Stdout.txt", "w", stdout);     String A, B;     while (Cin >> a >> b) {cout << sqrt_large (a) * Sqrt_large (b) << Endl; } return 0;  }


HPU1288 matrix Flip Coin "large number"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.