! HDU 4346 Flag Rgr How many kinds of interpolation-thinking & is difficult to reverse & thinking!

Source: Internet
Author: User

Test instructions: Only the R and G two flags, beautiful interpolation is the existence of two r of the midpoint interpolation G, now give you a string to represent now some places have been inserted, to find the beauty of how many kinds of interpolation

Analysis:

Beauty = Total-not beautiful

So we come to find out how many kinds of beauty there are.

Here is the question of thinking: an not-so-beautiful sequence must satisfy the condition that any two adjacent r is an odd distance away from each other and that all neighboring r distances are equal.

Odd this is obvious, if two adjacent R is not an odd number, then their midpoint exists, and is G, which contradicts with the not beautiful;

Distance is also very good proof: set three adjacent r,r1r2 distance of X1,R2R3 is x2, then r1r3=x1+x2, because x1,x2 are odd, so their and for even, then R1R3 midpoint exists, if x1! =X2, then that is to say R1R3 between the only R is not their midpoint, then fill their midpoint must be G, which is not beautiful contradiction. The evidence is fully integrated.

The next step is enumeration! Enumerates the start and distance of R. Note The enumeration process, special cases to be discussed.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <string>using namespace Std;const long long inf=1000000007;int T;char a[1000];int main () {scanf ( "%d", &t), while (t--) {        cin>>a;int len=strlen (a); int rall=0,num=0;for (int i=0;i<len;i++) {if (a[i]== ') R ') rall++;if (a[i]== '? ') num++;} Long Long ans=0;if (rall==0) Ans=1; Special Case 1, all gint ok=1;for (int i=0;i<len;i++) {if (a[i]== ' R ') ok=0;if (a[i]!= ' G ') {    if (rall==0) ans= (ans+1)%inf; Special Case 2, only one Rif (rall==1&&a[i]== ' R ') ans= (ans+1)%inf;  Special Case 2, only one rfor (int j=1;j+i<len;j+=2) {int r=0;if (a[i]== ' R ') r=1;for (int k=i+j;k<len;k+=j) {if (a[k]== ' G ') Break;if (a[k]== ' R ') r++;if (R==rall) ans= (ans+1)%inf;}}    } if (ok==0) break;        Note that when the starting point of an enumeration is an already existing R, the end enumeration}long long tmp=1;for (int i=0;i<num;++i) tmp= (2*tmp)%inf;ans= (Tmp-ans+inf) with this r as the starting point to run on one side %inf;printf ("%i64d\n", ans);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! HDU 4346 Flag Rgr How many kinds of interpolation-thinking & is difficult to reverse & thinking!

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.