Codeforces H. Malek Dance Club(找规律)
题目描述:
Malek Dance Club
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidentally Natalia Fan Club also has 2n members. Each member of MDC is assigned a unique id i from 0 to 2n - 1. The same holds for each member of NFC.
One of the parts of this tradition is one by one dance, where each member of MDC dances with a member of NFC. A dance pair is a pair of numbers (a, b) such that member a from MDC dances with member b from NFC.
The complexity of a pairs' assignment is the number of pairs of dancing pairs (a, b) and (c, d) such that a < c and b > d.
You are given a binary number of length n named x. We know that member i from MDC dances with member from NFC. Your task is to calculate the complexity of this assignment modulo 1000000007 (109 + 7).
Expression denotes applying «XOR» to numbers x and y. This operation exists in all modern programming languages, for example, in C++ and Java it denotes as «^», in Pascal — «xor».
Input
The first line of input contains a binary number x of lenght n, (1 ≤ n ≤ 100).
This number may contain leading zeros.
Output
Print the complexity of the given dance assignent modulo 1000000007 (109 + 7).
Examples
Input
Copy
11
Output
Copy
6
Input
Copy
01
Output
Copy
2
Input
Copy
1
Output
Copy
1
思路:
题目意思是给一个长度为n的01字符串x,然后将0到n-1的数与x做亦或,映射到另一组数,求这个形成的键值对的复杂度,根据复杂度定义,我们知道(a,b)与(c,d),当a<c&&b>d时算一个复杂度,由于我们是从小到大枚举键的,满足复杂度的第一个条件,只要再满足值是逆序的就可以了,题目就转换成了求有多少个逆序对。
我们可以来找一下规律:
n=3时,有
x=000
000=>000
001=>001
...
111=>111,值的逆序对为0,所以复杂度为零
x=001
000=>011
001=>000
010=>011
011=>010
...
111=>110,值的逆序对有4个,所以复杂度是四
以此类推,最终得到:\(a_0=0,a_1=4,a_2=8,...,a_7=28\),即\(a_x=4x\).
同理,n=2时有\(a_x=2x\).最后有:\(a_x=2^{n-1}x\).
由于是大数,需要用到快速幂和及时取余。
代码:
#include <iostream>
#include <string>
#define m 1000000007
using namespace std;
int n;
string s;
long long convert(string s)
{
long long ans = 0;
long long weight = 1;
for(int i = s.size()-1;i>=0;i--)
{
if(s[i]=='1')
{
ans = (ans+weight)%m;
}
weight = (weight%m*2)%m;
}
return ans;
}
long long q_mod(long long a,long long b,long long mod)
{
long long sum = 1;
while(b)
{
if(b&1)
{
sum = (sum%mod*a%mod)%mod;
}
a = (a%mod*a%mod)%mod;
b >>= 1;
}
return sum;
}
int main()
{
//cout << q_mod(2,3,m) << endl;
cin >> s;
n = s.size();
long long ans = convert(s);
ans = (ans%m*q_mod(2,n-1,m))%m;
cout << ans << endl;
}