H~Little Sheep and a paper(13.7.11)

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 ,注意取模的运算规则


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值