【USACO3.2.1】阶乘

因为10的产生只能是2*5,(10 也是2*5产生的)


也就是说,我把数字分解质因数,只分解2和5. 一个数字有几个2,几个5. 剩下的数字只保留个位,然后把个位数字全部乘起来积为P即可



最后我知道有我A个2, B个5。  因为A一定是大于B的, 一个2抵消一个5. 


所以答案就是(2^(A-B) * P )mod 10


Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.005 secs, 3368 KB]
   Test 2: TEST OK [0.005 secs, 3368 KB]
   Test 3: TEST OK [0.008 secs, 3368 KB]
   Test 4: TEST OK [0.008 secs, 3368 KB]
   Test 5: TEST OK [0.005 secs, 3368 KB]
   Test 6: TEST OK [0.005 secs, 3368 KB]
   Test 7: TEST OK [0.008 secs, 3368 KB]
   Test 8: TEST OK [0.008 secs, 3368 KB]
   Test 9: TEST OK [0.005 secs, 3368 KB]
   Test 10: TEST OK [0.008 secs, 3368 KB]

All tests OK.



/*
TASK:fact4
LANG:C++
*/
#include <iostream>
#include <cstdio>
using namespace std;
int n;

int er=0, ans=1;

inline int cal(int k)
{
	while (k % 2 == 0)
	{
		++ er;
		k /= 2;	
	}
	while (k % 5 == 0)
	{
		-- er;
		k /= 5;	
	}
	return k;
}

int main()
{
	freopen("fact4.in","r",stdin);
	freopen("fact4.out","w",stdout);
	cin >> n;
	for (int i = 2; i <= n; ++ i)
		ans = (ans * cal(i)) % 10;		
	for (int i = 1; i <= er; ++ i)	ans = (ans * 2) % 10;
	cout << ans << endl;
	return 0;
}


你可能感兴趣的:(【USACO3.2.1】阶乘)