欢迎访问我的个人网站==》 jiashubing.cn

HUST 1614 Little Sheep and a paper(数学题)

Little Sheep and a paper

Time Limit: 2 Sec  Memory Limit: 128 MB
Submissions: 95  Solved: 30

Description

       One day, god Sheep gets an AK(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 finds the paper can be folded for infinity times, and now Sheep thinks the paper is interesting, so he tries to fold the paper in half for many times. He finds out he has four way to make the paper folded in half, there are listed below:
 
图片请见 PDF版题目。 


At first, god Sheep keeps the face A of the paper faced to him,and he fold the paper for many times. In the end, Sheep opens up the paper, and keeps the face A faced to him again. Now the question is : How many creases on the face A of the paper are protruding? God sheep solves the question without 2 seconds. Can you?  You should make your anwser mod 100000009.

Input

       The first line of input contains a single positive integer N , which means the number of test cases. The following N lines gives N non-empty strings containing only characters in "UDLR", which is the sequences of the actions of the Sheep to fold paper. The length of the each string is a positive number less than 10^6.

Output

       For each case output the anwser mod 100000009 in a line.

Sample Input

4
L
LR
DLUR
ULULL

Sample Output

0
1
10
22


题目大意:给一张纸A面向上,可上下左右折叠,折完后展开,输出A面外凸折痕的条数;

以左右折叠为例,每一次向左或者向右折叠一次,竖着的折痕里边有对折后的纸总块数一半的折痕会凸出来(每一次对折纸的总块数增加了1倍),横着的折痕里边是已经凸出来的折痕的两倍(这是因为横着的这横数变成了原来的2倍)

 

 1 # include<stdio.h>
 2 # include<string.h>
 3 # include<math.h>
 4 # define mod 100000009
 5 
 6 int main()
 7 {
 8     int T,len,i;
 9     long int a,b,tmp; 
10     //a,b分别表示横着、竖着凸出的折痕,tmp表示总块数
11     char s[1000005];
12     scanf("%d",&T);
13     while(T--){
14         scanf("%s",s);
15         len=strlen(s);
16         a=b=0; tmp=1;
17         for(i=0;i<len;i++)
18         {
19             if(s[i]=='L' || s[i]=='R'){
20                 b=(b+tmp/2)%mod;
21                 a=(a*2)%mod;
22             }
23             else
24             {
25                 a=(a+tmp/2)%mod;
26                 b=(b*2)%mod;                
27             }
28             tmp *= 2;
29             tmp = tmp%(2*mod);    //注意是2倍的
30         }
31         printf("%lld\n",(a+b)%mod);
32     }
33     return 0;
34 }

 

 
posted @ 2013-03-17 13:27  贾树丙  阅读(317)  评论(0编辑  收藏  举报