hdu 6093---Rikka with Number(计数)
2018-06-17 22:06:19来源:未知 阅读 ()
题目链接
In radix d, a number K=(A1A2...Am)d(Ai∈[0,d),A1≠0) is good if and only A1−Am is a permutation of numbers from 0 to d−1.
A number K is good if and only if there exists at least one d≥2 and K is good under radix d.
Now, Yuta wants to calculate the number of good numbers in interval [L,R]
It is too difficult for Rikka. Can you help her?
For each testcase, the first line contains two decimal numbers L,R(1≤L≤R≤105000).
import java.math.BigInteger; import java.util.Scanner; public class Main { static int MAXN = 1600; static BigInteger[] dx = new BigInteger[MAXN]; static long MOD = 998244353; static long [] fac = new long [MAXN]; static void Init(){ dx[0] = BigInteger.ZERO; dx[1] = BigInteger.ZERO; for(int i=2; i<MAXN; i++){ dx[i] = BigInteger.ZERO; for(int j=i-1; j>=0; j--){ dx[i] = dx[i].multiply(BigInteger.valueOf(i)).add(BigInteger.valueOf(j)); } } fac[0] = fac[1] = 1; for(int i=2; i<MAXN; i++) fac[i]=fac[i-1]*i%MOD; } static int Low(BigInteger x){ int low=0, high=MAXN-1; while(low < high){ int mid = (low+high)/2; if(dx[mid].compareTo(x)>=0)high = mid; else low = mid+1; } return high; } public static void main(String[] args){ Init(); Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int cas=1; cas<=T; cas++){ int [] vis = new int [MAXN]; for(int i=0; i<MAXN; i++) vis[i] = 0; BigInteger L, R; L = in.nextBigInteger(); R = in.nextBigInteger(); int dl = 0, dr = 0; dl = Low(L)+1; dr = Low(R)-1; long ans = fac[dr]-fac[dl-1]; ans = (ans%MOD+MOD)%MOD; if(dl > dr) ans = 0; dl--; dr++; long ans2=0; BigInteger tmp = (BigInteger.valueOf(dl)).pow(dl-1); for(int i=dl; i>=1; i--){ int top = L.divide(tmp).intValue(); if(i==dl && top==0) { ans2 = (ans2+fac[dl]-fac[dl-1])%MOD; break; } int cnt=0; for(int o=top+1;o<dl;o++) if(vis[o]==0) cnt++; ans2 = (ans2+(cnt)*fac[i-1]%MOD)%MOD; if(vis[top] == 1) break; L = L.mod(tmp); if(i==1 && vis[top]==0) ans2=ans2+1; vis[top] = 1; tmp = tmp.divide(BigInteger.valueOf(dl)); } long ans1 = 0; for(int i=0; i<MAXN; i++) vis[i] = 0; tmp = (BigInteger.valueOf(dr)).pow(dr-1); for(int i=dr; i>=1; i--) { int top = R.divide(tmp).intValue(); if(i==dr && top==0) {ans1 = (ans1+fac[dr]-fac[dr-1]%MOD); break;} int cnt=0; for(int o=top+1;o<dr;o++) if(vis[o]==0) cnt++; ans1 = (ans1+(cnt)*fac[i-1]%MOD)%MOD; if(vis[top] == 1) break; R = R.mod(tmp); vis[top] = 1; tmp = tmp.divide(BigInteger.valueOf(dr)); } // System.out.println("ans1 -> "+ans1); // System.out.println("ans2 -> "+ans2); // System.out.println("ans -> "+ans); // System.out.println("DL -> "+dl); // System.out.println("DR -> "+dr); ans = ans+ans2 + fac[dr]-fac[dr-1]-ans1; if(dl==dr) ans=ans2-ans1; ans = (ans%MOD+MOD)%MOD; System.out.println(ans); } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU-2955-Robberies(0-1背包) 2020-03-30
- CodeForces 1313E Concatenation with intersection 2020-03-02
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash