同余&欧拉函数
同余:
简单地说,若有两个整数x,y且x%k==y%k,那么我们记xy(mod k)
同余运算满足以下性质:
1,传递性:若xy(mod k),且y
z(mod k),则x
z(mod k)
2.相加相乘性:即同余式两边可同时加或乘一个整数
剩余系:
即%k后均不相同的整数组成的数集,如(10,1,22,38,44),它们在%5后分别为(0,1,2,3,4)各不相我们说(10,1,22,38,44)是5的一个剩余系,而(0,1,2,3,4)就是5的标准剩余系
欧拉函数
欧拉函数(
(x)):
(x)==比x小且与x互质的正整数的个数;
若x==*
*...........*
(p为x的质因数),则
(x)==x*(1-
)*(1-
)*.........*(1-
)
欧拉定理:
若gcd(a,p)==1,则
拓展欧拉定理:
若b>,则
例题:
Period of an Infinite Binary Expansion:
Let {x} = 0.a1a2a3... be the binary representation of the fractional part of a rational number z. Suppose that {x} is periodic then, we can write
{x} = 0.a1a2...ar(ar+1ar+2...ar+s)w
for some integers r and s with r ≥ 0 and s > 0. Also, (ar+1ar+2...ar+s)wdenotes a nonterminating and repeating binary subsequence of {x}.
The subsequence x1 = a1a2 ... aris called the preperiod of {x} and x2 = ar+1ar+2 ... ar+s is the period of {x}.
Suppose that |x1| and |x2| are chosen as small as possible then x1 is called the least preperiod and x2 is called the least period of {x}.
For example, x = 1/10 = 0.0001100110011(00110011)w and 0001100110011 is a preperiod and 00110011 is a period of 1/10.
However, we can write 1/10 also as 1/10 = 0.0(0011)w and 0 is the least preperiod and 0011 is the least period of 1/10.
The least period of 1/10 starts at the 2nd bit to the right of the binary point and the the length of the least period is 4.
Write a program that finds the position of the first bit of the least period and the length of the least period where the preperiod is also the minimum of a positive rational number less than 1.
Input
Each line is test case. It represents a rational number p/q where p and q are integers, p ≥ 0 and q > 0.
Output
Each line corresponds to a single test case. It represents a pair where the first number is the position of the first bit of the least period and the the second number is the length of the least period of the rational number.
Sample
Inputcopy | Outputcopy |
---|---|
1/10 1/5 101/120 121/1472 | Case #1: 2,4 Case #2: 1,4 Case #3: 4,4 Case #4: 7,11 |
题解:
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
ll p,q;
ll o;
char c;
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
ll oula(ll q){
ll s=q;
for(ll i=2;i*i<=q;i++){
if(q%i==0){
s/=i,s*=(i-1);
while(q%i==0)q/=i;
}
}
if(q>1)s/=q,s*=(q-1);
return s;
}
ll fpow(ll base,ll power){
ll res=1;
while(power>0){
if(power&1)res=res*base%q;
power>>=1;
base=(base*base)%q;
}
return res;
}
int main () {
ll u=0;
while(~scanf("%lld/%lld",&p,&q)){
ll g=gcd(q,p);
ll ans=q;
p/=g,q/=g;
ll cnt=1;
while(q%2==0){
q/=2;
cnt++;
}
ll w=oula(q);
for(int i=1;i*i<=w;i++){
if(w%i==0){
ll z=i;
ll v=w/i;
if(fpow(2,z)==1)ans=min(ans,z);
if(fpow(2,v)==1)ans=min(ans,v);
}
}
u++;
printf("Case #%lld: %lld,%lld\n",u,cnt,ans);
}
}
本文来自博客园,作者:MegaSam,转载请注明原文链接:https://www.cnblogs.com/MegaSamTXL/p/17607142.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】