ACdream 1064 完美数
数位dp.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} int dp[][],t[]; void pre()
{
memset(dp,,sizeof dp); dp[][]=; // get时要× dp[][]=; // 0 1 2 4 5 6 7 9
dp[][]=; //
dp[][]=; // for(int i=;i<=;i++)
{
dp[i][]=*dp[i-][];
dp[i][]=dp[i-][]+*dp[i-][];
dp[i][]=dp[i-][]+*dp[i-][];
}
} int get(int x)
{
int sz=; while(x) t[sz++]=x%, x=x/;
for(int i=sz;i>=;i--) t[i]=t[i-]; int res=; bool san=,ba=; for(int i=sz;i>=;i--)
{
//该位可以放 0 1 2 3 4....t[i]-1
if(san==&&ba==)
{
//放 0 1 2
if(t[i]<=) res=res+
dp[i-][]*t[i]+
dp[i-][]*t[i]; //放 0 1 2 3 4 5 6 7
else if(t[i]<=) res=res+
dp[i-][]+
dp[i-][]*t[i]+
dp[i-][]*(t[i]-); //放 0 1 2 3 4 5 6 7 8
else if(t[i]==) res=res+
dp[i-][]+
dp[i-][]+
dp[i-][]*(t[i]-)+
dp[i-][]*(t[i]-); if(t[i]==) san=; if(t[i]==) ba=;
if(san==&&ba==) return res;
continue;
} int id,limit; if(san==) id=,limit=; else id=,limit=; //放 0 1 2 .. limit-1
if(t[i]<=limit) res=res+
dp[i-][]*t[i]+
dp[i-][id]*t[i]; //放 0 1 2 .. t[i]-1
else res=res+
dp[i-][]*(t[i]-)+
dp[i-][id]*(t[i]-); if(t[i]==) san=; if(t[i]==) ba=;
if(san==&&ba==) return res;
} return res;
} int main()
{
pre(); int T,L,R;
scanf("%d",&T); while(T--)
{
scanf("%d%d",&L,&R);
printf("%d\n",get(R+)-get(L));
}
return ;
}
ACdream 1064 完美数的更多相关文章
- 华为OJ平台——完美数
import java.util.Scanner; /** * * 完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数. * 它所有的真因子(即除了自身以外的约数)的和(即 ...
- SDUT 1220 完美数
完美数 Time Limit: 1000ms Memory limit: 65536K 题目描述 任何一个自然数的约数中都有1和它本身,我们把小于它本身的因数叫做这个自然数的真约数. 如6的所有真 ...
- 1000以内完全数(完美数)获取实现---基于python
"""题目: 如果一个数恰好等于它的因子之和,则称该数为"完全数" .各个小于它的约数(真约数,列出某数的约数,去掉该数本身,剩下的就是它的真约数)的 ...
- [51nod1232]完美数
如果一个数能够被组成它的各个非0数字整除,则称它是完美数.例如:1-9都是完美数,10,11,12,101都是完美数,但是13就不是完美数(因为13不能被数字3整除). 现在给定正整数x,y,求x和y ...
- [Swift]LeetCode507. 完美数 | Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- 51nod 1232 完美数 数位dp
1232 完美数 题目来源: 胡仁东 基准时间限制:2 秒 空间限制:131072 KB 如果一个数能够被组成它的各个非0数字整除,则称它是完美数.例如:1-9都是完美数,10,11,12,101都 ...
- Leetcode 507.完美数
完美数 对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为"完美数". 给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False 示例: ...
- 507 Perfect Number 完美数
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...
- Java实现 LeetCode 507 完美数
507. 完美数 对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为"完美数". 给定一个 整数 n, 如果他是完美数,返回 True,否则返回 False ...
随机推荐
- 【Java每日一题】20170116
20170113问题解析请点击今日问题下方的"[Java每日一题]20170116"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...
- Python 函数简介 之二
1.当函数有多个返回值时, 其多个返回值将以元组的形式出现 def test1(): print("in the test1") return 'end' def test2(): ...
- java基础练习 7
public class Seventh { public static void main(String[] args){ double x=0,a=1,b=9; while(x!=a*a-100& ...
- 原生js
- install OwnCloud9 on CentOS7
OwnCloud9下载地址: https://download.owncloud.org/community/owncloud-9.1.1.zip //安装配置数据库 #yum install mar ...
- 简单字符串匹配 Brute
/* 很简单 模式匹配的Brute-Force算法 Brute-Force算法实现模式匹配的思想是:从主串s=”s0s1…sn-1”的第一个字符开始和模式串t=”t0t1…tn-1”的第一个字符比较, ...
- lua中的面向对象编程
简单说说Lua中的面向对象 Lua中的table就是一种对象,看以下一段简单的代码: 上述代码会输出tb1 ~= tb2.说明两个具有相同值得对象是两个不同的对象,同时在Lua中table是引用类型的 ...
- Netty(7)源码-ByteBuf
一.ByteBuf工作原理 1. ByteBuf是ByteBuffer的升级版: jdk中常用的是ByteBuffer,从功能角度上,ByteBuffer可以完全满足需要,但是有以下缺点: ByteB ...
- VS2010下调试.NET源码
微软走向开源,是时候用vs去单步调试进入源码了.参考地址:http://blog.csdn.net/waxgourd0/article/details/6600182 可供参考的文章:https:// ...
- <poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies
本题链接:http://poj.org/problem?id=2139 Description: The cows have been making movies lately, so the ...