题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1005
题目大意:

f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
思路:

由于模7,循环节一定在49以内,所以计算前49位,找到循环节,直接输出a[n]即可。

start为循环节开始的下标,len为循环节长度,计算a[n]时,如果 n <= start,直接输出a[n],如果大于start,输出a[start + (n - start) % len];

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int maxn = 1e4 + ;
typedef long long ll;
int T, n, m;
int a[maxn];
int main()
{
int x;
while(cin >> n >> m >> x && (n + m + x))
{
memset(a, , sizeof(a));
a[] = , a[] = ;
bool flag = ;
int start, len;
for(int i = ; ; i++)
{
a[i] = n * a[i - ] + m * a[i - ];
a[i] %= ;
for(int j = ; j <= i - ; j++)
{
if(a[j] == a[i - ] && a[j + ] == a[i])
{
flag = ;
start = j;
len = i - j - ;
}
if(flag)break;
}
if(flag)break;
}/*
for(int i = 1; i < start; i++)cout<<a[i]<<" ";
for(int i = start; i <= start + len; i++)cout<<a[i]<<" ";
cout<<endl;
cout<<start<<" "<<len<<endl;*/
if(x <= start)cout<<a[x]<<endl;
else cout<<a[start + (x - start) % len]<<endl;
}
return ;
}

hdu1005 Number Sequence---找循环节的更多相关文章

  1. hdu 1005 Number Sequence(循环节)

    题意,f(1)=1,f(2)=1,f(n)=a*f(n-1)+b*f(n-2),求f(n)%7 这个题可能数据不够严谨,所以有些错误的做法也可以通过,比如7 7 50,应该输出0而不是1 解:找到关键 ...

  2. hdu1005 Number Sequence(寻找循环节)

    主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...

  3. "红色病毒"问题 HDU 2065 递推+找循环节

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=2065 递推类题目, 可以考虑用数学方法来做, 但是明显也可以有递推思维来理解. 递推的话基本就是状态 ...

  4. HDU1005 Number Sequence(找规律,周期是变化的)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...

  5. poj1026(置换找循环节)

    找到循环节,然后对应的变换 Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20377   Accepted:  ...

  6. HNU 12869 Sequence(循环节)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12869 解题报告:看到n的范围这么大,一看就是找规律,把前30 ...

  7. UVA 10706 Number Sequence (找规律 + 打表 + 查找)

    Problem B Number Sequence Input: standard input Output: standard output Time Limit: 1 second A singl ...

  8. hdu 1005(找循环节)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. hdu1005 Number Sequence(数论)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  10. HDU1005 Number Sequence (奇技淫巧模拟)

    A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...

随机推荐

  1. 笔记:Struts2 文件上传和下载

    为了上传文件必须将表单的method设置为POST,将 enctype 设置为 muiltipart/form-data,只有设置为这种情况下,浏览器才会把用户选择文件的二进制数据发送给服务器. 上传 ...

  2. poj 2681 字符串

    http://poj.org/problem?id=2681 给你任意长度的字符串,找出两串字符中不相同的字符个数(总数) #include<string> #include<cst ...

  3. 利用whoosh对mongoDB的中文文档建立全文检索

    1.建立索引 #coding=utf-8 from __future__ import unicode_literals __author__ = 'zh' import sys,os from wh ...

  4. 【ASP.NET Core】如何隐藏响应头中的 “Kestrel”

    全宇宙人民都知道,ASP.NET Core 应用是不依赖服务器组件的,因此它可以独立运行,一般是使用支持跨平台的 Kestrel 服务器(当然,在 Windows 上还可以考虑用 HttpSys,但要 ...

  5. 玩转接口测试工具fiddler 教程系列1

    我们在做web测试的时候,很多时候需要查看接口发送的数据返回的数据是否正常,这样可以排除是客户端的问题还是服务器的问题,举个例子来说,如果我们发现页面上面数据少了, 通过fiddler查看数据返回就少 ...

  6. android中xml tools属性详解(转)

    第一部分 安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果. 但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了 ...

  7. Android中文API (110) —— CursorTreeAdapter

    前言 本章内容是android.widget.CursorTreeAdapter,版本为Android 3.0 r1,翻译来自"深夜未眠",欢迎访问它的博客:"http: ...

  8. Oracle Orion tool check io(ORACLE Orion 工具查看以及校验IO)

    文档主要来自oracle官方文档performance 8.3章节 Oracle数据库提供了Orion,一种 I/O校准工具.Orion是预测Oracle数据库性能的工具,无需安装Oracle或创建数 ...

  9. java使用io创建文件与删除文件的工具类

    java中对于文件的操作,是再常见不过了.以下代码是自己代码中所用到的工具类,仅供参考. import java.io.File; import java.io.IOException; /** * ...

  10. node 基础精简

    Node 创建node应用 引入require模块   var http = require("http"); 创建服务器   http.createServer() 绑定端口: ...