zoukankan      html  css  js  c++  java
  • codeforces 10C Digital Root(非原创)

    Not long ago Billy came across such a problem, where there were given three natural numbers A, B and C from the range [1, N], and it was asked to check whether the equation AB = C is correct. Recently Billy studied the concept of a digital root of a number. We should remind you that a digital root d(x) of the number x is the sum s(x) of all the digits of this number, if s(x) ≤ 9, otherwise it is d(s(x)). For example, a digital root of the number 6543 is calculated as follows: d(6543) = d(6 + 5 + 4 + 3) = d(18) = 9. Billy has counted that the digital root of a product of numbers is equal to the digital root of the product of the factors' digital roots, i.e. d(xy) = d(d(x)d(y)). And the following solution to the problem came to his mind: to calculate the digital roots and check if this condition is met. However, Billy has doubts that this condition is sufficient. That's why he asks you to find out the amount of test examples for the given problem such that the algorithm proposed by Billy makes mistakes.

    Input

    The first line contains the only number N (1 ≤ N ≤ 106).

    Output

    Output one number — the amount of required A, B and C from the range [1, N].

    Example

    Input
    4
    Output
    2
    Input
    5
    Output
    6

    Note

    For the first sample the required triples are (3, 4, 3) and (4, 3, 3).

    这题理解了题意就很好做。

    转自:https://www.cnblogs.com/qscqesze/p/5439079.html

    题意

    问你[1,n]中有多少 AB!=C,但是D(A)D(B)=D(C)的

    D(A)是数根的意思,翻译过来就是这个数%9

    题解:

    容斥做,首先把所有的D(A)D(B)=D(C)的计算过来

    然后减去AB==C且D(A)D(B)=D(C)的,由于显然AB=C,那么D(A)D(B)=D(C)

    所以我们只需要减去AB=C的就好了,我们暴力枚举A,看B的个数有n/A个

    然后莽一波……

    注意,数根是1+(i-1)%9

    数根的推导:http://blog.csdn.net/ray0354315/article/details/53991199

    附ac代码:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cmath>
     4 #include <string>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <queue>
     8 #include <map>
     9 #include <vector>
    10 using namespace std;
    11 const int maxn = 1e6+10;
    12 typedef long long ll;
    13 const ll mod = 1e9+7;
    14 const int inf = 0x3f3f3f3f;
    15 const double eps=1e-6;
    16 ll ans[maxn];
    17 int main() {
    18     ios::sync_with_stdio(false);
    19     int n;
    20     cin>>n;
    21     ll sum=0;
    22     for(int i=1;i<=n;++i)   ans[1+(i-1)%9]++,sum-=n/i;
    23     for(int i=1;i<=9;++i)
    24     {
    25         for(int j=1;j<=9;++j)
    26         {
    27             sum+=ans[i]*ans[j]*ans[1+(i*j-1)%9];
    28         }
    29     }
    30     cout<<sum<<endl;
    31     return 0;
    32 }
    View Code
  • 相关阅读:
    windows 7 wifi热点配置
    Java中的try catch finaly先后调用顺序
    redis php扩展
    mysql索引
    cmd操作数据库的常用命令
    php ajax解决跨越问题
    git常用命令
    php,redis分布式锁防并发
    php商城下单,可以购买多件商品,redis防高并发
    php商城秒杀,redis防高并发
  • 原文地址:https://www.cnblogs.com/zmin/p/8395067.html
Copyright © 2011-2022 走看看