Problem Description
经过了很长很长的恋爱马拉松,帅气NT(Number Theory)终于决定要向迷人的CG(Computational Geometry)表白了!(对单身汪造成了0x7fffffff点伤害)
CG为了考验NT,于是决定最后再出一道难题。
CG给NT两个整数a,b,说这是我们俩的幸运数字,那么我们孩子(还没结婚就想着孩子= =b)的幸运数字就是1111…1(a个1)%b。
请你大声告诉我,我们孩子的幸运数字是多少!!!!
Input
多组数据,每组数据包括两个整数啊,a,b(1<=a<=10^16, 1<=b<=10^9)
数据组数大约为10000组
Output
对于每组数据,输出一个整数
Sample Input
3 3
4 7
5 18
Sample Output
0
5
5
Hint
111%3=0
1111%7=5
11111%18=5
就是a个1modb多少。
这个是借鉴别人的结论 = =
b | a的前提下
(a/b)mod m = a mod (mb) / b
111…111 % b= ((10^a - 1) / 9) % b = (10^a -1) % (9*b) / 9
直接套用 = =
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
ll pow_mod(ll a,ll n,ll m)
{
if(n == 0)return 1;
ll x = pow_mod(a,n/2,m);
ll ans = x*x%m;
if(n%2)ans = ans*a%m;
return ans;
}
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
#endif // LOCAL
ll a,b;
while(scanf("%lld%lld",&a,&b)!=EOF)
{
ll ans = (pow_mod(10,a,9*b) - 1)%(9*b)/9;
printf("%lld\n",ans);
}
return 0;
}