標籤:des style class blog code http
Problem DescriptionChinachen is a football fanatic, and his favorite football club is Juventus fc. In order to buy a ticket of Juv, he finds a part-time job in Professor Qu’s lab.
And now, Chinachen have received an arduous task——Data Processing.
The data was made up with N positive integer (n1, n2, n3, … ), he may calculate the number , you can assume mod N =0. Because the number is too big to count, so P mod 1000003 is instead.
Chinachen is puzzled about it, and can’t find a good method to finish the mission, so he asked you to help him.
InputThe first line of input is a T, indicating the test cases number.
There are two lines in each case. The first line of the case is an integer N, and N<=40000. The next line include N integer numbers n1,n2,n3… (ni<=N).
OutputFor each test case, print a line containing the test case number ( beginning with 1) followed by the P mod 1000003.
Sample Input
231 1 341 2 1 4
Sample Output
Case 1:4Case 2:6
又是一條數論題目,最近學習數論,看完書本感覺並不能掌握數論的,還是需要多多練習,多運用才能掌握這個思想武器的。
本題可以簡單點過,不需要太進階的數論內容;
但是也可以運用好數論的內容,可以應用上三個數論的內容:
1 擴充歐幾裡得
2 快速求模
3 乘法逆元(inverse of modulo)
2 快速求模,也可以產生一個數組,因為這裡最大是40000,故此數值不大,可以使用數組,然後查表,速度很快。
但是這裡使用快速的時間效率也幾乎接近常數,沒必要儲存一個數組。如下面的powMod函數。
3 乘法逆元的應用的原理就是:
如果求a / b % c;
那麼可以求得b的乘法逆元e;be % c == 1,那麼a / b % c == a / b * b * e % c;相當於 a / b % c == a / b * 1(b*e) % c,這個是求模的性質,相當於一般算術中的一個數乘以其倒數等於1.最後可以化簡為a * e % c == a / b % c;
這樣做題,可以說非常科學系統,下面速度也算挺快的,而且消耗記憶體很小。
靖心 http://blog.csdn.net/kenden23/article/details/29363389
#include <cstdio>class DataProcessing3094{const static int MOD = 1000003;long long s, t, g;void extGCD(int a, int b){if (b == 0){s = 1L, t = 0L;g = a;}else{extGCD(b, a % b);long long tmp = s;s = t;t = tmp - a / b * t;}}long long powMod(long long base, long long num, long long mod){long long ans = 1;while (num){if (num & 1) ans = ans * base % mod;base = base * base % mod;num >>= 1;}return ans % mod;}public:DataProcessing3094(){int T, N, a;scanf("%d", &T);for (int i = 1; i <= T; i++){scanf("%d", &N);long long ans = 0;for (int j = 0; j < N; j++){scanf("%d", &a);ans = (ans + powMod(2, a, MOD)) % MOD;}extGCD(MOD, N);//即使g不等於1,最後mod也會約去gt = (t % MOD + MOD) % MOD;//求其最小正整數ans = ans * t % MOD;printf("Case %d:%I64d\n",i,ans);}}};