Uvalive 3621 power calculus n Power at least several times multiplication to get a search

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

Enter an n (n<=1000)

Q: There is an X that requires at least a few multiplication to figure out x^n.

Ideas:

Memory Search

A[i] = J indicates that I power needs J step to get

Import Java.io.printwriter;import java.util.arraylist;import Java.util.arrays;import Java.util.Collections;import Java.util.comparator;import Java.util.iterator;import Java.util.linkedlist;import Java.util.PriorityQueue;import Java.util.scanner;import Java.util.treeset;import Java.util.queue;public class Main {static int N = 1000050;int[] A = new  Int[n];boolean dfs (int N, int num, int step) {//can be obtained N times with step step, the current number of steps is num step if (num > Step) return false;if (a[num] = = N) return True;if ((A[num] << (step-num)) < n)//always square up to Nreturn false;for (int i = 0; I <= num; i++) {a[num+1] = a[num]+a[  I];if (A[num+1] <= 10000 && DFS (n,num+1,step)) return true;a[num+1] = A[num]-a[i];if (a[num+1] > 0 && DFS (n,num+1, step)) return true;} return false;} void work () {when (true) {int n = cin.nextint (); if (n = = 0) break;int i;for (i = 0;; i++) {a[0] = 1;if (DFS (n, 0, I)) break; Out.println (i);}} Main () {cin = new Scanner (system.in); out = new PrintWriter (System.out);} public static void Main (string[] args) {main E = new Main (); E.work (); Out.close ();} Public Scanner cin;public static printwriter out;}


Uvalive 3621 power calculus n Power at least several times multiplication to get a search

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.