ZOJ ACM 1314 (JAVA)

Source: Internet
Author: User

Yesterday did a few topics, too simple, it is not written in the blog.

1314 This problem is also relatively simple, written out because I think there is a small trick, for time complexity and space complexity are more economical.

This topic is similar to the solution of a hash table, but simpler. When I got the title, I took it for granted that I wanted to put the query results in an array, and then traverse the query for the same mod value. But the feeling is certainly the most common method and not too efficient.

Later thought of some other query algorithms, such as two points, but it is not very appropriate to feel. Until I realized that this process is similar to the process of calculating a hash table, so directly using the MoD value as the subscript index of the array, you can directly navigate to whether the current value has been computed, so as to quickly determine whether a good loop.

public class Main {public static void main (String argv[]) {int Step, mod;java.util.scanner Scanner = new Java.util.Scanner (system.in); while (Scanner.hasnext ()) {String strLine = Scanner.nextline (); String strnums[] = Strline.split (""); Step = Integer.parseint (Strnums[0]); Mod = Integer.parseint (strnums[1]), int mods[] = new Int[mod];mods[0] = 0;int seed = 0; String strresult = "good Choice"; for (int i=1;i<mod;i++) {seed = (seed + Step)% mod;if (mods[seed] = = 0 && seed ! = 0) {Mods[seed] = seed;} else {strresult = "bad Choice"; break;}} System.out.format ("%10s", Step); System.out.format ("%10s", Mod); System.out.println ("    " +strresult+ "\ n");}}}


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.