Matrix Flip Coin

Source: Internet
Author: User
Tags ming

Test Instructions
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).

Thinking of solving problems
Mostly large numbers are then processed as strings.
Test instructions requires the number of the opposite side of the coin, that is, to seek to turn the odd number of coins, but odd-and odd-odd, if the position of the coin is (i,j), it is required that I and j of the approximate number of odd numbers; however, if the number is odd, then the number is the total square number, Then the problem can be converted to sqrt (m) *sqrt (n).

Code Implementation

#include <bits/stdc++.h> using namespace std;
String strsqrt (String str);
String Multiply (string str1,string str2);
int compare (string str1,string tostr,int pos);
    String strsqrt (String str) {int length=str.size ().
    String Str1= "", str2= "";
            for (int i=0;i< (length+1)/2;i++) {for (int j=0;j<=9;j++) {str1=str2;
            str1+=j+ ' 0 ';
                if (compare (multiply (STR1,STR1), str,2* ((length+1)/2-i-1)) {str2+=j-1+ ' 0 ';
            Break
        } if (j==9) str2+= ' 9 ';
}} return str2;
    }//String multiplier function string Multiply (string str1,string str2) {int i,j;
    String Tostr= "";
    int length1,length2;
    int result[1000]={0};
    Length1=str1.size (), length2=str2.size (); for (i=length1-1;i>=0;i--) {for (j=length2-1;j>=0;j--) {result[i+j+1]+= (str1[i]-' 0 ') * (str2[j]-' 0 ')
        ); }} for (i=length1+length2-1;i>=1;i--) {result[i-1]+=rESULT[I]/10;
    result[i]=result[i]%10;
    } for (i=0;result[i]==0;i++);
    for (j=i;j<length1+length2;j++) {tostr+=result[j]+ ' 0 ';
} return TOSTR;
    }//string comparison function int Compare (string str1,string tostr,int pos) {int length1=str1.size ();
    int length2=tostr.size ();
    if (Length2>length1+pos) return 0;
    if (Length2<length1+pos) return 1;
        for (int i=0;i<length2;i++) {if (str1[i]-' 0 ' <tostr[i]-' 0 ') return 0;
    if (str1[i]-' 0 ' >tostr[i]-' 0 ') return 1;
    }} int main (int argc, char *argv[]) {string n,m;
    cin>>n>>m;
    Cout<<multiply (STRSQRT (n), Strsqrt (m)) <<endl;
return 0;
 }
Related Article

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.