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

 import java.math.BigInteger;
import java.util.Scanner;
import java.io.*; public class Main {
BigInteger n0 = BigInteger.valueOf(0);
BigInteger n1 = BigInteger.valueOf(1);
BigInteger n2 = BigInteger.valueOf(2);
BigInteger n10 = BigInteger.valueOf(10);
BigInteger n9 = BigInteger.valueOf(9);
BigInteger rev = BigInteger.valueOf(-1);
BigInteger n45 = new BigInteger("45");
BigInteger p[] = new BigInteger [110];
BigInteger P[] = new BigInteger [110];
BigInteger dP[] = new BigInteger [110];
BigInteger M[] = new BigInteger [110];
void init(){
dP[0] = P[0] = p[0] = n0;
M[0] = n1;
for(int i = 1; i < 102; i++){
//System.out.println(i);
p[i] = M[i - 1].multiply(n45);
p[i] = p[i].subtract(n9.multiply(P[i - 1]));
//System.out.println(p[i]);
//System.out.println("ok");
P[i] = p[i].subtract(P[i - 1]);
dP[i] = dP[i - 1].add(p[i]);
M[i] = M[i - 1].multiply(n10);
//System.out.println("M" + i + " " + M[i]);
}
} BigInteger getAns(BigInteger num){
if(num.equals(n0)) return n0;
if(num.compareTo(n0) > 0 && num.compareTo(n9) <= 0) return num.add(n1).multiply(num).divide(n2);
String str = num.toString();
int len = str.length();
BigInteger first = num.divide(M[len - 1]);
BigInteger new_num = num.mod(M[len - 1]);
BigInteger tem = dP[len - 1];
//System.out.println("tem" + tem);
tem = tem.add(M[len - 1].multiply(first.multiply(first.subtract(n1)).divide(n2)));
tem = tem.subtract((first.subtract(n1)).multiply(P[len - 1]));
tem = tem.add(new_num.add(n1).multiply(first));
if(new_num.equals(n0)) return tem;
BigInteger sign = n1;
int tmp = len - 1;
while(new_num.divide(M[tmp]).equals(n0)){
tmp--;
sign = sign.multiply(rev);
}
while(new_num.compareTo(n0) > 0){
//System.out.println("tem " + tem);
//System.out.println("new_num" + new_num);
String new_str = new_num.toString();
int new_len = new_str.length();
BigInteger new_sign = (len - 1) % 2 == 0 ? n1 : rev;
int i = 1;
//System.out.println("new_len" + new_len);
while(i < new_len){
tem = tem.add(new_sign.multiply(p[i]));
//System.out.println("i" + i + "p[i]" + p[i] + "tem" + tem);
new_sign = new_sign.multiply(rev);
i++;
}
//System.out.println("ok");
//System.out.println("new_sign" + new_sign);
BigInteger new_first = new_num.divide(M[new_len - 1]);
tem = tem.add(new_first.subtract(n1).multiply(new_first).divide(n2).multiply(M[new_len - 1]).multiply(new_sign));
//System.out.println("tem" + tem);
tem = tem.add(new_sign.multiply(rev).multiply(new_first.subtract(n1)).multiply(P[new_len - 1]));
//System.out.println("tem" + tem);
new_num = new_num.mod(M[new_len - 1]);
tem = tem.add(new_first.multiply(n1.add(new_num)).multiply(new_sign));
}
return tem;
} BigInteger f(BigInteger num){
if(num.compareTo(n0) < 0){
BigInteger tem = num.divide(n9);
num = num.add(tem.multiply(n9).multiply(rev));
while(num.compareTo(n0) > 0) num = num.subtract(n9);
while(num.compareTo(n0) <= 0) num = num.add(n9);
}
if(num.compareTo(n0) >= 0 && num.compareTo(n9) <= 0) return num;
BigInteger tem = n0;
while(num.compareTo(n0) > 0){
tem = tem.add(num.mod(n10));
num = num.divide(n10);
}
return f(tem);
} void solve(){
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
while(T-- != 0){
String stra = cin.next();
String strb = cin.next();
BigInteger a = new BigInteger(stra);
BigInteger b = new BigInteger(strb);
BigInteger front = a.equals(n0) ? n0 : getAns(a.subtract(n1));
BigInteger rear = getAns(b);
//System.out.println("rear" + rear);
BigInteger ans = rear.subtract(front);
BigInteger fAns = f(ans);
if(fAns.equals(n0)) System.out.println("Error!");
else{
// System.out.println("ans" + ans);
//System.out.println("fAns" + fAns);
ans = ans.mod(fAns).add(fAns).mod(fAns);
System.out.println(ans);
}
}
cin.close();
}
public static void main(String[] args) throws IOException{
Main e = new Main();
e.init();
e.solve();
}
}

hdu4933 Miaomiao's Function的更多相关文章

  1. Javascript十六种常用设计模式

    单例模式 何为单例模式,就是无论执行多少次函数,都只会生成一个对象哈哈,看一个简单的demo function Instance(name) { this.name = name; } Instanc ...

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

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

  3. jsp中出现onclick函数提示Cannot return from outside a function or method

    在使用Myeclipse10部署完项目后,原先不出错的项目,会有红色的叉叉,JSP页面会提示onclick函数错误 Cannot return from outside a function or m ...

  4. JavaScript function函数种类

    本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通函数:介绍普通函数的特性:同名覆盖.arguments对象.默认返回值等. 2. 匿名函数:介绍匿名函数的特性:变量匿名函数.无名称匿名函数. ...

  5. 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()

    1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...

  6. jquery中的$(document).ready(function() {});

    当文档载入时执行function函数里的代码, 这部分代码主要声明,页面加载后 "监听事件" 的方法.例如: $(document).ready( $("a") ...

  7. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  8. 转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38

    转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38http://space.itpub. ...

  9. [Xamarin] 透過Native Code呼叫 JavaScript function (转帖)

    今天我們來聊聊關於如何使用WebView 中的Javascript 來呼叫 Native Code 的部分 首先,你得先來看看這篇[Xamarin] 使用Webview 來做APP因為這篇文章至少講解 ...

随机推荐

  1. c# Start/Stop/Check Status远程计算机的Windows Service

    static void Main(string[] args) { ConnectionOptions op = new ConnectionOptions(); // 登陆远程计算机的远程, op. ...

  2. 3n+1问题

    猜想: 对于任意大于1的自然数n,若n为奇数,则将n变为3n+1,否则变为n的一半. 经过若干次这样的变换,一定会使n变为1.例如3->10->5->16->8->2-& ...

  3. Redis分布式

    昨天公司技术大牛做了一个Redis分布式的技术分享: Redis分布式资源: http://redis.io/topics/cluster-tutorialhttp://redis.io/topics ...

  4. PostgreSQL Cascade Replication

    PostgreSQL Cascade Replication node1:master:10.2.208.10:repclia(user) node2:upstreamnode:10.2.208.11 ...

  5. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(2)

    12.2安装 Postgres-XC 可以从 http://postgres-xc.sourceforge.net/下载Postgres-XC.对于本书,我们使用1.0.3版本的Postgres-XC ...

  6. poj: 3006

    简单题 #include <iostream> #include <stdio.h> #include <string> #include <stack> ...

  7. hdu5314 Happy King

    树分治. 代码 #pragma comment(linker, "/STACK:102400000,102400000") #include<cstdio> #incl ...

  8. PAT乙级 1009. 说反话 (20)

    1009. 说反话 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一句英语,要求你编写程序,将句中 ...

  9. ANT命令总结(转载)

    1 Ant是什么? Apache Ant 是一个基于 Java的生成工具.生成工具在软件开发中用来将源代码和其他输入文件转换为可执行文件的形式(也有可能转换为可安装的产品映像形式).随着应用程序的生成 ...

  10. yii2.0分页

    本实例是对商品列表进行分页 1.Controller中,商品列表的方法actionList 引用分页类 actionList中: $goods_info=Goods::find()->joinW ...