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

 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. linux第7天 I/O的五种模型, select

    服务器端避免僵尸进程的方法: 1)通过忽略SIGCHLD信号,解决僵尸进程 signal(SIGCHLD, SIG_IGN) 2)通过wait方法,解决僵尸进程 signal(SIGCHLD, han ...

  2. yii中sphinx索引配置解析

    #MySQL数据源配置,详情请查看:http://www.coreseek.cn/products-install/mysql/#请先将var/test/documents.sql导入数据库,并配置好 ...

  3. 数据库中is null(is not null)与=null(!=null)的区别

    在标准SQL语言(ANIS SQL)SQL-92规定的Null值的比较取值结果都为False,既Null=Null取值也是False.NULL在这里是一种未知值,千变万化的变量,不是“空”这一个定值! ...

  4. paper 27 :图像/视觉显著性检测技术发展情况梳理(Saliency Detection、Visual Attention)

    1. 早期C. Koch与S. Ullman的研究工作. 他们提出了非常有影响力的生物启发模型. C. Koch and S. Ullman . Shifts in selective visual ...

  5. BETWEEN and

    select * from ( select *,ROW_NUMBER() over (ORDER BY AddTime desc) RowNumber from Product where ID n ...

  6. LDA-math-LDA 文本建模

    http://cos.name/2013/03/lda-math-lda-text-modeling/ 5. LDA 文本建模 5.1 游戏规则 对于上述的 PLSA 模型,贝叶斯学派显然是有意见的, ...

  7. pic计数

    #include <pic.h> //用的是PICC编译器 __CONFIG (HS & PROTECT & PWRTEN & BOREN & WDTDIS ...

  8. Android中的通知—Notification 自定义通知

    Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...

  9. 关闭 Windows 的常用端口

    netstat -ano 可以看到目前开着哪些端口 netstat -ano|findstr <端口号>   可以找到开放的端口的那条,最后还列出了 PID. 然后到任务管理器中,你可以查 ...

  10. svn权限控制

    http://blog.csdn.net/clever101/article/details/8159105 [groups] #核心层开发组成员 core_dev = lg,zjc #扩展层开发组成 ...