Problem B: Dating With Girls
Time Limit: 2 Sec Memory Limit: 128 MB
Submit: 64 Solved: 23[Submit][Status][Web Board]
Description
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:
![](http://ww2.sinaimg.cn/mw690/8b64fa07jw1e6fsfez34mj20fh0p30te.jpg)
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
4LLRDLURULULL
Sample Output
011022
HINT
/*以左右折叠为例,每一次向左或者向右折叠一次,
竖着的折痕里边有对折后的纸总块数一半的折痕会凸出来
(每一次对折纸的总块数增加了1倍),
横着的折痕里边是已经凸出来的折痕的两倍
(这是因为横着的这横数变成了原来的2倍)
*/
代码:
# include<stdio.h>
# include<string.h>
# include<math.h>
const int mod 100000009
int main()
{
int T,len,i;
long long a,b,tmp;
//a,b分别表示横着、竖着凸出的折痕,tmp表示总块数
char s[1000005];
scanf("%d",&T);
while(T--){
scanf("%s",s);
len=strlen(s);
a=b=0; tmp=1;
for(i=0;i<len;i++)
{
if(s[i]=='L' || s[i]=='R'){
b=(b+tmp/2)%mod;
a=(a*2)%mod;
}
else
{
a=(a+tmp/2)%mod;
b=(b*2)%mod;
}
tmp *= 2;
tmp = tmp%(2*mod); //注意是2倍的
}
printf("%lld\n",(a+b)%mod);
}
return 0;
}
Problem G: Matrix
Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 88 Solved: 15
[ Submit][ Status][ Web Board]
Description
To efficient calculate the multiplication of a sparse matrix is very useful in industrial filed. Let’s consider
this problem:
A is an N*N matrix which only contains 0 or 1. And we want to know the result of AA
T.
Formally, we define B = AA
T, Aij is equal to 1 or 0, and we know the number of 1 in matrix A is M
and your task is to calculate B.
Input
The input contains several test cases. The first line of input contains a integer C indicating the number
of the cases.
For each test case, the first line contains two integer N and M.
and each of next M lines contains two integer X and Y , which means Axyis 1.
N ≤ 100000, M ≤ 1000, C ≤ 10
Output
For each test case, it should have a integer W indicating how many element in Matrix B isn’t zero in one
line.
Sample Input
2
5 3
1 0
2 1
3 3
3 3
0 0
1 0
2 0
Sample Output
3
9
几乎全搬套用