您的位置:首页 > 其它

H~Little Sheep and a paper(13.7.11)

2013-07-13 21:06 337 查看
Problem H. Little Sheep and a paper

Input le: stdin

Output le: stdout

Time limit: 2 seconds

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 nds the paper can be folded for in nity times,

and now Sheep thinks the paper is interesting, so he tries to fold the paper in half for many times. He

nds out he has four way to make the paper folded in half, there are listed below:

At rst, 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.

Page 12 of 17

The 7th(2012) ACM Programming Contest of HUST Semilive

HUST ACM-ICPC Team, December 16, 2012

Input

The rst 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 106.

Output

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

Sample input and output


in

4

L

LR

DLUR

ULULL


out

0

1

10

22


这个问题主要是推导出公式(2^i-1)*2^j+(2^j-1)*2^i;通过本公式便可以的到所求折痕的数目,但是直接用公式会很麻烦,此题中提到要取模,所以应该在加上快速幂的模板

代码:

#include<iostream>
#include<string.h>
using namespace std;
const int p=100000009;
const int x=1000001;
char str[x];
long long quitemod(long long a,long long b,long long c)
{
long long ans;
long long x;
if(b==0) return 1;
if(b==1) return a%c;
x = quitemod(a,(b/2),c);
ans = x * x % c;
if ( b % 2 == 1 ) ans = (ans * a) % c;
return ans;
}
int main(){
long long  n,l;
cin>>n;
while(n--)
{
long long r;
cin>>str;
l=strlen(str);
long long m=0,n=0;
int flag;
if(l==1){cout<<"0"<<endl;}
else{
for(int j=0;j<l;j++){
if(str[j]=='L'||str[j]=='R'){m++;}
if(str[j]=='U'||str[j]=='D'){n++;}
}
if(str[0]=='L' || str[0]=='R') flag=1;
else flag=0;
if(!flag) {
int tmp=m;
m=n;
n=tmp;  }
m--;
r=((quitemod(2,m,p)-1)*quitemod(2,n,p)%p+(quitemod(2,n,p)-1)*quitemod(2,m,p)%p)%p;
cout<<r<<endl;}
}
return 0;
}
在取值类型应使用long long ,注意取模的运算规则
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: