码迷,mamicode.com
首页 > 其他好文 > 详细

2266: number(灵性数位dp)

时间:2018-04-23 00:07:22      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:col   set   ace   nbsp   code   ios   status   content   #define   

2266: number

时间限制: 1 Sec  内存限制: 128 MB
提交: 56  解决: 20
[提交][状态][讨论版][命题人:admin]

题目描述

某人刚学习了数位DP,他在某天忽然思考如下问题:

给定n,问有多少数对<x, y>满足:

x, y∈[1, n], x < y

           x, y中出现的[0, 9]的数码种类相同

输入

一个整数n (n <= 107)

输出

输出一个数即答案

样例输入

30

样例输出

3

提示

<1, 11> <2, 22> <12, 21>

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std ;

#define LL long long
int n ;
int times[1 << 10] ;
int b , s ;
LL result ;
// 注意result开longlong要不会爆炸

int main()
{

    cin >> n ;

    result = 0 ;
    memset(times , 0 , sizeof(times)) ;

    for(int i = 1 ; i <= n ; i++)
    {
        /*
        对从1到n的每个数字,分离每个数位上的数字,
        然后将1向左移动相应的位数,相同的数字移动的位置相同
        使用相同的数字组成的数,的s值是相同的
        而且s值每出现一次,都会出现前面s出现次数的数对对数
        */
        s = 0 ;
        int x = i ;
        while(x)
        {
            b = x % 10 ;
            x /= 10 ;
            s = s | (1 << b) ;
        }
        result += times[s] ;
        times[s] ++ ;
    }


    cout << result << endl ;

    return 0 ;
}

 

2266: number(灵性数位dp)

标签:col   set   ace   nbsp   code   ios   status   content   #define   

原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/8910461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!