数位dp模板
#include <bits/stdc++.h>
typedef long long LL; const int MOD = (int)1e9 + 7;
LL L,R,G,T;
int dp[62 + 1][2][2][2][2];
bool vis[62 + 1][2][2][2][2]; inline void add(int &a,int b) {
a += b;
if (a >= MOD) a -= MOD;
if (a < 0) a += MOD;
} int calc(int at,bool al,bool ar,bool bl,bool br) {
if (at == -1) {
return 1;
}
if (vis[at][al][ar][bl][br]) {
return dp[at][al][ar][bl][br];
} vis[at][al][ar][bl][br] = true;
int &ret = dp[at][al][ar][bl][br];
ret = 0; int l = L >> at & 1,
r = R >> at & 1,
x = (G ^ T) >> at & 1;
for (int a = 0; a < 2; ++ a) {
if (al && a < l) continue;
if (ar && a > r) continue;
int b = x ^ a;
if (bl && b < l) continue;
if (br && b > r) continue;
add(ret,calc(at - 1,al && a == l,ar && a == r,bl && b == l,br && b == r));
}
return ret;
} int work() {
if ((G ^ T) == 0) return (R - L + 1) % MOD;
memset(vis,false,sizeof(vis));
return ((R - L + 1) * 2 % MOD - calc(62,true,true,true,true) + MOD) % MOD;
} int main() {
int cas;
scanf("%d",&cas);
while (cas--) {
scanf("%I64d%I64d%I64d%I64d",&L,&R,&G,&T);
printf("%d\n",work());
}
}
GTW likes czf
从前,有两个人名叫GTW,DSY。一天,他们为了争夺DSY的妹子CZF,决定进行一次决斗。首先,CZF会给出一个区间l,l+1,l+2......rl,l+1,l+2......r,和两个数G,T。现在,CZF会在G,T两个数中随机一个数XX,在区间l,rl,r中随机一个数Y,进行一种特殊的运算@。CZF想要快速知道有多少数字可能会是答案。
然而GTW并不会做这道题,但是为了赢得CZF,他就来寻求你的帮助。
由于答案可能会很大,所以把最终的答案模1000000007。
我们规定运算X @ Y =((X and Y) or Y) xor X. ----------------------------------------------------------------------------------------------------------------------------------------------
给定一个长度为72的数a(a是山形的),求0~a-1中有多少个数是山形的。
http://codeforces.com/gym/100827 (E)
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>
#include <complex> #include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std; #define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()
#define fr(i,a,b) for(int i = (a);i <= (b);i++) int ri(){int x;scanf("%d",&x);return x;} ll dp[75][11][2][2];
string s; ll go(int pos,int lst,int up,int e)
{
if (pos == s.length())
return 1;
if (dp[pos][lst][up][e] != -1)
return dp[pos][lst][up][e];
ll res = 0;
int f = e ? s[pos] - '0' : 9;
int tmp = lst == 10 ? -1 : lst;
for(int i = 0;i <= f;i++)
{
if (up)
{
if (i >= tmp)
res += go(pos + 1,i,up,e & (i == f));
else
res += go(pos + 1,i,false,e & (i == f));
}
else
{
if (i <= tmp)
res += go(pos + 1,i,false,e & (i == f));
}
}
return dp[pos][lst][up][e] = res;
} int main()
{
//freopen("input.txt","rt",stdin);
//freopen("insider.in","rt",stdin);
//freopen("insider.out","wt",stdout); int T;
scanf("%d", &T);
while(T--)
{
cin >> s;
while(s.length() > 1 && s[0] == '0')
s = s.substr(1,s.length() - 1);
bool check = true;
int i;
for(i = 1;i < s.length();i++)
{
if (s[i] >= s[i - 1]);
else
break;
}
for(;i < s.length();i++)
if (s[i] > s[i - 1])
check = false;
if (check)
{
memset(dp,-1,sizeof(dp));
cout << go(0,10,1,1) - 1 << endl;
}
else
cout << -1 << endl;
} return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------
http://codeforces.com/problemset/problem/55/D
求一个区间内beautiful num的数量。(beautiful num:如果一个数能被它所有数位上的非0数整除,那么它就是一个beautiful num , 如12 ,15,而13不是)。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
ll dp[20][2520][49] ;
int orm[2521] ;
int cnt ;
int bit[20] ; void init () {
for (int i = 1 ; i <= 2520 ; i ++) {
if (2520%i == 0) orm[i] = ++ cnt ;
}
} ll calc (int pos , int pre , int lcm , int welt ) {
if (pos == -1)
return pre%lcm==0 ;
if (welt == 0 && dp[pos][pre][orm[lcm]] != -1)
return dp[pos][pre][orm[lcm]] ;
int f = welt?bit[pos]:9 ; ll ret = 0 ;
for (int i = 0 ; i <= f ; i ++) {
int curpre = (pre*10+i)%2520 ;
int curlcm = lcm ;
if (i) curlcm = curlcm*i/__gcd(curlcm,i) ;
ret += calc (pos-1,curpre,curlcm,welt && i==f) ;
}
//cout << "pos = " << pos << " pre = " << pre << " lcm = " << lcm << " welt = " << welt << endl ;
//cout << "ret = " << ret << endl ;
if (welt == 0) dp[pos][pre][orm[lcm]] = ret ;
return ret ;
} ll work (ll x) {
int pos=0 ;
while (x) {bit[pos++]=x%10;x/=10;}
return calc(pos-1 , 0 , 1 , 1) ;
} int main () {
init () ;
int T ;
cin >> T ;
memset (dp , -1 , sizeof(dp)) ;
//cout << "cnt = " << cnt << endl ;
while (T --) {
ll l , r ;
cin >> l >> r ;
cout << work(r)-work(l-1) << endl ;
}
return 0 ;
}
数位dp模板的更多相关文章
- HDU 2089 不要62(数位dp模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...
- POJ 3286 How many 0's(数位DP模板)
题目链接:http://poj.org/problem?id=3286 题目大意: 输入n,m,求[n,m]的所有数字中,0出现的总数是多少,前导零不算. 解题思路: 模板题,设dp[pos][num ...
- 数位dp模板 [dp][数位dp]
现在才想到要学数位dp,我是不是很弱 答案是肯定的 以一道自己瞎掰的题为模板 //题: //输入数字n //从0枚举到n,计算这n+1个数中含有两位数a的数的个数 //如12930含有两位数93 #i ...
- 51nod 1009 数字1的数量(数位dp模板)
给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数. 例如:n = 12,包含了5个1.1,10,12共包含3个1,11包含2个1,总共5个1. 数位dp的模板题 ...
- 51nod 1009 - 数字1的数量 - [数位DP][模板的应用以及解释]
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009 基准时间限制:1 秒 空间限制:131072 KB 给 ...
- HDU - 4722 Good Numbers 【找规律 or 数位dp模板】
If we sum up every digit of a number and the result can be exactly divided by 10, we say this number ...
- 数位dp 模板加例题
概念:所谓数位"dp",是指对数字的"位"进行的与计数有关的DP.一个数一个位,十位,百位,千位等,数的每一位就是数位.数位DP用来解决与数字操作有关的问题.例 ...
- 【hdu6148】Valley Numer【数位dp模板题】
题意 对于每组数据给出一个整数n(length(n)<=100),找出不大于n的数字中有多少是Valley Numer.对于Valley的定义是它每一位的数字要么是递增,要么是递减,要么是先递减 ...
- HDU 3555 Bomb(数位DP模板啊两种形式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The counter-terrorists found ...
- HDU 2089 不要62 数位DP模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 参考博客:https://www.cnblogs.com/HDUjackyan/p/914215 ...
随机推荐
- 理解 OpenStack + Ceph (9): Ceph 的size/min_size/choose/chooseleaf/scrubbing/repair 等概念
本系列文章会深入研究 Ceph 以及 Ceph 和 OpenStack 的集成: (1)安装和部署 (2)Ceph RBD 接口和工具 (3)Ceph 物理和逻辑结构 (4)Ceph 的基础数据结构 ...
- 《InsideUE4》-5-GamePlay架构(四)Pawn
<InsideUE4>-5-GamePlay架构(四)Pawn Tags: InsideUE4 我像是一颗棋 进退任由你决定 我不是你眼中唯一将领 却是不起眼的小兵 引言 欢迎来到Game ...
- NYOJ---540奇怪的排序
奇怪的排序 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 最近,Dr. Kong 新设计一个机器人Bill.这台机器人很聪明,会做许多事情.惟独对自然数的理解与人类不一 ...
- BZOJ1500: [NOI2005]维修数列[splay ***]
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 12278 Solved: 3880[Submit][Statu ...
- P1774 最接近神的人_NOI导刊2010[树状数组 逆序对 离散化]
题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门上方用古代文写着“神的殿堂”.小FF猜想里面应该就有王室的 ...
- NYOJ 451
光棍节的快乐 描述 光棍们,今天是光棍节.聪明的NS想到了一个活动来丰富这个光棍节. 规则如下: 每个光棍在一个纸条上写一个自己心仪女生的名字,然后把这些纸条装进一个盒子里,这些光 棍依次抽取一张纸条 ...
- [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)
题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...
- vuejs的动态过滤
想要通过vuejs动态过滤(这里动态指得是过滤的条件是动态变化的), 一直没找到好办法, 最蠢的办法当然是两个两个数组,一个作为原始副本数组 一个作为视图数组,这样当过滤条件变化的时候 动态拷贝原始数 ...
- Java的泛型反射
If the superclass is a parameterized type, the {@code Type} * object returned must accurately reflec ...
- HAOI2015 泛做
T1 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两之间的 ...