• NOIP模拟 ———number(假的数位dp)


    NOIP模拟题

    题头

    如果一个数能够表示成两两不同的 3 的幂次的和,就说这个数是好的。
    比如 13 是好的,因为 13 = 9 + 3 + 1 。
    又比如 90 是好的,因为 90 = 81 + 9 。
    现在我们用 a[i] 表示第 i 小的好数。
    比如 a[1] = 1, a[2] = 3, a[5] = 10 。
    给定 L,R,请求出 a[L] 到 a[R] 的 和 mod 232。
    输入格式
    第一行一个整数 T,表示数据组数。
    接下来 T 行,每行两个整数 L,R 表示一组询问。
    输出格式
    输出 T 行,每行为一个整数,表示相应询问的答案。
    样例数据 1
    输入
    5
    1 3
    3 3
    4 5
    6 7
    2 5
    输出
    8
    4
    19
    25
    26
    备注
    【数据范围】
    对 30% 的输入数据:1≤T≤100;R≤1000 。
    对 100% 的输入数据:1≤T≤100000;1≤L≤R≤10^18 。

    看到这数据范围就知道不能暴力做(虽然还从没见到能暴力做的题)

    开始以为自己能写出正解,(蜜汁自信)然后写了两个多小时然后果断挂了(我真是个天才),还是应该先写暴力的
    实际上是一个类似数位dp的思想(虽然我几乎不会数位dp,回过头要好好补补)
    L和R前面所有数的差即为解,用三进制数表示,当然是一个01串,对于每一位,统计它后面所有位所有可能中每一位1出现的总次数乘以每一位得值再相加;

    贴出代码,其实很好理解,仔细想一下

    (还被卡常了)

    #include<bits/stdc++.h>
    using namespace std;
    #define ull unsigned long long
    #define ll long long 
    ll mod=1LL<<32LL,l,r;
    inline ull read(){
    	char ch;
    	while((ch=getchar())<'0'||ch>'9'){;}
    	ull res=ch-'0';
    	while((ch=getchar())>='0'&&ch<='9')
    	res=res*10+ch-'0';
    	return res;
    }
    inline ull calc(ull a){
    	ull pre=0,ans=0,two=1,three=1,sum=0;
    	int step=0;
    	for(;a;a>>=1,step++)
    	{
    		if(a&1)
    		{
    			ans+=pre*three%mod;
    			if(ans>=mod)
    			ans=ans-(ans/mod)*mod;
    			ans+=sum*two%mod;
    			if(ans>=mod)
    			ans=ans-(ans/mod)*mod;
    		}
    		if(step) two=two*2%mod;
    		pre=pre+(a&1)*two;
    		if(pre>=mod)
    		pre=pre-(pre/mod)*mod;
    		sum+=three;
    		if(sum>=mod)
    		sum=sum-(sum/mod)*mod;
    		three=three*3;
    		if(three>=mod)
    		three=three-(three/mod)*mod;
    	}
    	return ans;
    }
    int main(){
    	int T;
    	T=read();
    	ull tot;
    	while(T--)
    	{
    		l=read(),r=read();
    		tot=calc(r+1)-calc(l);
    		if(tot>=mod)
    		tot=tot-(tot/mod)*mod;
    		printf("%I64d
    ",tot);		
    	}
    	return 0;
    }
    
  • 相关阅读:
    javascritp学习笔记(四)----引用类型
    Bootstrap学习笔记(二)-----网格系统
    Javascript学习笔记(三)--变量、作用域和内存问题
    Javascript学习笔记(二)--创建对象(七种模式)
    Javascript学习笔记(一)--理解对象
    ReentrantLock
    synchronized使用
    多线程在web项目中的存在方式
    多线程基础
    java集合Map体系
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/10366523.html
Copyright © 2020-2023  润新知