# ZOJ 3842 Cirno's Perfect Math Class (Kummer定理)

Cirno's Perfect Math Class
Time Limit: 2 Seconds      Memory Limit: 65536 KB
"Minnaa ~ Cirno no Sansuu Kyoushitsu Hajimaruyo ~ "

Cirno's perfect math class is fashionable over the world. But now, sadly, it must be stopped for some reason. In the last class,N students attended, and each student gave a gift to Cirno. But the gifts are too many so that Cirno just want to takeK(K is non-negative) gifts away.

As we all know, Cirno loves the number ⑨. Assume there areP ways to take awayK gifts. IfP > 0 andP is a multiple of 9, Cirno will acceptK as an alternative plan. Now, Cirno want to know how many plans that she has to think over.

Input

The number of test cases T(integer, about 50) will be given in the first line. ThenT lines follow. and each line contains a non-negative integerN(N <= 10^1000).

Output

One line for each case, the answer.

Sample Input

3
5
1
9
0

Sample Output

0
6
3
Author: LIN, Hanzhao

Source: ZOJ Monthly, January 2015

(1)C(n,k)不被3整除，

(2)C(n,k)被3整除但不被9整除，

Kummer定理相关：

import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static public void main(String[] args)
{
Scanner cin=new Scanner(System.in);
int T=cin.nextInt();
for(int ca=1;ca<=T;ca++)
{
BigInteger n=cin.nextBigInteger();
String s=n.toString(3);
BigInteger res=BigInteger.ONE;
for(int i=0;i<s.length();i++)
{
res=res.multiply(BigInteger.valueOf(s.charAt(i)-'0'+1));
}
BigInteger ans=res;
for(int i=1;i<s.length();i++)
{
.divide(BigInteger.valueOf(s.charAt(i)-'0'+1))
.multiply(BigInteger.valueOf(s.charAt(i-1)-'0'))
.multiply(BigInteger.valueOf('2'-s.charAt(i))));
}
}
}
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客