Basic algorithm POJ 1845 spoj divsum in Number Theory

Source: Internet
Author: User

Extended Euclidean inverse element (as long as a and B are mutually unique, there is a inverse element)
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <cmath>
# Define mod 9973.
Typedef long ll;
Void ExGcd (int a, int B, int & x, int & y ){
If (B = 0 ){
X = 1;
Y = 0;
Return;
}
ExGcd (B, a % B, x, y );
Int t = x;
X = y;
Y = t-a/B * y;
}
Int main (){
Int t, T, rev, x;
Int a, B;
Scanf ("% d", & T );
For (t = 1; t <= T; t ++ ){
Scanf ("% d", & a, & B );
ExGcd (B, mod, rev, x );
Printf ("% d \ n", (rev * a) % mod + mod) % mod );
}
}

 

O (n) base number table

[Cpp]
Int pn = 0, prime [MAXN], factor [MAXN]; // The minimum approximate number of elements whose factor [I] Is I
Void get_prime (int n ){
Int I, j;
For (I = 1; I <= n; I ++)
Factor [I] = I;
For (I = 2; I <= n; I ++ ){
If (I = factor [I]) prime [pn ++] = I;
For (j = 0; j <pn & prime [j] * I <= n; j ++ ){
Factor [I * prime [j] = prime [j];
If (I % prime [j] = 0)
Break;
}
}
}

POJ 1845 binary (1 + x ^ 2... + x ^ n) summation
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <cmath>
# Define mod 9901.
Typedef long ll;
Const int MAXN = 10000;
Int a, B, k;
Int p [MAXN], n [MAXN];
Ll quick (ll num, ll mi ){
Ll ans = 1;
While (mi ){
If (mi % 2) ans = (ans * num) % mod;
Num = (num * num) % mod;
Mi/= 2;
}
Return ans % mod;
}
Ll sum (ll num, ll mi ){
If (mi = 0) return 1;
If (mi % 2)
Return (sum (num, mi/2) * (1 + quick (num, mi/2 + 1) % mod;
Return (sum (num, mi/2-1) * (1 + quick (num, mi/2 + 1) + quick (num, mi/2) % mod;
}
Void factor (int ){
Int I;
K = 0;
For (I = 2; I * I <= a;) {// complexity decomposition INTEGER OF sqrt (n)
If (a % I = 0 ){
P [k] = I;
N [k] = 0;
While (a % I = 0 ){
N [k] ++;
A/= I;
}
K ++;
}
If (I> 2) I + = 2;
Else I ++;
}
If (a> 1 ){
P [k] =;
N [k] = 1;
K ++;
}
}
Int main (){
Int I;
Scanf ("% d", & a, & B );
Factor ();
Int ans = 1;
For (I = 0; I <k; I ++)
Ans = (ans * (sum (p [I], n [I] * B) % mod;
Printf ("% d \ n", ans );
}
POJ 1845 reverse RMB Method
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <cmath>
# Define mod 9901.
Typedef long ll;
Const int MAXN = 10000;
Int a, B, k;
Int p [MAXN], n [MAXN];
 
Void ExGcd (int a, int B, int & x, int & y ){
If (B = 0 ){
X = 1;
Y = 0;
Return;
}
ExGcd (B, a % B, x, y );
Int t = x;
X = y;
Y = t-a/B * y;
}
Int reverse (int num ){
Int rev, x;
ExGcd (num, mod, rev, x );
Return rev;
}
Ll quick (ll num, ll mi ){
Ll ans = 1;
While (mi ){
If (mi % 2) ans = (ans * num) % mod;
Num = (num * num) % mod;
Mi/= 2;
}
Return ans % mod;
}
Ll sum (ll num, ll mi ){
If (num % mod = 1) // note that this situation cannot be reversed because the num-1 and mod are not mutually exclusive
Return (mi + 1) % mod;
Return (quick (num, mi + 1)-1 + mod) % mod) * reverse (num-1) % mod + mod) % mod;
}
Void factor (int ){
Int I;
K = 0;
For (I = 2; I * I <= a;) {// complexity decomposition INTEGER OF sqrt (n)
If (a % I = 0 ){
P [k] = I;
N [k] = 0;
While (a % I = 0 ){
N [k] ++;
A/= I;
}
K ++;
}
If (I> 2) I + = 2;
Else I ++;
}
If (a> 1 ){
P [k] =;
N [k] = 1;
K ++;
}
}
Int main (){
Int I;
Scanf ("% d", & a, & B );
Factor ();
Int ans = 1;
For (I = 0; I <k; I ++)
Ans = (ans * (sum (p [I], n [I] * B) % mod + mod) % mod;
Printf ("% d \ n", ans );
}


SPOJ DIVSUM

[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <cmath>
 
Const int MAXN = 1501000, N = 500010;
 
Int pn = 0, prime [MAXN], factor [MAXN];
Void get_prime (int n ){
Int I, j;
For (I = 1; I <= n; I ++)
Factor [I] = I;
For (I = 2; I <= n; I ++ ){
If (I = factor [I]) prime [pn ++] = I;
For (j = 0; j <pn & prime [j] * I <= n; j ++ ){
Factor [I * prime [j] = prime [j];
If (I % prime [j] = 0)
Break;
}
}
}
 
Int main ()
{
Int cases;
Get_prime (N );
Scanf ("% d", & cases );
While (cases --)
{
Int n, tmpn;
Long ans = 1;
Scanf ("% d", & n );
Tmpn = n;
While (tmpn! = Factor [tmpn])
{
Long fac = factor [tmpn], mtp = fac;
While (tmpn % fac = 0)
{
Mtp * = fac;
Tmpn/= fac;
}
Ans * = (1-mtp)/(1-fac );
}
If (tmpn> 1)
Ans * = (1 + tmpn );
Ans-= n;
Printf ("% lld \ n", ans );
}
 
Return 0;
}

SPOJ DIVSUM ANOTHER METHOD
[Cpp]
# Include <cstdio>
Using namespace std;
Int sum [500100];
Int main ()
{
Int T, t, I, j, n;
Scanf ("% d", & T );
For (I = 1; I <= 500000; I ++ ){
For (j = 2; I * j <= 500000; j ++) {// If j starts from 1, it will calculate itself
Sum [I * j] + = I;
}
}
For (t = 1; t <= T; t ++ ){
Scanf ("% d", & n );
Printf ("% d \ n", sum [n]);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.