题意:f(1)="a",f(2)="b",f(i)=f(i-1)+f(i-2),"+"表示连接符。给定n,m,求f(n)的前m个字符的“next值”。

思路:并不知道如何一步步推出结论,只能打个表找找规律了:找到最小的i使得f(i)>m+1,则答案就是m-f(i-2)。然后就是大整数模板了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
#define X                   first
#define Y                   second
#define pb                  push_back
#define mp                  make_pair
#define all(a)              (a).begin(), (a).end()
#define fillchar(a, x)      memset(a, x, sizeof(a))
 
typedef long long ll;
typedef pair<intint> pii;
typedef unsigned long long ull;
 
#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}
 
const double PI = acos(-1.0);
const int INF = 1e9 + 7;
 
/* -------------------------------------------------------------------------------- */
 
 
struct BigInt {
    const static int maxI = 1e8;
    const static int Len = 8;
    typedef vector<int> vi;
    typedef long long LL;
    vi num;
    bool symbol;
 
    BigInt() {
        num.clear();
        symbol = 0;
    }
    BigInt(int x) {
        symbol = 0;
        if (x < 0) {
            symbol = 1;
            x = -x;
        }
        num.push_back(x % maxI);
        if (x >= maxI) num.push_back(x / maxI);
    }
    BigInt(bool s, vi x) {
        symbol = s;
        num = x;
    }
    BigInt(char s[]) {
        int len = strlen(s), x = 1, sum = 0, p = s[0] == '-';
        symbol = p;
        for (int i = len - 1; i >= p; i--) {
            sum += (s[i] - '0') * x;
            x *= 10;
            if (x == 1e8 || i == p) {
                num.push_back(sum);
                sum = 0;
                x = 1;
            }
        }
        while (num.back() == 0 && num.size() > 1) num.pop_back();
    }
 
    void push(int x) {
        num.push_back(x);
    }
 
    BigInt abs() const {
        return BigInt(false, num);
    }
 
    bool smaller(const vi &a, const vi &b) const {
        if (a.size() != b.size()) return a.size() < b.size();
        for (int i = a.size() - 1; i >= 0; i--) {
            if (a[i] != b[i]) return a[i] < b[i];
        }
        return 0;
    }
 
    bool operator < (const BigInt &p) const {
        if (symbol && !p.symbol) return true;
        if (!symbol && p.symbol) return false;
        if (symbol && p.symbol) return smaller(p.num, num);
        return smaller(num, p.num);
    }
 
    bool operator > (const BigInt &p) const {
        return p < *this;
    }
 
    bool operator == (const BigInt &p) const {
        return !(p < *this) && !(*this < p);
    }
 
    bool operator >= (const BigInt &p) const {
        return !(*this < p);
    }
 
    bool operator <= (const BigInt &p) const {
        return !(p < *this);
    }
 
    vi add(const vi &a, const vi &b) const {
        vi c;
        c.clear();
        int x = 0;
        for (int i = 0; i < a.size(); i++) {
            x += a[i];
            if (i < b.size()) x += b[i];
            c.push_back(x % maxI);
            x /= maxI;
        }
        for (int i = a.size(); i < b.size(); i++) {
            x += b[i];
            c.push_back(x % maxI);
            x /= maxI;
        }
        if (x) c.push_back(x);
        while (c.back() == 0 && c.size() > 1) c.pop_back();
        return c;
    }
 
    vi sub(const vi &a, const vi &b) const {
        vi c;
        c.clear();
        int x = 1;
        for (int i = 0; i < b.size(); i++) {
            x += maxI + a[i] - b[i] - 1;
            c.push_back(x % maxI);
            x /= maxI;
        }
        for (int i = b.size(); i < a.size(); i++) {
            x += maxI + a[i] - 1;
            c.push_back(x % maxI);
            x /= maxI;
        }
        while (c.back() == 0 && c.size() > 1) c.pop_back();
        return c;
    }
 
    vi mul(const vi &a, const vi &b) const {
        vi c;
        c.resize(a.size() + b.size());
        for (int i = 0; i < a.size(); i++) {
            for (int j = 0; j < b.size(); j++) {
                LL tmp = (LL)a[i] * b[j] + c[i + j];
                c[i + j + 1] += tmp / maxI;
                c[i + j] = tmp % maxI;
            }
        }
        while (c.back() == 0 && c.size() > 1) c.pop_back();
        return c;
    }
 
    vi div(const vi &a, const vi &b) const {
        vi c(a.size()), x(1, 0), y(1, 0), z(1, 0), t(1, 0);
        y.push_back(1);
        for (int i = a.size() - 1; i >= 0; i--) {
            z[0] = a[i];
            x = add(mul(x, y), z);
            if (smaller(x, b)) continue;
            int l = 1, r = maxI - 1;
            while (l < r) {
                int m = (l + r + 1) >> 1;
                t[0] = m;
                if (smaller(x, mul(b, t))) r = m - 1;
                else l = m;
            }
            c[i] = l;
            t[0] = l;
            x = sub(x, mul(b, t));
        }
        while (c.back() == 0 && c.size() > 1) c.pop_back();
        return c;
    }
 
    BigInt operator + (const BigInt &p) const {
        if (!symbol && !p.symbol) return BigInt(false, add(num, p.num));
        if (!symbol && p.symbol) {
            return *this >= p.abs() ?
            BigInt(false, sub(num, p.num)) : BigInt(true, sub(p.num, num));
        }
        if (symbol && !p.symbol) {
            return (*this).abs() > p ?
            BigInt(true, sub(num, p.num)) : BigInt(false, sub(p.num, num));
        }
        return BigInt(true, add(num, p.num));
    }
 
    BigInt operator - (const BigInt &p) const {
        return *this + BigInt(!p.symbol, p.num);
    }
 
    BigInt operator * (const BigInt &p) const {
        BigInt res(symbol ^ p.symbol, mul(num, p.num));
        if (res.symbol && res.num.size() == 1 && res.num[0] == 0)
            res.symbol = false;
        return res;
    }
 
    BigInt operator / (const BigInt &p) const {
        if (p == BigInt(0)) return p;
        BigInt res(symbol ^ p.symbol, div(num, p.num));
        if (res.symbol && res.num.size() == 1 && res.num[0] == 0)
            res.symbol = false;
        return res;
    }
 
    BigInt operator % (const BigInt &p) const {
        return *this - *this / p * p;
    }
 
    void show() const {
        if (symbol) putchar('-');
        printf("%d", num[num.size() - 1]);
        for (int i = num.size() - 2; i >= 0; i--) {
            printf("%08d", num[i]);
        }
        putchar('\n');
    }
 
    int TotalDigit() const {
        int x = num[num.size() - 1] / 10, t = 1;
        while (x) {
            x /= 10;
            t++;
        }
        return t + (num.size() - 1) * Len;
    }
 
};
const int md = 258280327;
BigInt f[1234];
char s[1000];
 
void pre_init() {
    f[0] = 0;
    f[1] = f[2] = 1;
    for (int i = 3; i <= 1111; i ++) {
        f[i] = f[i - 1] + f[i - 2];
    }
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt""r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    pre_init();
    int T;
    cin >> T;
    while (T --) {
        int n;
        cin >> n;
        scanf("%s", s);
        BigInt buf(s);
        int L = 1, R = 1111;
        while (L < R) {
            int M = (L + R) >> 1;
            if (f[M] > buf + 1) R = M;
            else L = M + 1;
        }
        ((buf - f[L - 2]) % md).show();
    }
    return 0;
}

[hdu5351]找规律,大整数模板的更多相关文章

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

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

  2. [带符号大整数模板]vector版

    #include <iostream> #include <cstdio> #include <vector> #include <cstring> u ...

  3. OpenJudge 2737 大整数除法

    链接地址:http://bailian.openjudge.cn/practice/2737/ 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 求2个大的正整数相除的商 输入 第 ...

  4. vijos - P1447开关灯泡 (大数模板 + 找规律 + 全然数 + python)

    P1447开关灯泡 Accepted 标签:CSC WorkGroup III[显示标签] 描写叙述 一个房间里有n盏灯泡.一開始都是熄着的,有1到n个时刻.每一个时刻i,我们会将i的倍数的灯泡改变状 ...

  5. Pollard-Rho大整数拆分模板

    随机拆分,简直机智. 关于过程可以看http://wenku.baidu.com/link?url=JPlP8watmyGVDdjgiLpcytC0lazh4Leg3s53WIx1_Pp_Y6DJTC ...

  6. HDU - 4722 Good Numbers 【找规律 or 数位dp模板】

    If we sum up every digit of a number and the result can be exactly divided by 10, we say this number ...

  7. hdu 5051 找规律?+大trick

    http://acm.hdu.edu.cn/showproblem.php?pid=5051 打表找规律 据说是http://zh.wikipedia.org/wiki/%E6%9C%AC%E7%A6 ...

  8. 大整数类BIGN的设计与实现 C++高精度模板

    首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...

  9. [国家集训队]整数的lqp拆分 数学推导 打表找规律

    题解: 考场上靠打表找规律切的题,不过严谨的数学推导才是本题精妙所在:求:$\sum\prod_{i=1}^{m}F_{a{i}}$ 设 $f(i)$ 为 $N=i$ 时的答案,$F_{i}$ 为斐波 ...

随机推荐

  1. 一个好的olap框架

    一.何为一个好的olap框架? 框架大概分为两种: (1)底层技术框架,专注于抽象底层技术,如网络通信netty.中间件kafka等 (2)开发人员框架,专注于提高开发效率,如spring的面向切面和 ...

  2. ROM定制开发教程-Android adb命令用法与实例解析

    一.什么是ADB Android Debug Bridge(adb)是一个命令行工具,可让您与模拟器或连接的Android设备进行通信.您可以在android sdk / platform-tools ...

  3. 百度关键词搜索工具 v1.1|url采集工具 v1.1

    功能介绍:关键词搜索工具 批量关键词自动搜索采集 自动去除垃圾二级泛解析域名 可设置是否保存域名或者url 持续更新中

  4. 【08NOIP提高组】笨小猴

    笨 小 猴 来自08年NOIP提高组的第一题 1.题目描述 [题目描述] 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头痛.经实验证明,用这种方法去选择选项的时候选对的几率非常大!这种方法的具体 ...

  5. deepin右键发送博客

    1. 概述 deepin系统上没有像样的笔记软件.为知笔记未提供deb打包的软件,很遗憾.商店提供的软件,界面停留在上个世纪了. 这个时候如果想通过笔记来分享到博客是一件非常困难的事情. 本篇博客就是 ...

  6. ado.net 面向对象

    面向对象:就是一个大的转换器,建立起一条通道通往数据库然后通过通道将所需(方法)数据从转换器往返于外部界面端 1   首先在项目里创建文件夹:      右击项目———添加个文件夹App_Cod 2 ...

  7. Mybatis源码详解系列(三)--从Mapper接口开始看Mybatis的执行逻辑

    简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...

  8. 13.create-react-app 构建的项目使用代理 proxy

    1. 正常运行 npm run eject 2. create-react-app 的版本在低于 2.0 的时候可以在 package.json 增加 proxy 配置, 配置成如下: "p ...

  9. 几个可以提高工作效率的Python内置小工具

    在这篇文章里,我们将会介绍4个Python解释器自身提供的小工具.这些小工具在笔者的日常工作中经常用到,减少了各种时间的浪费,然而,却很容易被大家忽略.每当有新来的同事看到我这么使用时,都忍不住感叹, ...

  10. 数值计算方法实验之newton多项式插值 (Python 代码)

    一.实验目的 在己知f(x),x∈[a,b]的表达式,但函数值不便计算或不知f(x),x∈[a,b]而又需要给出其在[a,b]上的值时,按插值原则f(xi)=yi (i=0,1,……, n)求出简单函 ...