SPOJ LUCIFER (数位dp)
来源:互联网 发布:生产计划编制软件 编辑:程序博客网 时间:2024/05/18 18:18
LUCIFER - LUCIFER Number
Lucifer is the only human whi has defeated RA-ONE in a computer game ..
RA-One is after lucifer for revenge and G-One is there to protect him ...
All thi G-One and Ra-one Nonsense has disturbed lucifers life..
He wants to get Rid of Ra-One and kill him . He found that Ra-One can be killed only by throwing Lucifer number of weapons at him.
Lucifer number shares the some properties of Ra-One Numbers numbers and G-One Numbers
Any number is LUCIFER NUMBER if the Difference between Sum of digits at even location and Sum of digits at odd location is prime number .. For eg... for 20314210 is lucifer number
digits at odd location 0,2,1,0
digits at even location 1,4,3,2
diff = (1+4+3+2)-(0+2+1+0)=10-3 = 7 ..... a prime number.
Lucifer has access to a Warehouse which has lots of weapons ..
He wants to know in how many ways can he kill him.
Can you help him?
Input
First line will have a number 't' denoting the number of test cases.
each of the following t lines will have 2 numbers 'a' , 'b'
Output
Print single number per test case, depicting the count of Lucifer numbers in the range a,b inclusive.
Example
Input:
5
200 250
150 200
100 150
50 100
0 50
Output:
2
16
3
18
6NOTE: t will be less than 100
from and to will be between 0 and 10^9 inclusive
/*链接:http://www.spoj.com/problems/LUCIFER/en/题意:求一个区间内偶数位置减去奇数位置的差为质数的数的个数思路:数位dp dp[i][j][k] 第i位,奇数位和为j,偶数位和为k的情况*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define bug printf("hihi\n")#define eps 1e-8typedef long long ll;using namespace std;#define N 11int dp[N][90][90];int bit[N];int pri[200];void inint(){ int i,j; pri[1]=1; pri[0]=1; for(i=2;i<200;i++) if(!pri[i]) for(j=i*2;j<200;j+=i) pri[j]=1;}int dfs(int pos,int odd,int even,bool bound){ if(pos==0) { if(even<=odd) return 0; if(pri[even-odd]) return 0; return 1; } if(!bound&&dp[pos][odd][even]>=0) return dp[pos][odd][even]; int up=bound ? bit[pos]:9; int ans=0; for(int i=0;i<=up;i++) { int tt; if(pos&1) tt=dfs(pos-1,odd+i,even,bound&&i==up); else tt=dfs(pos-1,odd,even+i,bound&&i==up); ans+=tt; } if(!bound) dp[pos][odd][even]=ans; return ans;}int solve(int x){ int i,j,len=0; while(x) { bit[++len]=x%10; x/=10; } return dfs(len,0,0,true);}int main(){ int i,j,t; memset(dp,-1,sizeof(dp)); inint(); scanf("%d",&t); int x,y; while(t--) { scanf("%d%d",&x,&y); printf("%d\n",solve(y)-solve(x-1)); } return 0;}
- SPOJ LUCIFER (数位dp)
- SPOJ 1128 数位DP
- SPOJ - PARTPALI(数位dp)
- SPOJ KPSUM 数位DP
- SPOJ 1182 数位DP+二分
- SPOJ MYQ10 (数位DP)
- SPOJ BALNUM (数位DP+状压)
- SPOJ RAONE(数位dp)
- SPOJ BALNUMBalanced Numbers 数位dp
- SPOJ BALNUM (数位DP)
- spoj CPCRC1C(数位dp)
- SPOJ - MYQ10 Mirror Number (数位DP)
- [数位dp] spoj 10738 Ra-One Numbers
- [数位dp] spoj 10606 Balanced Numbers
- [数位dp] spoj 10738 Ra-One Numbers
- spoj 1182 Sorted bit squence (数位dp)
- 【数位DP】SPOJ 10606 BALNUM Balanced Numbers
- SPOJ MYQ10 Mirror Number 数位dp
- uva348Optimal Array Multiplication Sequence (最优矩阵链乘+路径输出)
- LLVM links
- c语言 curl
- arc下的block使用注意事项
- [贪心]HDU1085 Moving Tables
- SPOJ LUCIFER (数位dp)
- java中通过对象,javabean配置文件,反射属性值
- SpringMVC访问静态资源的几种方法
- mybatis 实现一对一,一对多,多对多关联查询 小结
- java模拟http调用百度识图
- js获取项目根路径
- LeetCode——复制图
- android studio 程序真机运行中文显示乱码
- GRE写作必备句型