[2016-03-03] [UVA] [1374] [Power calculus]

Source: Internet
Author: User
Tags cmath

[2016-03-03] [UVA] [1374] [Power calculus]
  • Time: 2016-03-03-16:14:01 Thursday
  • Topic Number: UVA 1374
  • The main topic: Give x exponent N, ask, x after how many times to get x^n
  • Input: N
  • Output: Number of times
  • Analysis:
      • Find all possible ways to multiply, use DFS, proper pruning optimization, and become ida*
      • The multiplication of x becomes the addition of the exponent
      • Each time the square, the minimum number of times is the number of squares. This for Maxd beginnings
  • Method:
      • Enumerates the number of occurrences of each bit, the current word count, plus the number of times enumerated, into the next layer of Dfs
      • Pruning: return 0 if expected minimum number of times plus current count > Maxd
  • Problems encountered during the problem solving process:
      • There is no need to take the maximum value to determine the remaining, the direct current value is continuously squared (exponential addition) 1<< (maxd-cur-1) times can be

#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; typedef Long long LL; #define The CLR (x , Y) memset ((×), (y),sizeof((x))) #define for (x, Y, z) for ( int.) = (y);(x) < (*); + + (x)) #define FORD ( x, Y, z) for(int.) = (y);(x) >= (z);--(x)) #define FOR2 ( x, Y, z) for((×) = (y);(x) < (z); + + (x) )#define FORD2 ( x, Y, z) for((>=) = (y);(x) (z);--(x) )
const int MAXN = + +; int N,ARR[MAXN * 2]; Arr saves the index that has occurred int dfs (int cur,int curd,const int & maxd) { if (curd = = Maxd) return arr[cur-1] = = N; //The current value can be up to square (1<< (Maxd-curd)) times, after calculating so many times, is less than n if (arr[cur-1] * 1<< (Maxd-curd) < n) return 0; //Enumerate indices that have occurred FORD (i,cur-1,0) { //Generate new value, go to next layer Arr[cur] = arr[cur-1] + arr[i]; if (DFS (cur + 1,curd + 1, maxd)) return 1;Arr[cur] = arr[cur-1]-arr[i]; if (DFS (cur + 1,curd + 1, maxd)) return 1;        } return 0;}int init () { int tmp = n,cnt = 0;While (tmp > 1) {tmp/= 2;++cnt;        } return cnt;}int Main () { //freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); while (~scanf ("%d", &n) && N) {For (int i = init ();; ++i) {arr[0] = 1; if(Dfs (1,0,i)) {printf ("%d\n", i); break;                         }                }                      } return 0;}





From for notes (Wiz)

[2016-03-03] [UVA] [1374] [Power calculus]

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.