P4317 花神的数论题 dp
这题我一开始就想到数位dp了,其实好像也不是很难,但是自己写不出来。。。常规套路,f[i][j][k][t],从后往前填数,i位,j代表是否卡着上沿,k是现在有几个1,t是想要有几个。记忆化搜索就ok啦!
题干:
题目背景 众所周知,花神多年来凭借无边的神力狂虐各大 OJ、OI、CF、TC …… 当然也包括 CH 啦。
题目描述 话说花神这天又来讲课了。课后照例有超级难的神题啦…… 我等蒟蒻又遭殃了。 花神的题目是这样的:设 sum(i)\text{sum}(i)sum(i) 表示 iii 的二进制表示中 的个数。给出一个正整数 NNN ,花神要问你 ∏i=1Nsum(i)\prod_{i=}^{N}\text{sum}(i)∏i=1Nsum(i) ,也就是 sum()∼sum(N)\text{sum}()\sim\text{sum}(N)sum()∼sum(N) 的乘积。
输入输出格式
输入格式: 一个正整数 N。 输出格式: 一个数,答案模 的值。 输入输出样例
输入样例#: 复制 输出样例#: 复制 说明 对于 % 的数据,N≤^
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(register int i = a;i <= n;++i)
#define lv(i,a,n) for(register int i = a;i >= n;--i)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
const int N = ;
const int mod = ;
ll n;
int x[N],cnt = ;
ll f[N][][N][N];
ll ans[N];
ll qpow(ll a,ll b)
{
ll tot = ;
while(b)
{
if(b & )
{
tot *= a;
tot %= mod;
}
a *= a;
a %= mod;
b >>= ;
}
return tot;
}
ll _f(int cur,int up,int tmp,int d)
{
if(!cur)
return tmp == d;
if(~f[cur][up][tmp][d])
return f[cur][up][tmp][d];
int lim = up ? x[cur] : ;
ll ret = ;
for(int i = ;i <= lim;++i)
{
ret += _f(cur - ,up && i == lim,tmp + (i == ),d);
}
return f[cur][up][tmp][d] = ret;
}
ll solve()
{
while(n)
{
x[++cnt] = n & ;
n >>= ;
}
for(int i = ;i <= ;i++)
{
memset(f,-,sizeof(f));
ans[i] = _f(cnt,,,i);
}
ll ret = ;
for(int i = ;i <= ;++i)
{
ret = ret * qpow(i,ans[i]) % mod;
}
}
int main()
{
read(n);
printf("%lld\n",solve());
}
P4317 花神的数论题 dp的更多相关文章
- DP,数论————洛谷P4317 花神的数论题(求1~n二进制中1的个数和)
玄学代码(是洛谷题解里的一位dalao小粉兔写的) //数位DP(二进制)计算出f[i]为恰好有i个的方案数. //答案为∏(i^f[i]),快速幂解决. #include<bits/stdc+ ...
- P4317 花神的数论题 动态规划?数位DP
思路:数位$DP$ 提交:5次(其实之前A过,但是调了调当初的程序.本次是2次AC的) 题解: 我们分别求出$sum(x)=i$,对于一个$i$,有几个$x$,然后我们就可以快速幂解决. 至于求个数用 ...
- 洛谷 P4317 花神的数论题 || bzoj3209
https://www.lydsy.com/JudgeOnline/problem.php?id=3209 https://www.luogu.org/problemnew/show/P4317 设c ...
- Luogu P4317 花神的数论题
也是一道不错的数位DP,考虑先转成二进制后再做 转化一下问题,考虑统计出\([1,n]\)中在二进制下有\(i\)个\(1\)的方案数\(cnt_i\),那么答案显然就是\(\prod i^{cnt_ ...
- 洛谷P4317 花神的数论题
洛谷题目链接 数位$dp$ 我们对$n$进行二进制拆分,于是就阔以像十进制一样数位$dp$了,基本就是套模板.. 接下来是美滋滋的代码时间~~~ #include<iostream> #i ...
- 洛谷 P4317 花神的数论题(组合数)
题面 luogu 题解 组合数 枚举有多少个\(1\),求出有多少种数 扫描\(n\)的每一位\(1\), 强制选\(0\)然后组合数算一下有多少种方案 Code #include<bits/s ...
- P4317 花神的数论题
题目 洛谷 数学方法学不会%>_<% 做法 爆搜二进制下存在\(i\)位\(1\)的情况,然后快速幂乘起来 My complete code #include<bits/stdc++ ...
- P4317 花神的数论题,关于luogu题解粉兔做法的理解
link 题意 设 \(\text{sum}(i)\) 表示 \(i\) 的二进制表示中 \(1\) 的个数.给出一个正整数 \(N\) ,求 \(\prod_{i=1}^{N}\text{sum}( ...
- 【洛谷】4317:花神的数论题【数位DP】
P4317 花神的数论题 题目背景 众所周知,花神多年来凭借无边的神力狂虐各大 OJ.OI.CF.TC …… 当然也包括 CH 啦. 题目描述 话说花神这天又来讲课了.课后照例有超级难的神题啦…… 我 ...
随机推荐
- iOS-runtime-objc_setAssociatedObject(关联对象以及传值)
例子: static const char kRepresentedObject; - (IBAction)doSomething:(id)sender { UIAlertView *alert = ...
- MTK andorid从底层到上层添加驱动
1 [编写linux驱动程序] 1.1 一.编写驱动核心程序 1.2 二.配置Kconfig 1.3 三.配置Makefile 1.4 四.配置系统的autoconfig 1.5 五.编译 2 [编写 ...
- Attribute "not-null" must be declared for element type "property"解决办法
Attribute "not-null" must be declared for element type "property"解决办法 在hiberante ...
- BZOJ1700: [Usaco2007 Jan]Problem Solving 解题
每月m<=1000块钱,有n<=300道题,要按顺序做,每月做题要花钱,花钱要第一个月预付下个月立即再付一次,给出预付和再付求最少几个月做完题,第一个月不做. 神奇的DP..竟没想出来.. ...
- make only output error/warning message( 编译时,只输出错误信息和警告信息)
make > /dev/null 这样,正常的信息被重定向输出到/dev/null,错误和警告信息会输出到标准错误设备(standard error,相对于标准输入/输出设备来说).
- iOS present出一个背景为半透明的试图
WDKChatRoomViewController *roomVC = [[WDKChatRoomViewController alloc] init]; roomVC.titleStr = [gro ...
- nagios+logstash实时监控java日志(一)
https://blog.csdn.net/yanggd1987/article/details/64121459
- POJ 1328 Radar Installation【贪心 区间问题】
题目链接: http://poj.org/problem?id=1328 题意: 在x轴上有若干雷达,可以覆盖距离d以内的岛屿. 给定岛屿坐标,问至少需要多少个雷达才能将岛屿全部包含. 分析: 对于每 ...
- httpclient请求去掉返回结果string中的多余转义字符
public String doGet() { String uriAPI = "http://XXXXX?str=I+am+get+String"; String result= ...
- 表皮囊肿?wtf
https://baike.baidu.com/item/%E8%A1%A8%E7%9A%AE%E5%9B%8A%E8%82%BF/7852024?fr=aladdin