HDU 6108(整除判断 数学)

题意是求在给定的 P 进制下,满足条件的数字 B 有多少。条件:若任何一个数的各位数字之和能被 B 整除,则该数可被 B 整除。

在 p 进制下,每个正整数都可以都可以表示为:a0*p^0 + a1*p^1 + a2*p^2 +…+an*p^n

(a0*p^0 + a1*p^1 + a2*p^2 +…+an*p^n) % B = 0 ⇒(a0 % B + a1 % B * p^1 % B +…+ an % B * p^n % B)% B = 0    (1)

(a0 + a1 + … + an) % B = 0 ⇒ (a0 % B + … + an % B) % B = 0                            (2)

(1)和(2)等价  当且仅当  p % B = 1( 即 p^n % B = 1),而 p % (p - 1) = 1,问题就转化成了求 p - 1 的因子个数。

代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int t,a,cnt,tmp;
 6     scanf("%d",&t);
 7     while(t--)
 8     {
 9         scanf("%d",&a);
10         --a;
11         cnt = 1;
12         for(int i = 2; i*i <= a; ++i)
13         {
14             if(a%i==0)
15             {
16                 tmp = 0;
17                 while(a%i==0)
18                 {
19                     a/=i;
20                     ++tmp;
21                 }
22                 cnt*=(tmp+1);
23             }
24         }
25         if(a>1) cnt<<=1;
26         printf("%d
",cnt);
27     }
28     return 0;
29 }
View Code

感谢这些博客的作者:

https://blog.csdn.net/hackertom/article/details/77131192

https://blog.csdn.net/qq_40679299/article/details/80583222

原文地址:https://www.cnblogs.com/Taskr212/p/9784374.html