#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. 自定义框架(MyMvc)

    //初次接触自定义框架,简单的登录功能的实现流程:: 当我们实现登录功能的时候,首先会创建一个login.jsp 会写这些登录表单 <form action="loginAction. ...

  2. Eclipse InstaSearch搜索词法 (很多并不支持)

    1. 中文翻译 http://www.cnblogs.com/xing901022/p/4974977.html 2. 英文原文 http://lucene.apache.org/core/3_0_3 ...

  3. ThinkPHP实现支付宝接口功能

    最近做系统,需要实现在线支付功能,毫不犹豫,选择的是支付宝的接口支付功能.这里我用的是即时到帐的接口,具体实现的步骤如下:一.下载支付宝接口包下载地址:https://doc.open.alipay. ...

  4. Java开发环境的搭建以及使用eclipse从头一步步创建java项目

    一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近的JDK即可..http://www.orac ...

  5. node基础11:接受参数

    1.接收参数 在Node中接受GET/POST请求的参数不像PHP那样,在PHP中直接有全局变量$_GET,$_POST来接受url,或者请求体重的参数. 在node中接受GET参数使用url.par ...

  6. 配置文件类 Properties

    Properties(配置文件类): 主要用于生产配置文件与读取配置文件的信息. Properties属于集合类,继承于Hashtable. Properties要注意的细节:    1. 如果配置文 ...

  7. mvc自定义全局异常处理

    异常信息处理是任何网站必不可少的一个环节,怎么有效显示,记录,传递异常信息又成为重中之重的问题.本篇将基于上篇介绍的html2cancas截图功能,实现mvc自定义全局异常处理.先看一下最终实现效果: ...

  8. Jquery揭秘系列:ajax原生js实现

    讲到ajax这个东西,我们要知道两个对象XMLHTTPRequest和ActiveXObject ,提供了对 HTTP 协议的完全的访问,包括做出 POST 和 HEAD 请求以及普通的 GET 请求 ...

  9. 51Nod-1265 四点共面

    51Nod 1265 : http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1265 1265 四点共面 基准时间限制:1 秒 ...

  10. C# 使用网易邮箱发送邮件

    代码如下,值得注意的有一下几点:1.网易要求使用代理密码而不暴露邮箱密码2.要求邮箱开通smtp服务3.要求邮件发送者邮箱和认证的邮箱地址一致. private void SendEmail(stri ...