题目传送
题意:
就是求那个函数的值
思路:
首先我们可以看到,ai 都是c的幂次方
那么每一个ai的值有什么规律呢?
其实题目已经给出提示了,n是由二进制给出的。那么我们给a这个数组的前n项和他的下标二进制比较,那么我们可以看到:下标的二进制中1的数量有多少(num),那么a[i] = cnum。
那么规律找出来了,接下来就是组合数学问题了。就是在给定的n中,选多少个1和多少个0的问题。但是不能超过n
假如n = 100010100011001(二进制表示),我们怎么组合数学呢?
我们先假设从左向右下标依次为0~14.那么我们先预处理哪几个1的位置,然后我们再组合
那么第一个1(也就是下标0的位置),我们可以选和不选这个位置的1。
1.我们先不选这个位置的1,那么这个位置后面的数可以全部选(不会大于n),那么直接组合数学在13个数中选0个,选1个,选2个…
2.当我们选了这个数的时候,那么这个数后一位肯定不能选(因为后面那一位是0,选了就必定大于n了)。我们只能选择这个数的下一个1(也就是下标4的位置),那么同理这个数也可以选或者不选。继续进行步骤1即可。
3.需要注意的是:我们需要预处理组合数学所需要的的阶乘,而在阶乘中,我们又需要逆元,所以还要预处理逆元,还有我们还要预处理c的次方。不然都会t掉(就算加了快速幂,在多次运算下,也会t掉。盲估计一波,快速幂时间复杂度为logn ,在排列组合时,最坏复杂度为nlogn,还是很可能会t的)
AC代码
#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-5;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
ll ans[3050],c,arr[3050],m,sum,Mod[3050],C[3050];
string s;
ll quick_pow(ll a,ll b)//快速幂
{
ll ans = 1;
while(b)
{
if(b&1)
ans = a*ans%mod;
a = a*a%mod;
b>>=1;
}
return ans;
}
void init()//预处理
{
ans[0] = ans[1] = Mod[0] = 1;
for(int i = 1;i <= 3001;i++)//阶乘
ans[i] = ans[i-1]*i%mod;
for(int i = 1;i <= 3001;i++)//逆元
Mod[i] = quick_pow(ans[i],mod-2)%mod;
for(int i = 0;i <= 3001;i++)//c的i次方
C[i] = quick_pow(c,i)%mod;
}
void solve()
{
int len = s.size();
for(int i = 0;i < m;i++)
{
ll a = len-arr[i]-1;
for(int j = 0;j <= a;j++)
{
ll num = ans[a]*Mod[j]%mod*Mod[a-j]%mod;
num = num*C[j+i]%mod;
sum = (sum + num) % mod;
}
}
sum = (sum + C[m])%mod;//加上全选的次数
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin >> s >> c;
for(int i = 0;i < s.size();i++)
if(s[i] == '1')
arr[m++] = i;
init();
solve();
cout << sum << endl;
}