51nod 1009 数字1的数量
2017-03-16 13:03
225 查看
给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数。
例如:n = 12,包含了5个1。1,10,12共包含3个1,11包含2个1,总共5个1。
Input
输入N(1 <= N <= 10^9)
Output
输出包含1的个数
Input示例
12
Output示例
5
1位数的情况:大于等于1的时候,有1个,小于1就没有。 2位数的情况:N=13,个位数出现的1的次数为2,分别为1和11,十位数出现1的次数为4,分别为10,11,12,13,所以f(N) = 2+4。N=23,个位数出现的1的次数为3,分别为1,11,21,十位数出现1的次数为10,分别为10~19,f(N)=3+10。由此我们发现,个位数出现1的次数不仅和个位数有关,和十位数也有关,如果个位数大于等于1,则个位数出现1的次数为十位数的数字加1;如果个位数为0,个位数出现1的次数等于十位数数字。而十位数上出现1的次数也不仅和十位数相关,也和个位数相关:如果十位数字等于1,则十位数上出现1的次数为个位数的数字加1,假如十位数大于1,则十位数上出现1的次数为10。 3位数的情况:N=123个位出现1的个数为13:1,11,21,…,91,101,111,121十位出现1的个数为20:10~19,110~119百位出现1的个数为24:100~123 我们可以继续分析4位数,5位数,推导出下面一般情况: 假设N,我们要计算百位上出现1的次数,将由三部分决定:百位上的数字,百位以上的数字,百位一下的数字。如果百位上的数字为0,则百位上出现1的次数仅由更高位决定,比如12013,百位出现1的情况为100~199,1100~1199,2100~2199,…,11100~11199,共1200个。等于更高位数字乘以当前位数,即12 * 100。如果百位上的数字大于1,则百位上出现1的次数仅由更高位决定,比如12213,百位出现1的情况为100~199,1100~1199,2100~2199,…,11100~11199,12100~12199共1300个。等于更高位数字加1乘以当前位数,即(12 + 1)*100。 如果百位上的数字为1,则百位上出现1的次数不仅受更高位影响,还受低位影响。例如12113,受高位影响出现1的情况:100~199,1100~1199,2100~2199,…,11100~11199,共1200个,但它还受低位影响,出现1的情况是12100~12113,共114个,等于低位数字113+1。
例如:n = 12,包含了5个1。1,10,12共包含3个1,11包含2个1,总共5个1。
Input
输入N(1 <= N <= 10^9)
Output
输出包含1的个数
Input示例
12
Output示例
5
1位数的情况:大于等于1的时候,有1个,小于1就没有。 2位数的情况:N=13,个位数出现的1的次数为2,分别为1和11,十位数出现1的次数为4,分别为10,11,12,13,所以f(N) = 2+4。N=23,个位数出现的1的次数为3,分别为1,11,21,十位数出现1的次数为10,分别为10~19,f(N)=3+10。由此我们发现,个位数出现1的次数不仅和个位数有关,和十位数也有关,如果个位数大于等于1,则个位数出现1的次数为十位数的数字加1;如果个位数为0,个位数出现1的次数等于十位数数字。而十位数上出现1的次数也不仅和十位数相关,也和个位数相关:如果十位数字等于1,则十位数上出现1的次数为个位数的数字加1,假如十位数大于1,则十位数上出现1的次数为10。 3位数的情况:N=123个位出现1的个数为13:1,11,21,…,91,101,111,121十位出现1的个数为20:10~19,110~119百位出现1的个数为24:100~123 我们可以继续分析4位数,5位数,推导出下面一般情况: 假设N,我们要计算百位上出现1的次数,将由三部分决定:百位上的数字,百位以上的数字,百位一下的数字。如果百位上的数字为0,则百位上出现1的次数仅由更高位决定,比如12013,百位出现1的情况为100~199,1100~1199,2100~2199,…,11100~11199,共1200个。等于更高位数字乘以当前位数,即12 * 100。如果百位上的数字大于1,则百位上出现1的次数仅由更高位决定,比如12213,百位出现1的情况为100~199,1100~1199,2100~2199,…,11100~11199,12100~12199共1300个。等于更高位数字加1乘以当前位数,即(12 + 1)*100。 如果百位上的数字为1,则百位上出现1的次数不仅受更高位影响,还受低位影响。例如12113,受高位影响出现1的情况:100~199,1100~1199,2100~2199,…,11100~11199,共1200个,但它还受低位影响,出现1的情况是12100~12113,共114个,等于低位数字113+1。
#include <bits/stdc++.h> //#include <ext/pb_ds/tree_policy.hpp> //#include <ext/pb_ds/assoc_container.hpp> //using namespace __gnu_pbds; using namespace std; #define pi acos(-1) #define endl '\n' #define me(x) memset(x,0,sizeof(x)); #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) typedef long long LL; const int INF=0x3f3f3f3f; const LL LINF=0x3f3f3f3f3f3f3f3fLL; const int dx[]={-1,0,1,0,-1,-1,1,1}; const int dy[]={0,1,0,-1,1,-1,1,-1}; const int maxn=1e3+5; const int maxx=2e6+100; const double EPS=1e-7; const int mod=1e9+7; template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);} template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);} template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));} template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));} //typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree; /*lch[root] = build(L1,p-1,L2+1,L2+cnt); rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*/ /*lch[root] = build(L1,p-1,L2,L2+cnt-1); rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/ long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);} int _pow(LL a,LL n) { LL ret=1; while(n) { if(n&1) ret=ret*a%mod; a=a*a%mod; n>>=1; } return ret; } int inv(int x) { return _pow(x,mod-2); } LL f(LL n) { LL ans = 0; LL i = 1; LL current = 0, after = 0, before = 0; // int t=0; while ((n / i) != 0) { current = (n / i) % 10; before = n / (i * 10); after = n - (n / i) * i; if (current > 1) { ans += (before + 1) * i; } else if (current == 0) { ans += before * i; } else if (current == 1) { ans += before * i + after + 1; } i = i * 10; // cout<<after<<"**********"<<endl; //printf("t:%d before:%d current:%d after:%d \n",++t,before,current,after); } return ans; } int main() { LL n; cin>>n; cout<<f(n)<<endl; }
相关文章推荐
- 51nod 1009:数字1的数量
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量 (统计1的总个数,好题)
- 51nod 1009:数字1的数量
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量(数学)
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51Nod ----1009 数字1的数量
- 51nod 1009 数字1的数量
- 51nod-【1009 数字1的数量】
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 【51Nod】1009 - 数字1的数量(思维,好题)
- 51nod 1009 数字1的数量
- 51Nod 1009 数字1的数量(数位dp)
- 51nod 1009 数字1的数量