Algorithm Note 168: Previous question matrix flip coin (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

 1 problem description Problem DescriptionXiao 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 FormatThe input data contains one row, two positive integers n m, meaning the topic description. output Formatoutputs a positive integer that indicates how many coins are at the beginning of the opposite direction. Sample Input2 3Sample Output1data size and conventionsfor 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).

2 Solutions

The specific code is as follows:

ImportJava.math.BigInteger;ImportJava.util.Scanner; Public classMain { Public StaticBigInteger getsqrt (String A) {string sqrt= "0"; String Pre= "0"; BigInteger Twenty=NewBigInteger ("20"); BigInteger Temp1=Biginteger.zero; BigInteger Temp2=Biginteger.zero; intLen =a.length (); if(len% 2 = = 1) {A= "0" +A; Len= len + 1; }         for(inti = 0;i < len/2;i++) {BigInteger TEMPN=NewBigInteger (pre + a.substring (i*2, i*2 + 2));  for(intj = 0;j <= 9;j++) {BigInteger TEMPJ=NewBigInteger (j+ ""); Temp1= Twenty.multiply (NewBigInteger (sqrt)). Add (TEMPJ). Multiply (TEMPJ); TEMPJ=Tempj.add (Biginteger.one); Temp2= Twenty.multiply (NewBigInteger (sqrt)). Add (TEMPJ). Multiply (TEMPJ); if(Temp1.compareto (TEMPN) <= 0 && Temp2.compareto (TEMPN) > 0) {sqrt= sqrt +J; Pre=tempn.subtract (TEMP1). toString ();  Break; }            }        }        return NewBigInteger (sqrt); }         Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); String N=In.next (); String m=In.next (); BigInteger result=getsqrt (n). Multiply (Getsqrt (m));    SYSTEM.OUT.PRINTLN (result); }}

Algorithm Note 168: Previous question matrix flip coin (Java)

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.