Little Sheep and a paper解题报告

来源:互联网 发布:php防注入代码 编辑:程序博客网 时间:2024/05/22 04:24

题目摘要:One day, Little Sheep gets anAK(all kill) in a contest, so he is very boring, then he just plays with a paper.The paper has two faces called A and B. Sheep _nds the paper can be folded forin_nity times,and now Sheep thinks the paper is interesting, so he tries tofold the paper in half for many times. He finds out he has four way to make thepaper folded in half, there are listed below:


At first, Little Sheep keeps the face A ofthe paper faced to him,and he fold the paper for many times.In the end, Sheepopens up the paper, and keeps the face A faced to him again. Now the questionis :How many creases on the face A of the paper are protruding? God sheepsolves the question without 2 seconds. Can you? You should make your answer mod100000009.

题目大意:折纸游戏,按照规定,有四种折纸方式,给出一串折纸方式,求纸面A的凸起折痕数。

输入输出要求

Input

The first line of input contains a singlepositive integer N , which means the number of test cases.

The following N lines gives N non-emptystrings containing only characters in "UDLR", which is the sequencesof the actions of the Sheep to fold paper. The length of the each string is apositive number less than 106.

 

Output

For each case output the answer mod100000009 in a line.

输入输出样例

Sample input

4

L

LR

DLUR

ULULL

 

Sample output

0

1

10

22

解题思路:根据纸被折成的份数来解决问题,假设竖着折,折一下面被分成2份,凸起为0,折两下面被分成4份,凸起为1,即2的1次方减一。折三下面被分成8份,凸起为3,即2的2次方减一。在竖着折的同时,每横着折一次,竖着的折痕翻倍。横着折同理。

代码

#include<iostream>

#include<cstring>

using namespace std;

 

const int maxn=1000000+5;

const int mod=100000009;

char str[maxn];

int main()

{

       intN;

       cin>>N;

       while(N--)

       {

              cin>>str;

              longint h=0,v=0;

              intm=1;

              for(inti=0;i<strlen(str);i++)

              {

                     if((str[i]=='R')||(str[i]=='L'))

                     {

                            h=(h+m/2)%mod;

                            v=2*v%mod;

                     }

                     else

                     {

                            v=(v+m/2)%mod;

                            h=2*h%mod;

                     }

                     m=2*m;

                     m=m%(2*mod);

              }

              cout<<(h+v)%mod<<endl;

       }

       return0;

}

解题感想:开始套公式算,测试样例都过了,就是WA,不知道怎么回事,算次方的时候用的是快速幂,时间绝对不会超时的,后来标程和数据出来,测试太大的数据就会出错,但还是不知道错在哪,到后来不得不换思路。