使用pow(x,y)大量 [英] Using pow(x,y) in large numbers
问题描述
我输入数字n,其中1 <= n <= 10 ^ 5。我需要一个长度n。所以我使用pow(10,n-1),但它不工作当n = 100000。错误是什么?
I am inputting a number n where 1<=n<=10^5. I need a number of length n. So I use pow(10,n-1) but it doesnt work when n=100000. What is the error ?
编辑:其codeforces div2第152轮问题B。
Its codeforces div2 round 152 problem B.
Chilly Willy想要查找长度n的最小数目,使得它可以被所有数字Willy已知的(2,3,5和7)来除。帮助他。
Chilly Willy wants to find the minimum number of length n, such that it is simultaneously divisible by all numbers Willy already knows (2, 3, 5 and 7). Help him with that.
数字的长度是十进制数字的数字,不含前导零。
A number's length is the number of digits in its decimal representation without leading zeros.
Input
单个输入行包含一个整数n(1≤n≤10 ^ 5)。
Input A single input line contains a single integer n (1 ≤ n ≤ 10^5).
我的代码工作到n = 19。它在预测试9失败。
My code works upto n=19. It fails on pretest 9.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int f=0;
unsigned long long n;unsigned long long out;
cin>>n;
unsigned long long num=1;unsigned long long lim=10;
for(unsigned long long z=0;z<n;z++)
{num=num*10;lim=lim*10;}num=num/10;lim=lim/10;
for(;num<lim;num++)
{
if((num%2==0)&&(num%3==0)&&(num%5==0)&&(num%7==0)){f=1;out=num;break;}
}
if(f==1){cout<<out;}
else if(f==0){cout<<"-1";}
return 0;
}
推荐答案
不重要的;你不能只使用内置的类型 int
, double
, long
,等等。为了计算具有100000数字的数字,您需要有超过300000位(几千字节);这是不容易的。
Working with big numbers is not trivial; you cannot just use built-in types like int
, double
, long
, etc for this. In order to calculate a number with 100000 digits, you need to have more than 300000 bits (a few kilobytes); this is in no way easy. Instead, you can print the answer without calculating it!
表示数字 num
可被2,3整除,5和7与 num%210 == 0
相同。因此,您的问题的答案如下:
Saying that a number num
is divisible by 2, 3, 5 and 7 is the same as num % 210 == 0
. So the answer to your question looks like this:
100000000000... (really many zeros) ...00000xy0
所有你需要的是找到两个数字x和y,并打印上面的数字。
All you need is to find two digits x and y, and print the above "number".
所以你必须计算 pow(10,99999)%210
而不计算 pow )
。为此,先从 pow(10,0)= 1
开始,再乘以10:
So you have to calculate pow(10, 99999) % 210
without calculating pow(10, 99999)
. To do it, start with pow(10, 0) = 1
and multiply by 10 successively:
pow(10, 0) % 210 = 1
pow(10, 1) % 210 = (1 * 10) % 210 = 10
pow(10, 2) % 210 = (10 * 10) % 210 = 100
pow(10, 3) % 210 = (100 * 10) % 210 = (1000 % 210) = 160
pow(10, 4) % 210 = (160 * 10) % 210 = (1600 % 210) = 130
pow(10, 5) % 210 = (130 * 10) % 210 = (1300 % 210) = 40
...
计算 pow(10,99999)%210
以这种方式(假设它是
xyz
),添加 210 - xyz
因此,要输出答案,请打印 1
,然后打印99996次 0
,然后打印 210 - xyz
。
After you calculate pow(10, 99999) % 210
in this manner (suppose it's xyz
), adding 210 - xyz
will make the number divisible by 210. So, to output the answer, print 1
, then print 99996 times 0
, then print 210 - xyz
.
这篇关于使用pow(x,y)大量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!