Backward Digit Sums (POJ-3187)

Source: Internet
Author: User

Would have thought it would time out, it seems to think more, 63ms, it seems that the ability to judge the complexity of time or not AH. This question is exactly where the next_permutation () function comes in.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < Queue> #include <cmath>using namespace Std;int n,sum,a[100],a[100];int main () {    scanf ("%d%d", &n, &sum);    for (int i=0;i<n;i++) a[i] = i+1;    Do {for        (int i=0;i<n;i++) a[i] = A[i];        int cur = n;        bool OK = false;        if (n==1) {printf ("1\n"); return 0;}        while (true) {for            (int i=0;i<cur-1;i++) a[i] = a[i]+a[i+1];            if (cur==2) {                if (a[0]==sum) ok = true;                break;            }            cur--;        }        if (OK) {            printf ("%d", a[0]);            for (int i=1;i<n;i++) printf ("%d", A[i]);            printf ("\ n");            break;        }    } while (Next_permutation (a,a+n));    return 0;}


Backward Digit Sums (POJ-3187)

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.