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

 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. Java类加载器深入探索

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 什么是.class文件? class文件全名称为Java class文件,主要在平台无关性和网 ...

  2. 企业Openvpn环境部署

                   企业Openvpn环境部署                                         作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任 ...

  3. codeforces 520 Two Buttons

    http://codeforces.com/problemset/problem/520/B B. Two Buttons time limit per test 2 seconds memory l ...

  4. Python基础(1)python+Eclipse+pydev环境搭建

    编辑器:Python 自带的 IDLE 简单快捷, 学习Python或者编写小型软件的时候.非常有用.         编辑器: Eclipse + pydev插件 1. Eclipse是写JAVA的 ...

  5. poj: 2159

    简单题,看起来很凶 #include <iostream> #include <stdio.h> #include <string> #include <st ...

  6. 1019: A+B和C比大小

    1019: A+B和C比大小 时间限制: 1 Sec  内存限制: 128 MB提交: 518  解决: 300[提交][状态][讨论版] 题目描述 给定区间[-231, 231]内的3个整数A.B和 ...

  7. 简单常用的sql,统计语句,陆续整理添加吧

    1. 分段统计分数      if object_id('[score]') is not null drop table [score] go create table [score]([学号] i ...

  8. 4. 星际争霸之php设计模式--工厂方法模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  9. 开启xp_cmdshell

    --打开xp_cmdshell EXEC sp_configure 'show advanced options', 1GORECONFIGUREGOEXEC sp_configure 'xp_cmd ...

  10. php常见细节错误

    PHP编程中10个最常见的错误 PHP是一种非常流行的开源服务器端脚本语言,你在万维网看到的大多数网站都是使用php开发的.本篇经将为大家介绍PHP开发中10个最常见的问题,希望能够对朋友有所帮助. ...