启智树提高组day4T3 T3(t3.cpp,1s,512MB)

题面描述

输入格式

输出格式

样例输入

样例输出

数据范围

题解

task1

暴力dfs

10分

Code

 1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstring>
5 #include<cmath>
6 #include<map>
7 #include<set>
8 #include<queue>
9 #include<vector>
10 #define IL inline
11 #define re register
12 #define LL long long
13 #define ULL unsigned long long
14 using namespace std;
15 //1e1
16 template<class T>inline void read(T&x)
17 {
18 char ch=getchar();
19 while(!isdigit(ch))ch=getchar();
20 x=ch-'0';ch=getchar();
21 while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
22 }
23 int G[55];
24 template<class T>inline void write(T x)
25
26 {
27 int g=0;
28 do{G[++g]=x%10;x/=10;}while(x);
29 for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
30 }
31 const ULL M = 1e9+7;
32 ULL range[64]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296,8589934592,17179869184,34359738368,68719476736,137438953472,274877906944,549755813888,1099511627776,2199023255552,4398046511104,8796093022208,17592186044416,35184372088832,70368744177664,140737488355328,281474976710656,562949953421312,1125899906842624,2251799813685248,4503599627370496,9007199254740992,18014398509481984,36028797018963968,72057594037927936,144115188075855872,288230376151711744,576460752303423488,1152921504606846976,2305843009213693952,4611686018427387904,9223372036854775808};
33 ULL dfs(ULL num,ULL now)
34 {
35 if(num==0) return 1;
36 ULL ans=0;
37 for(int i=now;i>=0;i--)
38 {
39 if(range[i]>num) continue;
40 // cout<<"dfs"<<i<<endl;
41 ans+=dfs(num-range[i],i);
42 }
43 return ans%M;
44 }
45
46 ULL n,T;
47 int main()
48 {
49 cin>>T;
50 while(T--)
51 {
52 read(n);
53 write(dfs(n,64)%M);
54 }
55
56 return 0;
57 }

task2

加入记忆化(先用map)

Code

 1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstring>
5 #include<cmath>
6 #include<map>
7 #include<set>
8 #include<queue>
9 #include<vector>
10 #define IL inline
11 #define re register
12 #define LL long long
13 #define ULL unsigned long long
14 using namespace std;
15 //1e5
16 template<class T>inline void read(T&x)
17 {
18 char ch=getchar();
19 while(!isdigit(ch))ch=getchar();
20 x=ch-'0';ch=getchar();
21 while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
22 }
23 int G[55];
24 template<class T>inline void write(T x)
25 {
26 int g=0;
27 do{G[++g]=x%10;x/=10;}while(x);
28 for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
29 }
30 const ULL M = 1e9+7;
31 ULL range[64]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296,8589934592,17179869184,34359738368,68719476736,137438953472,274877906944,549755813888,1099511627776,2199023255552,4398046511104,8796093022208,17592186044416,35184372088832,70368744177664,140737488355328,281474976710656,562949953421312,1125899906842624,2251799813685248,4503599627370496,9007199254740992,18014398509481984,36028797018963968,72057594037927936,144115188075855872,288230376151711744,576460752303423488,1152921504606846976,2305843009213693952,4611686018427387904,9223372036854775808};
32 struct Pair{
33 ULL a,b;
34
35 };
36 bool operator<(Pair y,Pair x){
37 return (y.a==x.a)?y.b<x.b:y.a<x.a;
38 }
39 map<Pair,ULL>his;
40 ULL dfs(ULL num,ULL now)
41 {
42 if(num==0) return 1;
43 if(his.find({num,now})!=his.end()) return his[{num,now}];
44 ULL ans=0;
45 for(int i=now;i>=0;i--)
46 {
47 if(range[i]>num) continue;
48 // cout<<"dfs"<<i<<endl;
49 ans+=dfs(num-range[i],i);
50 }
51 return his[{num,now}]=ans%M;
52 }
53
54 ULL n,T;
55 int main()
56 {
57 cin>>T;
58 while(T--)
59 {
60 read(n);
61 write(dfs(n,64)%M);
62 }
63 return 0;
64 }

task3

不幸的是,刚刚两种方法都只有10分!!!

通过用task2打表发现

 1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstring>
5 #include<cmath>
6 #include<map>
7 #include<set>
8 #include<queue>
9 #include<vector>
10 #define IL inline
11 #define re register
12 #define LL long long
13 #define ULL unsigned long long
14 using namespace std;
15 //1e5
16 template<class T>inline void read(T&x)
17 {
18 char ch=getchar();
19 while(!isdigit(ch))ch=getchar();
20 x=ch-'0';ch=getchar();
21 while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
22 }
23 int G[55];
24 template<class T>inline void write(T x)
25 {
26 int g=0;
27 do{G[++g]=x%10;x/=10;}while(x);
28 for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
29 }
30 const ULL M = 1e9+7;
31 ULL range[64]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296,8589934592,17179869184,34359738368,68719476736,137438953472,274877906944,549755813888,1099511627776,2199023255552,4398046511104,8796093022208,17592186044416,35184372088832,70368744177664,140737488355328,281474976710656,562949953421312,1125899906842624,2251799813685248,4503599627370496,9007199254740992,18014398509481984,36028797018963968,72057594037927936,144115188075855872,288230376151711744,576460752303423488,1152921504606846976,2305843009213693952,4611686018427387904,9223372036854775808};
32 struct Pair{
33 ULL a,b;
34
35 };
36 bool operator<(Pair y,Pair x){
37 return (y.a==x.a)?y.b<x.b:y.a<x.a;
38 }
39 map<Pair,ULL>his;
40 ULL dfs(ULL num,ULL now)
41 {
42 if(num==0) return 1;
43 if(his.find({num,now})!=his.end()) return his[{num,now}];
44 ULL ans=0;
45 for(int i=now;i>=0;i--)
46 {
47 if(range[i]>num) continue;
48 // cout<<"dfs"<<i<<endl;
49 ans+=dfs(num-range[i],i);
50 }
51 return his[{num,now}]=ans%M;
52 }
53
54 ULL n,T,t;
55 int main()
56 {
57 cin>>T;
58 for(int i=1;i<=T;i++) cout<<i<<"\t"<<dfs(i,64)<<"\t"<<dfs(i,64)-n<<endl,n=dfs(i,64);
59 return 0;
60 }

记忆化打表

  1. 第i个和第i-1个的值相同(当i>1且i为奇数的时候)
  2. f[i]=f[i/2]+f[i-1]

差分一下就可以发现规律

http://oeis.org/search?q=1%2C2%2C4%2C6%2C10%2C14%2C20%2C26&sort=&language=english&go=Search

在OEIS上也能搜到

http://oeis.org/A000123

但是上面也只有这个式子

这里就可以用记忆化+打表,大概可以做出10^9

Code

 1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstring>
5 #include<cmath>
6 #include<map>
7 #include<set>
8 #include<queue>
9 #include<vector>
10 #define IL inline
11 #define re register
12 #define LL long long
13 #define ULL unsigned long long
14 using namespace std;
15 //1e5
16 template<class T>inline void read(T&x)
17 {
18 char ch=getchar();
19 while(!isdigit(ch))ch=getchar();
20 x=ch-'0';ch=getchar();
21 while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
22 }
23 int G[55];
24 template<class T>inline void write(T x)
25 {
26 int g=0;
27 do{G[++g]=x%10;x/=10;}while(x);
28 for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
29 }
30 const ULL M = 1e9+7;
31 //ULL range[63]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296,8589934592,17179869184,34359738368,68719476736,137438953472,274877906944,549755813888,1099511627776,2199023255552,4398046511104,8796093022208,17592186044416,35184372088832,70368744177664,140737488355328,281474976710656,562949953421312,1125899906842624,2251799813685248,4503599627370496,9007199254740992,18014398509481984,36028797018963968,72057594037927936,144115188075855872,288230376151711744,576460752303423488,1152921504606846976,2305843009213693952,4611686018427387904};
32 struct Pair{
33 ULL a,b;
34
35 };
36 bool operator<(Pair y,Pair x){
37 return (y.a==x.a)?y.b<x.b:y.a<x.a;
38 }
39 map<ULL,ULL>his;
40 ULL dfs(ULL num)
41 {
42 if(num==0) return 1;
43 if(num&1&&num>1) return dfs(num-1);
44 if(his.find(num)!=his.end()) return his[num];
45 ULL ans=0;
46 ans=dfs(num/2)+dfs(num-1);
47 return his[num]=ans%M;
48 }
49
50 ULL n,T;
51 int main()
52 {
53 cin>>T;
54 his[1]=1;
55 his[2]=2;
56 while(T--)
57 {
58 read(n);
59 write(dfs(n)%M);
60 }
61 return 0;
62 }

记忆化打表

Std

Code

 1 #include<bits/stdc++.h>
2 using namespace std;
3 using ll=long long;
4 const ll mod=1e9+7;
5 int T;
6 ll n,f[70][70],g[70][70],S[70][70],S1[70][70];
7 ll po(ll a,ll b){ll r=1;for(;b;b/=2,a=a*a%mod)if(b&1)r=r*a%mod;return r;}
8 ll solve(int x,int k){
9 if(!(n>>x))return k?0:1;
10 if(f[x][k]>-1e18)return f[x][k];
11 ll res=0,t=1;
12 for(int i=0;i<=k+1;i++)(res+=g[k][i]*t)%=mod,(t*=((n>>x)+1)%mod)%=mod;
13 (res*=solve(x+1,0))%=mod;t=1;
14 for(int i=0;i<=k+1;i++)(res-=solve(x+1,i)*g[k][i]%mod*t)%=mod,(t*=2)%=mod;
15 return f[x][k]=res;
16 }
17 int main(){
18 scanf("%d",&T);
19 for(int i=0;i<70;i++){
20 S[i][0]=S1[i][0]=!i;
21 for(int j=1;j<=i;j++)
22 S[i][j]=(S[i-1][j-1]+j*S[i-1][j])%mod,
23 S1[i][j]=(S1[i-1][j-1]+(i-1)*S1[i-1][j])%mod;
24 }
25 for(int k=0;k<=64;k++)
26 for(int j=0;j<=k;j++){
27 ll cf=S[k][j]*po(j+1,mod-2)%mod;
28 for(int i=0;i<=j+1;i++)(g[k][i]+=cf*S1[j+1][i]*(j+i&1?1:-1))%=mod;
29 }
30 while(T--){
31 scanf("%lld",&n);
32 memset(f,192,sizeof f);
33 printf("%lld\n",(solve(1,0)+mod)%mod);
34
35 }
36 return 0;
37 }

小结

别等黄花菜都凉了才——

别等服务器都炸了才交代码

启智树提高组day4T3 T3(t3.cpp,1s,512MB)的更多相关文章

  1. 【NOIP2015提高组】 Day1 T3 斗地主

    [题目描述] 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4& ...

  2. JZOJ 6904. 【2020.11.28提高组模拟】T3 树上询问(query)

    题目 你有一棵 \(n\) 节点的树 ,回答 \(m\) 个询问,每次询问给你两个整数 \(l,r\) ,问存在多少个整数 \(k\) 使得从 \(l\) 沿着 \(l \to r\) 的简单路径走 ...

  3. 【NOIP2016提高组】 Day1 T3 换教室

    题目链接:https://www.luogu.org/problemnew/show/P1850 此题正解为dp. 我们先用floyd处理出任意两个教室之间的距离,用dis[i][j]表示. 用f[i ...

  4. 【NOIP2015提高组】 Day2 T3 运输计划

    题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司,该公司有很多个运输计划,每个运输计划形如: ...

  5. 【NOIP2016提高组】 Day2 T3 愤怒的小鸟

    题目传送门:https://www.luogu.org/problemnew/show/P2831 说个题外话:NOIP2014也有一道题叫做愤怒的小鸟. 这题自测时算错了eps,导致被卡了精度,从1 ...

  6. 【NOIP2015模拟11.5】JZOJ8月5日提高组T3 旅行

    [NOIP2015模拟11.5]JZOJ8月5日提高组T3 旅行 题目 若不存在第\(k\)短路径时,输出"Stupid Mike" 题解 题意 给出一个有\(n\)个点的树 问这 ...

  7. JZOJ2020年8月11日提高组T3 页

    JZOJ2020年8月11日提高组T3 页 题目 Description 战神阿瑞斯听说2008年在中华大地上,将举行一届规模盛大的奥林匹克运动会,心中顿觉异常兴奋,他想让天马在广阔的天空上,举行一场 ...

  8. 【GDKOI2014】JZOJ2020年8月13日提高组T3 壕壕的寒假作业

    [GDKOI2014]JZOJ2020年8月13日提高组T3 壕壕的寒假作业 题目 Description Input Output 输出n行.第i行输出两个整数,分别表示第i份作业最早完成的时刻以及 ...

  9. JZOJ2020年8月10日提高组T3 玩诈欺的小杉

    JZOJ2020年8月10日提高组T3 玩诈欺的小杉 题目 Description 是这样的,在小杉的面前有一个N行M列的棋盘,棋盘上有\(N*M\)个有黑白棋的棋子(一面为黑,一面为白),一开始都是 ...

  10. 【佛山市选2013】JZOJ2020年8月7日提高组T3 海明距离

    [佛山市选2013]JZOJ2020年8月7日提高组T3 海明距离 题目 描述 对于二进制串a,b,他们之间的海明距离是指两个串异或之后串中1的个数.异或的规则为: 0 XOR 0 = 0 1 XOR ...

随机推荐

  1. 【插件介绍】Mesh2Geom插件

    Mesh to Geometry Plugin,来自达索官方论坛社区 原帖链接:Mesh to Geometry Plugin plugin feature: 允许Abaqus 用户从网格文件生成几何 ...

  2. Java的数据类型详解

    java的为强类型语言,所以要求变量的使用要严格符合规定,所有的变量都必须先定义后在使用: 什么是变量? 变量顾名思义,就是可变的量:是程序中最基本的存储单元,其要素要包括:变量名.变量类型和作用域: ...

  3. 【vscode】vscode配置汇编环境

    [vscode]vscode配置汇编环境 前言 ‍ 因为近来个人的课程涉及到汇编语言,加上个人目前是个vscode的重度使用者,所以,要捣鼓一下汇编的配置. 自然,有很多博客写过如何配置,但是每个人在 ...

  4. linux php安装mongodb 扩展

    下载扩展 首先从这个网站选择适合你当前 php 版本的的 mongodb 扩展 https://pecl.php.net/package/mongodb wget https://pecl.php.n ...

  5. CentOS 版本选择DVD、Everything、LiveCD、Minimal、NetInstall

    CentOS 7.X,主要是下载的时候有很多版本供选择,如何选择? DVD版:这个是常用版本,就是普通安装版了,推荐大家安装.里面包含大量的常用软件,大部分情况下安装时无需再在线下载,体积为4G.Ev ...

  6. CSAPP学习笔记——Chapter10,11 系统级I/O与网络编程

    CSAPP学习笔记--Chapter10,11 系统级I/O与网络编程 Chapter10 系统级I/O 系统级I/O这一章的内容,主要可以通过这张图概括: Unix I/O模型是在操作系统内核中实现 ...

  7. 网络编程-关闭连接(2)-Java的NIO在关闭socket时,究竟用了哪个系统调用函数?

    背景 在上一讲网络编程-关闭连接-C/C++相关系统调用中,提到过,目前项目使用Netty框架来实现的网络编程,查看netty源码可以得知,netty最终是调用了java Nio的close接口做的关 ...

  8. apisix~hmac-auth插件的使用

    hmac-auth插件需要和 Consumer 一起使用,API 的使用者必须将密匙添加到请求头中以验证其请求,下面介绍它的主要用法 参数 algorithm 算法 默认hmac-sha256 [&q ...

  9. 【虚拟机】VirtualBox设置共享文件夹

    VirtualBox设置共享文件夹 1.选中你要设置的虚拟机,点设置 2.共享文件夹,点右边的加号,设置一个共享文件夹路径,选择其他, 3.选一个你知道的位置,比如我的在E盘的共享文件夹下面 4.选好 ...

  10. Tengine-rpm 基于Tengine 3.1深度定制优化

    Tengine RPM Tengine是亚洲最大的电子商务网站淘宝网推出的高性能的HTTP和反向代理web服务器.它基于 Nginx HTTP 服务器,拥有许多高级功能.事实证明,Tengine 在淘 ...