#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模板的更多相关文章

  1. HDU 2089 不要62(数位dp模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...

  2. POJ 3286 How many 0's(数位DP模板)

    题目链接:http://poj.org/problem?id=3286 题目大意: 输入n,m,求[n,m]的所有数字中,0出现的总数是多少,前导零不算. 解题思路: 模板题,设dp[pos][num ...

  3. 数位dp模板 [dp][数位dp]

    现在才想到要学数位dp,我是不是很弱 答案是肯定的 以一道自己瞎掰的题为模板 //题: //输入数字n //从0枚举到n,计算这n+1个数中含有两位数a的数的个数 //如12930含有两位数93 #i ...

  4. 51nod 1009 数字1的数量(数位dp模板)

    给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数. 例如:n = 12,包含了5个1.1,10,12共包含3个1,11包含2个1,总共5个1.   数位dp的模板题   ...

  5. 51nod 1009 - 数字1的数量 - [数位DP][模板的应用以及解释]

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009 基准时间限制:1 秒 空间限制:131072 KB 给 ...

  6. 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 ...

  7. 数位dp 模板加例题

    概念:所谓数位"dp",是指对数字的"位"进行的与计数有关的DP.一个数一个位,十位,百位,千位等,数的每一位就是数位.数位DP用来解决与数字操作有关的问题.例 ...

  8. 【hdu6148】Valley Numer【数位dp模板题】

    题意 对于每组数据给出一个整数n(length(n)<=100),找出不大于n的数字中有多少是Valley Numer.对于Valley的定义是它每一位的数字要么是递增,要么是递减,要么是先递减 ...

  9. HDU 3555 Bomb(数位DP模板啊两种形式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The counter-terrorists found ...

  10. HDU 2089 不要62 数位DP模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 参考博客:https://www.cnblogs.com/HDUjackyan/p/914215 ...

随机推荐

  1. The Engine Document of JustWeEngine

    JustWeEngine - Android FrameWork An easy open source Android Native Game FrameWork. Github Game core ...

  2. 【JavaScript 插件】实现图片倒影效果 - reflex.js

    目前版本: reflex.js 1.5 适用的主流浏览器: Mozilla Firefox 1.5+, Opera 9+, Safari and IE6+ 原理:通过 canvas 重画图片,显示倒影 ...

  3. java 链表数据结构

    首先,单链表相对于队列的优势在于存储地址不是连续的,这样的意义在于,操作其中的某一个位置的元素时不需要对之前的其他元素都进行内存操作,大大的为我们的计算机减压了.下面直接进入正题: 先要定义一个结点类 ...

  4. BZOJ1878: [SDOI2009]HH的项链[树状数组 离线]

    1878: [SDOI2009]HH的项链 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 3486  Solved: 1738[Submit][Statu ...

  5. redis 学习笔记(1)-编译、启动、停止

    一.下载.编译 redis是以源码方式发行的,先下载源码,然后在linux下编译 1.1 http://www.redis.io/download 先到这里下载Stable稳定版,目前最新版本是2.8 ...

  6. spring独立事务分析

    最近在ssm框架的项目中需要用到独立事务的实现,找了半天,搜集了以下理论知识为实现做准备.事务管理器为datasource (1)Spring在transactiondefinition接口中规定了7 ...

  7. Servlet和JSP

    Servlet 一.Servlet 的生命周期. servlet 有良好的生存期的定义,包括加载和实例化.初始化.处理请求以及服务结束.这个生存期由javax.servlet.Servlet 接口 的 ...

  8. Sqlserver 语法总结

    修改列类型 alter table PRO_Element_b alter column matname varchar(1024) 更改一个表中的数据到另外一个表中 update a set a.n ...

  9. mysql5.7.10 的源码安装

    mysql 5.7.10的源码安装:http://fyduan.blog.51cto.com/4234935/1729873cmake . -DCMAKE_INSTALL_PREFIX=/usr/lo ...

  10. 剑指offer 面试题64 数据流的中位数

    struct cmp { bool operator()(double a, double b) { return a > b; } }; class Solution { public: vo ...