Problem F: Little Sheep and a paper
Time Limit: 2 Sec Memory Limit:128 MBDescription
One day, Little 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, Little 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 answer 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 answer mod 100000009 in a line.
Sample Input
4
L
LR
DLUR
ULULL
Sample Output
0
1
10
22
----------------------------------------------------------------------------------------------------------
解题报告:
暂缓
----------------------------------------------------------------------------------------------------------
代码:
#include<stdio.h>
#include<string.h>
const long MOD=100000009;
long long power(long long a,long b)
{
if( !b ) return 1;
if( 1==b ) return a%MOD;
long long x = power(a,b>>1);
long long ans = x*x%MOD;
if( b&1 ) ans = a*ans%MOD;
return ans;
}
int main()
{
long n,i,x,y;
long long sumx,sumy;
char ch[1000005];
scanf("%ld",&n);
while( n-- )
{
scanf("%s",ch);
for(x=y=0,i=1;ch[i];i++)
{
if( 'D'==ch[i] || 'U'==ch[i] ) y++;
else x++;
}
sumx=power(2,x)%MOD;
sumy=power(2,y)%MOD;
printf("%lld\n",((sumx-1)*sumy%MOD+(sumy-1)*sumx%MOD)%MOD);
}
return 0;
}