"紅色病毒"問題
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3073 Accepted Submission(s): 1312
Problem Description醫學界發現的新病毒因其蔓延速度和Internet上傳播的"紅色病毒"不相上下,被稱為"紅色病毒",經研究發現,該病毒及其變種的DNA的一條單鏈中,胞嘧啶,腺嘧啶均是成對出現的。
現在有一長度為N的字串,滿足一下條件:
(1) 字串僅由A,B,C,D四個字母組成;
(2) A出現偶數次(也可以不出現);
(3) C出現偶數次(也可以不出現);
計算滿足條件的字串個數.
當N=2時,所有滿足條件的字串有如下6個:BB,BD,DB,DD,AA,CC.
由於這個資料肯能非常龐大,你只要給出最後兩位元字即可.
Input每組輸入的第一行是一個整數T,表示測試執行個體的個數,下面是T行資料,每行一個整數N(1<=N<2^64),當T=0時結束.
Output對於每個測試執行個體,輸出字串個數的最後兩位,每組輸出後跟一個空行.
Sample Input
41420113142460
Sample Output
Case 1: 2Case 2: 72Case 3: 32Case 4: 0Case 1: 56Case 2: 72Case 3: 56
總結:1.開始WA了好幾次,後來才發現,題目的輸入資料範圍 2^64,所有 int 不能用,只能用大整數 BigInteger才儲存。2. 題目資料比較大, 很容易出現 Time Limit Exceeded 問題,所以我們要用大數除餘的方法:思路:由題知:(泰勒級數推導) 組合數學
指數型母函數問題
引例:假設有8個元素,其中a1重複3次,
a2重複2次,a3重複3次。從中取r個組合,,
這樣,對於一個多重集,其中a1重複n1次,a2 重複n2次,…,ak重複nk次,
從中取r個排列的不同排列數所對應的指數型母函數為
G(x)=(1+x/1!+x^2/2!+…——x^n1/n1!)(1+x/1!+x^2/2!+…)…(1+x/1!+x^2/2!+…+x^n/n!)
定義:對於序列a0,a1,a2,…,函數
G(x)=a。+a1/1!*x+a2/2!*x^2+a3/3!*x^3…+ak/k!*x^k+…
稱為序列a0,a1,a2,…對應的指數型母函數。G(X) = ( 1+ x + x^2/2! + x^4/! + .. )^2 * ( 1 + x + x^2/2! + x^3/3! +... )^2
A, C 只能出現偶數或者不出現情況 B, D出現方式不限制
得: x^n 項係數 a(n) = (4^n+2*2^n)/(4*n!)
求的:count=(4^n+2*2^n)/4 %100
import java.util.*;import java.io.*;import java.math.BigInteger;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(new BufferedInputStream(System.in)); while(sc.hasNextInt()){ int t=sc.nextInt(); if(t==0) System.exit(0); BigInteger nu=BigInteger.valueOf(1); for(int i=1;i<=t;i++){ BigInteger n=sc.nextBigInteger(); BigInteger m=(pow(4,n.longValue()-1).add(pow(2,n.longValue()-1))).mod(BigInteger.valueOf(100)); System.out.println("Case "+i+": "+m); } System.out.println(); } } //快速冪的演算法 public static BigInteger pow(long a,long b){ BigInteger temp=BigInteger.valueOf(1); BigInteger sum=BigInteger.valueOf(a); while(b!=0){ if((b&1)!=0) temp=temp.multiply(sum).mod(BigInteger.valueOf(100)); sum=sum.multiply(sum).mod(BigInteger.valueOf(100)); b=b>>1; } return temp; }}