C. Classy Numbers

题目链接:https://codeforces.com/contest/1036/problem/C

题意:

给出n个询问,每个询问给出Li,Ri,问在这个闭区间中有多少个数满足,除开0之外,最多只有4个数字。

题解:

由于题目给出的数满足前缀性质,所以我们可以直接求[1,r]这个区间中有多少个数满足就好了。

具体做法就是从高位往低位来看,然后如果当前数组不为0,假设为p,那么当前数组对答案的贡献就比较好计算了,假设后面的数有x位,目前已经有k个数字了。

那么分两种情况:一种是当前这位为0的时候,那么贡献就是C(x,3-k);另外一种就是当前这位为1~p-1的时候,贡献就是(p-1)*C(x,3-k-1)。

当前为p的情况我们留在后面统计就行了,因为这种情况主要是取决后面的数的。

细节见代码吧:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll C[][];
int T;
ll a,b;
int num[],pow9[]={,,,};
ll calc(ll x,ll k){
ll ans = ;
if(x<=) return ;
for(int i=;i<=k;i++) ans+=C[x][i]*pow9[i];
return ans ;
}
ll solve(ll x){
memset(num,,sizeof(num));
int n;
for(n=;x;n++){
num[n]=x%;
x/=;
}
n--;
ll ans = ;
for(int i=n,cur=;i>=;i--){
if(!num[i]) continue ;
ans+=calc(i-,cur);
cur--;
ans+=(num[i]-)*calc(i-,cur);
if(cur==) break ;
//if(i==1) ans++;
}
return ans ;
}
int main(){
ios::sync_with_stdio(false);cin.tie();
C[][]=;C[][]=;
for(int i=;i<=;i++){
C[i][]=;
for(int j=;j<=i;j++){
C[i][j]=C[i-][j]+C[i-][j-];
}
}
cin>>T;
while(T--){
cin>>a>>b;
//cout<<solve(b)<<" "<<solve(a-1)<< '\n';
cout<<solve(b)-solve(a-)<< '\n';
}
return ;
}

Educational Codeforces Round 50 (Rated for Div. 2) C. Classy Numbers的更多相关文章

  1. Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)

    题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...

  2. Educational Codeforces Round 50 (Rated for Div. 2)的A、B、C三题AC代码

    A题链接:https://codeforces.com/contest/1036/problem/A A题AC代码: #include <stdio.h> #include <std ...

  3. Educational Codeforces Round 50 (Rated for Div. 2)F. Relatively Prime Powers

    实际上就是求在[2,n]中,x != a^b的个数,那么实际上就是要求x=a^b的个数,然后用总数减掉就好了. 直接开方求和显然会有重复的数.容斥搞一下,但实际上是要用到莫比乌斯函数的,另外要注意减掉 ...

  4. Educational Codeforces Round 50 (Rated for Div. 2) E. Covered Points

    注释上都有解析了,就不写了吧,去重的问题就用set解决,并且呢第i个线段最多和其他线段产生i-1个交点,n^2logn. #include <cmath> #include <cst ...

  5. Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers

    链接:https://codeforces.com/contest/1167/problem/B 题意: This is an interactive problem. Remember to flu ...

  6. Educational Codeforces Round 65 (Rated for Div. 2)B. Lost Numbers(交互)

    This is an interactive problem. Remember to flush your output while communicating with the testing p ...

  7. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  8. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. Spring Cloud(十):服务网关 Zuul(路由)【Finchley 版】

    Spring Cloud(十):服务网关 Zuul(路由)[Finchley 版]  发表于 2018-04-23 |  更新于 2018-05-09 |  通过之前几篇 Spring Cloud 中 ...

  2. 四分树 (Quadtrees UVA - 297)

    题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...

  3. python简单循环生成器

    import time #循环生成器 def traversal_list(alist, i): while True: length = len(alist) i = i%(length) yiel ...

  4. Thinkphp实现excel导出数据

    前端: 点击导出触发click事件,传值export指令和args关键字(args是指我们是否有查询取哪些数据)到控制器 $(document).on("click", " ...

  5. 第一课——从main到WinMain

    一.Visual C++6.0 由微软公司推出的基于Windows系统的可视化集成开发环境(IDE) 微软公司为其开发了功能强大的MFC(Microsoft Foundation Class,微软基础 ...

  6. BluetoothSocket详解

    一. BluetoothSocket简介 1. 简介 客户端与服务端 : BluetoothSocket 和 BluetoothServerSocket 类似于Java中的套接字的 Socket 和 ...

  7. holoeverywhere修改actionbar背景

    <style name="Holo.Theme.Light.MyActionBar" parent="Holo.Base.Theme.Light.DarkActio ...

  8. LintCode-67.二叉树的中序遍历

    二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...

  9. iOS-开发过程中应用间跳转问题

  10. TCP系列27—窗口管理&流控—1、概述

    在前面的内容中我们依次介绍了TCP的连接建立和终止过程和TCP的各种重传方式.接着我们在这部分首先关注交互式应用TCP连接相关内容如延迟ACK.Nagle算法.Cork算法等,接着我们引入流控机制(f ...