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:
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
这个题其实是一个找规律的题,弄清楚折纸后凸出来的折痕的规律,就是一个十分简单的题了。
折了好多纸之后,很快就发现了规律。解题的关键就是分开计算横折和竖折的次数。
首先,第一次折纸,无论怎么折,凸出的折痕都是没有的,凸出折痕数都是0,所以不计数。
然后,左折和右折、上折和下折是没有区别的,所以只横折或者只竖折仅和折的次数有关。之后再竖折或者横折不影响总数,分开计算横折(h)和竖折(s)的次数就行了。
我们,假设先竖折,可以很轻松的找到规律,凸出的折痕为sum=2^(s-1)。
接着是横折,每横折一次,竖折产生的凸出折痕数就会翻一倍,所以每横折一次,sum=sum*2。
竖折会把纸张分成2^s块。
横折产生的凸出折痕数是竖折分成块数的一半,然后乘以横折产生的折痕数就行了。
#include<iostream>
#include<stdio.h>
using namespace std;
#define M 100000009;
char z[1000010];
long long pow(int a,int b)
{
long long r=1,base=a;
while(b!=0)
{
if(b&1)
r=r*base%M;
base=base*base%M;
b>>=1;
}
return r;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,i;
int l,s;
int sum;
scanf("%d",&n);
while(n--)
{
l=0;s=0;sum=0;
scanf("%s",&z);
for(i=1;z[i];i++)
{
if(z[i]=='L'||z[i]=='R')
{
l++;
}
if(z[i]=='U'||z[i]=='D')
{
s++;
}
}
if(s+l==0)
{
printf("0\n");
}
else
if(s+l==1)
{
printf("1\n");
}
else
{
if(l!=0)
{
sum=int(sum+pow(2,l)-1)%M;
for(i=1;i<=s;i++)
{
sum=(sum*2)%M;
}
sum=sum+pow(2,l)*(pow(2,s)-1)%M;
}
else
{
sum=int(sum+pow(2,s)-1)%M;
}
sum=sum%M;
printf("%d\n",sum);
}
}
return 0;
}