Zoj 3929 Deque and Balls (recursive to think)

Source: Internet
Author: User

Ideas for reference from: http://blog.csdn.net/doris1104/article/details/51126910 thanks

Find out the pattern of the last ball adjacent to the front ball (to satisfy the left greater than the right) and 2^ two ball spacing related

-----> The closer the number of neighbors.

1#include <cstdio>2#include <cstring>3#include <cstdlib>4 5#include <string>6#include <algorithm>7#include <Set>8#include <map>9#include <vector>Ten#include <stack> One#include <queue> A#include <cmath> -  -#include <iostream> the using namespacestd; -  -typedefLong Longll; - Const intMoD =1000000007; +ll ans[100005],two[100005],num[100005],sum[100005]; -  + /* A Num[a]: Used to save the number of times a needs to be subtracted at num[tmp] = (Num[tmp] + two[i-1])% MoD; the closer the number of neighbors, the more you subtract if the two numbers are the same -      - *sum[i] = (Sum[i-1] + two[i])% MoD; - Situation and -                  - Ans[i] = (2*ans[i-1] + sum[i-2]-num[tmp] + MoD)% MoD; in The previous situation and the addition of this - Both ends can be inserted to Previous conditions + */ -  the voidinit () { *two[0] = two[1] =1; $      for(inti =2; I <100005; i + +){Panax NotoginsengTwo[i] = (2*two[i-1])%MoD; -     } thesum[0] =1; +      for(inti =1; I <100005; i + +){ ASum[i] = (sum[i-1] + two[i])%MoD; the     } +      - } $  $ intMain () { -      -     intT; the init (); -scanf"%d",&T);Wuyi      while(t--){ the         intN; -scanf"%d",&n); Wumemset (ans,0,sizeof(ans)); -memset (NUM,0,sizeof(num));  About          for(inti =1, TMP; I <= N; i + +){ $scanf"%d",&tmp); -Ans[i] = (2*ans[i-1] + sum[i-2]-num[tmp] + MoD)%MoD; -NUM[TMP] = (Num[tmp] + two[i-1]) %MoD; -         } Aprintf"%lld\n", (ans[n]*2)%MoD); +     } the      -     return 0; $}

Zoj 3929 Deque and Balls (recursive to think)

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.