hdu4933 Miaomiao's Function

水水的计数题,关键在细节。

  1 import java.math.BigInteger;
  2 import java.util.Scanner;
  3 import java.io.*;
  4
  5 public class Main {
  6     BigInteger n0 = BigInteger.valueOf(0);
  7     BigInteger n1 = BigInteger.valueOf(1);
  8     BigInteger n2 = BigInteger.valueOf(2);
  9     BigInteger n10 = BigInteger.valueOf(10);
 10     BigInteger n9 = BigInteger.valueOf(9);
 11     BigInteger rev = BigInteger.valueOf(-1);
 12     BigInteger n45 = new BigInteger("45");
 13     BigInteger p[] = new BigInteger [110];
 14     BigInteger P[] = new BigInteger [110];
 15     BigInteger dP[] = new BigInteger [110];
 16     BigInteger M[] = new BigInteger [110];
 17     void init(){
 18         dP[0] = P[0] = p[0] = n0;
 19         M[0] = n1;
 20         for(int i = 1; i < 102; i++){
 21             //System.out.println(i);
 22             p[i] = M[i - 1].multiply(n45);
 23             p[i] = p[i].subtract(n9.multiply(P[i - 1]));
 24             //System.out.println(p[i]);
 25             //System.out.println("ok");
 26             P[i] = p[i].subtract(P[i - 1]);
 27             dP[i] = dP[i - 1].add(p[i]);
 28             M[i] = M[i - 1].multiply(n10);
 29             //System.out.println("M" + i + "  " + M[i]);
 30         }
 31     }
 32
 33     BigInteger getAns(BigInteger num){
 34         if(num.equals(n0)) return n0;
 35         if(num.compareTo(n0) > 0 && num.compareTo(n9) <= 0) return num.add(n1).multiply(num).divide(n2);
 36         String str = num.toString();
 37         int len = str.length();
 38         BigInteger first = num.divide(M[len - 1]);
 39         BigInteger new_num = num.mod(M[len - 1]);
 40         BigInteger tem = dP[len - 1];
 41         //System.out.println("tem" + tem);
 42         tem = tem.add(M[len - 1].multiply(first.multiply(first.subtract(n1)).divide(n2)));
 43         tem = tem.subtract((first.subtract(n1)).multiply(P[len - 1]));
 44         tem = tem.add(new_num.add(n1).multiply(first));
 45         if(new_num.equals(n0)) return tem;
 46         BigInteger sign = n1;
 47         int tmp = len - 1;
 48         while(new_num.divide(M[tmp]).equals(n0)){
 49              tmp--;
 50              sign = sign.multiply(rev);
 51         }
 52         while(new_num.compareTo(n0) > 0){
 53             //System.out.println("tem " + tem);
 54             //System.out.println("new_num" + new_num);
 55             String new_str = new_num.toString();
 56             int new_len = new_str.length();
 57             BigInteger new_sign = (len - 1) % 2 == 0 ? n1 : rev;
 58             int i = 1;
 59             //System.out.println("new_len" + new_len);
 60             while(i < new_len){
 61                 tem = tem.add(new_sign.multiply(p[i]));
 62                 //System.out.println("i" + i + "p[i]" + p[i] + "tem" + tem);
 63                 new_sign = new_sign.multiply(rev);
 64                 i++;
 65             }
 66             //System.out.println("ok");
 67             //System.out.println("new_sign" + new_sign);
 68             BigInteger new_first = new_num.divide(M[new_len - 1]);
 69             tem = tem.add(new_first.subtract(n1).multiply(new_first).divide(n2).multiply(M[new_len - 1]).multiply(new_sign));
 70             //System.out.println("tem" + tem);
 71             tem = tem.add(new_sign.multiply(rev).multiply(new_first.subtract(n1)).multiply(P[new_len - 1]));
 72             //System.out.println("tem" + tem);
 73             new_num = new_num.mod(M[new_len - 1]);
 74             tem = tem.add(new_first.multiply(n1.add(new_num)).multiply(new_sign));
 75         }
 76         return tem;
 77     }
 78
 79     BigInteger f(BigInteger num){
 80         if(num.compareTo(n0) < 0){
 81             BigInteger tem = num.divide(n9);
 82             num = num.add(tem.multiply(n9).multiply(rev));
 83             while(num.compareTo(n0) > 0) num = num.subtract(n9);
 84             while(num.compareTo(n0) <= 0) num = num.add(n9);
 85         }
 86         if(num.compareTo(n0) >= 0 && num.compareTo(n9) <= 0) return num;
 87         BigInteger tem = n0;
 88         while(num.compareTo(n0) > 0){
 89             tem = tem.add(num.mod(n10));
 90             num = num.divide(n10);
 91         }
 92         return f(tem);
 93     }
 94
 95     void solve(){
 96         Scanner cin = new Scanner(System.in);
 97         int T = cin.nextInt();
 98         while(T-- != 0){
 99             String stra = cin.next();
100             String strb = cin.next();
101             BigInteger a = new BigInteger(stra);
102             BigInteger b = new BigInteger(strb);
103             BigInteger front = a.equals(n0) ? n0 : getAns(a.subtract(n1));
104             BigInteger rear = getAns(b);
105             //System.out.println("rear" + rear);
106             BigInteger ans = rear.subtract(front);
107             BigInteger fAns = f(ans);
108             if(fAns.equals(n0)) System.out.println("Error!");
109             else{
110                 // System.out.println("ans" + ans);
111                 //System.out.println("fAns" + fAns);
112                 ans = ans.mod(fAns).add(fAns).mod(fAns);
113                 System.out.println(ans);
114             }
115         }
116         cin.close();
117     }
118     public static void main(String[] args) throws IOException{
119         Main e = new Main();
120         e.init();
121         e.solve();
122     }
123 }

hdu4933 Miaomiao's Function

时间: 2024-10-06 04:39:41

hdu4933 Miaomiao's Function的相关文章

[hdu 4933]Miaomiao&#39;s Function 数位DP+大数

Miaomiao's Function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 79    Accepted Submission(s): 18 Problem Description Firstly , Miaomiao define two functions f(x) , g(x): (K is the smallest

HDU 4933 Miaomiao&#39;s Function(数位DP + 高精度)

题目连接 : http://acm.hdu.edu.cn/showproblem.php?pid=4933 题意 : 自己看吧,还是很容易理解的,就一个公式而已. 方法 : 比赛的时候想到两次数位DP:先对所有的数进行一次mod9 的数位DP,再得出的答案x为新的mod,再进行一次mod,后来写着写着发现似乎有点问题,对于answer = 0要特判,然后没想到好的方法,比赛就结束了. 赛后用java写了一发大数过的,对于题解中用多个大质数去判这种神奇的作法只能意会下了 , 貌似rand() 也行

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

现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解echarts是个怎样技术的开发者来说,可以到echarts官网进行学习了解,官网有详细的API文档和实例供大家参考学习. 2.以下是我在工作中实现整理出来的实例源码: 公用的支持js文件 echarts.js.echarts.min.js,还有其他的图表需要支持的js文件也可以到官网下载 echa

帮同学做的大一大作业:《我的家乡—郑州》

---恢复内容开始--- 最近在上海上学的一个高中同学让我帮忙,帮她做她们的计算机课程大作业. 由于关系不错我也不好意思拒绝就帮忙做了,因为这个学期刚刚开始接触HTML5和css,所以制作过程中有很多不懂的,而且由于HTML5是选修课,一星期只有一节,所以做这个花费了比较多的时间,这个网站是我制作的第一个网站,比较有纪念意义,所以发在博客上,作为纪念. 通过去做这个作业,我了解到很多课上学不到的东西.因为没有美工,从头到尾,都是我一个人在臆想,刚开始的时候,根本无从下手,我去参考别人做的家乡网站

Is virtual function table really necessary for C++

OOP polymorphism  In OOP languages, a base-class object pointer can do function call according to the actual type of the object. Let's see an example in Java. public class Animal{ public void spark(){ System.out.println("spark"); } } public clas

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio

安装python 第三方库遇到的安装问题 microsoft visual studio c++ 10.0 is required,Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?

问题一: microsoft visual studio c++ 10.0 is required 安装scrapy时候出现需要vc c++ 10,有时安装其他也会有. 解决方法:安装vc 2010,安装过2017无效,安装过程也不一样. 问题二: 安装好,出现Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed? 解决办法: 1.pip install wheel 2. 到http://

[c++] Inline Function

The inline functions are a C++ enhancement feature to increase the execution time of a program. Compiler replace the definition at compile time instead of referring function defination at runtime. NOTE - This is a suggestion to compiler to make the f

js instanceof Object Function

Object.Function是javascript中顶级的两个对象,同时也属于两个顶级的构造器,function Object(){}.function Function(){}.Object.Function为两个独立的预先创建的两个对象.new Object创建一个具有Object特性的新的一个对象,new Function创建一个具有Function特性的一个新对象. Object是一个对象,包含__proto__.prototype属性. Object.__proto__ = func