【题意】

  给出一个正整数d(2<=d<=100);一个元素个数为d-1的集合a,每个集合元素ai对应一个1~d-1的整数;以及定义相同的大小为d集合bi。

  f函数的定义原题已给出。求数m最少经过多少次函数操作变为数k,无解输出NO。

【题解】

  我们可发现当x>=d时,f(x)=d*f(x/d)+b[x mod d],x<d时,f(x)=a[x]。

  不难发现其实这就是在x的d进制下,对每一位进行变换,某一位如果是x,要么变a[x],要么变b[x]。所以答案就是当某一次操作后,每一位都与目标数一致。

  于是对m在d进制下的每一位找第一次到达目标数的次数以及循环节(之所以有a与b两个集合就是防止最高位变为0)。最后就成了求n个模线性方程组求解的问题。

【代码】

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <algorithm>
  5 #define ll long long
  6 using namespace std;
  7 struct node
  8 {
  9     ll a,r;
 10 }a[1000];
 11 int n,A[105],B[105],flag[105],po[1000],sum[1000],c[1000],d[1000];
 12 ll D;
 13 char st[105];
 14 int read()
 15 {
 16     int x=0,sign=1;char ch;
 17     do{ch=getchar();if(ch=='-')sign=-sign;}while(ch<'0'||ch>'9');
 18     do{x=x*10+ch-48;ch=getchar();}while(ch>='0'&&ch<='9');
 19     return x*sign;
 20 }
 21 void exgcd (ll a,ll b,ll &x,ll &y)
 22 {
 23     if (b==0)
 24     {
 25         D=a;
 26         x=1;
 27         y=0;
 28         return;
 29     }
 30     exgcd(b,a%b,x,y);
 31     ll t=x;
 32     x=y;
 33     y=t-a/b*y;
 34 }
 35 ll crt(node a[],int n)
 36 {
 37     for (int i=1;i<=n;++i)    if (a[i].r==-1) return -1;
 38     ll A=a[1].a,R=a[1].r,x,y,q;
 39     for (int i=2;i<=n;++i)
 40     {
 41         exgcd(A,-a[i].a,x,y);
 42         if ((a[i].r-R)%D)return -1;
 43         q=-a[i].a/D;
 44         x=(a[i].r-R)/D%q*x%q;
 45         R=A*x+R;
 46         A=q*A;
 47     }
 48     if (A<0) A=-A;
 49     return (R%A+A)%A;
 50 }
 51 node rpt(int A[],int n,int x,int y)
 52 {
 53     int cnt=0;
 54     for (int i=0;i<=n;++i)    flag[i]=0;
 55     while (!flag[x])
 56         flag[x]=++cnt,x=A[x];
 57     return {cnt,flag[y]-1};
 58 }
 59 void div(int sum[],int d)
 60 {
 61     po[0]=0;int x=0;
 62     for (int i=1;i<=sum[0];++i)
 63     {
 64         x=x*10+sum[i];
 65         if (!po[0] && x>=d || po[0])
 66             po[++po[0]]=x/d,x%=d;
 67     }
 68     sum[0]=po[0];
 69     for (int i=1;i<=po[0];++i)    sum[i]=po[i];
 70 }
 71 int mo(int sum[],int d)
 72 {
 73     int x=0;
 74     for (int i=1;i<=sum[0];++i)
 75     {
 76         x=x*10+sum[i];
 77         if (x>=d)    x=x%d;
 78     }
 79     return x;
 80 }
 81 void get(int c[],int d)
 82 {
 83     scanf("%s",st+1);
 84     sum[0]=strlen(st+1);
 85     for (int i=1;i<=sum[0];++i)
 86         sum[i]=st[i]-48;
 87     c[0]=0;
 88     while (sum[0])
 89     {
 90         c[++c[0]]=mo(sum,d);
 91         div(sum,d);
 92     }
 93     for (int i=1;i+i<=c[0];++i)        swap(c[i],c[c[0]-i+1]);
 94 }
 95 int main()
 96 {
 97     while (1)
 98     {
 99         n=read();
100         if (n==-1)    break;
101         for (int i=1;i<n;++i)    A[i]=read();
102         for (int i=0;i<n;++i)    B[i]=read();
103         get(c,n);get(d,n);
104         if (c[0]!=d[0]){puts("NO");continue;}
105         a[1]=rpt(A,n-1,c[1],d[1]);
106         for (int i=2;i<=c[0];++i)    a[i]=rpt(B,n,c[i],d[i]);
107         ll ans=crt(a,c[0]);
108         if (ans==-1)    puts("NO");
109         else printf("%I64d\n",ans);
110     }
111     return 0;
112 }

View Code

  说实话写的有点丑。。。。

转载于:https://www.cnblogs.com/Bleacher/p/6869339.html

POJ3708 Recurrent Function相关推荐

  1. DSRN——Image Super-Resolution via Dual-State Recurrent Network

    Image Super-Resolution via Dual-State Recurrent Network CVPR(2018) 代码网址:https://github.com/WeiHan3/d ...

  2. 《挑战程序设计竞赛(第2版)》习题册攻略

    本项目来源于GitHub 链接: 项目GitHub链接 1 前言 项目为<挑战程序设计竞赛(第2版)>习题册攻略,已完结.可配合书籍或笔记,系统学习算法. 题量:约200道,代码注释内含详 ...

  3. 挑战程序竞赛系列(41):4.1中国剩余定理

    挑战程序竞赛系列(41):4.1中国剩余定理 详细代码可以fork下Github上leetcode项目,不定期更新. 练习题如下: POJ 1006: Biorhythms POJ 2891: Str ...

  4. ACM 数学类题目推荐

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

  5. (zhuan) Recurrent Neural Network

    Recurrent Neural Network 2016年07月01日 Deep learning Deep learning 字数:24235 this blog from: http://jxg ...

  6. RNN(Recurrent Neural Network)的几个难点

    1. vanish of gradient RNN的error相对于某个时间点t的梯度为: \(\frac{\partial E_t}{\partial W}=\sum_{k=1}^{t}\frac{ ...

  7. 循环神经网络教程Recurrent Neural Networks Tutorial, Part 1 – Introduction to RNNs

    Recurrent Neural Networks (RNNs) are popular models that have shown great promise in many NLP tasks. ...

  8. Paper:《Generating Sequences With Recurrent Neural Networks》的翻译和解读

    Paper:<Generating Sequences With Recurrent Neural Networks>的翻译和解读 目录 Generating Sequences With ...

  9. All of Recurrent Neural Networks (RNN)

    - notes for the Deep Learning book, Chapter 10 Sequence Modeling: Recurrent and Recursive Nets. Meta ...

最新文章

  1. access数据类型百度百科_Access数据库属于什么数据库。
  2. div中移除某个元素 js_[JS基础] 13 - 其他 JS 基础
  3. 操作系统:经典进程同步问题 之 生产者-消费者问题、读者-写者问题、哲学家进餐问题
  4. 关于国内部分电子病历编辑器的评价
  5. 记录一下flex布局左边固定,右边100%
  6. ubuntu服务器python3.6报错ModuleNotFoundError: No module named '_bz2'
  7. 洛谷——P1425 小鱼的游泳时间
  8. android sim卡命令,Android常用命令
  9. jQuery Mobile开发的新闻阅读器,适应iphone和android手机
  10. CTF杂项题解题思路与方法
  11. php异步实现,避免长时间等待
  12. 计算机毕业设计ssm游泳馆管理平台
  13. 史上超强的鲨鱼---Megalodon 巨齿鲨
  14. ThinkPHP5 实现短信验证码注册功能
  15. iOS14.0验证已关闭!
  16. 解决Android调试微信页面,chrome的inspect弹出空白
  17. 最长公共子序列问题-----题目
  18. Java并发编程艺术
  19. php 访问服务器上图片不显示,php显示云服务器上图片不显示
  20. TOS和DSCP总结

热门文章

  1. php查询手册 git,git blame
  2. Swift黑科技:还在争论MVC和MVVM?博主独创幽灵架构MV!
  3. [TensorFlow深度学习入门]实战七·简便方法实现TensorFlow模型参数保存与加载(ckpt方式)
  4. iso格式的镜像文件安装
  5. Git merge 报告 “Already up-to-date“尽管存在差异
  6. Redis事务的ACID性质
  7. 关闭docker进程
  8. 如何备份Github博客至GitCafe
  9. 少儿机器人编程主要使用的语言有啥
  10. 基于sklearn的k均值类聚模型