Recurrent Function
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1233   Accepted: 336

Description

Dr. Yao is involved in a secret research on the topic of the properties of recurrent function. Some of the functions in this research are in the following pattern:

in which set {ai} = {1, 2, …, d-1} and {bi} = {0, 1, …, d-1}.
We denote:

Yao's question is that, given two positive integer m and k, could you find a minimal non-negative integer x that

Input

There are several test cases. The first line of each test case contains an integer d (2≤d≤100). The second line contains 2d-1 integers: a1, …, ad-1, followed by b0, ..., bd-1. The third line contains integer m (0<m≤10100), and the forth line contains integer k (0<k≤10100). The input file ends with integer -1. 

Output

For each test case if it exists such an integer x, output the minimal one. We guarantee the answer is less than 263. Otherwise output a word "NO". 

Sample Input

2
1
1 0
4
7
2
1
0 1
100
200
-1

Sample Output

1
NO

Hint

For the first sample case, we can see that f(4)=7. And for the second one, the function is f(i)=i
 
题意:求解递归方程,该类方程在具体数学第一章中有明确介绍,这里只给结论,若要求解f(x),可以先将x转化成c进制c进制一共m位,即(xmxm-1...x1x0)c,那么有结论:
f((xmxm-1...x1x0)c)=(axmbxm-1bxm-2...bx1bx0)c
思路:结论已介绍,可以看出将x化成c进制后每一位都在进行置换操作,c进制首位用ai来置换,后面几位都用bi来置换。那么如果将k也转化成d进制,那么如果m的d进制的每一位通过不断置换之后最终与k的每一位分别相等,置换的最小次数x就是所求。
我们设最少置换x次,m的d进制的每一位各自置换到k的每一位需要ci次,并且每一位置换足够次数会产生轮回,轮回的大小记为loop(i)
那么x满足条件:
x==c1 mod(loop(1))
x==c2 mod(loop(2))
.....
x==cn mod(loop(n))
这样就是求满足上述模方程组的最小解即可。
 
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<set>
#include<string>
#include<queue>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
const int N_MAX = +;
typedef long long ll;
// 大数类
class bign
{
#define base 1000
#define digit 3
private:
int _arr[];
int _m;
void bign::_simplify(void)
{
for (int i = ; i <= _m; i++)
{
if (i == _m && _arr[i] >= base) _m++;
_arr[i + ] += _arr[i] / base;
_arr[i] %= base;
}
}
public:
bign::bign(void) : _m() { memset(_arr, , sizeof(_arr)); }
bign::bign(int init) : _m()
{
memset(_arr, , sizeof(_arr));
_arr[] = init;
_simplify();
}
friend istream& operator >> (istream& fin, bign& a)
{
char init[]; int len, b, t;
fin >> init;
len = strlen(init); a._m = -;
for (int i = len - ; i >= ;)
{
t = , b = ;
for (int j = ; j < digit && i >= ; j++, i--)
{
t += (init[i] - '') * b;
b *= ;
}
a._arr[++a._m] = t;
}
return fin;
}
friend bign operator / (bign a, int b)
{
for (int i = a._m; i >= ; i--)
{
if (a._arr[i] < b && i == a._m && i != ) a._m--;
if (i > ) a._arr[i - ] += (a._arr[i] % b) * base;
a._arr[i] /= b;
} return a;
}
friend int operator % (bign a, int b)
{
for (int i = a._m; i >= ; i--)
{
if (i == ) return a._arr[i] % b;
else a._arr[i - ] += (a._arr[i] % b) * base;
}
}
friend bool operator == (bign a, bign b)
{
if (a._m != b._m) return false;
for (int i = ; i <= a._m; i++)
if (a._arr[i] != b._arr[i]) return false;
return true;
}
}; pair<ll,ll> trans(ll *a,ll m,ll k ) {//采用a置换,m为需要置换的数,k为m置换的终点,返回循环次和圈的大小
ll num = ;//num为从m到k的循环次数,不存在则为-1
ll tmp = m;
while (tmp!=k) {
if (num != && tmp == m) { num = -; break; }
num++;
tmp = a[tmp];
}
if (num == -)return make_pair(-, );
int loop = ;
while () {
if (loop != && tmp == k) { break; }
loop++;
tmp = a[tmp];
}
return make_pair(num,loop);
} ll gcd(ll a,ll b) {
if (b == )return a;
return gcd(b, a%b);
} ll extgcd(ll a,ll b,ll &x,ll &y) {
if (b == ) {
x = ; y = ;
return a;
}
ll ans = extgcd(b,a%b,x,y);
ll tmp = x;
x = y;
y = tmp - a / b*y;
return ans;
}
ll mod_inverse(ll a,ll m) {
ll x, y;
extgcd(a,m,x,y);
return (m + x%m) % m;
} pair<ll, ll>linear_congruence(const ll *A,const ll *B,const ll*M,const int& num) {
ll x = ,m = ;
for (int i = ; i < num;i++) {
ll a = A[i] * m, b = B[i] - A[i] * x, d = gcd(M[i], a);
if (b%d != )return make_pair(, -);
ll t = b / d*mod_inverse(a / d, M[i] / d) % (M[i] / d);
x = x + m*t;
m *= M[i] / d;
}
return make_pair((x%m+m)%m, m);
} ll d;
ll a[N_MAX], b[N_MAX];
bign k, m;
ll kd[N_MAX], md[N_MAX];//存储k和m的D进制形式
ll A[N_MAX], B[N_MAX], M[N_MAX];
int main() {
while(scanf("%lld",&d)&&d!=-) {
for (int i = ; i < d;i++)scanf("%lld",&a[i]);
for (int i = ; i < d;i++)scanf("%lld",&b[i]);
cin >> m >> k;
int num = ;
while (!(m==)) {
md[num++] = m%d;
m =m/ d;
}
int num2 = ;
while (!(k == )) {
kd[num2++] = k%d;
k = k / d;
}
if (num != num2) { puts("NO"); continue; }
pair<ll, ll>P;
bool flag = ;
for (int i = ; i < num-;i++) {
P = trans(b,md[i],kd[i]);
if (P.first == -) { puts("NO"); flag = ; break; }
A[i] = , B[i] = P.first, M[i] = P.second;
}
if (flag)continue; P = trans(a, md[num - ], kd[num - ]);
if (P.first == -) { puts("NO"); continue; }
A[num - ] = , B[num - ] = P.first, M[num - ] = P.second;
P = linear_congruence(A,B,M,num);
if(P.second==-) { puts("NO"); continue; }
else printf("%lld\n",P.first);
}
return ;
}

poj 3708 Recurrent Function的更多相关文章

  1. [转] POJ数学问题

    转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...

  2. acm数学(转)

    这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法    这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...

  3. ACM数学

     1.burnside定理,polya计数法 这个专题我单独写了个小结,大家可以简单参考一下:polya 计数法,burnside定理小结 2.置换,置换的运算 置换的概念还是比较好理解的,< ...

  4. (转) Written Memories: Understanding, Deriving and Extending the LSTM

    R2RT   Written Memories: Understanding, Deriving and Extending the LSTM Tue 26 July 2016 When I was ...

  5. 从rnn到lstm,再到seq2seq(一)

    rnn的的公式很简单: 对于每个时刻,输入上一个时刻的隐层s和这个时刻的文本x,然后输出这个时刻的隐层s.对于输出的隐层s 做个ws+b就是这个时刻的输出y. tf.scan(fn, elems, i ...

  6. poj3708(公式化简+大数进制装换+线性同余方程组)

    刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...

  7. ACM数学知识体系

    在盛情收到学弟邀请给他们整理ACM数学方面的知识体系,作为学长非常认真的弄了好久,希望各学弟不辜负学长厚爱!!!非常抱歉因为电脑全盘格式化好多word.PPT都丢失,我尽量具体地给大家找到各知识点学习 ...

  8. Image Captioning 经典论文合辑

    Image Caption: Automatically describing the content of an image domain:CV+NLP Category:(by myself, y ...

  9. 通过百度echarts实现数据图表展示功能

    现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...

随机推荐

  1. Java写诗程序

    import java.util.Random; public class test_word { public static void main(String[] args) { System.ou ...

  2. Luogu P1666 前缀单词

    校内资格赛题目,差点高一就要\(\tt{AFO}\)了 30分思路 对30%的数据,满足$1≤n≤10 $ 所以我们可以子集枚举,实际得分40pts #include<iostream> ...

  3. VS快捷键总结(开发中经常遇到)

    1.窗口快捷键  (大家有没有发现但凡跟窗口挂上钩的快捷键当中都有一个W,那是因为W代表Windows也就是窗口的意思) Ctrl+W,W: 浏览器窗口 (浏览橱窗用有道的翻译是window shop ...

  4. 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers

    我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...

  5. vue.js笔记1.0

    事件: 事件冒泡行为: 1.@click="show($event)" show:function (ev) { ev.cancelBubble=true; } 2.@click. ...

  6. Win2008 Server配置PHP环境

    Win2008 Server配置PHP环境   阅读目录 创建一个网站 配置PHP环境 配置iis的“处理应用程序映射” 在配置PHP环境之前要先配置好IIS. 传送门-> Win2008 Se ...

  7. 如何在微信中发送"相册"文件时有选择性地显示视频文件

    相信很多微信用户在使用微信给朋友,同事发送相册中的文件时,微信会显示你手机中的视频文件,这样很不方便. 如果要完全不显示视频文件: 随便在手机中建立一个文件夹,名字叫 ".nomedia&q ...

  8. LeetCode(279)Perfect Squares

    题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...

  9. hdu 4565

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  10. CodeForces 489F DP Special Matrices

    首先统计一下前m行中,有x列1的个数为0,有y列1的个数为1. 设d(i, j)表示有i列1的个数为0,有j列1的个数为1,能到达这个状态的矩阵的个数. 则d(x, y) = 1 每一行都是两个1一起 ...