多校10 1002 HDU 6172 Array Challenge

题意

There’s an array that is generated by following rule.

\(h_0=2,h_1=3,h_2=6,h_n=4h_{n-1}+17h_{n-2}-12h_{n-3}-16\)

And let us define two arrays bnandan as below.

\(
b_n=3h_{n+1} h_n+9h_{n+1} h_{n-1}+9h_n^2+27h_n h_{n-1}-18h_{n+1}-126h_n-81h_{n-1}+192(n>0)
\)

\(
a_n=b_n+4^n
\)

Now, you have to print \(\left \lfloor \sqrt{a_n} \right \rfloor\) , n>1.

Your answer could be very large so print the answer modular 1000000007.

题解

首先要打表,然后就可以:

方法1,找规律:

令\(f_n=\left \lfloor \sqrt{a_n} \right \rfloor\),打表出来,发现接近7倍关系,再打出 \(7f_{n-1}-f_{n}\),会发现是\(f_{n-2}\)的4倍,所以\(f_n=7f_{n-1}-4f_{n-2}\)。再用矩阵快速幂。注意取模有负数。

方法2,猜:

前几项和h的前几项差不多,于是模仿h的递推式,\(f_n=4f_{n-1}+17f_{n-2}-12f_{n-3}\),刚好符合。

方法3,BM算法:

如果递推式是线性的,就把前几项带进去就可以得到递推式。

具体算法介绍建议看这个:Berlekamp–Massey algorithm(From Wikipedia)

"Shift-register synthesis and BCH decoding"

方法4,数学递推,我不会。

代码

#include <cstdio>
#include <map>
#include <cstdlib>
#include <queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,l,r) for (int i=l;i<r;++i)
typedef unsigned long long ull;
int dx[4]={1,1,-1,-1},dy[4]={0,1,0,-1};
map<ull,bool>vis;
struct Sta{
int a[6][6],step,x,y;
Sta(){step=x=y=0;}
};
int gujia(Sta s){
int ans=0;
rep(i,0,6)rep(j,0,i+1)
if(s.a[i][j])ans+=abs(s.a[i][j]-i);
return ans;
}
ull haxi(Sta s){
ull ans=0;
rep(i,0,6)rep(j,0,i+1){
ans<<=3;ans|=s.a[i][j];
}
return ans;
}
int bfs(Sta s){
vis.clear();
queue<Sta>q;q.push(s);
while(!q.empty()){
Sta now=q.front();q.pop();
if(gujia(now)==0)return now.step;
rep(i,0,4){
int x=now.x,y=now.y;
int nx=x+dx[i],ny=y+dy[i];
if(nx>=0 && nx<6 && ny>=0 && ny<=nx){
swap(now.a[x][y],now.a[nx][ny]);
now.x=nx,now.y=ny,++now.step;
ull hx=haxi(now);
if(!vis[hx]&&gujia(now)+now.step<21){
q.push(now);
vis[hx]=true;
}
swap(now.a[x][y],now.a[nx][ny]);
now.x-=dx[i],now.y-=dy[i],--now.step;
}
}
}
return -1;
}
int main() {
int t;
scanf("%d",&t);
while(t--){
Sta s;
rep(i,0,6)
rep(j,0,i+1){
scanf("%d",&s.a[i][j]);
if(s.a[i][j]==0)s.x=i,s.y=j;
}
int ans=bfs(s);
if(ans==-1)puts("too difficult");else printf("%d\n",ans);
}
return 0;
}
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define pb push_back
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
const ll mod=1000000007;
ll qpow(ll a,ll b) {ll res=1;for(a%=mod;b;b>>=1,a=a*a%mod)if(b&1)res=res*a%mod;return res;}
VI BM(VI s) {//c[0]s[0]+c[1]s[1]+...=0
VI C(1,1),B(1,1);
int L=0,m=1,rev=1;
rep(n,0,SZ(s)) {
ll d=0;
rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
if (d==0) ++m;
else if (2*L<=n) {
VI T=C;
ll c=mod-d*rev%mod;
C.resize(SZ(B)+m);
rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
L=n+1-L; B=T; rev=qpow(d,mod-2); m=1;
} else {
ll c=mod-d*rev%mod;
C.resize(SZ(B)+m);
rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
++m;
}
}
rep(i,0,SZ(C))printf("%dx[%d]%s",C[i],i,i+1==SZ(C)?"=0\n":"+");
return C;
}
调用:BM(VI{31,197,1255,7997})

【hdu 6172】Array Challenge(数列、找规律)的更多相关文章

  1. 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...

  2. HDU - 6172:Array Challenge (BM线性递推)

    题意:给出,三个函数,h,b,a,然后T次询问,每次给出n,求sqrt(an); 思路:不会推,但是感觉a应该是线性的,这个时候我们就可以用BM线性递推,自己求出前几项,然后放到模板里,就可以求了. ...

  3. HDU 3032 multi-sg 打表找规律

    普通NIM规则加上一条可以分解为两堆,标准的Multi-SG游戏 一般Multi-SG就是根据拓扑图计算SG函数,这题打表后还能发现规律 sg(1)=1 sg(2)=2 sg(3)=mex{0,1,2 ...

  4. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

  5. hdu 1030 Delta-wave(数学题+找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1030 Delta-wave Time Limit: 2000/1000 MS (Java/Others ...

  6. HDU 5703 Desert 水题 找规律

    已知有n个单位的水,问有几种方式把这些水喝完,每天至少喝1个单位的水,而且每天喝的水的单位为整数.看上去挺复杂要跑循环,但其实上,列举几种情况之后就会发现是找规律的题了= =都是2的n-1次方,而且这 ...

  7. HDU 4910 Problem about GCD 找规律+大素数判断+分解因子

    Problem about GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. HDU 4572 Bottles Arrangement(找规律,仔细读题)

    题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1 ...

  9. HDU 1041 Computer Transformation(找规律加大数乘)

    主要还是找规律,然后大数相乘 #include<stdio.h> #include<string.h> #include<math.h> #include<t ...

  10. HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

随机推荐

  1. react虚拟dom diff算法

    react虚拟dom:依据diff算法 前端:更新状态.更新视图:所以前端页面的性能问题主要是由Dom操作引起的,解放Dom操作复杂性 刻不容缓 因为:Dom渲染慢,而JS解析编译相对非常非常非常快! ...

  2. poj2226 Muddy Fields 填充棒子(二分匹配)

    参考博客:https://blog.csdn.net/liujc_/article/details/51287019 参考博客:https://blog.csdn.net/acdreamers/art ...

  3. B. Switches and Lamps

    链接 [https://codeforces.com/contest/985/problem/B] 题意 给你n,m,分别是n个开关,m个灯 给一个n*m的字符矩阵aij=1,表示i可以控制j这个灯 ...

  4. An error occurred while updating the entries. See the inner exception for details.

    EF插入或更新数据时出现错误提示:An error occurred while updating the entries. See the inner exception for details.的 ...

  5. 一个6亿的表a,一个3亿的表b,通过外间tid关联,你如何最快的查询出满足条件的第50000到第50200中的这200条数据记录

    1.如果A表TID是自增长,并且是连续的,B表的ID为索引 select * from a,b where a.tid = b.id and a.tid>500000 limit 200; 2. ...

  6. PS制作墙壁上海报卷页图片效果

    1.首先,打开PS,新建合适的画布. 2.为了使背景具有质感,执行滤镜—滤镜库—纹理化,具体参数按你的感觉来. 3.新建画布“图层1”,为了方便观察,填充为灰色画布,ctrl+t适当缩小画布大小,如图 ...

  7. 福州大学软件工程1816 | W班 第2次作业成绩排名

    作业链接 词频统计基础功能 评分细则 本次个人项目分数由两部分组成(博客分满分40分+程序得分满分60分) 博客评分规则 在文章开头给出你们Fork仓库的Github项目地址.(1') 在开始实现程序 ...

  8. shell脚本--CGI获取请求数据(GET / POST)

    Case 1: 获取地址栏传递的参数(即通过GET方式) CGI的环境变量中有个QUERY_STRING,可以获取地址栏传递的参数,该参数可以是手动加上的,也可以是通过表单的get方式提交的,比如下面 ...

  9. from、where、group、with、having、order、union、limit 的使用

    顺序很重要 每次看数据库的一些语法时,都很自然的略过那一大堆的规则,比如说线下面这段select的语法: select [field1,field2...] func_namefrom table1, ...

  10. 自己用习惯的idea快捷键笔记

    Ctrl + Space 自动完成(win10下冲突不能用,自己换成 Alt + \ ) 切换方法是菜单中依次打开 file -> settings -> keymap,搜索complet ...