sgu 169 Numbers
来源:互联网 发布:淘宝网宠物窝 编辑:程序博客网 时间:2024/05/01 03:15
169. Numbers
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard
output: standard
output: standard
Let us call P(n) - the product of all digits of number n (in decimal notation).
For example, P(1243)=1*2*4*3=24; P(198501243)=0.
Let us call n to be a good number, if (p(n)<>0) and (n mod P(n)=0).
Let us call n to be a perfect number, if both n and n+1 are good numbers.
You are to write a program, which, given the number K, counts all such
numbers n that n is perfect and n contains exactly K digits in decimal notation.
For example, P(1243)=1*2*4*3=24; P(198501243)=0.
Let us call n to be a good number, if (p(n)<>0) and (n mod P(n)=0).
Let us call n to be a perfect number, if both n and n+1 are good numbers.
You are to write a program, which, given the number K, counts all such
numbers n that n is perfect and n contains exactly K digits in decimal notation.
Input
Only one number K (1<=K<=1000000) is written in input.
Output
Output the total number of perfect k-digit numbers.
Sample test(s)
Input
1
Output
8
k=1 时情况比较特殊,有8种
如果k>1 我们研究除最后一位的其他位,我们将证明他们都是1
否则假设 有某一位不为 1 假设为a (a>1 )
那么n%a==0 将n+1 我们将发现 (n+1) % a !=0
否则假设a|(n+1)
我们有
n=k*a ,n=k*a+1
k*a+1 = 0 (mod a)
1 = 0 mod(a)
a=1矛盾!
所以除最后一位的其他位均为1
现在我们来看看最后一位有几种可能。
显然最后一位可以是 1,2,5
不能为 4 8
最后一位若是3,则有(k-1)%3==0
最后一位若是7 则有(k-1)%6==0
有了以上结论,我们就很容易解决本道题。
贴代码:
k=1 时情况比较特殊,有8种
如果k>1 我们研究除最后一位的其他位,我们将证明他们都是1
否则假设 有某一位不为 1 假设为a (a>1 )
那么n%a==0 将n+1 我们将发现 (n+1) % a !=0
否则假设a|(n+1)
我们有
n=k*a ,n=k*a+1
k*a+1 = 0 (mod a)
1 = 0 mod(a)
a=1矛盾!
所以除最后一位的其他位均为1
现在我们来看看最后一位有几种可能。
显然最后一位可以是 1,2,5
不能为 4 8
最后一位若是3,则有(k-1)%3==0
最后一位若是7 则有(k-1)%6==0
有了以上结论,我们就很容易解决本道题。
贴代码:
#include<iostream>#include<cstring>#include<cstdio>#include<set>#include<algorithm>#include<vector>#include<cstdlib>#define inf 0xfffffff#define CLR(a,b) memset((a),(b),sizeof((a)))#define FOR(a,b) for(int a=1;a<=(b);(a)++)using namespace std;int const nMax = 1010;int const base = 10;typedef int LL;typedef pair<LL,LL> pij;//#define int long long// std::ios::sync_with_stdio(false);int k;/*测试用的 int sum(int n) { int s=1; while(n){ s*=(n%10); n/=10; } return s;}bool f(int n) { int s=sum(n) ; if(s&&n%s==0) return true; return false;}bool g(int n) { return f(n)&&f(n+1) ;}main(){ int s=0; for(int i=1;i<10000000ll;i++)if(g(i)) cout<<i<<endl; return 0;}*/int main(){ cin>>k; if(k==1){ cout<< 8 << endl; return 0; } int ans=1; if(k%6==1&&k%3==1) ans++; if(k%3==1) ans+=2; cout<<ans<<endl;}
- sgu 169 Numbers
- SGU 169 Numbers
- SGU 169 Numbers(傻逼题)
- sgu-169 Numbers 很好的数学题
- SGU - 169 - Numbers (找规律)
- sgu 108-self-numbers 2
- SGU 108 Self-numbers 2
- SGU 113 Nearly prime numbers
- [SGU]108. Self-numbers II
- sgu 108 Self-numbers 2
- sgu 113 Nearly prime numbers
- [SGU]113. Nearly prime numbers
- SGU 113 Nearly prime numbers
- sgu 108 self-numbers 2
- sgu 330——Numbers
- sgu 113 Nearly prime numbers
- SGU 159 - Self-Replicating Numbers
- sgu 108 Self-numbers 2
- 交叉染色法判断二分图
- Oracle同义词与DBlink
- RDLC实现同一份Report不同格式页之旁门左道
- java中遍历实体类属性和类型
- iOS 6的Rotation
- sgu 169 Numbers
- 单链表置逆
- Excel工具类,本类提供JAVA中对XLS文件的简单操作(转载过来稍作了修改)。
- 使用gedit出现No protocol specified (gedit:14333): Gtk-WARNING **: cannot open display: :0.0
- java MD5加密解密
- VC透明显示带透明度PNG图片及不带透明度的图片
- JS中数据的计算和转换(一)
- [LeetCode]Binary Tree Level Order Traversal II
- DataGridView控件中加入ComboBox下拉列表框的实现(C#)