poj 3708 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
Output
Sample Input
2
1
1 0
4
7
2
1
0 1
100
200
-1
Sample Output
1
NO
Hint
#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的更多相关文章
- [转] POJ数学问题
转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...
- acm数学(转)
这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...
- ACM数学
1.burnside定理,polya计数法 这个专题我单独写了个小结,大家可以简单参考一下:polya 计数法,burnside定理小结 2.置换,置换的运算 置换的概念还是比较好理解的,< ...
- (转) Written Memories: Understanding, Deriving and Extending the LSTM
R2RT Written Memories: Understanding, Deriving and Extending the LSTM Tue 26 July 2016 When I was ...
- 从rnn到lstm,再到seq2seq(一)
rnn的的公式很简单: 对于每个时刻,输入上一个时刻的隐层s和这个时刻的文本x,然后输出这个时刻的隐层s.对于输出的隐层s 做个ws+b就是这个时刻的输出y. tf.scan(fn, elems, i ...
- poj3708(公式化简+大数进制装换+线性同余方程组)
刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...
- ACM数学知识体系
在盛情收到学弟邀请给他们整理ACM数学方面的知识体系,作为学长非常认真的弄了好久,希望各学弟不辜负学长厚爱!!!非常抱歉因为电脑全盘格式化好多word.PPT都丢失,我尽量具体地给大家找到各知识点学习 ...
- Image Captioning 经典论文合辑
Image Caption: Automatically describing the content of an image domain:CV+NLP Category:(by myself, y ...
- 通过百度echarts实现数据图表展示功能
现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...
随机推荐
- 最大长度回文子串(Manacher's algorithm)
输出最大长度的回文子串. string longestPalindrome(string s) { int id, mx, i, j, len, maxlen; vector<char> ...
- #ifdef #else #endif #fi #ifndef 的用法
预处理就是在进行编译的第一遍词法扫描和语法分析之前所作的工作.说白了,就是对源文件进行编译前,先对预处理部分进行处理,然后对处理后的代码进行编译.这样做的好处是,经过处理后的代码,将会变的很精短. ...
- Your Ride Is Here
纯粹的水题= = /* ID:yk652321 LANG:C++ TASK:ride */ #include<iostream> #include<cstring> #incl ...
- Java常用的一些容器
转自:https://www.cnblogs.com/LipeiNet/p/5888513.html 前言:在java开发中我们肯定会大量的使用集合,在这里我将总结常见的集合类,每个集合类的优点和缺点 ...
- php代码压缩
php代码压缩,除可以使用token_get_all进行压缩之外,还可以使用系统自带的函数 php_strip_whitespace (PHP 5) php_strip_whitespace — ...
- MyBatis的mapper.xml文件的参数问题:org.apache.ibatis.builder.IncompleteElementException: Could not find parameter map
配置参数类型有两种选择,即:parameterType和parameterMap 不管参数是否是基本数据类型还是map类型,都是使用parameterType. 版权声明:本文为博主原创文章,未经博主 ...
- 有关nmap的5个常用的扫描指令
[以下IP可替换成需要被测试的IP网段] 1.ping扫描:扫描192.168.0.0/24网段上有哪些主机是存活的: nmap -sP 192.168.0.0/24 2.端口扫描:扫描192.1 ...
- Python模块(三)(正则,re,模块与包)
1. 正则表达式 匹配字符串 元字符 . 除了换行 \w 数字, 字母, 下划线 \d 数字 \s 空白符 \n 换行符 \t 制表符 \b 单词的边界 \W \D \S 非xxx [ ...
- dotnet core 运行程序注意事项
.net core 程序 debug版本无法直接运行,因为没有相关的dll,只有在进入项目文件夹那一层,执行 dotnet run 来运行,会自动链接 当前用户的个人文件夹下的 .nuget/.pac ...
- Scala学习-02-方法
算数和操作符重载 所有的操作符都是方法. a + b 是一种缩写形式 : a .+ b “+”是方法名(操作符重载) ++和—— Scala中并没有“++”和“——”.需要使用“+=”和“-=” ...