CF:394B - Very Beautiful Number(逆推)

B. Very Beautiful Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.

The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactlyp decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.

The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!

Input

The single line contains integers px (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).

Output

If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.

Sample test(s)
input
6 5
output
142857
input
1 2
output
Impossible
input
6 4
output
102564
Note

Sample 1: 142857·5 = 714285.

Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible".


记念只离AC一步之遥的题!!!

错在最后一个样例,简单不能忍受!!!不过这也正好是代码的魅力吧!真的是不能出一点差错才行,唉……

因为我是从1到9假设是p这个数的最后一位数的,然后把这个数乘以x,然后其值的个位数就是p这个数的百倍数,这样依次推下去!!

逆推法:


比如上面第一个样例:6   5                答案是142857


从1循环到9假设是p这个数的个位数,然后在7这个数,即假设7是p这个数的个位数,7*5=35,35这个数的个位数5就是p这个数的百位数,然后35%10+5*5=28,28这个数的个位数8就是p的下一位数(逆方向看),依次下去这样算就可以得到758241,反过来就是142857,正好是答案,但是因为是从1到9假设其为p这个数的个位数的,所以要验证得到的数是不是题目中要求的……验证一下就行了……详见代码!


这个推算由最后一位数放到第一位增大x倍可以看出来的,其实刚开始看题目我就看出来了,但是万没想到好的算法,然后做C错了一发,感觉C理解错了反正也做不出来,又重新回来做B,一步一步才写出来的,预判通过开心死!没想到比赛结束在最后一个样例错了!哭


但是……靠!到最后一位数的时候因为可能>9,会出现P+1位的数,所以最后一位处理没想好就漏了,导致昨晚比赛WA在最后一个样例了,唉……


最后一个样例是:2   7.

按刚才那个算法,刚开始推出来的反数就是79,原数反过来就是97,然后验证的时候因为要乘以7,得到刚推出来的字符串是79(如果是这样就好了,答案就对了),但是97*7=679,而推出来的是79是,所以在6那位没有加入字符串,导致最后679只取了79正好对上刚推出来的答案,然后输出就是97了……晕……七九六十三那6没加入进位,唉……没想到最后一个样例都被出题的人想到了,只想说太神了!也只能说自己这步没想到啊!!!最后一个数相乘的进位竟然忘写了!!!!!!!!


现在回想起来逆推的话,其实就是DP思想我觉得呢!就是字符串的逆推嘛!曾经队友说:时刻都得有DP的思想!现在想想真对!

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define f(i,a,n) for(i=a;i<n;i++)
#define F(i,a,n) for(i=a;i<=n;i++)
#define MM 1000002
#define MN 3005
#define INF 16843009000000
using namespace std;
typedef long long ll;
int p,x,i,j;
int main()
{
    cin>>p>>x;
    if(x==1&&p!=1)  for(i=0; i<p; i++) cout<<1;
    else if(x==1&&p==1) cout<<1<<endl;
    else if(p==1&&x!=1) puts("Impossible");
    else
    {
        for(j=1; j<=9; j++)
        {
            string a,b,c,d,f;
            int e=0,sum=0,sum1,w,r,k=0,l=j;
            a+=j+'0';
            while(k++<p-1)
            {
                e+=l*x;
                a+=e%10+'0';
                l=e%10;
                e/=10;
            }
            if(a[a.size()-1]=='0') continue;
            for(i=a.size()-1; i>=0; i--) b+=a[i];
            c+=b[p-1];
            //cout<<b<<endl;
            for(i=0; i<b.size()-1; i++) c+=b[i];//cout<<c<<endl;
            int la=0;
            for(i=b.size()-1;i>=0;i--)
            {
                la+=(b[i]-'0')*x;
                d+=la%10+'0';
                la=la/10;
            }
            if(la>0) d+=la%10+'0'; //这步就是昨晚忘了,靠!!!
            for(i=d.size()-1; i>=0; i--) f+=d[i];
            //cout<<d<<endl;
            if(f==c) {cout<<b<<endl;break;}
        }
        if(j==10) puts("Impossible");
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值