zoukankan      html  css  js  c++  java
  • 2266: number(灵性数位dp)

    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 ;
    }
  • 相关阅读:
    4月21日Java作业
    5.14 Java作业
    第十周java作业
    4月30号作业
    第七周上机
    4.9Java
    通宵看剧有感
    error: pathspec 'xxxxxxxxx' did not match any file(s) known to git
    markdown格式测试
    博客申请通过啦
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/8910461.html
Copyright © 2011-2022 走看看