Both questions are self-defense questions.
The first question is a simple decimal self-defense number judgment:
Take 376 as an Example
376 Multiplier
X 376 Multiplier
----------
2256 first part product = reciprocal first of the multiplier *
2632 product of the Second Part = multiplier * the second to the last of the Multiplier
1128 third part product = multiplier * the last third digit of the Multiplier
----------
141376 Product
This issue is concerned with the last three digits of the product. By analyzing the process of generating the last three digits of the product, we can see that in each part of the product, not every digit of the product has an impact on the last three digits of the product. The sum-up rule can be obtained: in the three-digit multiplication, the credits that affect the last three digits of the product are as follows:
In the first part of the accumulation process: the last three digits of the multiplier * the reciprocal of the Multiplier
The second part is in accumulation: the last two digits of the multiplier * the second to the last of the Multiplier
Third part: accumulation: the last digit of the multiplier * the last third digit of the Multiplier
After the sum of the last three digits of the product is obtained, the last three digits are the last three digits of the product of the three digits. Such a rule can be extended to the product of different digits for the same problem.
Code:
[Cpp]
# Include <iostream>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <math. h>
# Include <vector>
Using namespace std;
Int main ()
{
/* # Ifndef ONLINE_JUDGE
Freopen ("in.txt", "r", stdin );
# Endif */
Int n;
While (scanf ("% d", & n )! = EOF)
{
// Number of digits
Int k = 1;
If (n = 0 | n = 1)
{
Printf ("NO \ n ");
Continue;
}
Int temp = n;
While (temp/10! = 0)
{
Temp/= 10;
K * = 10;
}
// Obtain the remainder Coefficient
Int kk = k * 10;
K = kk;
Int part = 0;
Int mul = 10;
While (k> = 10)
{
// Multiplier
Int a = n % k;
// Multiplier
Int B = (n-(n/mul) * mul)/(mul/10) * (mul/10 );
// Partial product
Part + = (a * B) % kk;
Part % = kk;
K/= 10;
Mul * = 10;
}
If (n = part)
{
Printf ("YES \ n ");
}
Else
{
Printf ("NO \ n ");
}
}
Return 0;
}
# Include <iostream>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <math. h>
# Include <vector>
Using namespace std;
Int main ()
{
/* # Ifndef ONLINE_JUDGE
Freopen ("in.txt", "r", stdin );
# Endif */
Int n;
While (scanf ("% d", & n )! = EOF)
{
// Number of digits
Int k = 1;
If (n = 0 | n = 1)
{
Printf ("NO \ n ");
Continue;
}
Int temp = n;
While (temp/10! = 0)
{
Temp/= 10;
K * = 10;
}
// Obtain the remainder Coefficient
Int kk = k * 10;
K = kk;
Int part = 0;
Int mul = 10;
While (k> = 10)
{
// Multiplier
Int a = n % k;
// Multiplier
Int B = (n-(n/mul) * mul)/(mul/10) * (mul/10 );
// Partial product
Part + = (a * B) % kk;
Part % = kk;
K/= 10;
Mul * = 10;
}
If (n = part)
{
Printf ("YES \ n ");
}
Else
{
Printf ("NO \ n ");
}
}
Return 0;
}
The second question is to solve the Self-Defense Number Problem of the N-base K-bit.
Search and traverse from low to high in all cases.
[Cpp]
# Include <iostream>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <math. h>
# Include <vector>
# Include <algorithm>
Using namespace std;
Struct Node
{
Char self [2005];
};
Node p [1000];
Int a [2005];
Int m;
Int B, n;
Char Change (int x)
{
If (x <= 9)
Return '0' + x;
Else
Return 'A' + X-10;
}
// Search from low to high
Void dfs (int dep, int sum)
{
If (dep> n)
{
If (a [n]> 0 | n = 1)
{
For (int I = n; I> = 1; I --)
{
P [m]. self [n-I] = Change (a [I]);
}
P [m]. self [n] = '\ 0 ';
M ++;
}
Return;
}
Int tol = 0;
For (a [dep] = 0; a [dep] <B; a [dep] ++)
{
Tol = 0;
For (int I = 1; I <= dep; I ++)
{
Int j = dep-I + 1;
Tol + = a [I] * a [j];
}
If (tol + sum) % B = a [dep])
{
Dfs (dep + 1, (tol + sum)/B );
}
}
}
Bool cmp (Node a, Node B)
{
Return strcmp (a. self, B. self) <0;
}
Int main ()
{
# Ifndef ONLINE_JUDGE
Freopen ("in.txt", "r", stdin );
# Endif
While (scanf ("% d", & B, & n )! = EOF)
{
M = 0;
Dfs (1, 0 );
Sort (p, p + m, cmp );
Printf ("% d \ n", m );
For (int I = 0; I <m; I ++)
{
Printf ("% s \ n", p [I]. self );
}
}
Return 0;
}
# Include <iostream>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <math. h>
# Include <vector>
# Include <algorithm>
Using namespace std;
Struct Node
{
Char self [2005];
};
Node p [1000];
Int a [2005];
Int m;
Int B, n;
Char Change (int x)
{
If (x <= 9)
Return '0' + x;
Else
Return 'A' + X-10;
}
// Search from low to high
Void dfs (int dep, int sum)
{
If (dep> n)
{
If (a [n]> 0 | n = 1)
{
For (int I = n; I> = 1; I --)
{
P [m]. self [n-I] = Change (a [I]);
}
P [m]. self [n] = '\ 0 ';
M ++;
}
Return;
}
Int tol = 0;
For (a [dep] = 0; a [dep] <B; a [dep] ++)
{
Tol = 0;
For (int I = 1; I <= dep; I ++)
{
Int j = dep-I + 1;
Tol + = a [I] * a [j];
}
If (tol + sum) % B = a [dep])
{
Dfs (dep + 1, (tol + sum)/B );
}
}
}
Bool cmp (Node a, Node B)
{
Return strcmp (a. self, B. self) <0;
}
Int main ()
{
# Ifndef ONLINE_JUDGE
Freopen ("in.txt", "r", stdin );
# Endif
While (scanf ("% d", & B, & n )! = EOF)
{
M = 0;
Dfs (1, 0 );
Sort (p, p + m, cmp );
Printf ("% d \ n", m );
For (int I = 0; I <m; I ++)
{
Printf ("% s \ n", p [I]. self );
}
}
Return 0;
}